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
metadata.nameidentifies the PrometheusRule object in Kubernetes.metadata.labelsare used by the Prometheus instance to discover rules (seeruleSelectorbelow).spec.groupscontains one or more rule groups. Each group has anameand aruleslist, matching regular Prometheus rule files.
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.- Create a file (for example
rules.yaml) containing yourPrometheusRuleresource as shown in the example above. - Apply it to the cluster:
- 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.
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.spec.groups[].rules list of a PrometheusRule object.
Example: Alertmanager reload failure
- Prometheus Operator (GitHub): https://github.com/prometheus-operator/prometheus-operator
- Prometheus rules documentation: https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/