Integration of Prometheus and Grafana and Make their data to be remain Persistent

Integration of Prometheus and Grafana and Make their data to be remain Persistent

Task Overview:

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 Begin :)

Github Link :

To Perform this task, We create a YAML file for each services like Deployment, Service, PVC. We have created total 7 yaml files in the same directory :

No alt text provided for this image

First we will create the PVC i.e Persistent Volume Claim for both Prometheus and Grafana to make their data remain Persistent(Permanent). This file will create the storage.

prometheus-pvc.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: prometheus-pvc
  labels: 
    name: prompvc


spec:
  accessModes: 
  - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

grafana-pvc.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-pvc
  labels:
    name: grafanapvc


spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

After creating the Storage for both Grafana and prometheus, we will create Deployment yaml file for both. This file will launch our pod using the given image. In last we are attaching our storage which we have created using the pvc.

prometheus-deployment.yml

apiVersion: apps/v1
kind: Deployment


metadata:
  name: prometheus-deployment
  labels: 
    app: prom


spec:
  selector:
    matchLabels:
      app: prom
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: prom
    spec:
      containers:
      - image: vimal13/prometheus
        name: prometheus-container


        volumeMounts:
        - name: prometheus-storage
          mountPath: /prometheus
       
      volumes:
      - name: prometheus-storage
        persistentVolumeClaim:
          claimName: prometheus-pvc

grafana-deployment.yml

apiVersion: apps/v1
kind: Deployment


metadata:
  name: grafana-deployment
  labels: 
    app: grafana


spec:
  selector:
    matchLabels:
      app: grafana
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
      - image: grafana/grafana
        name: grafana-container


        volumeMounts:
        - name: grafana-storage
          mountPath: /var/lib/grafana
       
      volumes:
      - name: grafana-storage
        persistentVolumeClaim:
          claimName: grafana-pvc

After creating the PVC and Deployment, we create the Service file for both prometheus and grafana. This file we will set the port number where we want our prometheus and grafana will run the service. Also we are Exposing it to the Outside world.

prometheus-svc.yml

apiVersion: v1
kind: Service
metadata:
  name: prometheus-svc
  labels: 
    app: prom


spec:
  ports:
    - port: 9090
      name: prom 
      nodePort: 30000
  type: NodePort
  selector:
    app: prom

grafana-svc.yml

apiVersion: v1
kind: Service
metadata:
  name: grafana-svc
  labels:
    app: grafana


spec: 
  ports:
    - port: 3000
      name: grafana
      nodePort: 32000
  type: NodePort
  selector:
    app: grafana

After this, we will create one Kustomization yaml file. This file will run all the file in one single go in the written order of file name, and also help us to deploy the whole environment in just a single command.

kustomization.yml

apiVersion: kustomize.config.k8s.io/v1beta1

kind: Kustomization

resources:
  - prometheus-pvc.yml
  - grafana-pvc.yml
  - prometheus-deployment.yml
  - grafana-deployment.yml
  - prometheus-svc.yml
  - grafana-svc.yml

We have to only run one single command for running this Kustomization file. This file will create the whole environment for us.

To run this kustomization file we will run the below cmd :

kubectl apply -k .

And now our Deployment has been configured as well as we have created the pvc, which will remain our data permanent :)

Output after running the above cmd :

No alt text provided for this image

To use the services of Prometheus and Grafana, we have to use following ip :

<minikube ip>:<port number we have given in service file>

Note: We will get the minikube ip by simply running the cmd : minikube ip in the cmd prompt.

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

Now lets see the advantage of creating the PVC i.e Persistent Volume Claim. We are intentionally deleting all the Services and Deployment that we have created and again launch these services. But what we will see that, our data won't be deleted and this is the important role of PVC.

No alt text provided for this image

Note : Here in the above image, we can clearly see, how our graph going down when we deleted all the services and again graph started going up when we relaunch all the services.

Thanks to Mr. Vimal daga sir for giving us this Task.

Thanks for Reading :)

Vikas Kumar

Software Engineer @evalueserve | Full Stack developer (MERN) | Node.js | Python | JavaScript | DB | Docker

4 年

great work sourabh

Sanju Pal

Software Engineer 2 @ Progress | Golang & Kubernetes Specialist | Building Cloud-Native Solutions

4 年

Great????

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

Sourabh Choudhary的更多文章

社区洞察

其他会员也浏览了