Alerting rules allow you to define alert conditions based on Prometheus Quey Language (PromQL) expressions and to send notifications about firing alerts to an external service. Whenever the alert expression results in one or more vector elements at a given point in time, the alert counts as active for these elements' label sets. If you have installed Prometheus with Helm, you have yet a Custom Resource Definitions (CRD) called PrometheusRule
. Below is an example
In this example we have inserted 2 rules that evaluate the rate of http requests to Prometheus dashboard pages. We will then update these web pages via the browser, to trigger the alert. Also note the labels
adopted in this manifest. Similarly to what was seen in Monitor a Service, the labels must match those present in the ruleSelector
field.
Trigger the rules
Now let's move to the Prometheus dashboard and update the sub-path /graph
and /alerts
(framed in yellow at the top left of the image) a few times (15-20 times should be enough), in order to activate the rules adopted. As you can see from the image, the created alerts have been activated and, in fact, are in firing status. After a few minutes, if no other http requests are made, these alerts will return to the starting status, i.e. inactive. The Watchdog alert, at the top of the screenshot, is always present: is an alert meant to ensure that the entire alerting pipeline is functional.
Send alert via e-mail
Let's try to send our alerts by e-mail. To do this we will use 2 other CRDs: Alertmanager
and AlertmanagerConfig
. Note the analogy with Prometheus
and PrometheusRule
. Also in this case we have linked the 2 CRDs through labels
(however they can be chosen arbitrarily). Launch a describe on the Alertmanager component
Create and configure the AlertmanagerConfig
component for sending e-mails (find more details on the official website), using the file below. As you can see, Gmail is used as mail provider here. It isn’t recommended that you use your personal password for this, so you should create an App Password. To do that, go to Account Settings -> Security -> Signing in to Google -> App password (if you don’t see App password as an option, you probably haven’t set up 2-Step Verification and will need to do that first). Copy the newly-created password in the Secret below, after having encrypted it with the command echo <password> | base64 -w0
.
After executing an apply of the above file, it is advisable to take a look at the logs of the following Pods, to see if there are any errors during the execution
From the above logs you can read what is the path, inside the Pod Alertmanager-0
, in which the newly added configurations are saved. This information can also be found in the Alertmanager dashboard. To reach it, simply expose the service (via ingress or NodePort)
Within the dashboard you will find the same alerts of the Prometheus UI and, moreover, you can silence them for a defined period of time, filter them if there were many and much more. Finally, try to generate some alerts, if you do not want to wait for some error to occur spontaneously, and verify the receipt of the e-mail at the address indicated in the configuration of the AlertmanagerConfig
component. You can, for example, reactivate the rateGraph or rateAlerts rules already used previously, as in the screenshot below