Integrating Prometheus with Grafana
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 Start…
We need to create a PVC (Persistence Volume Claim) for Prometheus. Following YAML code to do the same is as follows:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-prometheus
labels:
name: pvcprometheus
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 4Gi
Now, we need to create a service for exporting the Prometheus using NodePort.
apiVersion: v1
kind: Service
metadata:
name: prometheus-svc
labels:
app: prometheus-service
spec:
selector:
app: prometheus
type: NodePort
ports:
- nodePort: 2300
port: 9090
targetPort: 9090
name: prometheus-port
Now, we created a deployment for Prometheus. I’ve used an image that was available on Docker Hub. However, you can easily create your own & push it on the Docker Hub as per your requirement. Since creating own image has been discussed in very detail in many of my earlier projects,
The YAML code to creare the Deployment is as follows:
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus-deployment
spec:
replicas: 1
selector:
matchLabels:
tier: monitor
template:
metadata:
labels:
tier: monitor
spec:
containers:
- name: prometheus
image: prom/prometheus ports:
- containerPort: 9090 volumeMounts:
- name: prometheus-volume
mountPath: /data volumes:
- name: prometheus-volume
persistentVolumeClaim:
claimName: pvc-prometheus
Now we do the same steps with Grafana
Now Creating PVC for Grafana
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-grafana
labels:
name: pvcgrafana
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 4Gi
Next, we create a service to expose the Grafana using NodePort.
apiVersion: v1
kind: Service
metadata:
name: grafana-svc
labels:
app: grafana-service
spec:
selector:
app: grafana
type: NodePort
ports:
- nodePort: 2301
port: 3000
targetPort: 3000
name: port-grafana
Now, we create a deployment for Grafana. Here also , We have used an available image for Grafana from Dokcer Hub.
apiVersion: apps/v1
kind: Deployment
metadata:
name: grafana
labels:
app: grafana
tier: deploy
spec:
replicas: 1
selector:
matchLabels:
tier: monitoring
template:
metadata:
labels:
tier: monitoring
spec:
containers:
- name: grafana-container
image: grafana/grafana:latest ports:
- containerPort: 3000 volumeMounts:
- name: grafana-volume
mountPath: /var/lib/grafana volumes:
- name: grafana-volume
persistentVolumeClaim:
claimName: pvc-grafana
Now that all our components are ready, we’ll create a kustomization file in which we will mention the sequence in which all these files will be created. We only need to run this kustomization file & everything will be done.
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization resources:
- pvcprometheus.yml
- pvcgrafana.yml
- prometheusdeploy.yml
- grafanadeploy.yml
- prometheusservice.yml
- grafanaservice.yml
We can create this file by going to the folder in this file is present & running the following command in present & running the following commnad in cmd prompt:
$ kubectl apply -k
Prometheus & Grafana both will be launched on Kubernetes & connected to a PVC to ensure there is no data loss in the process. Both the pods will be exposed to hte outside world. Since we have used Deployment to launch this arch., we need not to worry about the pod crash. The Replica Set (RS) working in background will relaunch the pods with same PVC in case they crash somehow. No data will be lost.
All you need to do now is to open the Prometheus & Grafana in your browser using Minikube IP & the mentioned port port & start working ..!
THANK YOU!
My Linkedin — Rohan Khandelwal