Skip to main content
In this lesson we’ll learn how to add scrape targets to Prometheus by using the ServiceMonitor custom resource provided by the Prometheus Operator. ServiceMonitors let you declare Prometheus scrape configuration in Kubernetes objects instead of editing Prometheus config files directly. The Prometheus Operator installs several Custom Resource Definitions (CRDs) that provide higher-level abstractions for deploying and configuring Prometheus components (Prometheus instances, Alertmanagers, ServiceMonitors, PodMonitors, etc.).
The image contains text explaining that the Prometheus operator includes several custom resource definitions that provide high-level abstraction for deploying and configuring Prometheus.
List the installed CRDs:
Example truncated output:
Key CRDs and their use cases: What is a ServiceMonitor?
  • A ServiceMonitor is a CRD that defines a set of targets for Prometheus to monitor and scrape.
  • It expresses scrape configuration (endpoints, path, interval, job label) in Kubernetes objects so you don’t edit Prometheus config files directly.
  • The Prometheus Operator translates ServiceMonitor resources into Prometheus scrape_configs.
The image explains that service monitors define targets for Prometheus and provide a declarative Kubernetes syntax to avoid direct configuration changes.
Example flow
  1. Deploy your application (Deployment + Service).
  2. Create a ServiceMonitor that selects the Service (via labels) and specifies endpoints (port, path, interval).
  3. The Prometheus Operator discovers the ServiceMonitor and adds targets to Prometheus’ scrape configuration.
Example manifests
  • Service exposes the application on port 3000 with port name web and labels job: node-api and app: api.
  • ServiceMonitor selects the Service by matchLabels, sets jobLabel: job (so the Prometheus job name is taken from the Service job label), and configures endpoints (scrape interval and metrics path).
Service manifest (api-service.yaml):
ServiceMonitor manifest (api-service-monitor.yaml):
Prometheus discovers ServiceMonitors based on the Prometheus custom resource configuration. Check the Prometheus CR’s serviceMonitorSelector and serviceMonitorNamespaceSelector to know which ServiceMonitors (labels and namespaces) your Prometheus instance will use. When using the kube-prometheus-stack Helm chart, ServiceMonitors often need the label release: prometheus.
Verify Prometheus CR configuration Inspect the Prometheus custom resource to see how it discovers ServiceMonitors:
Look for:
If the Prometheus CR selects ServiceMonitors with release: prometheus, ensure your ServiceMonitor includes that label. Apply the manifests Apply the Service and ServiceMonitor (together or separately):
Example output:
Check ServiceMonitors:
Verify targets in Prometheus Open the Prometheus UI and navigate to Status → Targets. You should see a job corresponding to your ServiceMonitor, for example: serviceMonitor/default/api-service-monitor/0. A Deployment with multiple replicas will show multiple endpoints (one per pod).
The image shows a Prometheus monitoring dashboard displaying the status of several service endpoints, with details such as their state, labels, last scrape time, and scrape duration.
Querying metrics Confirm metrics ingestion by querying Prometheus for metrics associated with your job label (job="node-api"). Example metric lines returned from Prometheus:
A useful quick check in Prometheus: up{job="node-api"} — a value of 1 indicates a healthy scrape target. Inspect the generated Prometheus configuration The Prometheus Operator converts ServiceMonitors into scrape_configs. In the Prometheus UI → Status → Configuration, search for the job name (e.g., serviceMonitor/default/api-service-monitor/0) to view the generated config. This shows how your ServiceMonitor settings translate into Prometheus scrape settings and relabel rules.
The image shows a Prometheus interface displaying a list of metrics related to API performance, with options for viewing configuration and various statistics.
Example generated snippet:
All configuration declared in the ServiceMonitor (port name, path, interval, jobLabel and selector) will be reflected in the generated Prometheus scrape config and relabel rules.
Namespaces and labels matter. Prometheus discovers ServiceMonitors according to the Prometheus CR’s selectors—if your ServiceMonitor is in a different namespace or lacks the expected label, Prometheus will not pick it up. Also ensure required RBAC permissions are in place for Prometheus to read ServiceMonitor resources in the target namespaces.
Summary
  • ServiceMonitors provide a declarative method to register Prometheus scrape targets in Kubernetes.
  • Make sure the Prometheus CR’s serviceMonitorSelector and serviceMonitorNamespaceSelector include the labels and namespaces of your ServiceMonitors.
  • Create Services with meaningful labels and named ports, then create ServiceMonitors that select those Services and define scrape parameters (path, port, interval, jobLabel).
  • Verify in Prometheus UI → Status → Targets that the job and endpoints appear and metrics are being scraped.
Links and references

Watch Video