Skip to main content
This article walks through the Prometheus configuration that the kube-prometheus-stack Helm chart installs by default and explains how Kubernetes Service Discovery (SD) is used to find scrape targets dynamically. You’ll see which SD role the chart uses, sample scrape configurations produced from ServiceMonitor resources, and the relabeling logic that filters endpoints to the intended targets.

Kubernetes service discovery roles

Prometheus supports multiple Kubernetes SD roles. Below is a concise reference of the most common roles and when to use them.
The kube-prometheus-stack Helm chart uses role: endpoints by default. This gives Prometheus access to all endpoints, and relabeling rules are applied so ServiceMonitor-derived jobs only scrape the intended endpoints.

Representative global configuration

Below is a cleaned excerpt of the default Prometheus configuration produced by the Helm chart. Note the kubernetes_sd_configs block is set to role: endpoints, and alerting is wired to Alertmanager discovered via ServiceMonitor.
Key points:
  • role: endpoints is used so Prometheus discovers IP:port pairs for every Service.
  • Alertmanager is discovered via labels and endpoint port selection, then referenced in alertmanagers.
  • Rule files are mounted into the rule_files section.

How ServiceMonitor resources become scrape jobs

Helm (via the Prometheus Operator flow) converts ServiceMonitor objects into Prometheus scrape_configs. Each ServiceMonitor results in a scrape job configured to search the endpoints SD stream and then apply relabeling to select only the endpoints that match the ServiceMonitor’s labels and port name. Below is a representative scrape_config for the Alertmanager ServiceMonitor and the relabeling used to keep only the intended Alertmanager endpoint.
Relabel rules explained:
  • Copy the job label into a temporary label for bookkeeping (__tmp_prometheus_job_name).
  • The keep rules ensure the discovered endpoint belongs to a Service with specific labels:
    • app=kube-prometheus-stack-alertmanager
    • release=prometheus
    • self-monitor=true These combined label checks ensure only the Alertmanager Service is kept for this job.
  • __meta_kubernetes_endpoint_port_name: http-web ensures the endpoint port is the intended HTTP port.
  • Node/Pod relabels extract the target name into node/pod labels for easier identification.
  • The namespace is recorded on the target label set.
Prometheus uses the same SD block and pattern for all ServiceMonitor-derived jobs (kubelet, apiserver, kube-controller-manager, kube-proxy, kube-state-metrics, node-exporter, CoreDNS, etc.). The relabeling is what narrows the endpoints stream down to the exact endpoints each job should scrape.

Example: API server ServiceMonitor (HTTPS + service account auth)

Some components (like the API server) require TLS and token-based authorization. The ServiceMonitor-derived scrape job includes authorization and tls_config to securely access /metrics over HTTPS:

Inspecting Service Discovery and active targets

You can verify how the SD and relabel rules behave in the Prometheus UI:
  • Status → Service Discovery shows the raw discovered endpoints from Kubernetes.
  • Status → Targets shows the final active targets Prometheus is scraping (with their UP/Down state, last scrape, and scrape duration).
A screenshot of the Prometheus web UI on the "Service Discovery" page showing a searchable list of serviceMonitor entries (kube components like apiserver, kubelet, proxy, node-exporter) with counts of active targets and "show more" buttons. The Prometheus logo and navigation bar are visible at the top.
When configured by the Helm chart and Prometheus Operator, most of these scrape jobs are created automatically from ServiceMonitor resources — you rarely need to write relabel rules from scratch unless you require custom scraping behavior.
A screenshot of the Prometheus "Targets" page showing monitored service endpoints with their UP state, labels, last scrape times, and scrape durations. Multiple serviceMonitor entries (alertmanager, apiserver, coredns, kube-proxy) with IP/metrics endpoints are listed.

Summary

  • The kube-prometheus-stack Helm chart configures Prometheus to use Kubernetes endpoints SD (role: endpoints) for broad discovery.
  • ServiceMonitor resources are translated into scrape_configs that filter the endpoints stream using relabel_configs, ensuring each job scrapes only its intended targets.
  • Use the Prometheus UI (Service Discovery and Targets) to inspect discovered endpoints and active scrapes.
  • Custom relabeling is available when you need to alter defaults or implement more selective scraping.

Watch Video