Integrate Prometheus and Grafana over Kubernetes Cluster

Integrate Prometheus and Grafana over Kubernetes Cluster

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:

  1. Deploy them as pods on top of Kubernetes by creating resources Deployment, ReplicaSet, Pods or Services.
  2. And make their data to be remain persistent.
  3. 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

No alt text provided for this image

Dockerfile used to build Grafana image

No alt text provided for this 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

No alt text provided for this image

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

No alt text provided for this image

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

No alt text provided for this image

This is the code creating a PVC for Prometheus

No alt text provided for this image

Step 3 -> Create a deployment for both prometheus and grafana that will create the pod, mount the pv.

Deployment for Grafana

No alt text provided for this image

Deployment for Prometheus

No alt text provided for this image

Finally, kustomization.yml to bind all these files together and execute in a particular order

No alt text provided for this image

To execute kustomization file the this command

kubectl create -k .

No alt text provided for this image

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

No alt text provided for this image

Now head over to the Kubernetes cluster IP on the exposed port and see the output as below.

No alt text provided for this image
No alt text provided for this image

GitHub link where all codes are available


Amit Sharma

CKA || 1xAWS || 4xGCP || 1xAzure || 2xRedHat Certified || DevOps Engineer [???????]@Searce Inc || Freelancer || Terraform || Ansible || GitLab || Jenkins || Kubernetes || Docker || Openshift || AWS || GCP || Azure

4 年

?????????? great

回复
Rohit K.

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.

要查看或添加评论,请登录

Girish Choudhary的更多文章

社区洞察

其他会员也浏览了