Monitoring Kubernetes Clusters with Prometheus and Grafana


Introduction

Kubernetes is a powerful container orchestration platform, but monitoring it is crucial for ensuring reliability, performance, and scalability. 

Prometheus and Grafana are two of the most popular open-source tools for monitoring and visualizing Kubernetes clusters. 

In this guide, we’ll walk you through setting up Prometheus and Grafana on Kubernetes and configuring dashboards for real-time insights.

Why Use Prometheus and Grafana for Kubernetes Monitoring?

Prometheus: The Monitoring Backbone

  • Collects metrics from Kubernetes nodes, pods, and applications.
  • Uses a powerful query language (PromQL) for analyzing data.
  • Supports alerting based on predefined conditions.

Grafana: The Visualization Tool

  • Provides rich dashboards with graphs and metrics.
  • Allows integration with multiple data sources (e.g., Prometheus, Loki, Elasticsearch).
  • Enables alerting and notification management.

Step 1: Installing Prometheus and Grafana on Kubernetes

Prerequisites

Before starting, ensure you have:

  • A running Kubernetes cluster
  • kubectl and Helm installed

1. Add the Prometheus Helm Chart Repository

Helm makes it easy to deploy Prometheus and Grafana using predefined configurations.

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

2. Install Prometheus and Grafana Using Helm

sh
helm install prometheus prometheus-community/kube-prometheus-stack --namespace monitoring --create-namespace

This command installs:
Prometheus — For collecting metrics
Grafana — For visualization
Alertmanager — For notifications
Node-exporter — To collect system-level metrics

Step 2: Accessing Prometheus and Grafana

Once deployed, we need to access the Prometheus and Grafana web interfaces.

1. Accessing Prometheus

Check the Prometheus service:

sh
kubectl get svc -n monitoring

Forward the Prometheus server port:

sh
kubectl port-forward svc/prometheus-kube-prometheus-prometheus 9090 -n monitoring

Now, open http://localhost:9090 in your browser.

2. Accessing Grafana

Retrieve the Grafana admin password:

sh
kubectl get secret --namespace monitoring prometheus-grafana -o jsonpath="{.data.admin-password}" | base64 --decode
Forward the Grafana service port:
sh
kubectl port-forward svc/prometheus-grafana 3000:80 -n monitoring

Open http://localhost:3000, and log in with:

  • Username: admin
  • Password: (obtained from the previous command)

Step 3: Configuring Prometheus as a Data Source in Grafana

  1. Open Grafana and navigate to Configuration > Data Sources.
  2. Click Add data source and select Prometheus.
  3. Set the URL to:
  1. Click Save & Test to verify the connection.

Step 4: Importing Kubernetes Dashboards into Grafana

Grafana provides ready-made dashboards for Kubernetes monitoring.

  1. Go to Dashboards > Import.
  2. Enter a dashboard ID from Grafana’s dashboard repository.
  • Example: Use 3119 for Kubernetes cluster monitoring.
  1. Select Prometheus as the data source and click Import.

You’ll now have a real-time Kubernetes monitoring dashboard! 🎯

Step 5: Setting Up Alerts in Prometheus and Grafana

Creating Prometheus Alerting Rules

Create a ConfigMap for alerts:

yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: alert-rules
namespace: monitoring
data:
alert.rules: |
groups:
- name: InstanceDown
rules:
- alert: InstanceDown
expr: up == 0
for: 5m
labels:
severity: critical
annotations:
summary: "Instance {{ $labels.instance }} is down"

Apply it to the cluster:

sh
kubectl apply -f alert-rules.yaml

Configuring Grafana Alerts

  • Navigate to Alerting > Notification Channels.
  • Set up a channel (Email, Slack, or PagerDuty).
  • Define alert rules based on metrics from Prometheus.

Step 6: Enabling Persistent Storage for Prometheus

By default, Prometheus stores data in memory. To make the data persistent, configure storage:

Modify the Helm values:

yaml
prometheus:
server:
persistentVolume:
enabled: true
size: 10Gi

Apply the changes:

sh
helm upgrade prometheus prometheus-community/kube-prometheus-stack -f values.yaml

Conclusion

In this guide, we’ve set up Prometheus and Grafana to monitor a Kubernetes cluster. You now have:
Real-time dashboards in Grafana
Prometheus alerts to detect failures
Persistent storage for historical metrics

WEBSITE: https://www.ficusoft.in/devops-training-in-chennai/

Comments

Popular posts from this blog

Best Practices for Secure CI/CD Pipelines

What is DevSecOps? Integrating Security into the DevOps Pipeline

SEO for E-Commerce: How to Rank Your Online Store