Skip to main content

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: the infra and k8s configurations send a set of alerts to Slack.
  • deadManSwitchWebhookUrl: the deadmanswitch configuration sends the DeadMansSwitch alert 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 AlertmanagerConfig resources only from the monitoring namespace.
  • 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 the namespace label.
  • Each AlertmanagerConfig gets a copy of the alerts that match its route. Prometheus Operator sets continue: true on 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.

  1. Write the password of the SMTP account in a file, for example secrets/smtp-password next to furyctl.yaml.

    caution

    This 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.

  2. Write the AlertmanagerConfig in a file, for example manifests/email-my-app.yaml next to furyctl.yaml:

    manifests/email-my-app.yaml
    apiVersion: monitoring.coreos.com/v1alpha1
    kind: AlertmanagerConfig
    metadata:
    name: email-my-app
    namespace: monitoring
    spec:
    route:
    receiver: email-my-app
    groupBy:
    - alertname
    # An alert must match all the matchers
    matchers:
    - name: namespace
    matchType: "="
    value: my-app
    - name: severity
    matchType: "="
    value: critical
    receivers:
    - name: email-my-app
    emailConfigs:
    - to: oncall@example.com
    from: alertmanager@example.com
    smarthost: smtp.example.com:587
    authUsername: alertmanager@example.com
    authPassword:
    name: alertmanager-smtp
    key: smtp-password
    sendResolved: true

    To select the alerts by name, use the alertname label. The =~ match type accepts a regular expression, for example KubePodCrashLooping|KubeDeploymentReplicasMismatch. The Alerts page lists the alerts that SD includes, with their severity.

  3. Add the AlertmanagerConfig and a Secret with the password to furyctl.yaml:

    furyctl.yaml
    spec:
    distribution:
    customResources:
    - ./manifests/email-my-app.yaml
    customPatches:
    secretGenerator:
    - name: alertmanager-smtp
    namespace: monitoring
    files:
    - ./secrets/smtp-password
    options:
    disableNameSuffixHash: true

    The Secret has one key, with the name of the file: smtp-password. The disableNameSuffixHash option keeps the name of the Secret, because the AlertmanagerConfig refers to this name. For more information, see Keep the patches in separate files.

  4. 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.

  1. Write the address of the central Alertmanager in a file, for example secrets/central-alertmanager.yaml next to furyctl.yaml:

    secrets/central-alertmanager.yaml
    - scheme: https
    api_version: v2
    static_configs:
    - targets:
    - alertmanager.example.com

    The file contains a list of alertmanager_config entries of the Prometheus configuration. Use the fields of this format to add authentication, for example basic_auth, and a custom CA certificate.

    caution

    If the file holds credentials and you put it in a git repository, encrypt it first, for example with git-crypt or SOPS.

  2. Write the patch for the Prometheus resource in a file, for example patches/prometheus-central-alertmanager.yaml next to furyctl.yaml:

    patches/prometheus-central-alertmanager.yaml
    - op: add
    path: /spec/additionalAlertManagerConfigs
    value:
    name: central-alertmanager
    key: central-alertmanager.yaml

    The 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.

  3. Add the Secret and the patch to furyctl.yaml:

    furyctl.yaml
    spec:
    distribution:
    customPatches:
    secretGenerator:
    - name: central-alertmanager
    namespace: monitoring
    files:
    - ./secrets/central-alertmanager.yaml
    options:
    disableNameSuffixHash: true
    patches:
    - target:
    group: monitoring.coreos.com
    version: v1
    kind: Prometheus
    name: k8s
    namespace: monitoring
    path: ./patches/prometheus-central-alertmanager.yaml

    The Secret has one key, with the name of the file. The disableNameSuffixHash option keeps the name of the Secret, because the patch refers to this name. For more information, see Keep the patches in separate files.

  4. 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.

note

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.