INTEGRATE PROMETHEUS AND GRAFANA ON THE TOP OF KUBERNETES
Abhilash Sharma
Android Developer @SISGAIN | Android App Development | Kotlin | Java
Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.
Prometheus is a free software application used for event monitoring and alerting. It records real-time metrics in a time series database built using a HTTP pull model, with flexible queries and real-time alerting.
Grafana is a 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.
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
Steps wise explaination of task:-
1. Deploy Prometheus and Grafana as pods on top of Kubernetes by creating resources Deployment, ReplicaSet, Pods or Services.
Now let us create the required PVC,service and deployment to make Prometheus data persistent and expose it as well.
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: prompvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 2Gi
The above code will create persistent volume claim(PVC) for prometheus pod.
apiVersion: v1 kind: Service metadata: name: prometheus labels: app: prm spec: ports: - port: 9090 selector: app: prm type: NodePort
The above code will create service for Prometheus pod.
apiVersion: apps/v1 kind: Deployment metadata: name: deployprom labels: app: prm spec: replicas: 1 selector: matchLabels: app: prm strategy: type: Recreate template: metadata: labels: app: prm spec: containers: - image: vimal13/prometheus name: prm ports: - containerPort: 9090 name: prm volumeMounts: - name: configpr mountPath: /prometheus-2.19.2.linux-amd64/prometheus.yml subPath: prometheus.yml - name: prompvc mountPath: /prometheus-2.19.2.linux-amd64/data volumes: - name: configpr configMap: name: configpr - name: prompvc persistentVolumeClaim: claimName: prompvc
The above code creates deployment for Prometheus.
This is the code for creating one service, PVC, and deployment resource for launch, expose and make Grafana persistent...
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: grafpvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 2Gi
The above code creates persistent volume claim for Grafana.
apiVersion: v1 kind: Service metadata: name: svcgrafana labels: app: grf spec: ports: - port: 3000 selector: app: graf type: NodePort
The above code will create service for Prometheus pod.
apiVersion: apps/v1 kind: Deployment metadata: name: deploygraf labels: app: grf spec: replicas: 1 selector: matchLabels: app: grf strategy: type: Recreate template: metadata: labels: app: grf spec: containers: - image: vimal13/grafana name: grf ports: - containerPort: 3000 name: grf volumeMounts: - name: grafpvc mountPath: "/usr/share/grafana/data" volumes: - name: grafpvc persistentVolumeClaim: claimName: grafpvc
The above code creates Deployment for Grafana .
Prometheus is configured via command-line flags and a configuration file. While the command-line flags configure immutable system parameters (such as storage locations, amount of data to keep on disk and in memory, etc.), the configuration file defines everything related to scraping jobs and their instances, as well as which rule files to load.
so we need to create a ConfigMap for attaching config file with prometheus server...
apiVersion: v1 kind: ConfigMap metadata: name: config labels: app: prm data: prometheus.yml: | global: scrape_interval: 15s evaluation_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090']
Now we can use kustomization file to launch the whole setup in a click
apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - configmap.yml - pvcprom.yml - svcprom.yml - promdeploy.yml - svcgraf.yml - pvcgraf.yml - graphdeploy.yml
Now execute the kustomization file run the following command:
kubectl apply -k .
We can check all the resources using the following command:
kubectl get all
This gives the port for Prometheus and Grafana as well in our case it is 31373,31872 respectively for Grafana and Prometheus.
The following are respective Pometheus and Grafana consoles after launching the resources.
To open Prometheus we need to use the IP along with the port allotted and same for the Grafana.
The above image shows Prometheus (port no:-31872) integrated with Grafana (port no:- 31373).
The end.