{"id":46256,"date":"2024-05-15T01:47:59","date_gmt":"2024-05-15T01:47:59","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=46256"},"modified":"2024-05-29T03:05:09","modified_gmt":"2024-05-29T03:05:09","slug":"icinga-tutorials-how-to-monitor-kubernetes-using-icinga","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/icinga-tutorials-how-to-monitor-kubernetes-using-icinga\/","title":{"rendered":"icinga tutorials: How to monitor Kubernetes using icinga"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Step 1: Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ensure you have Icinga 2 and Icinga Web 2 installed on your Icinga server.<\/li>\n\n\n\n<li>Kubernetes cluster where you want to implement monitoring.<\/li>\n\n\n\n<li>Administrative access to both the Kubernetes cluster and the Icinga server.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Install and Configure kubectl on Icinga Server<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">kubectl is the Kubernetes command-line tool that allows you to run commands against Kubernetes clusters.\n\nInstall kubectl:\n\n$ sudo apt-get update &amp;&amp; sudo apt-get install -y apt-transport-https gnupg2\n$ curl -s https:<span class=\"hljs-comment\">\/\/packages.cloud.google.com\/apt\/doc\/apt-key.gpg | sudo apt-key add -<\/span>\n$ <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"deb https:\/\/apt.kubernetes.io\/ kubernetes-xenial main\"<\/span> | sudo tee -a \/etc\/apt\/sources.<span class=\"hljs-keyword\">list<\/span>.d\/kubernetes.<span class=\"hljs-keyword\">list<\/span>\n$ sudo apt-get update\n$ sudo apt-get install -y kubectl\n\nConfigure kubectl:\n\n$ Copy your Kubernetes cluster<span class=\"hljs-string\">'s kubeconfig file to your Icinga server.\n$ Set the KUBECONFIG environment variable or use the default location (~\/.kube\/config).<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Step 3: Install Kubernetes Monitoring Plugin<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">There are various plugins available <span class=\"hljs-keyword\">for<\/span> monitoring Kubernetes, such <span class=\"hljs-keyword\">as<\/span> check_kube, which can be installed on the Icinga server.\n\nDownload and install the monitoring plugin:\n\n$ cd \/usr\/lib\/nagios\/plugins\n$ sudo wget https:<span class=\"hljs-comment\">\/\/example.com\/path\/to\/check_kube  # Replace with actual URL<\/span>\n$ sudo chmod +x check_kube<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Step 4: Configure Icinga 2<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">You need to define commands, services, and hosts <span class=\"hljs-keyword\">in<\/span> Icinga <span class=\"hljs-number\">2<\/span> <span class=\"hljs-keyword\">for<\/span> Kubernetes monitoring.\n\nDefine Command <span class=\"hljs-keyword\">in<\/span> Icinga:\nCreate or edit a configuration file:\n\n$ sudo nano \/etc\/icinga2\/conf.d\/commands.conf\n\nAdd a command definition <span class=\"hljs-keyword\">for<\/span> Kubernetes checks:\n\nobject CheckCommand <span class=\"hljs-string\">\"check_kubernetes\"<\/span> {\n    command = &#91; <span class=\"hljs-string\">\"\/usr\/lib\/nagios\/plugins\/check_kube\"<\/span> ]\n    <span class=\"hljs-built_in\">arguments<\/span> = {\n        <span class=\"hljs-string\">\"-K\"<\/span> = <span class=\"hljs-string\">\"$kubeconfig$\"<\/span>,\n        <span class=\"hljs-string\">\"-N\"<\/span> = <span class=\"hljs-string\">\"$namespace$\"<\/span>,\n        <span class=\"hljs-string\">\"-D\"<\/span> = <span class=\"hljs-string\">\"$deployment$\"<\/span>\n    }\n}\n\nDefine Host <span class=\"hljs-keyword\">for<\/span> Kubernetes Cluster:\nAdd a host object <span class=\"hljs-keyword\">for<\/span> your Kubernetes cluster:\n\nobject Host <span class=\"hljs-string\">\"kubernetes-cluster\"<\/span> {\n    <span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"generic-host\"<\/span>\n    address = <span class=\"hljs-string\">\"127.0.0.1\"<\/span>  <span class=\"hljs-comment\">\/\/ Use an appropriate address<\/span>\n    check_command = <span class=\"hljs-string\">\"hostalive\"<\/span>\n}\n\nDefine Service <span class=\"hljs-keyword\">for<\/span> Kubernetes Monitoring:\nCreate services to monitor various components:\n\napply Service <span class=\"hljs-string\">\"check-deployment\"<\/span> {\n    <span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"generic-service\"<\/span>\n    check_command = <span class=\"hljs-string\">\"check_kubernetes\"<\/span>\n    vars.kubeconfig = <span class=\"hljs-string\">\"\/path\/to\/kubeconfig\"<\/span>\n    vars.namespace = <span class=\"hljs-string\">\"default\"<\/span>\n    vars.deployment = <span class=\"hljs-string\">\"example-deployment\"<\/span>\n    assign where host.name == <span class=\"hljs-string\">\"kubernetes-cluster\"<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Step 5: Reload Icinga 2<\/h2>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">After configuring your commands and services, reload Icinga 2 to apply changes:\n\nsudo systemctl reload icinga2<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Step 6: Verify and Adjust<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">&lt;ul style=<span class=\"hljs-string\">\"border: 0px solid rgb(227, 227, 227); --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-pan-x: ; --tw-pan-y: ; --tw-pinch-zoom: ; --tw-scroll-snap-strictness: proximity; --tw-gradient-from-position: ; --tw-gradient-via-position: ; --tw-gradient-to-position: ; --tw-ordinal: ; --tw-slashed-zero: ; --tw-numeric-figure: ; --tw-numeric-spacing: ; --tw-numeric-fraction: ; --tw-ring-inset: ; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(69,89,164,.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; --tw-blur: ; --tw-brightness: ; --tw-contrast: ; --tw-grayscale: ; --tw-hue-rotate: ; --tw-invert: ; --tw-saturate: ; --tw-sepia: ; --tw-drop-shadow: ; --tw-backdrop-blur: ; --tw-backdrop-brightness: ; --tw-backdrop-contrast: ; --tw-backdrop-grayscale: ; --tw-backdrop-hue-rotate: ; --tw-backdrop-invert: ; --tw-backdrop-opacity: ; --tw-backdrop-saturate: ; --tw-backdrop-sepia: ; --tw-contain-size: ; --tw-contain-layout: ; --tw-contain-paint: ; --tw-contain-style: ; list-style-type: disc; margin: 0px 0px 1.25em; padding: 0px 0px 0px 1.625em; color: rgb(13, 13, 13); font-family: S\u00f6hne, ui-sans-serif, system-ui, -apple-system, \"<\/span>Segoe UI<span class=\"hljs-string\">\", Roboto, Ubuntu, Cantarell, \"<\/span>Noto Sans<span class=\"hljs-string\">\", sans-serif, \"<\/span>Helvetica Neue<span class=\"hljs-string\">\", Arial, \"<\/span>Apple Color Emoji<span class=\"hljs-string\">\", \"<\/span>Segoe UI Emoji<span class=\"hljs-string\">\", \"<\/span>Segoe UI <span class=\"hljs-built_in\">Symbol<\/span><span class=\"hljs-string\">\", \"<\/span>Noto Color Emoji<span class=\"hljs-string\">\"; font-size: 16px;\"<\/span>&gt;<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">\"border: 0px solid rgb(227, 227, 227); box-sizing: border-box; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-pan-x: ; --tw-pan-y: ; --tw-pinch-zoom: ; --tw-scroll-snap-strictness: proximity; --tw-gradient-from-position: ; --tw-gradient-via-position: ; --tw-gradient-to-position: ; --tw-ordinal: ; --tw-slashed-zero: ; --tw-numeric-figure: ; --tw-numeric-spacing: ; --tw-numeric-fraction: ; --tw-ring-inset: ; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(69,89,164,.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; --tw-blur: ; --tw-brightness: ; --tw-contrast: ; --tw-grayscale: ; --tw-hue-rotate: ; --tw-invert: ; --tw-saturate: ; --tw-sepia: ; --tw-drop-shadow: ; --tw-backdrop-blur: ; --tw-backdrop-brightness: ; --tw-backdrop-contrast: ; --tw-backdrop-grayscale: ; --tw-backdrop-hue-rotate: ; --tw-backdrop-invert: ; --tw-backdrop-opacity: ; --tw-backdrop-saturate: ; --tw-backdrop-sepia: ; --tw-contain-size: ; --tw-contain-layout: ; --tw-contain-paint: ; --tw-contain-style: ; margin-bottom: 0.5em; margin-top: 0.5em; padding-left: 0.375em;\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">span<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">\"border: 0px solid rgb(227, 227, 227); box-sizing: border-box; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-pan-x: ; --tw-pan-y: ; --tw-pinch-zoom: ; --tw-scroll-snap-strictness: proximity; --tw-gradient-from-position: ; --tw-gradient-via-position: ; --tw-gradient-to-position: ; --tw-ordinal: ; --tw-slashed-zero: ; --tw-numeric-figure: ; --tw-numeric-spacing: ; --tw-numeric-fraction: ; --tw-ring-inset: ; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(69,89,164,.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; --tw-blur: ; --tw-brightness: ; --tw-contrast: ; --tw-grayscale: ; --tw-hue-rotate: ; --tw-invert: ; --tw-saturate: ; --tw-sepia: ; --tw-drop-shadow: ; --tw-backdrop-blur: ; --tw-backdrop-brightness: ; --tw-backdrop-contrast: ; --tw-backdrop-grayscale: ; --tw-backdrop-hue-rotate: ; --tw-backdrop-invert: ; --tw-backdrop-opacity: ; --tw-backdrop-saturate: ; --tw-backdrop-sepia: ; --tw-contain-size: ; --tw-contain-layout: ; --tw-contain-paint: ; --tw-contain-style: ; font-weight: 600; color: var(--tw-prose-bold); margin-top: 1.25em; margin-bottom: 1.25em;\"<\/span>&gt;<\/span>Verify in Icinga Web 2<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">span<\/span>&gt;<\/span>: Log into Icinga Web 2 and check the newly configured services for your Kubernetes cluster. Ensure they are reporting correctly.<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">span<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">\"border: 0px solid rgb(227, 227, 227); box-sizing: border-box; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-pan-x: ; --tw-pan-y: ; --tw-pinch-zoom: ; --tw-scroll-snap-strictness: proximity; --tw-gradient-from-position: ; --tw-gradient-via-position: ; --tw-gradient-to-position: ; --tw-ordinal: ; --tw-slashed-zero: ; --tw-numeric-figure: ; --tw-numeric-spacing: ; --tw-numeric-fraction: ; --tw-ring-inset: ; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(69,89,164,.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; --tw-blur: ; --tw-brightness: ; --tw-contrast: ; --tw-grayscale: ; --tw-hue-rotate: ; --tw-invert: ; --tw-saturate: ; --tw-sepia: ; --tw-drop-shadow: ; --tw-backdrop-blur: ; --tw-backdrop-brightness: ; --tw-backdrop-contrast: ; --tw-backdrop-grayscale: ; --tw-backdrop-hue-rotate: ; --tw-backdrop-invert: ; --tw-backdrop-opacity: ; --tw-backdrop-saturate: ; --tw-backdrop-sepia: ; --tw-contain-size: ; --tw-contain-layout: ; --tw-contain-paint: ; --tw-contain-style: ; font-weight: 600; color: var(--tw-prose-bold); margin-top: 1.25em; margin-bottom: 1.25em;\"<\/span>&gt;<\/span>Adjustments<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">span<\/span>&gt;<\/span>: Depending on your specific monitoring needs, you may need to add additional service checks or refine existing ones to cover different Kubernetes objects like nodes, pods, and services.<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span><\/span>&lt;<span class=\"hljs-regexp\">\/ul&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Alternate Method<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Step <span class=\"hljs-number\">1<\/span>: Install <span class=\"hljs-keyword\">and<\/span> Configure the Icinga2 Kubernetes Plugin\nBefore you start, ensure that you have kubectl configured on the same server where your Icinga2 agent is running. This will allow the Icinga2 agent to <span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">kubectl<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">gather<\/span> <span class=\"hljs-title\">metrics<\/span> <span class=\"hljs-title\">from<\/span> <span class=\"hljs-title\">Kubernetes<\/span>.\n\n<span class=\"hljs-title\">Install<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">Kubernetes<\/span> <span class=\"hljs-title\">plugin<\/span> <span class=\"hljs-title\">for<\/span> <span class=\"hljs-title\">Icinga2<\/span>: <span class=\"hljs-title\">You<\/span> <span class=\"hljs-title\">might<\/span> <span class=\"hljs-title\">need<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">write<\/span> <span class=\"hljs-title\">custom<\/span> <span class=\"hljs-title\">check<\/span> <span class=\"hljs-title\">scripts<\/span> <span class=\"hljs-title\">or<\/span> <span class=\"hljs-title\">find<\/span> <span class=\"hljs-title\">existing<\/span> <span class=\"hljs-title\">plugins<\/span> <span class=\"hljs-title\">that<\/span> <span class=\"hljs-title\">can<\/span> <span class=\"hljs-title\">use<\/span> <span class=\"hljs-title\">kubectl<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">fetch<\/span> <span class=\"hljs-title\">data<\/span> <span class=\"hljs-title\">from<\/span> <span class=\"hljs-title\">Kubernetes<\/span>. <span class=\"hljs-title\">There<\/span> <span class=\"hljs-title\">are<\/span> <span class=\"hljs-title\">community<\/span> <span class=\"hljs-title\">plugins<\/span> <span class=\"hljs-title\">available<\/span>, <span class=\"hljs-title\">or<\/span> <span class=\"hljs-title\">you<\/span> <span class=\"hljs-title\">can<\/span> <span class=\"hljs-title\">create<\/span> <span class=\"hljs-title\">simple<\/span> <span class=\"hljs-title\">bash<\/span> <span class=\"hljs-title\">scripts<\/span> <span class=\"hljs-title\">that<\/span> <span class=\"hljs-title\">wrap<\/span> <span class=\"hljs-title\">kubectl<\/span> <span class=\"hljs-title\">commands<\/span>.\n\n<span class=\"hljs-title\">Example<\/span> <span class=\"hljs-title\">script<\/span>: <span class=\"hljs-title\">Here<\/span>'<span class=\"hljs-title\">s<\/span> <span class=\"hljs-title\">a<\/span> <span class=\"hljs-title\">basic<\/span> <span class=\"hljs-title\">example<\/span> <span class=\"hljs-title\">of<\/span> <span class=\"hljs-title\">a<\/span> <span class=\"hljs-title\">script<\/span> <span class=\"hljs-title\">that<\/span> <span class=\"hljs-title\">checks<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">number<\/span> <span class=\"hljs-title\">of<\/span> <span class=\"hljs-title\">running<\/span> <span class=\"hljs-title\">pods<\/span> <span class=\"hljs-title\">in<\/span> <span class=\"hljs-title\">a<\/span> <span class=\"hljs-title\">specific<\/span> <span class=\"hljs-title\">namespace<\/span>:\n\n\n#!\/<span class=\"hljs-title\">bin<\/span>\/<span class=\"hljs-title\">bash<\/span>\n<span class=\"hljs-title\">NAMESPACE<\/span>=$1\n<span class=\"hljs-title\">MIN_PODS<\/span>=$2\n\n# <span class=\"hljs-title\">Get<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">number<\/span> <span class=\"hljs-title\">of<\/span> <span class=\"hljs-title\">running<\/span> <span class=\"hljs-title\">pods<\/span>\n<span class=\"hljs-title\">RUNNING_PODS<\/span>=$(<span class=\"hljs-title\">kubectl<\/span> <span class=\"hljs-title\">get<\/span> <span class=\"hljs-title\">pods<\/span> --<span class=\"hljs-title\">namespace<\/span> $<span class=\"hljs-title\">NAMESPACE<\/span> --<span class=\"hljs-title\">field<\/span>-<span class=\"hljs-title\">selector<\/span>=<span class=\"hljs-title\">status<\/span>.<span class=\"hljs-title\">phase<\/span>=<span class=\"hljs-title\">Running<\/span> --<span class=\"hljs-title\">no<\/span>-<span class=\"hljs-title\">headers<\/span> | <span class=\"hljs-title\">wc<\/span> -<span class=\"hljs-title\">l<\/span>)\n\n# <span class=\"hljs-title\">Check<\/span> <span class=\"hljs-title\">if<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">number<\/span> <span class=\"hljs-title\">of<\/span> <span class=\"hljs-title\">running<\/span> <span class=\"hljs-title\">pods<\/span> <span class=\"hljs-title\">is<\/span> <span class=\"hljs-title\">less<\/span> <span class=\"hljs-title\">than<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">minimum<\/span> <span class=\"hljs-title\">required<\/span>\n<span class=\"hljs-title\">if<\/span> &#91; \"$<span class=\"hljs-title\">RUNNING_PODS<\/span>\" -<span class=\"hljs-title\">lt<\/span> \"$<span class=\"hljs-title\">MIN_PODS<\/span>\" ]; then\n  <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"CRITICAL - Only $RUNNING_PODS running pods in $NAMESPACE, minimum required is $MIN_PODS\"<\/span>\n  <span class=\"hljs-keyword\">exit<\/span> <span class=\"hljs-number\">2<\/span>\n<span class=\"hljs-keyword\">else<\/span>\n  <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"OK - $RUNNING_PODS running pods in $NAMESPACE\"<\/span>\n  <span class=\"hljs-keyword\">exit<\/span> <span class=\"hljs-number\">0<\/span>\nfi\nSave this script on the Icinga2 agent machine, <span class=\"hljs-keyword\">and<\/span> make sure it<span class=\"hljs-string\">'s executable (chmod +x check_kube_pods.sh).\n\nStep 2: Configure Icinga2 Agent\nYou need to configure your Icinga2 agent to execute the Kubernetes monitoring script.\n\nDefine the check command:\n\n\nobject CheckCommand \"check_kube_pods\" {\n  command = &#91; \"\/path\/to\/check_kube_pods.sh\" ]\n  arguments = {\n    \"--namespace\" = \"$namespace$\"\n    \"--min-pods\" = \"$min_pods$\"\n  }\n}\nAdd the service definition to Icinga2:\n\n\napply Service \"kube-pods\" {\n  import \"generic-service\"\n  check_command = \"check_kube_pods\"\n  vars.namespace = \"default\"\n  vars.min_pods = 10\n  assign where host.vars.kubernetes == true\n}\nStep 3: Define Host and Service in Icinga2\nYou need to ensure that your Kubernetes cluster or specific nodes are defined as hosts in your Icinga2 configuration.\n\nDefine a host for your Kubernetes cluster:\n\n\nobject Host \"kubernetes-cluster\" {\n  import \"generic-host\"\n  address = \"127.0.0.1\"  \/\/ Use an internal or external IP address\n  vars.kubernetes = true\n}\nStep 4: Restart Icinga2\nAfter setting up the configurations, restart Icinga2 to apply the changes.\n\n\nsudo systemctl restart icinga2\nStep 5: Validate the Setup\nCheck the Icinga2 dashboard or use the Icinga2 CLI to ensure that the new checks are being executed and that they return the expected results.\n\n\nicinga2 object list --type Service\nThis setup will allow you to start monitoring basic Kubernetes metrics. You can expand this by adding more complex checks, such as node resource usage, pod status by deployment, or custom metrics exposed by your applications running in Kubernetes.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>","protected":false},"excerpt":{"rendered":"<p>Step 1: Prerequisites Step 2: Install and Configure kubectl on Icinga Server Step 3: Install Kubernetes Monitoring Plugin Step 4: Configure Icinga 2 Step 5: Reload Icinga 2 Step 6:&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[8217],"tags":[],"class_list":["post-46256","post","type-post","status-publish","format-standard","hentry","category-icinga"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/46256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=46256"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/46256\/revisions"}],"predecessor-version":[{"id":46312,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/46256\/revisions\/46312"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=46256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=46256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=46256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}