Prometheus Service Discovery Not Finding Targets Fix
In this tutorial, you'll learn about Prometheus Service Discovery Not Finding Targets Fix. We cover key concepts, practical examples, and best practices.
Your Prometheus service discovery finds zero targets — the SD provider is configured but returns 0 targets under Status → Service Discovery. The API credentials are wrong, the SD role is misconfigured, or the services are not labeled for discovery.
The Problem
scrape_configs:
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
Service Discovery: kubernetes-pods — 0 targets
Prometheus uses the in-cluster service account to discover pods. If the service account lacks permissions or the cluster is not reachable, zero targets are returned.
Step-by-Step Fix
1. Verify service account permissions
# RBAC for Prometheus service account
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups: [""]
resources: ["pods", "services", "endpoints", "nodes", "nodes/metrics"]
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. Configure SD providers correctly
scrape_configs:
# Node discovery
- job_name: 'kubernetes-nodes'
kubernetes_sd_configs:
- role: node
# Pod discovery
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
regex: "true"
action: keep
# Service discovery
- job_name: 'kubernetes-services'
kubernetes_sd_configs:
- role: service
3. Check SD status in Prometheus UI
# Open Prometheus UI → Status → Service Discovery
# Check each SD provider for:
# - Number of discovered targets
# - Last error message
# - Labels available per target
# API endpoint for SD status
curl http://prometheus:9090/api/v1/status/discovery
4. Configure Consul SD
scrape_configs:
- job_name: 'consul-services'
consul_sd_configs:
- server: 'localhost:8500'
datacenter: 'dc1'
services:
- 'web'
- 'api'
relabel_configs:
- source_labels: [__meta_consul_service]
regex: '(.*)'
target_label: service
replacement: '${1}'
5. Configure EC2 SD
scrape_configs:
- job_name: 'ec2-instances'
ec2_sd_configs:
- region: us-east-1
access_key: 'AKIA...'
secret_key: '...'
port: 9100
Expected output:
Service Discovery: kubernetes-pods — 150 targets
Labels: __meta_kubernetes_pod_label_app, __meta_kubernetes_pod_label_version, ...
Targets: 10.0.0.1:8080, 10.0.0.2:8080, ...
Prevention Tips
- Verify RBAC permissions for Kubernetes SD
- Check Prometheus server logs for SD errors
- Use Prometheus UI → Status → Service Discovery to debug
- Use relabel_configs to filter discovered targets
- Start with one SD provider before adding more
Common Mistakes with service discovery
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty 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