Skip to main content
In this lesson we’ll add targets to Prometheus using the Kubernetes-native ServiceMonitor resource provided by the Prometheus Operator. First, a quick note about Custom Resource Definitions (CRDs): the Prometheus Operator installs multiple CRDs that expose high-level Kubernetes abstractions for deploying and configuring Prometheus and its related resources.
A dark presentation slide titled "CRDs" showing text that explains the Prometheus operator includes several custom resource definitions to provide a high-level abstraction for deploying and configuring Prometheus. The phrase "custom resource definitions" is highlighted.
To list all CRDs installed in your cluster:
Two CRDs to focus on for adding Prometheus scrape targets:
A presentation slide titled "Service Monitors" explaining that service monitors define targets for Prometheus to monitor and scrape. It notes they let you avoid editing Prometheus configs by using a declarative Kubernetes syntax, with "Service monitors" and "declarative" highlighted.
What is a ServiceMonitor? How ServiceMonitor and Service relate
  • The ServiceMonitor selects Kubernetes Services via selector.matchLabels.
  • Endpoints in the ServiceMonitor refer to named ports on the Service (port: <name>) and can set path, interval, scheme, etc.
  • jobLabel (optional) maps a Service label to the Prometheus job name.
Example: Service + ServiceMonitor (combined YAML)
Notes on the example:
  • The Service has labels job: node-api and app: api.
  • The Service exposes a named port web on port 3000.
  • The ServiceMonitor selects Services with app: api, scrapes the web port at /swagger-stats/metrics every 30s.
  • jobLabel: job tells Prometheus to use the Service label job (value: node-api) as the Prometheus job name.
Ensure the ServiceMonitor includes a label that matches the Prometheus instance’s serviceMonitorSelector (commonly release: prometheus). If the ServiceMonitor does not match that selector, the Prometheus instance will not discover it automatically.
Prometheus instance configuration
  • The Prometheus CRD configures which ServiceMonitors it should discover through serviceMonitorSelector. Check your Prometheus custom resource to confirm the selector:
If your Prometheus has a selector like release: prometheus, make sure your ServiceMonitor includes metadata.labels.release: prometheus. Apply the resources
  • Add the Service and ServiceMonitor YAML (for example, append to api-deploy.yaml) and apply:
Verify the ServiceMonitor exists:
Prometheus discovery and verification
  • In the Prometheus UI go to Status → Targets. You should see a job that corresponds to your ServiceMonitor. In the example above the job will be node-api, since the Service label job: node-api is used.
  • If the Deployment has multiple replicas, each pod appears as a separate target.
  • To confirm metrics ingestion, run queries in Prometheus. Example (truncated):
Generated scrape config
  • The Prometheus Operator translates each ServiceMonitor into a Prometheus scrape_config. An excerpt of the generated configuration for this ServiceMonitor:
Summary
  • ServiceMonitors let you declaratively add Prometheus scrape targets using Kubernetes resources instead of editing Prometheus config files directly.
  • Key checks when using ServiceMonitors:
    • The ServiceMonitor selector.matchLabels must select the intended Service.
    • The ServiceMonitor endpoints must reference the Service’s named ports.
    • The ServiceMonitor must carry the label(s) (e.g. release: prometheus) expected by your Prometheus instance’s serviceMonitorSelector.
Links and references

Watch Video