

- A ServiceMonitor is a CRD that declares which Kubernetes Services Prometheus should scrape.
- It enables declarative discovery: create Service and ServiceMonitor resources and the Prometheus Operator converts them into Prometheus
scrape_configs, avoiding manual edits to Prometheus config files. - See the ServiceMonitor API docs for full fields and examples: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#servicemonitor
- The ServiceMonitor selects Kubernetes Services via
selector.matchLabels. - Endpoints in the ServiceMonitor refer to named ports on the Service (
port: <name>) and can setpath,interval,scheme, etc. jobLabel(optional) maps a Service label to the Prometheus job name.
- The Service has labels
job: node-apiandapp: api. - The Service exposes a named port
webon port3000. - The ServiceMonitor selects Services with
app: api, scrapes thewebport at/swagger-stats/metricsevery 30s. jobLabel: jobtells Prometheus to use the Service labeljob(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.- The Prometheus CRD configures which ServiceMonitors it should discover through
serviceMonitorSelector. Check your Prometheus custom resource to confirm the selector:
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:
- 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 labeljob: node-apiis used. - If the Deployment has multiple replicas, each pod appears as a separate target.
- To confirm metrics ingestion, run queries in Prometheus. Example (truncated):
- The Prometheus Operator translates each ServiceMonitor into a Prometheus
scrape_config. An excerpt of the generated configuration for this ServiceMonitor:
- 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.matchLabelsmust 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’sserviceMonitorSelector.
- The ServiceMonitor
- Prometheus Operator — ServiceMonitor API: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#servicemonitor
- Prometheus Operator GitHub: https://github.com/prometheus-operator/prometheus-operator
- Prometheus scrape_config docs: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config
- Kubernetes CRDs docs: https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/