Learn to add new alerting rules using the Prometheus operator and register them with a Prometheus instance through a Custom Resource Definition.
In this lesson, you will learn how to add new alerting rules using the Prometheus operator. Similar to service monitors used for target registration, the Prometheus operator provides a Custom Resource Definition (CRD) called PrometheusRule. This CRD enables you to register alerting rules with a Prometheus instance. The configuration is nearly identical to that of a service monitor, with the main differences being the resource type and the specifics of the rule specification.
This configuration is similar to the alerting rules you would normally define in the traditional prometheus.yaml file, but now it is encapsulated as a Kubernetes resource.
kubectl get prometheuses.monitoring.coreos.com -o yaml
you might see an output like:
Copy
Ask AI
ruleSelector: matchLabels: release: prometheus
The ruleSelector property works similarly to a service discovery selector. It tells Prometheus which PrometheusRule resources to load dynamically by matching labels. Therefore, ensure that any PrometheusRule you create includes the label release: prometheus (or the corresponding label used in your configuration).
Ensure that your PrometheusRule metadata labels match the rule selector specified in your Prometheus configuration.
To add new rules to your deployment, follow these steps:
Create a file (e.g., rules.yaml) and paste your PrometheusRule configuration into it.
Apply the configuration with the following command:
Copy
Ask AI
kubectl apply -f rules.yaml
If you encounter an error indicating that no objects were provided for the apply command, verify that your file is saved and contains valid YAML content.
After applying the configuration, check the creation of your PrometheusRule by running:
Copy
Ask AI
kubectl get prometheusrule
You should see output similar to the example below, where your API rule appears among the pre-existing rules: