Monitor Kubernetes Cluster using Prometheus and Grafana

In this article, we will learn Monitor Kubernetes Cluster using Prometheus and Grafana | How to Monitor Kubernetes Using Prometheus and Grafana. Kubernetes monitoring is essential for understanding the health and performance of your cluster. Prometheus and Grafana are popular tools that simplify the monitoring process. This article walks through the setup process for monitoring Kubernetes using Minikube, Prometheus, and Grafana.

Prerequisites

  • AWS Account with Ubuntu 24.04 LTS EC2 Instance.
  • Minikube and kubectl, Helm Installed
  • Basic knowledge of Kubernetes

Step #1:Set Up Ubuntu EC2 Instance

Update the Package List.

sudo apt update
1

Installs essential tools like curl, wget and apt-transport-https.

sudo apt install curl wget apt-transport-https -y
2

Installs Docker, a container runtime that will be used as the VM driver for Minikube.

sudo apt install docker.io -y
3

Add the current user to the Docker group, allowing the user to run Docker commands without sudo.

sudo usermod -aG docker $USER
4

Adjust permissions for the Docker socket, enabling easier communication with the Docker daemon.

sudo chmod 666 /var/run/docker.sock
5

Checks if the system supports virtualization.

egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
6

Install KVM and Related Tools.

sudo apt install qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils virtinst libvirt-daemon
7

Add User to Virtualization Groups.

sudo adduser $USER libvirt
sudo adduser $USER libvirt-qemu
8

Reload Group.

newgrp libvirt
newgrp libvirt-qemu
9

Step #2:Install Minikube and kubectl

Download the latest Minikube binary.

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
10 2

Install it to /usr/local/bin, making it available system-wide.

sudo install minikube-linux-amd64 /usr/local/bin/minikube
11

Use minikube version command to confirm the installation.

minikube version
12

Download the latest version of kubectl (Kubernetes CLI).

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
13

Make the kubectl binary executable.

chmod +x ./kubectl
14

move it to /usr/local/bin

sudo mv kubectl /usr/local/bin/
15

Use kubectl version command to check the installation.

kubectl version --client --output=yaml
16

Step #3:Start the Minikube

Start Minikube with Docker as the driver.

minikube start --vm-driver docker
17

To Check the status of Minikube run the following command.

minikube status
18

Step #4:Install the Helm

Download the helm, a package manager for Kubernetes.

curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
19

Change its permissions.

chmod 700 get_helm.sh
20

Install the helm.

./get_helm.sh
21

Check its version to confirm the installation.

helm version
22

Add Prometheus Helm Chart Repository.

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
23

update the Helm repositories to fetch the latest charts.

helm repo update
24

Step #5:Configure Prometheus and Grafana

Create a custom-values.yaml file to configure Prometheus and Grafana services as NodePort

nano custom-values.yaml
25

Add the following configuration.

prometheus:
  service:
    type: NodePort
grafana:
  service:
    type: NodePort
26

Deploy the Prometheus and Grafana stack.

helm upgrade --install kube-prometheus-stack prometheus-community/kube-prometheus-stack -f custom-values.yaml
27

Step #6:Access Prometheus and Grafana

List the services to get NodePort details.

kubectl get services
28 2

Forward the Prometheus service to port 9090.

kubectl port-forward --address 0.0.0.0 svc/kube-prometheus-stack-prometheus 9090:9090
30

Access the UI of Prometheus on web browser using http://<EC2-Public-IP>:9090.

33

Click on alerts to see them.

36

Open the duplicate tab and Forward the Grafana service to port 3000.

kubectl port-forward --address 0.0.0.0 svc/kube-prometheus-stack-grafana 3000:80
29 1

Access the UI of Grafana on web browser using http://<EC2-Public-IP>:3000.

34

Retrieve Grafana admin credentials using following commands.

kubectl get secret --namespace default kube-prometheus-stack-grafana -o jsonpath="{.data.admin-user}" | base64 --decode ; echo
31 1

admin is our Username.

kubectl get secret --namespace default kube-prometheus-stack-grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo
32

prom-operator is our password.

35

Step #7:Use Grafana Dashboards

Go to Dashboards.

37

You can see the few default dashboards there.

38

There are many dashboards you can use, using these dashboards we can easily monitor our kubernetes cluster. For example below is Kubernetes/API sever dashboard.

39

You can also import dashboards from the Grafana library.

Access the Grafana library. Search for Grafana Labs in web browser.

41 1

Select a desired dashboard, e.g., 11455 for K8s/Storage/Volumes/Namespace.

Search for K8s/Storage/Volumes/Namespace

42 1
43

Copy its ID.

44

In Grafana, go to Dashboards > New > Import.

45

Enter the dashboard ID (11455) and click Load.

46

Select Prometheus as a data source and Click Import to add the dashboard..

47
kubernetes

Conclusion:

In conclusion, today we have successfully set up Kubernetes monitoring with Prometheus and Grafana. We have seen the step to install tools, configure a Kubernetes cluster, and deploy monitoring solutions. Using Prometheus and Grafana, you gain powerful insights into your cluster’s performance. Use the default and custom dashboards to monitor the health and performance of your Kubernetes cluster. You can extend this setup to include alerting and custom metrics for deeper insights.

Related Articles:

Pull Image from DockerHub Private Registry using Helm in Kubernetes

Reference:

Official Opentelemetry Page

About Prasad Hole

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link