Prometheus and Grafana Monitoring Lab¶
- In this lab, we will learn how to set up and configure *
PrometheusandGrafanafor monitoring a Kubernetes cluster. - You will install
Prometheusto collect metrics from the cluster andGrafanato visualize those metrics. - By the end of this lab, you will have a functional monitoring stack that provides insights into the health and performance of your Kubernetes environment.
Prometheus and Grafana Setup and Configuration Guide¶
- This guide serves as a comprehensive walkthrough of the steps to set up
PrometheusandGrafanaon your Kubernetes cluster. - It includes hands-on steps for installing
PrometheususingHelm, configuringPrometheusto collect metrics, setting upGrafanato visualize key metrics, and automating the setup using a bash script.
Introduction to Prometheus and Grafana¶
Prometheus¶
Prometheusis an open-source systems monitoring and alerting toolkit designed for reliability and scalability.- It collects and stores metrics as time-series data, providing powerful querying capabilities.
- It is commonly used in Kubernetes environments for monitoring cluster health, application performance, and infrastructure.
Grafana¶
Grafanais a popular open-source data visualization tool that works well withPrometheus.- It allows you to create dashboards and visualize metrics in real-time, providing insights into system performance and application health.
Grafanasupports a wide range of visualization options, includinggraphs,heatmaps,tables, and more.- Together,
PrometheusandGrafanaprovide a powerful stack for monitoring and alerting in Kubernetes.
Part 01 - Installing Prometheus and Grafana¶
Helm Charts
We will use Helm, to deploy Prometheus and Grafana.
Step 01 - Add Prometheus and Grafana Helm Repositories¶
- Let’s add the official
Helmcharts forPrometheusandGrafana:
# Add Prometheus community Helm repository
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
# Add Grafana Helm repository
helm repo add grafana https://grafana.github.io/helm-charts
# Update your Helm repositories to make sure they are up-to-date
helm repo update
Step 02 - Install Prometheus Stack¶
Prometheusis installed using theprometheus-stackHelmchart.
# Install Prometheus
# Alertmanager
# Node Exporter
# Create the `monitoring` namespace if it does not exist.
helm install prometheus \
--namespace monitoring \
--create-namespace \
prometheus-community/kube-prometheus-stack
# Verify the status of the release using the following:
helm status prometheus -n monitoring
Step 03 - Install Grafana¶
- Now, let’s install
Grafana. Grafanawill be deployed in the samemonitoringnamespace.
helm install grafana grafana/grafana --namespace monitoring
# Verify the status of the release using the following:
helm status grafana -n monitoring
Step 04 - Access Grafana¶
Grafanawill expose a service in your Kubernetes cluster.- To access it, you need a password and port forwarding.
# In order to get the Grafana admin password, run the following command:
kubectl get secret grafana \
--namespace monitoring \
-o jsonpath='{.data.admin-password}' | base64 --decode ; echo
# Set the port forwarding so you can access the service using your browsers
kubectl port-forward \
--namespace monitoring \
service/grafana 3000:80
- Verify that you can access
**Grafana - Open your browser and navigate to http://localhost:3000
- The default login is:
- Username :
admin - Password : (the password you retrieved earlier)
- Username :
Accessing Grafana on Google Cloud Shell
If you are running your cluster in Google Cloud Shell, you cannot use localhost for port forwarding. Instead, use the Cloud Shell Web Preview:
- Run the port-forward command as usual:
- In Google Cloud Shell, click the “Web Preview” button (top right) and select “Preview on port 3000”.
- Grafana will open in a new browser tab.
- Username:
admin - Password: (the password you retrieved earlier)
Note: You can use any available port (e.g., 3000, 3001) in the port-forward command, just match it in the Web Preview.
Part 02 - Configuring Prometheus¶
Prometheuscan collect various metrics from your Kubernetes cluster automatically if the right exporters are enabled.- The kube-prometheus-stack chart that you installed earlier automatically configures
Prometheusto scrape a number of Kubernetes components (likekubelet,node-exporter, andkube-state-metrics) for various metrics.
Step 01 - Verify Prometheus Metrics Collection¶
- You can check if
Prometheusis correctly scraping metrics by navigating toPrometheus’ web UI.
# Port-forward the Prometheus service:
kubectl port-forward \
--namespace monitoring \
svc/prometheus-operated 9090:9090
- Verify that you can access
Prometheus - Open http://localhost:9090
- In the expression field paste the following:
# This query will show the current status of the `kube-state-metrics` job
up{job="kube-state-metrics"}
Accessing Prometheus on Google Cloud Shell
If you are running your cluster in Google Cloud Shell, you cannot use localhost for port forwarding. Instead, use the Cloud Shell Web Preview:
- Run the port-forward command as usual:
- In Google Cloud Shell, click the “Web Preview” button (top right) and select “Preview on port 9090”.
- Prometheus will open in a new browser tab.
Note: You can use any available port (e.g., 9090, 9091) in the port-forward command, just match it in the Web Preview.
Part 03 - Configuring Grafana¶
- In this part we will set
grafanato display the Cluster’s CPUs, Memory, and Requests. Grafanadashboards can be configured to display real-time metrics for CPU, memory, and requests.Prometheusstores these metrics andGrafanawill queryPrometheusto display them.
Step 01 - Add Prometheus as a Data Source in Grafana¶
- Log into
Grafanaat: http://localhost:3000, or use the Cloud Shell Web Preview. - Click on the hamburger icon on the left sidebar to open the Configuration menu.
- Click on Data Sources.
- Click Add data source and choose Prometheus.
- In the URL field, enter the Prometheus server URL:
http://prometheus-operated:9090. - Click Save & Test to confirm that the connection is working.
Step 02 - Create a Dashboard to Display Metrics¶
- Next step is to create a dashboard and panels to display the desired metrics.
-
To create a dashboard in
Grafanafor CPU, memory, and requests do the following: -
In
Grafana, open the left sidebar menu and select Dashboard. - Click Add visualization.
- Choose
Data Source(as we defined it previously). - In the panel editor, click on the
Codeoption (right side of the query builder). - Enter the below queries to visualize metric(s): Note: To add new query click on the
+ Add query - Save the dashboard.
- CPU Usage
sum(rate(container_cpu_usage_seconds_total{namespace="default", container!="", container!="POD"}[5m])) by (pod, namespace)
- Memory Usage :
sum(container_memory_usage_bytes{namespace="default", container!="", container!="POD"}) by (pod, namespace)
- Request Count :
sum(rate(http_requests_total{job="kubelet", cluster="", namespace="default"}[5m])) by (pod, namespace)
Step 03 - Get Number of Pods in the Cluster¶
- To track the number of pods running in the cluster, add new panel with the following query:
# This query counts the number of pods running in all the namespaces
count(kube_pod_info{}) by (namespace)
- Add another query which will count the number of pods under the namespace
monitoring:
Tip
We have already defined query based upon namespaces before.... You can use the same approach to filter by other labels as well.
Step 04: Customize the Panel¶
- Change the visualization by changing the Graph Style