DEV Community

Cover image for Monitoring a Kubernetes Cluster using Prometheus and Grafana with Helm
pankaj892
pankaj892

Posted on

Monitoring a Kubernetes Cluster using Prometheus and Grafana with Helm

Monitoring a Kubernetes cluster is essential for maintaining its health, optimizing performance, and ensuring reliability. Prometheus and Grafana are industry-standard tools that, when combined, offer robust monitoring, alerting, and visualization capabilities.
In this guide, we'll walk through deploying Prometheus and Grafana on Kubernetes using Helm charts, streamlining the setup process for efficient monitoring.

Why Use Prometheus and Grafana?

  • Prometheus:

    1. Scalability: Easily handles high cardinality metrics and dynamic service discovery within Kubernetes.
    2. Flexibility: Supports custom metrics and powerful querying capabilities.
    3. Reliability: Time-series database optimized for fast querying of metrics data.
  • Grafana:

    1. Visualization: Provides intuitive, customizable dashboards for visualizing Prometheus metrics.
    2. Alerting: Configurable alerts based on metric thresholds or specific conditions.
    3. Integration: Easily integrates with various data sources, including Prometheus, to consolidate monitoring data.

Prerequisites

  1. Kubernetes Cluster: Ensure you have access to a Kubernetes cluster where you can deploy applications. You can set it up locally using KinD/Minikube or use one hosted on the cloud.
  2. Helm: Install Helm on your local machine and initialize it to connect to your Kubernetes cluster. If you haven't installed Helm yet, follow the instructions here.

Deploying Prometheus with Helm

  • Step 1: Add Prometheus Helm Repository
$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
$ helm repo update
Enter fullscreen mode Exit fullscreen mode
  • Step 2: Install Prometheus
$ helm install prometheus prometheus-community/prometheus
Enter fullscreen mode Exit fullscreen mode

This command installs Prometheus with default configurations suitable for monitoring Kubernetes.

  • Step 3: Access Prometheus UI To access the Prometheus UI, port-forward the Prometheus server pod to your local machine:
$ kubectl port-forward service/prometheus-server 9090:80
Enter fullscreen mode Exit fullscreen mode

Open http://localhost:9090 in your web browser to access the Prometheus expression browser and other features.

Prometheus UI

Deploying Grafana with Helm

Grafana can also be easily deployed using the official Helm chart from the Grafana community repository.

  • Step 1: Add Grafana Helm Repository
helm repo add grafana https://grafana.github.io/helm-charts
helm repo update
Enter fullscreen mode Exit fullscreen mode
  • Step 2: Install Grafana
helm install grafana grafana/grafana
Enter fullscreen mode Exit fullscreen mode

This installs Grafana with a default admin password. Retrieve the password using:

kubectl get secret --namespace default grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo
Enter fullscreen mode Exit fullscreen mode
  • Step 3: Access Grafana UI To access the Grafana UI, port-forward the Grafana pod to your local machine:
kubectl port-forward service/grafana 3000:3000
Enter fullscreen mode Exit fullscreen mode

Open http://localhost:3000 in your web browser. Log in with username admin and the password retrieved earlier.

Grafana Home

After logging in you are greeted with the following UI
Grafana UI

Configuring Prometheus Data Source in Grafana

Once both Prometheus and Grafana are running, we need to configure Grafana to use Prometheus as a data source.

  • Navigate to Configuration > Data Sources > Add data source.

  • Choose Prometheus as the type.

Choose Data Source Page

Test Prometheus Connection

Importing Grafana Dashboards

Grafana provides pre-built dashboards for monitoring Kubernetes clusters. You can import these dashboards to visualize metrics collected by Prometheus.

  • Go to Create > Import in Grafana.

  • Use the dashboard IDs from Grafana's official repository or community dashboards.

  • I am using the official one from Grafana you can find it here

Prometheus ID

  • Select the Prometheus data source you configured earlier.

  • And now we can see our dashboard which is collecting metrics from Prometheus and displaying it on dashboard in realtime

Grafana Dashboard

  • You can customize the imported dashboard as needed to monitor your Kubernetes cluster effectively.

Conclusion

Deploying Prometheus and Grafana using Helm charts simplifies the setup and configuration of monitoring for your Kubernetes cluster. This setup provides scalable metric collection, robust visualization capabilities, and flexible alerting mechanisms. By leveraging these tools, you can gain deep insights into your Kubernetes infrastructure's performance and ensure proactive management of your applications.

If you have any questions comment it down I'll try to help

Top comments (0)