Route alerts
This page tells you how to send the alerts of an SD cluster to your own receivers:
How SD routes alerts
The Monitoring module installs Prometheus and Alertmanager when the monitoring type is prometheus or mimir. With the prometheusAgent type, the cluster has no Alertmanager, and this page does not apply.
Prometheus evaluates the alert rules and sends the alerts to the Alertmanager of the cluster, in the monitoring namespace. The default configuration of Alertmanager sends the alerts to no receiver.
The receivers come from AlertmanagerConfig resources in the monitoring namespace. SD adds some of them when you set these fields of .spec.distribution.modules.monitoring.alertmanager:
slackWebhookUrl: theinfraandk8sconfigurations send a set of alerts to Slack.deadManSwitchWebhookUrl: thedeadmanswitchconfiguration sends theDeadMansSwitchalert to a heartbeat service, for example healthchecks.io.
You can add your own AlertmanagerConfig resources next to the SD ones. These facts apply to them:
- Alertmanager reads
AlertmanagerConfigresources only from themonitoringnamespace. - The matchers of a route apply to the alerts of all the namespaces. SD sets the matcher strategy of Alertmanager to
None, so Prometheus Operator does not add a matcher for thenamespacelabel. - Each
AlertmanagerConfiggets a copy of the alerts that match its route. Prometheus Operator setscontinue: trueon the first-level route of each configuration. A new email route does not stop the Slack notifications.
Send specific alerts by email
In this example, Alertmanager sends the critical alerts of the my-app namespace to an email address.
-
Write the password of the SMTP account in a file, for example
secrets/smtp-passwordnext tofuryctl.yaml.cautionThis file holds a password in clear text. If you put this file in a git repository, encrypt it first, for example with git-crypt or SOPS. For more information, see PKI and secrets.
-
Write the
AlertmanagerConfigin a file, for examplemanifests/email-my-app.yamlnext tofuryctl.yaml:manifests/email-my-app.yamlapiVersion: monitoring.coreos.com/v1alpha1kind: AlertmanagerConfigmetadata:name: email-my-appnamespace: monitoringspec:route:receiver: email-my-appgroupBy:- alertname# An alert must match all the matchersmatchers:- name: namespacematchType: "="value: my-app- name: severitymatchType: "="value: criticalreceivers:- name: email-my-appemailConfigs:- to: oncall@example.comfrom: alertmanager@example.comsmarthost: smtp.example.com:587authUsername: alertmanager@example.comauthPassword:name: alertmanager-smtpkey: smtp-passwordsendResolved: trueTo select the alerts by name, use the
alertnamelabel. The=~match type accepts a regular expression, for exampleKubePodCrashLooping|KubeDeploymentReplicasMismatch. The Alerts page lists the alerts that SD includes, with their severity. -
Add the
AlertmanagerConfigand a Secret with the password tofuryctl.yaml:furyctl.yamlspec:distribution:customResources:- ./manifests/email-my-app.yamlcustomPatches:secretGenerator:- name: alertmanager-smtpnamespace: monitoringfiles:- ./secrets/smtp-passwordoptions:disableNameSuffixHash: trueThe Secret has one key, with the name of the file:
smtp-password. ThedisableNameSuffixHashoption keeps the name of the Secret, because theAlertmanagerConfigrefers to this name. For more information, see Keep the patches in separate files. -
Apply the configuration:
furyctl apply --phase distribution
Alertmanager loads the new configuration in a few seconds. To see the result, open the Alertmanager web interface and go to Status. The configuration shows a receiver with the name monitoring/email-my-app/email-my-app: Prometheus Operator adds the namespace and the name of the AlertmanagerConfig before the name of each receiver.
For the other receivers that Alertmanager supports, for example PagerDuty, Microsoft Teams or a webhook, see the AlertmanagerConfig API reference.
Send the alerts to a central Alertmanager
When you operate many clusters, you can send their alerts to one central Alertmanager. In this example, Prometheus sends each alert to the Alertmanager of the cluster and to the central Alertmanager.
Each alert has the k8s_cluster label, with the value of .metadata.name from furyctl.yaml. The central Alertmanager can use this label to group and route the alerts of each cluster.
-
Write the address of the central Alertmanager in a file, for example
secrets/central-alertmanager.yamlnext tofuryctl.yaml:secrets/central-alertmanager.yaml- scheme: httpsapi_version: v2static_configs:- targets:- alertmanager.example.comThe file contains a list of
alertmanager_configentries of the Prometheus configuration. Use the fields of this format to add authentication, for examplebasic_auth, and a custom CA certificate. -
Write the patch for the Prometheus resource in a file, for example
patches/prometheus-central-alertmanager.yamlnext tofuryctl.yaml:patches/prometheus-central-alertmanager.yaml- op: addpath: /spec/additionalAlertManagerConfigsvalue:name: central-alertmanagerkey: central-alertmanager.yamlThe patch adds a field and does not change the list of the Alertmanagers of the cluster. Prometheus continues to send the alerts to the local Alertmanager too.
-
Add the Secret and the patch to
furyctl.yaml:furyctl.yamlspec:distribution:customPatches:secretGenerator:- name: central-alertmanagernamespace: monitoringfiles:- ./secrets/central-alertmanager.yamloptions:disableNameSuffixHash: truepatches:- target:group: monitoring.coreos.comversion: v1kind: Prometheusname: k8snamespace: monitoringpath: ./patches/prometheus-central-alertmanager.yamlThe Secret has one key, with the name of the file. The
disableNameSuffixHashoption keeps the name of the Secret, because the patch refers to this name. For more information, see Keep the patches in separate files. -
Apply the configuration:
furyctl apply --phase distribution
Prometheus loads the new configuration in a few minutes. To make sure that Prometheus uses the central Alertmanager, get the list of the active Alertmanagers from the Prometheus API:
kubectl -n monitoring exec prometheus-k8s-0 -c prometheus -- \
wget -qO- http://localhost:9090/api/v1/alertmanagers
The activeAlertmanagers list contains the address of the central Alertmanager.
The firewall between the cluster and the central Alertmanager must allow the connections from the nodes that run Prometheus. The network policies of SD do not block them: Prometheus can connect to all the addresses.