kube-prometheus-stack 配置AlertManager

首先需要知道有两种 Prometheus 规则 :

prometheus.ymlPrometheus监控 的主配置文件,但是并不是定义了所有的Prometheus规则,而是命名包含实际规则的其他文件。传统上, alerting rulesrecording rules 是拆分到单独文件中的。

虽然在 prometheus 服务器pod中

alertmanager.config 定义

alertmanager.config 提供了指定 altermanager 的配置,这样就能够自己定制一些特定的 receivers ,不过可能更方便是直接 apply

简单配置alertmanager的接收者就能够收到通知,这里采用 prometheus-webhook-dingtalk
## Configuration for alertmanager
## ref: https://prometheus.io/docs/alerting/alertmanager/
##
alertmanager:
...
  ## Alertmanager configuration directives
  ## ref: https://prometheus.io/docs/alerting/configuration/#configuration-file
  ##      https://prometheus.io/webtools/alerting/routing-tree-editor/
  ##
  config:
    global:
      resolve_timeout: 5m
    inhibit_rules:
      - source_matchers:
          - 'severity = critical'
        target_matchers:
          - 'severity =~ warning|info'
        equal:
          - 'namespace'
          - 'alertname'
      - source_matchers:
          - 'severity = warning'
        target_matchers:
          - 'severity = info'
        equal:
          - 'namespace'
          - 'alertname'
      - source_matchers:
          - 'alertname = InfoInhibitor'
        target_matchers:
          - 'severity = info'
        equal:
          - 'namespace'
    route:
      group_by: ['namespace']
      group_wait: 30s
      group_interval: 5m
      repeat_interval: 12h
      receiver: 'cloud_atlas_alert'
      routes:
      - receiver: 'cloud_atlas_alert'
        matchers:
          - alertname =~ "InfoInhibitor|Watchdog"
    receivers:
    - name: cloud_atlas_alert
      webhook_configs:
        - url: http://192.168.6.115:8060/dingtalk/cloud_atlas_alert/send
    templates:
    - '/etc/alertmanager/config/*.tmpl'

这里主要将默认配置修改如下:

  • alertmanager.config 配置中 receivernull 替换成需要接收的 prometheus-webhook-dingtalk 配置中的 target 名字,这里是 cloud_atlas_alert

  • 添加 prometheus-webhook-dingtalkwebhook 配置,注意URL路径中包括了 target ( cloud_atlas_alert ) ,也就是 URL 必须是 http://<prometheus-webhook-dingtalk服务器IP>:8060/dingtalk/<target>/send

结合 prometheus-webhook-dingtalk 部署运行的服务器,就能立即收到钉钉通知(MarkDown格式),并且能够 @ 指定用户(根据手机号码或者工号),类似:

../../../_images/alert_dingtalk.png

参考