Kubernetes Monitoring Setup with Prometheus, AlertManager, Grafana, and Node Exporter

Setting Up Kubernetes Monitoring with Prometheus, AlertManager, Grafana, and Node Exporter

Monitoring a Kubernetes cluster is crucial for ensuring the health and performance of applications running within it. In this guide, we’ll set up a comprehensive monitoring solution using Prometheus, AlertManager, Grafana, and Node Exporter. This setup will allow you to collect metrics, visualize them, and set up alerts for various conditions.

Prerequisites

Before we begin, make sure you have the following:

  • A running Kubernetes cluster
  • kubectl configured to interact with your cluster
  • Git installed on your local machine

Step-by-Step Guide

1. Clone the Kubernetes Prometheus Repository

First, we need to clone the repository that contains the Kubernetes Prometheus setup.

1
2
3
4
sudo git clone https://github.com/techiescamp/kubernetes-prometheus
kubectl create namespace monitoring

cd kubernetes-prometheus

2. Deploy Prometheus

Next, we will deploy Prometheus by creating the necessary resources.

1
2
3
4
kubectl create -f clusterRole.yaml
kubectl create -f config-map.yaml
kubectl create -f prometheus-deployment.yaml 
kubectl create -f prometheus-service.yaml --namespace=monitoring

3. Deploy Kube State Metrics

Kube State Metrics is a simple service that listens to the Kubernetes API server and generates metrics about the state of the objects.

1
2
3
cd ..
sudo git clone https://github.com/devopscube/kube-state-metrics-configs.git
kubectl apply -f kube-state-metrics-configs/

4. Deploy AlertManager

AlertManager handles alerts sent by client applications such as the Prometheus server. It takes care of deduplicating, grouping, and routing them to the correct receiver integrations such as email, PagerDuty, or OpsGenie.

1
2
3
4
5
6
7
sudo git clone https://github.com/bibinwilson/kubernetes-alert-manager.git

cd kubernetes-alert-manager/
kubectl create -f AlertManagerConfigmap.yaml
kubectl create -f AlertTemplateConfigMap.yaml
kubectl create -f Deployment.yaml
kubectl create -f Service.yaml

5. Deploy Grafana

Grafana is a powerful visualization tool that integrates seamlessly with Prometheus. It provides dashboards and graphs for visualizing time-series data.

1
2
3
4
5
6
7
cd ..
sudo git clone https://github.com/bibinwilson/kubernetes-grafana.git
cd kubernetes-grafana/

kubectl create -f grafana-datasource-config.yaml
kubectl create -f deployment.yaml
kubectl create -f service.yaml

6. Deploy Node Exporter

Node Exporter is a Prometheus exporter for hardware and OS metrics exposed by *nix kernels. It allows you to monitor system metrics such as CPU, memory, and disk usage.

1
2
3
4
5
6
cd ..
sudo git clone https://github.com/bibinwilson/kubernetes-node-exporter
cd kubernetes-node-exporter/

kubectl create -f daemonset.yaml
kubectl create -f service.yaml

Conclusion

With these steps, you have successfully set up a comprehensive monitoring solution for your Kubernetes cluster. Prometheus will collect the metrics, AlertManager will handle the alerts, Grafana will provide visualization, and Node Exporter will monitor the node-level metrics.

This setup will help you gain insights into the performance of your applications and the health of your Kubernetes cluster, enabling you to take proactive measures to ensure everything runs smoothly.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash

# Clone Kubernetes Prometheus repository
sudo git clone https://github.com/techiescamp/kubernetes-prometheus
kubectl create namespace monitoring

cd kubernetes-prometheus

# Create Prometheus resources
kubectl create -f clusterRole.yaml
kubectl create -f config-map.yaml
kubectl create -f prometheus-deployment.yaml 
kubectl create -f prometheus-service.yaml --namespace=monitoring

cd ..
# Clone kube-state-metrics configs repository
sudo git clone https://github.com/devopscube/kube-state-metrics-configs.git
kubectl apply -f kube-state-metrics-configs/

# Clone Kubernetes AlertManager repository
sudo git clone https://github.com/bibinwilson/kubernetes-alert-manager.git

cd kubernetes-alert-manager/

# Create AlertManager resources
kubectl create -f AlertManagerConfigmap.yaml
kubectl create -f AlertTemplateConfigMap.yaml
kubectl create -f Deployment.yaml
kubectl create -f Service.yaml

cd ..
# Clone Kubernetes Grafana repository
sudo git clone https://github.com/bibinwilson/kubernetes-grafana.git
cd kubernetes-grafana/

# Create Grafana resources
kubectl create -f grafana-datasource-config.yaml
kubectl create -f deployment.yaml
kubectl create -f service.yaml

cd ..

# Clone Kubernetes Node Exporter repository
sudo git clone https://github.com/bibinwilson/kubernetes-node-exporter
cd kubernetes-node-exporter/

# Create Node Exporter resources
kubectl create -f daemonset.yaml
kubectl create -f service.yaml