DEV Community

Cover image for Monitoring Your Kubernetes Cluster: A Beginner’s Guide to Setting Up Prometheus and Grafana
Pravesh Sudha
Pravesh Sudha

Posted on

Monitoring Your Kubernetes Cluster: A Beginner’s Guide to Setting Up Prometheus and Grafana

Learn how to install, configure, and visualize Kubernetes metrics with Prometheus and Grafana for effective monitoring.

💡 Introduction

Welcome to the world of DevOps! Today, we’ll walk through setting up Prometheus and Grafana on a Kind Kubernetes cluster using Helm. But before we dive into the technical steps, let’s understand what these tools are and why they matter.

We’ll be using AWS to create an EC2 instance, which will act as our main machine for setting up the cluster. If you’re new to AWS, Kubernetes, Prometheus, or Grafana, don’t worry—I’ll break it down for you.

What Are These Tools?

  • AWS (Amazon Web Services): A cloud platform that provides services like computing, storage, and networking.

  • Kind (Kubernetes IN Docker): A tool that lets you run a lightweight Kubernetes cluster on your local machine using Docker. It’s similar to Minikube but uses container nodes instead of virtual machines.

  • Prometheus: A powerful monitoring tool that scrapes metrics from Kubernetes nodes, pods, and applications. You can also instrument your applications using Prometheus client libraries.

  • Grafana: A visualization tool that takes data from Prometheus (or other sources like Loki) and displays it in interactive dashboards.

Project Workflow

Here’s a step-by-step breakdown of what we’ll be doing:

✅ Create an EC2 instance (t2.medium)
✅ Install Docker and Kind on the instance
✅ Create a Kubernetes cluster using Kind
✅ Install and configure kubectl (Kubernetes command-line tool)
✅ Apply Kubernetes manifests for deployment
✅ Install Helm (a package manager for Kubernetes)
✅ Deploy the kube-prometheus-stack using Helm
✅ Access Prometheus on port 9090
✅ Access Grafana on port 3000


💡 Pre-requisites

Before we dive into setting up Prometheus and Grafana, let’s make sure you have the necessary requirements in place.

✨ What You’ll Need:

  • An AWS Account: Since we’ll be creating an EC2 instance to act as our main machine, you need access to AWS. If you don’t have an account yet, you can sign up at aws.amazon.com.
  • Basic Understanding of Kubernetes and Docker: While I’ll walk you through each step, having some familiarity with Kubernetes (k8s) and Docker will make things easier. If you’re new to these technologies, here’s a quick breakdown:
  • Docker: A tool for containerizing applications, allowing them to run consistently across different environments.
  • Kubernetes: An orchestration system that manages containerized applications across multiple nodes.

Once you have these prerequisites ready, we’re all set to move forward with the setup! 🚀


Step 1: Creating an AWS EC2 Instance

  1. Log in to your AWS account and navigate to the EC2 Dashboard.
  2. Click Launch Instance and configure the instance as follows:
    • Name: Prom-Graf-Project
    • OS: Ubuntu (latest version)
    • Instance Type: t2.medium
    • Key-Pair: Select an existing key or create a new one
    • Storage: Allocate at least 15GB
  3. Click Launch Instance.

Image description

Connect to the Instance

Once the instance is running, SSH into it using:

ssh -i /path/to/key-pair.pem ubuntu@<EC2-PUBLIC-IP>
Enter fullscreen mode Exit fullscreen mode

Install Docker

sudo apt update
sudo apt install docker.io -y
sudo usermod -aG docker $USER && newgrp docker
Enter fullscreen mode Exit fullscreen mode

Step 2: Installing Kind and Kubectl

Clone the GitHub repo containing required scripts:

git clone https://github.com/Pravesh-Sudha/k8s-kind-voting-app.git
cd k8s-kind-voting-app/kind-cluster
Enter fullscreen mode Exit fullscreen mode

Install Kind

chmod +x install_kind.sh
./install_kind.sh
Enter fullscreen mode Exit fullscreen mode

Install Kubectl

chmod +x install_kubectl.sh
./install_kubectl.sh
kind create cluster --config=config.yml
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Deploy Kubernetes Manifests

cd ../k8s-specifications
kubectl apply -f .
Enter fullscreen mode Exit fullscreen mode

Image description


Step 3: Installing Helm, Prometheus, and Grafana

Install Helm

curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh
Enter fullscreen mode Exit fullscreen mode

Install Kube-Prometheus-Stack

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo add stable https://charts.helm.sh/stable
helm repo update
kubectl create namespace monitoring

helm install kind-prometheus prometheus-community/kube-prometheus-stack --namespace monitoring \
  --set prometheus.service.nodePort=30000 \
  --set prometheus.service.type=NodePort \
  --set grafana.service.nodePort=31000 \
  --set grafana.service.type=NodePort
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Verify Deployment

kubectl get svc -n monitoring
kubectl get namespace
Enter fullscreen mode Exit fullscreen mode

Expose Prometheus and Grafana Services

kubectl port-forward svc/kind-prometheus-kube-prome-prometheus -n monitoring 9090:9090 --address=0.0.0.0 &
kubectl port-forward svc/kind-prometheus-grafana -n monitoring 3000:80 --address=0.0.0.0 &
Enter fullscreen mode Exit fullscreen mode
  • Prometheus Dashboard: http://<EC2-IP>:9090

Image description

  • Grafana Dashboard: http://<EC2-IP>:3000

Image description


Step 4: Running Prometheus Queries

Open Prometheus Dashboard (http://<EC2-IP>:9090) and run the following queries:

CPU Usage (%)

sum (rate (container_cpu_usage_seconds_total{namespace="default"}[1m])) / sum (machine_cpu_cores) * 100
Enter fullscreen mode Exit fullscreen mode

Memory Usage by Pod

sum (container_memory_usage_bytes{namespace="default"}) by (pod)
Enter fullscreen mode Exit fullscreen mode

Network Data Received by Pod

sum(rate(container_network_receive_bytes_total{namespace="default"}[5m])) by (pod)
Enter fullscreen mode Exit fullscreen mode

Network Data Transmitted by Pod

sum(rate(container_network_transmit_bytes_total{namespace="default"}[5m])) by (pod)
Enter fullscreen mode Exit fullscreen mode

Generate Traffic

Expose the voting application:

kubectl port-forward service/vote 5000:5000 --address=0.0.0.0 &
Enter fullscreen mode Exit fullscreen mode

Image description

Visit http://<EC2-IP>:5000, interact with the app (cast votes, refresh), then re-run queries in Prometheus.

Image description

Image description


Step 5: Visualizing Metrics in Grafana

  1. Open Grafana at http://<EC2-IP>:31000
  2. Login Credentials:
    • Username: admin
    • Password: prom-operator
  3. Navigate to Dashboard → New → Import Dashboard
  4. Enter Dashboard ID: 15661
  5. Set the data source as Prometheus and click Import.

Image description

Your metrics are now visualized in Grafana! 🎉

Image description


Conclusion

With this setup, you now have a fully functional monitoring system for your Kubernetes cluster using Prometheus and Grafana. You can further:

✅ Add custom Grafana dashboards
✅ Enable Alertmanager for alerts
✅ Integrate Loki for log monitoring

By continuously monitoring your cluster, you ensure better performance, reliability, and efficiency. Happy monitoring! 🚀

Top comments (2)

Collapse
 
eliasanjr profile image
EliasAnJr

Thanks for sharing. This post is extremely good

Collapse
 
pravesh_sudha_3c2b0c2b5e0 profile image
Pravesh Sudha

Anything for the community :)