PrometheusRule that lets you register Prometheus rules with a Prometheus instance. The CRD’s structure mirrors a standard Prometheus rules file, but it is stored as a Kubernetes custom resource.
PrometheusRule example
Below is an examplePrometheusRule resource. It defines a rule group named api with an alert called down.
metadata.labelsincludesrelease: prometheus. Prometheus discoversPrometheusRuleobjects using the Prometheus resource’sruleSelector(explained below), so these labels must match what your Prometheus instance is selecting.spec.groupsuses the same grouping construct as a standard Prometheus rules file — each group has anameand a set ofrules.
How Prometheus finds rules
Prometheus uses a selector on the Prometheus custom resource to discover and registerPrometheusRule objects dynamically. Check your Prometheus resource (CR) for the ruleSelector configuration. Example:
Make sure your
PrometheusRule objects include labels that match the ruleSelector on your Prometheus resource (for example, release: prometheus). Otherwise Prometheus will not pick them up.ruleSelector only matches labels on the PrometheusRule resource. If your PrometheusRule is in a different namespace than your Prometheus CR, confirm your Operator installation supports cross-namespace rule discovery or place the rule in the same namespace as the Prometheus resource.Key fields in a PrometheusRule
Steps to register rules
- Create a rules file (for example
rules.yaml) containing thePrometheusRuleresource shown above. - Apply it to your cluster:
- Verify the resource exists:
api-rules. To confirm Prometheus has registered the rule, open the Prometheus UI and navigate to Status → Rules. You should see the api group and the down alert listed and in a healthy state.
Additional example rules
The following examples show multiple alerts grouped together. Add them tospec.groups[*].rules inside your PrometheusRule resource.
Troubleshooting tips
- If rules do not appear in the Prometheus UI, confirm the
PrometheusRulelabels match theruleSelectoron your Prometheus resource and that the rule resource is in the expected namespace. - Check the Prometheus Operator logs and the Prometheus server logs for any errors while loading rules.
- Use
kubectl describe prometheusrules <name>to inspect events or validation errors on the resource.
References
- Prometheus Operator documentation: https://github.com/prometheus-operator/prometheus-operator
- Prometheus rules and alerts: https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/