Services DevOps DevSecOps Cloud Consulting Infrastructure Automation Managed Services AIOps MLOps DataOps Microservices 🔐 Private AINEW Solutions DevOps Transformation CI/CD Automation Platform Engineering Security Automation Zero Trust Security Compliance Automation Cloud Migration Kubernetes Migration Cloud Cost Optimisation AI-Powered Operations Data Platform Modernisation SRE & Observability Legacy Modernisation Managed IT Services 🔐 Private AI DeploymentNEW Products ✨ ZippyOPS AINEW 🛡️ ArmorPlane 🔒 DevSecOpsAsService 🖥️ LabAsService 🤝 Collab 🧪 SandboxAsService 🎬 DemoAsService Bootcamp 🔄 DevOps Bootcamp ☁️ Cloud Engineering 🔒 DevSecOps 🛡️ Cloud Security ⚙️ Infrastructure Automation 📡 SRE & Observability 🤖 AIOps & MLOps 🧠 AI Engineering 🎓 ZOLS — Free Learning Company About Us Projects Careers Get in Touch

How to Set Up Prometheus Monitoring in Kubernetes

How to Set Up Prometheus Monitoring in Kubernetes

In today’s tech landscape, monitoring is crucial for ensuring the health and performance of your infrastructure. Prometheus monitoring in Kubernetes is an essential practice for DevOps teams, enabling them to detect issues, troubleshoot system problems, and optimize resource management. In this guide, we’ll walk you through the process of setting up Prometheus in Kubernetes, step-by-step, while also highlighting some key best practices for managing this powerful tool.

Prometheus monitoring setup in Kubernetes dashboard

Why Use Prometheus for Kubernetes Monitoring?

Prometheus is an open-source monitoring and alerting toolkit that helps teams monitor system performance and health. When integrated with Kubernetes, it provides visibility into metrics such as CPU usage, memory consumption, and network traffic across your containers. By leveraging Prometheus monitoring in Kubernetes, organizations can proactively manage system health and resource usage.

ZippyOPS offers consulting, implementation, and managed services, specializing in DevOps, DataOps, and Cloud solutions. With expertise in AIOps, MLOps, and infrastructure security, ZippyOPS can help streamline your monitoring setup and ensure your Kubernetes clusters are optimized for performance. Explore our services and solutions for tailored support.

Setting Up Prometheus Monitoring in Kubernetes

To get started with Prometheus, we need to configure Kubernetes to handle its deployment and monitoring tasks. Here’s how to set it up:

1. Create a Kubernetes Namespace for Monitoring

First, create a dedicated namespace for all your monitoring components to keep things organized and avoid conflicts with other resources. To create the namespace, run the following command:

kubectl create namespace monitoring

This will set up a monitoring namespace where all Prometheus-related components will reside.

2. Create a ClusterRole and Bind it to the Monitoring Namespace

To ensure Prometheus has the necessary permissions to monitor Kubernetes resources, you need to create a ClusterRole and bind it to the monitoring namespace.

Create the ClusterRole with read access:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
  - apiGroups: [""]
    resources: ["nodes", "nodes/proxy", "services", "endpoints", "pods"]
    verbs: ["get", "list", "watch"]
  - apiGroups: ["extensions"]
    resources: ["ingresses"]
    verbs: ["get", "list", "watch"]
  - nonResourceURLs: ["/metrics"]
    verbs: ["get"]

Then, bind this role to the monitoring namespace:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus
subjects:
  - kind: ServiceAccount
    name: default
    namespace: monitoring

Apply the configuration:

kubectl create -f clusterRole.yaml

3. Create a ConfigMap for Prometheus Configuration

To ensure flexibility in managing your Prometheus configuration, externalize the Prometheus setup in a Kubernetes ConfigMap. This allows you to update configurations without rebuilding the Prometheus image.

Create a ConfigMap YAML file and copy the Prometheus configuration into it. Then, create the ConfigMap with:

kubectl create -f config-map.yaml

4. Deploy Prometheus on Kubernetes

Now, it’s time to deploy Prometheus. First, create a deployment YAML file (prometheus-deployment.yaml) to mount the Prometheus configuration as a volume and ensure the right storage paths. Here’s an example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-deployment
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus-server
  template:
    metadata:
      labels:
        app: prometheus-server
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus
          args:
            - "--config.file=/etc/prometheus/prometheus.yml"
            - "--storage.tsdb.path=/prometheus/"
          ports:
            - containerPort: 9090
          volumeMounts:
            - name: prometheus-config-volume
              mountPath: /etc/prometheus/
            - name: prometheus-storage-volume
              mountPath: /prometheus/
      volumes:
        - name: prometheus-config-volume
          configMap:
            defaultMode: 420
            name: prometheus-server-conf
        - name: prometheus-storage-volume
          emptyDir: {}

Deploy Prometheus with:

kubectl create -f prometheus-deployment.yaml

5. Expose Prometheus Service

To access the Prometheus dashboard, expose it via a Kubernetes Service. This will allow you to access the Prometheus UI using any node IP on a specified port (e.g., 30000).

Create the Prometheus Service YAML file:

apiVersion: v1
kind: Service
metadata:
  name: prometheus-service
  namespace: monitoring
  annotations:
    prometheus.io/scrape: 'true'
    prometheus.io/port: '9090'
spec:
  selector:
    app: prometheus-server
  type: NodePort
  ports:
    - port: 8080
      targetPort: 9090
      nodePort: 30000

Apply the service with:

kubectl create -f prometheus-service.yaml --namespace=monitoring

Once deployed, you can access the Prometheus dashboard by navigating to any node’s IP address with port 30000.

6. Querying Prometheus Metrics

Prometheus offers powerful querying capabilities to analyze system performance. After running for a few minutes, Prometheus will start collecting data, which can then be queried using its query language.

For instance, you can query memory usage of containers by their image:

container_memory_usage_bytes{image="CONTAINER:VERSION"}

For more flexibility, use regular expressions to query multiple image versions:

container_memory_usage_bytes{image=~"CONTAINER:.*"}

Prometheus helps you zoom out from container-level metrics to more global insights, such as monitoring memory usage across namespaces or clusters.

Best Practices for Prometheus in Kubernetes

For optimal results with Prometheus monitoring in Kubernetes, it’s essential to:

  • Regularly update your configurations through Kubernetes ConfigMaps.
  • Use efficient queries to avoid unnecessary strain on the system.
  • Continuously monitor your system’s health to ensure that Prometheus is collecting the right metrics for your infrastructure needs.

By incorporating Prometheus monitoring into your Kubernetes setup, you can gain better control over your system performance, proactively detect issues, and scale resources more effectively.

If you need assistance with Kubernetes monitoring or optimizing your infrastructure, ZippyOPS offers expert DevOps, DataOps, and cloud-based solutions. Our team can help implement a tailored MLOps, AIOps, or DevSecOps strategy for your organization. Contact us at sales@zippyops.com to get started today.

Leave a Comment

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

Scroll to Top