Skip to content

Prometheus Kubernetes Pod Discovery Not Working Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Prometheus Kubernetes Pod Discovery Not Working Fix. We cover key concepts, practical examples, and best practices.

Your Prometheus discovers zero pods from Kubernetes — Kubernetes_sd_configs returns no targets, or the discovered pods have wrong ports. The service account lacks permissions, pod annotations are missing, or relabel rules drop discovered targets.

The Problem

scrape_configs:
  - job_name: 'kubernetes-pods'
    kubernetes_sd_configs:
      - role: pod
0 targets discovered

Prometheus uses the pod's service account to query the Kubernetes API. If the service account does not have permission to list pods, zero targets are returned.

Step-by-Step Fix

1. Create RBAC permissions

apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
  - apiGroups: [""]
    resources: ["pods", "services", "endpoints", "nodes"]
    verbs: ["get", "list", "watch"]
---
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: prometheus
    namespace: monitoring

2. Annotate pods for Prometheus scraping

apiVersion: v1
kind: Pod
metadata:
  name: myapp
  annotations:
    prometheus.io/scrape: "true"
    prometheus.io/port: "8080"
    prometheus.io/path: "/metrics"
spec:
  containers:
    - name: app
      image: myapp:latest
      ports:
        - containerPort: 8080

3. Configure relabeling for pod annotations

scrape_configs:
  - job_name: 'kubernetes-pods'
    kubernetes_sd_configs:
      - role: pod
    relabel_configs:
      # Scrape only pods with prometheus.io/scrape=true
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
        regex: "true"
        action: keep
      # Use annotation port
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_port]
        regex: "(\\d+)"
        action: replace
        target_label: __port__
      # Use annotation path
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
        regex: "(.+)"
        target_label: __metrics_path__
        replacement: "${1}"

4. Discover by namespace

scrape_configs:
  - job_name: 'kubernetes-pods'
    kubernetes_sd_configs:
      - role: pod
        namespaces:
          names:
            - production
            - staging
    relabel_configs:
      - source_labels: [__meta_kubernetes_namespace]
        action: keep
        regex: 'production|staging'

5. Verify discovery

# Check Prometheus UI → Status → Service Discovery
# Look for kubernetes-pods entry
# Verify targets and labels

# Test API access from Prometheus pod
kubectl exec -it prometheus-0 -- wget -qO- \
  --ca-cert=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt \
  --header="Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
  https://kubernetes.default.svc/api/v1/pods

Expected output:

Discovered targets: 25
  pod-1 (10.0.0.1:8080) — app=myapp, env=production
  pod-2 (10.0.0.2:8080) — app=myapp, env=production
  ...

Prevention Tips

  • Create RBAC ClusterRole with pod/service/endpoint access
  • Annotate pods with <a href="/devops/prometheus-grafana/">prometheus</a>.io/scrape: "true" to opt-in
  • Use relabeling to filter and transform pod labels
  • Limit discovery to relevant namespaces
  • Verify target discovery in Prometheus UI

Common Mistakes with kubernetes discovery

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' causing stack overflow on large lists

These mistakes appear frequently in real-world PROMETHEUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What's the difference between pod and endpoint roles?

role: pod discovers individual pods and scrapes metrics directly from pod IPs. role: endpoints discovers Services and scrapes based on endpoints — useful when you want Prometheus to use the Service IP and load balancing.

How do I scrape all pods without annotations?

Omit the annotation-based keep rule to scrape all pods: remove the <a href="/devops/prometheus-grafana/">prometheus</a>.io/scrape: "true" filter. This discovers all pods in the cluster. Combine with namespace filtering to control scope.

Can I scrape metrics from sidecar containers?

Yes, each container in a pod is scraped separately if it exposes a metrics endpoint. Use __meta_kubernetes_pod_container_name in relabeling to add the container name as a label for differentiating container metrics.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro