Integrate Prometheus and Grafana over Kubernetes Cluster
Girish Choudhary
Software Engineer @ Us2.ai | ICPC | Python | Django | JavaScript | node.js | React | AWS | GCP | Docker | K8s | DevOps | MLOps | ML/AI
We will be using Prometheus and Grafana in this task
let's start with their introduction
Prometheus:
Prometheus is an open-source system monitoring and alerting toolkit originally built at SoundCloud.
Prometheus's main features are:
- a multi-dimensional data model with time series data identified by metric name and key/value pairs
- PromQL, a flexible query language to leverage this dimensionality
- no reliance on distributed storage; single server nodes are autonomous
- targets are discovered via service discovery or static configuration
- multiple modes of graphing and dashboarding support
Grafana:
Grafana is multi-platform open-source analytics and interactive visualization web application. It provides charts, graphs, and alerts for the web when connected to supported data sources. It is expandable through a plug-in system. End users can create complex monitoring dashboards using interactive query builders.
Grafana connects with every possible data source, commonly referred to as databases such as Graphite, Prometheus, Influx DB, ElasticSearch, MySQL, PostgreSQL, etc.
Grafana being an open-source solution also enables us to write plugins from scratch for integration with several different data sources.
The tool helps us study, analyze & monitor data over a period of time, technically called time-series analytics.
if you want to see the working of Graphana use this link: https://play.grafana.org/
Task
Integrate Prometheus and Grafana and perform in following way:
- Deploy them as pods on top of Kubernetes by creating resources Deployment, ReplicaSet, Pods or Services.
- And make their data to be remain persistent.
- And both of them should be exposed to outside world.
let's get started:-
we will be needing individual Docker images of Prometheus and Grafana.
Dockerfile used to build Prometheus image
Dockerfile used to build Grafana image
for building these images use
docker build -t image_name:version /path
after build upload these images on docker hub using docker push image_name
now its time for YAML codes
Step 1 -> Create a Persistent Volume on kubernetes that will be used for storing data for both prometheus and grafana.
This is the code creating a PV for Grafana
Host path is the location of the folder for which we want to make persistent that will be mounted on the pods whenever it is created.
This is the code creating a PV for Prometheus
Step 2 -> Create a Persistent Volume Claim on Kubernetes that will provide the storage for storing data for both Prometheus and Grafana.
This is the code creating a PVC for Grafana
This is the code creating a PVC for Prometheus
Step 3 -> Create a deployment for both prometheus and grafana that will create the pod, mount the pv.
Deployment for Grafana
Deployment for Prometheus
Finally, kustomization.yml to bind all these files together and execute in a particular order
To execute kustomization file the this command
kubectl create -k .
Expose the ports using
kubectl expose deployment prom-deploy --port=9090 --type=NodePort kubectl expose deployment grafana-deploy --port=3000 --type=NodePort
after exposing use kubectl get all
Now head over to the Kubernetes cluster IP on the exposed port and see the output as below.
GitHub link where all codes are available
CKA || 1xAWS || 4xGCP || 1xAzure || 2xRedHat Certified || DevOps Engineer [???????]@Searce Inc || Freelancer || Terraform || Ansible || GitLab || Jenkins || Kubernetes || Docker || Openshift || AWS || GCP || Azure
4 年?????????? great
Meta Top Security Researcher 2018 - 2023 | Building AI & LLM Security Products | BlackHat Speaker | CVE-2024-44330 | CVE-2024-9145
4 年But we can also collect data without Prometheus right? We can use grafana only to pull data and shoe charts. I don't know, if it's compulsory in kubernetes or not, but i don't think Prometheus is that much important in general. Let me know if I am wrong here.