Integrating Prometheus and Grafana with Kubernetes(K8s)
Sandeep Singh Rajawat
Former Business Technology Solutions Associate @ ZS Associate
To create different resources in Kubernetes like Pods, Deployment, Service, PVC we use YAML configuration files. Here is my YAML files to create resources.
Prom-pvc.yml & graf-pvc.yml :–
For making data persistence we need some storage and in K8s we have a resource named PV i.e. Persistance Volume to provide us some storage. But to get storage from this PV we need to request the storage, so for requesting storage we use PVC, and Persistent Volume Claim (PVC) is a request for storage by a user. So here we are creating PVC using these YAML files. prom-pvc.yml is a file for Prometheus and graf-pvc.yml is a file for Grafana.
Monitor-env-dep.yml –
Deployments represent a set of multiple, identical Pods with no unique identities. A Deployment runs multiple replicas of your application and automatically replaces any instances that fail or become unresponsive. Actually, the power of Kubernetes comes from Deployment. Running Pods without deployment will not be monitored by K8s. Here is YAML code for creating deployment
apiVersion: apps/v1 kind: Deployment metadata: name: monitoring-env-dep spec: selector: matchLabels: pod: prom-graf-pod replicas: 1 template: metadata: labels: pod: prom-graf-pod spec: containers: - name: prometheus image: sandeep2910/ubuntu_prometheus:v3 ports: - containerPort: 9090 volumeMounts: - name: prom-vol mountPath: /data/ - name: grafana image: grafana/grafana ports: - containerPort: 3000 volumeMounts: - name: graf-vol mountPath: /var/lib/grafana/ volumes: - name: prom-vol persistentVolumeClaim: claimName: prometheus-pvc - name: graf-vol persistentVolumeClaim: claimName: grafana-pvc
Service.yml –
As our Prometheus and Grafana containers are running inside Pod and which is inside the K8s cluster. If order to connect with them and to use them we need to expose them to the outside world. So to exposing we use K8s Service resource i.e. a service is an abstract way to expose an application running on a set of Pods. Here is my YAML file to create a service for our deployment which expose our pods to the outside world
apiVersion: v1 kind: Service metadata: name: monitoring-env-svc spec: type: NodePort selector: pod: prom-graf-pod ports: - port: 9090 protocol: TCP name: prom-port - port: 3000 protocol: TCP name: graf-port
Kustomization.yml -
For creating any resource using the YAML file, we have to run that file. For Bigger Infrastructure running the YAML file for every individual resource will take so much time. So in K8s, we can use the Kustomization.yml file and on running, it will create the entire K8s Infrastructure for us. In the Kustomization.yml file, we need to put names of YAML files of all the resources that we need to create.
Here's my Kustomization.yml and to run this we'll use this command "kubectl apply -k . "
apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - graf-pvc.yml - prom-pvc.yml - monitor-env-dep.yml - service.yml
Now after applying Kustomization Pods, Deployment, PVC, and Service are created for us. Just one single command "kubectl apply –k ." and complete infrastructure is created
We have also created service to expose our Pods to the public world and we see the service URL that we can use to connect to our pods by "minikube service service_name --url"
So we are connected to our Prometheus and Grafana by that service URLs
Business Technology Solutions Associate Consultant @ ZS Pune
4 年Great work??