MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

icinga tutorials: How to monitor Kubernetes using icinga

Step 1: Prerequisites

  • Ensure you have Icinga 2 and Icinga Web 2 installed on your Icinga server.
  • Kubernetes cluster where you want to implement monitoring.
  • Administrative access to both the Kubernetes cluster and the Icinga server.

Step 2: Install and Configure kubectl on Icinga Server

kubectl is the Kubernetes command-line tool that allows you to run commands against Kubernetes clusters.

Install kubectl:

$ sudo apt-get update && sudo apt-get install -y apt-transport-https gnupg2
$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
$ echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
$ sudo apt-get update
$ sudo apt-get install -y kubectl

Configure kubectl:

$ Copy your Kubernetes cluster's kubeconfig file to your Icinga server.
$ Set the KUBECONFIG environment variable or use the default location (~/.kube/config).Code language: PHP (php)

Step 3: Install Kubernetes Monitoring Plugin

There are various plugins available for monitoring Kubernetes, such as check_kube, which can be installed on the Icinga server.

Download and install the monitoring plugin:

$ cd /usr/lib/nagios/plugins
$ sudo wget https://example.com/path/to/check_kube  # Replace with actual URL
$ sudo chmod +x check_kubeCode language: JavaScript (javascript)

Step 4: Configure Icinga 2

You need to define commands, services, and hosts in Icinga 2 for Kubernetes monitoring.

Define Command in Icinga:
Create or edit a configuration file:

$ sudo nano /etc/icinga2/conf.d/commands.conf

Add a command definition for Kubernetes checks:

object CheckCommand "check_kubernetes" {
    command = [ "/usr/lib/nagios/plugins/check_kube" ]
    arguments = {
        "-K" = "$kubeconfig$",
        "-N" = "$namespace$",
        "-D" = "$deployment$"
    }
}

Define Host for Kubernetes Cluster:
Add a host object for your Kubernetes cluster:

object Host "kubernetes-cluster" {
    import "generic-host"
    address = "127.0.0.1"  // Use an appropriate address
    check_command = "hostalive"
}

Define Service for Kubernetes Monitoring:
Create services to monitor various components:

apply Service "check-deployment" {
    import "generic-service"
    check_command = "check_kubernetes"
    vars.kubeconfig = "/path/to/kubeconfig"
    vars.namespace = "default"
    vars.deployment = "example-deployment"
    assign where host.name == "kubernetes-cluster"
}Code language: JavaScript (javascript)

Step 5: Reload Icinga 2

After configuring your commands and services, reload Icinga 2 to apply changes:

sudo systemctl reload icinga2

Step 6: Verify and Adjust

<ul style="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รถhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px;"><li style="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 style="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;">Verify in Icinga Web 2</span>: Log into Icinga Web 2 and check the newly configured services for your Kubernetes cluster. Ensure they are reporting correctly.<span style="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;">Adjustments</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.</li></ul>Code language: JavaScript (javascript)

Alternate Method


Step 1: Install and Configure the Icinga2 Kubernetes Plugin
Before 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 use kubectl to gather metrics from Kubernetes.

Install the Kubernetes plugin for Icinga2: You might need to write custom check scripts or find existing plugins that can use kubectl to fetch data from Kubernetes. There are community plugins available, or you can create simple bash scripts that wrap kubectl commands.

Example script: Here's a basic example of a script that checks the number of running pods in a specific namespace:


#!/bin/bash
NAMESPACE=$1
MIN_PODS=$2

# Get the number of running pods
RUNNING_PODS=$(kubectl get pods --namespace $NAMESPACE --field-selector=status.phase=Running --no-headers | wc -l)

# Check if the number of running pods is less than the minimum required
if [ "$RUNNING_PODS" -lt "$MIN_PODS" ]; then
  echo "CRITICAL - Only $RUNNING_PODS running pods in $NAMESPACE, minimum required is $MIN_PODS"
  exit 2
else
  echo "OK - $RUNNING_PODS running pods in $NAMESPACE"
  exit 0
fi
Save this script on the Icinga2 agent machine, and make sure it's executable (chmod +x check_kube_pods.sh).

Step 2: Configure Icinga2 Agent
You need to configure your Icinga2 agent to execute the Kubernetes monitoring script.

Define the check command:


object CheckCommand "check_kube_pods" {
  command = [ "/path/to/check_kube_pods.sh" ]
  arguments = {
    "--namespace" = "$namespace$"
    "--min-pods" = "$min_pods$"
  }
}
Add the service definition to Icinga2:


apply Service "kube-pods" {
  import "generic-service"
  check_command = "check_kube_pods"
  vars.namespace = "default"
  vars.min_pods = 10
  assign where host.vars.kubernetes == true
}
Step 3: Define Host and Service in Icinga2
You need to ensure that your Kubernetes cluster or specific nodes are defined as hosts in your Icinga2 configuration.

Define a host for your Kubernetes cluster:


object Host "kubernetes-cluster" {
  import "generic-host"
  address = "127.0.0.1"  // Use an internal or external IP address
  vars.kubernetes = true
}
Step 4: Restart Icinga2
After setting up the configurations, restart Icinga2 to apply the changes.


sudo systemctl restart icinga2
Step 5: Validate the Setup
Check the Icinga2 dashboard or use the Icinga2 CLI to ensure that the new checks are being executed and that they return the expected results.


icinga2 object list --type Service
This 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.Code language: PHP (php)

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Optum Lab 2 – Services – LoadBalancer

This lab shows how a Kubernetes Service gives a group of pods one stable address and spreads traffic across them, using the ClusterIP, NodePort and LoadBalancer types….

Read More

Troubleshooting Knative Guide

This page collects kubectl commands for troubleshooting Knative Serving: finding an OOMKilled activator, checking the net-istio-controller, load testing a Knative Service (ksvc), inspecting the autoscaler and its…

Read More

Kubectl: Persistent Volumes PV & PersistentVolumeClaim PVC

To turn an existing PersistentVolumeClaim (PVC) into a YAML manifest you can deploy again, run kubectl get pvc <name> -o yaml and remove the fields the cluster…

Read More

How to install Knative Servingย 1.7.4ย Manually?

To install Knative Serving 1.7.4 manually, you apply the Serving CRDs, then the core components, then a networking layer such as Istio or Kourier. Each step is…

Read More

How to Install and Configure Headlamp for Kubernetes on macOS, Linux and Windows

Managing Kubernetes is rarely difficult because of a shortage of tools. The challenge is knowing what is happening inside a cluster quickly enough to make the right…

Read More

How to Install and Configure K9s on macOS, Linux and Windows: Step-by-Step Guide

A complete hands-on Kubernetes management tutorial for DevOps Engineers, SREs, Platform Engineers and Kubernetes Administrators. K9s is a free, open-source, terminal-based Kubernetes management tool that simplifies the…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x