How does the Prometheus Operator decide which ServiceMonitors to load?
IntermediateConceptKubernetes Monitoring, Scale & Cloud
Answer
The Prometheus resource has selectors:
spec:
serviceMonitorSelector:
matchLabels: { release: monitoring }
serviceMonitorNamespaceSelector: {} # {} = all namespaces; nil = only own namespace
ruleSelector:
matchLabels: { release: monitoring }
A ServiceMonitor is loaded only if its labels match serviceMonitorSelector and its namespace matches serviceMonitorNamespaceSelector. The classic gotcha with kube-prometheus-stack: by default it only selects objects labelled release: <helm release name>. Either add that label or set:
prometheus:
prometheusSpec:
serviceMonitorSelectorNilUsesHelmValues: false
ruleSelectorNilUsesHelmValues: false
Also, the ServiceMonitor port must be the name of a Service port, not a number.
More on Kubernetes Monitoring, Scale & Cloud
What is kube-prometheus-stack and what does it install?BeginnerWhat CRDs does the Prometheus Operator provide?IntermediateWhich cAdvisor container memory metric should you watch, and why?IntermediateHow do metrics-server and the custom/external metrics APIs relate to HPA?IntermediateWhich kube-state-metrics metrics do you use most?BeginnerWhat endpoints does the kubelet expose for Prometheus?Advanced