Skip to main content
Previously we covered how to add new targets. Now we’ll look at adding alerting and recording rules using the Prometheus Operator. The Prometheus Operator exposes a Kubernetes CRD named PrometheusRule. You use this CRD to register Prometheus rule groups and rules in the same format as a normal Prometheus rules file. The structure is effectively identical to the standard Prometheus YAML but wrapped in a Kubernetes resource. Example PrometheusRule
Key points:
  • metadata.name identifies the PrometheusRule object in Kubernetes.
  • metadata.labels are used by the Prometheus instance to discover rules (see ruleSelector below).
  • spec.groups contains one or more rule groups. Each group has a name and a rules list, matching regular Prometheus rule files.
Table — PrometheusRule core fields Prometheus discovers PrometheusRule objects using the ruleSelector configured on the Prometheus resource. You can inspect this selector with kubectl:
Make sure your PrometheusRule objects include the same label(s) that your Prometheus ruleSelector matches (for example, release: prometheus). Otherwise Prometheus will not pick up the rules.
Applying rules
  1. Create a file (for example rules.yaml) containing your PrometheusRule resource as shown in the example above.
  2. Apply it to the cluster:
Successful output:
Verify the resource exists:
Or describe it for more detail:
Checking rules in Prometheus UI
  • Open your Prometheus UI.
  • Navigate to Status → Rules.
  • You should see your rule group (for example, api) and the rules listed there. If the rule appears and is listed as healthy, it was successfully registered.
Troubleshooting quick commands
If Prometheus does not register your rules, first check label matching between the Prometheus ruleSelector and your PrometheusRule metadata.labels. Labels must match exactly. Also review Prometheus logs for parsing or evaluation errors.
Examples of additional rules Below are a couple of additional rule snippets that illustrate typical alerting rules. Place them inside the spec.groups[].rules list of a PrometheusRule object. Example: Alertmanager reload failure
Example: Alertmanager cluster members inconsistent
References

Watch Video