Prometheus Kubernetes Pod Discovery Not Working Fix
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
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro