Deploy Prometheus on Kubernestes Cluster with Persistent Volume

Introduction

Prometheus is an free and open-source systems monitoring and alerting toolkit originally built at SoundCloud. It is designed to collect and store time-series data, and provide a powerful query language to analyze and alert on that data.

Kubernetes is a free and open source container management tool, we can deploy and manage containerized applications across a cluster of nodes. It provides features such as automated rollouts and rollbacks, self-healing, load balancing, and scaling, all of which make it easier to manage and scale containerized applications.

In this post, We will show you how to deploy Prometheus on Kubernetes cluster.

Step 1: Deploy Storage

To create a storage configuration yaml file.

nano prometheus-storage.yaml

Paste the following storage configuration.

kind: PersistentVolume
apiVersion: v1
metadata:
  name: prometheus-pv-volume
  labels:
    type: local
    app: prometheus
spec:
  storageClassName: manual
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/prometheus_data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: prometheus-pv-claim
  labels:
    app: prometheus
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Gi

We need to replace the k8s node mounting path and storage capicity and save the edit from nano editor.

To deploy the prometheus storage.

kubectl create -f prometheus-storage.yaml

To validate the volume storage.

kubectl get pvc

Step 2: Deploy Prometheus

To create prometheus-deployment yaml file.

nano prometheus-deployment.yaml

Paste the following deployment config like replica and docker container repo and ports.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus:latest
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 9090
          volumeMounts:
            - mountPath: /prometheus/data
              name: prometheus
      volumes:
        - name: prometheus
          persistentVolumeClaim:
            claimName: prometheus-pv-claim

To create Prometheus deployment.

kubectl create -f prometheus-deployment.yaml

To validate the deployment.

kubectl get deployment

To validate the pods.

kubectl get pods

Step 3: Deploy Prometheus Service

To make prometheus accessible from the network we need to create prometheus service yaml file.

nano prometheus-service.yaml

Paste the following configuration.

apiVersion: v1
kind: Service
metadata:
  name: prometheus-service
  labels:
    app: prometheus
spec:
  type: NodePort
  ports:
   - port: 9090
  selector:
   app: prometheus

We need to replace the port number and network type as per your needs and save edit from nano editor.

To deploy service of Prometheus.

kubectl create -f prometheus-service.yaml

To validate the service.

kubectl get svc

Step 4: Accessing Prometheus

We need to use the external IP address / dns name with port 9090 to get Prometheus server on the browser.

Step 5: Clean Up

If we want to destroy the Prometheus resources, We can do that by following given commands.

To destroy Prometheus service.

kubectl delete svc prometheus-service

To destroy Prometheus deployment.

kubectl delete deployment prometheus

Conclusion

We have successfully deployed the Prometheus server on the K8S cluster. If you are still facing problems, feel free to leave a commit. 

Author

Deploy Prometheus on Kubernestes Cluster with Persistent Volume

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top