Skip to main content
This lesson shows how to add alerting and recording rules to Prometheus when using the Prometheus Operator. The Operator exposes a Custom Resource Definition (CRD) named 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 example PrometheusRule resource. It defines a rule group named api with an alert called down.
Notes on the example:
  • metadata.labels includes release: prometheus. Prometheus discovers PrometheusRule objects using the Prometheus resource’s ruleSelector (explained below), so these labels must match what your Prometheus instance is selecting.
  • spec.groups uses the same grouping construct as a standard Prometheus rules file — each group has a name and a set of rules.

How Prometheus finds rules

Prometheus uses a selector on the Prometheus custom resource to discover and register PrometheusRule 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

  1. Create a rules file (for example rules.yaml) containing the PrometheusRule resource shown above.
  2. Apply it to your cluster:
  1. Verify the resource exists:
You should see an entry such as 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 to spec.groups[*].rules inside your PrometheusRule resource.

Troubleshooting tips

  • If rules do not appear in the Prometheus UI, confirm the PrometheusRule labels match the ruleSelector on 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

Watch Video