Monitoring your Kubernetes cluster is critical for ensuring that your services are always available and running. And before you scour the internet for a monitoring system, why not try Grafana and Prometheus Kubernetes cluster monitoring?
Grafana and Prometheus Kubernetes Cluster Monitoring report on potential performance bottlenecks, cluster health, and performance metrics. Visualize network consumption, pod resource utilization trends, and a high-level summary of cluster activity all at once.
In this article, you will learn what are Prometheus and Grafana for Kubernetes monitoring and how to monitor your Kubernetes cluster, viewing internal state metrics with Prometheus and the Grafana dashboard.
Table of contents:
- Introduction to Prometheus and Grafana
- How to monitor Kubernetes cluster using Prometheus and Grafana
- Create Namespace and Add Helm Charts Repo
- Deploying Helm Charts to Created Namespace
- Accessing the Prometheus Instance and Viewing the Internal State Metrics
- Visualizing a Cluster’s Internal State Metric on Prometheus
- Monitoring and Visualizing with Grafana Dashboards
- Conclusion
- Introduction to Prometheus and Grafana
Prometheus
There are many open-source time-series databases available today, including Graphite, InfluxDB, and Cassandra, but none are as well-liked among Kubernetes users as Prometheus. Prometheus, which started out as a SoundCloud project and is now a part of CNCF (Cloud Native Computing Foundation), has become the open-source de-facto standard for monitoring Kubernetes.
In a nutshell, Prometheus's multi-dimensional data model, PromQL (the Prometheus querying language), built-in alerting features, a pull vs. push approach, and, of course, the always expanding community are what set it apart from other time-series databases. The two projects are now tightly connected, allowing users to quickly operate Prometheus on top of Kubernetes using the Prometheus Operator. These differentiators make Prometheus a great option for Kubernetes users.
Grafana
An open-source, cross-platform online application for interactive visualization and analytics is called Grafana. When connected to supported data sources, it offers charts, graphs, and alerts for the web. It is extendable using a plug-in architecture. A licensed Grafana Enterprise version with additional features is also available as a self-hosted installation or an account on the Grafana Labs cloud service. Using interactive query builders, end users may design sophisticated monitoring dashboards. The front end and back end of Grafana are each composed of TypeScript and Go code, respectively.
There are a number of reasons Grafana is so popular, its ability to integrate with a long list of data sources being one of them. Grafana is extremely robust, featuring a long list of capabilities such as alerts, annotations, filtering, data source-specific querying, visualization and dashboarding, authentication/authorization, cross-organizational collaboration, and plenty more.
- How to monitor Kubernetes cluster using Prometheus and Grafana: step by step guide
If you want to create your own monitoring dashboard for your Kubernetes cluster. You’ll be using Prometheus (https://prometheus.io//) to handle the extraction, transformation, and loading of all your assets from the Kubernetes cluster. Grafana will be used for querying, visualizing, monitoring, and notifying.
Prerequisites:
- Docker: To install Docker on your local machine, follow the download instructions.
- Kubectl: To install Kubectl on your local machine, follow the download instructions.
- Helm: To install Helm on your local machine, follow the download instructions (https://helm.sh/docs/intro/install/). It will be used for deploying the Prometheus operator.
- Kubernetes cluster: To install Kubernetes on your local machine, follow the download instructions.
- Create Namespace and Add Helm Charts Repo
The first step is to create a namespace in the Kubernetes cluster. It will establish a separate place in your Kubernetes cluster for the Prometheus and Grafana servers to be deployed into.
kubectl create namespace monitoring
After you have run the above commands, you need to add the Prometheus-community helm repo and also update the repo. To do so, run the following commands.
# Add prometheus-community repo
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
# Update helm repo
helm repo update
- Deploying Helm Charts to Created Namespace
Run the helm install command listed below to deploy the Helm chart for the kube-prometheus stack after adding the Helm repo. You can use any other name in place of "monitoring."
This Helm chart configures a thorough Prometheus Kubernetes monitoring stack by acting on a collection of Custom Resource Definitions (CRDs). Run the following commands to deploy the chart to the Kubernetes cluster.
helm install prometheus prometheus-community/kube-prometheus-stack --namespace monitoring
To verify the deployment of your Kube-Prometheus stack, run the following command.
- Accessing the Prometheus Instance and Viewing the Internal State Metrics
The next step is to utilize Prometheus and Grafana to monitor your Kubernetes cluster after you have successfully deployed their instances there. To accomplish this, we must allow traffic to the Prometheus pod in order to monitor the internal metrics of your cluster. You will be able to use your browser to access the Prometheus server as a result. To find out which Prometheus server you will be redirecting traffic to, run the command below.
Next, run the below kubectl port-forward command to forward the local port 9090 to your cluster via the Prometheus service (svc/monitoring-kube-prometheus-prometheus). To do so run the following commands.
Open a web browser, and navigate to either of the URLs below to access your Prometheus instance.
Navigate to http://localhost:9090 if you’re following along with local windows, Linux, or mac machines. Navigate to your server’s IP address followed by port 9090 (i.e., http://YOUR_SERVER_IP:9090) if you’re using a cloud server.
- Visualizing a Cluster’s Internal State Metric on Prometheus
Now, to view the internal status metrics of our Kubernetes cluster, we will now run some Prometheus queries. We'll concentrate on CPU usage. For additional information on the Prometheus query language, You can visit Prometheus’ official website.
- Monitoring and Visualizing with Grafana Dashboard
Prometheus visualization options are limited, only a Graph. Prometheus is great for collecting metrics from targets set as tasks, aggregating the metrics, and storing them locally on the workstation. However, when it comes to traditional resource monitoring adding Grafana is a great option.
Metrics produced by server components such as node exporter, CoreDNS, and others are collected by Prometheus. Grafana obtains these metrics from Prometheus and displays them in a number of ways on its dashboard.
- Conclusion
In this article, we learned about Kubernetes monitoring using Prometheus and Grafana. We also saw step by step guide to set up Prometheus to Kubernetes cluster nodes and Grafana dashboard to visualize key internal metrics
References:
[1] https://adamtheautomator.com/prometheus-kubernetes/
[2] https://earthly.dev/blog/grafana-and-prometheus-k8s/
[3] https://grafana.com/grafana/dashboards/315-kubernetes-cluster-monitoring-via-prometheus/
[4] https://logz.io/blog/open-source-monitoring-tools-for-kubernetes/