暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Alertmanager告警配置文件和告警规则详解

354


1. 什么是 Alertmanager?

Alertmanager 是 Prometheus 生态中的一个重要组件,用于处理 Prometheus 发送的告警(Alerts)。它提供了告警分组、抑制、去重、路由以及告警通知等功能。它可以在发现问题时立即通知相关负责人,使运维人员能快速响应并采取措施。

下面是告警是流程图:

1.1 Alertmanager 的主要功能

  • 告警分组(Grouping):将相似的告警合并,减少告警数量。
  • 去重(Deduplication):如果同一告警在短时间内重复触发,Alertmanager 只会发送一次。
  • 抑制(Inhibition):当某个高优先级告警触发时,屏蔽低优先级的相关告警。
  • 路由(Routing):将不同类型的告警发送到不同的接收者(如邮件、Slack、Webhook)。
  • 通知(Notification):支持多种通知方式,如邮件、Slack、PagerDuty、Webhook 等。

2. Alertmanager 配置文件详解

完整的配置文件,这个只是实例,生产环境需要根据实际配置

global:
  resolve_timeout: 5m  
  smtp_smarthost: 'smtp.example.com:587'
  smtp_from: 'alert@example.com'
  smtp_auth_username: 'user@example.com'
  smtp_auth_password: 'yourpassword'
  smtp_hello: "qq.com"
  smtp_require_tls: false

route:
  receiver: 'default'
  group_by: ['alertname''cluster''service']  # 分组依据
  group_wait: 30s  # 第一次分组告警发送前的等待时间
  group_interval: 5m  # 组内新告警的发送间隔
  repeat_interval: 3h  # 相同告警重复发送的间隔
  routes:
    - match:
        severity: critical
      receiver: 'email'
      continuetrue# 继续匹配后续规则
    - match:
        severity: warning
      receiver: 'slack'
    - match:
        alertname: InstanceDown
      receiver: 'webhook'

receivers:
  - name: 'default'
    webhook_configs:
      - url: 'http://webhook.example.com/alert'# Webhook 地址

  - name: 'email'
    email_configs:
      - to: 'admin@example.com'
        from: 'alert@example.com'
        smarthost: 'smtp.example.com:587'
        auth_username: 'user@example.com'
        auth_password: 'yourpassword'
        send_resolved: true# 发送告警恢复通知

  - name: 'slack'
    slack_configs:
      - channel: '#alerts'
        send_resolved: true
        api_url: 'https://hooks.slack.com/services/XXX/YYY/ZZZ'
        title: '{{ .CommonAnnotations.summary }}'
        text: '{{ .CommonAnnotations.description }}'

  - name: 'webhook'
    webhook_configs:
      - url: 'http://alert-handler.local/notify'

inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['instance']

Alertmanager 的配置文件 alertmanager.yml
 主要包括以下几个部分,每个部分的作用及参数如下。

2.1 global 配置

全局配置,Global块配置下的配置选项在本配置文件内的所有配置项下可见,但是文件内其他位置的子配置可以覆盖Global配置。

global:
  resolve_timeout: 5m  # 告警恢复后等待 5 分钟再标记为已解决
  smtp_smarthost: 'smtp.example.com:587'  # 邮件服务器地址
  smtp_from: 'xxx@example.com'  # 发送邮件的地址
  smtp_auth_username: 'xxx@example.com'  # 邮件服务器认证用户名
  smtp_auth_password: 'yourpassword'  # 邮件服务器认证密码

  • resolve_timeout
    :在告警恢复后,等待多长时间才将其标记为“已解决”。
  • smtp_smarthost
    :用于发送邮件告警的邮件服务器地址。
  • smtp_from
    :用于发送告警邮件的发件人地址。
  • smtp_auth_username
     和 smtp_auth_password
    :用于认证 SMTP 服务器的用户名和授权码。
  • smtp_require_tls
    :是否开启tls认证

2.2 route 配置

告警路由配置,用于告警信息的分组路由,可以将不同分组的告警发送给不同的收件人。

route:
  receiver: 'default' 
  group_by: ['alertname''cluster''service']  
  group_wait: 30s  
  group_interval: 5m 
  repeat_interval: 3h 

  • receiver
    :默认的接收器(如果没有匹配到具体的路由规则,则使用此接收器)。
  • group_by
    :定义告警分组的方式,确保相同服务的告警不会重复发送。
  • group_wait
    :在发送第一个告警前等待的时间,避免瞬时波动导致告警风暴。
  • group_interval
    :在同一分组内,新增告警的发送间隔。
  • repeat_interval
    :相同告警重复发送的时间间隔,防止过度通知。

2.3 routes 配置

路由子配置,优先级高于route,配置和route一样的

  routes:
    - match:
        severity: critical
      receiver: 'email'
      continuetrue
    - match:
        severity: warning
      receiver: 'slack'
    - match:
        alertname: InstanceDown
      receiver: 'webhook'

  • match
    :定义匹配规则,例如 severity: critical
     表示匹配所有严重告警。
  • receiver
    :匹配该规则的告警将发送到指定接收器。
  • continue
    :如果为 true
    ,则匹配该规则后仍会继续匹配其他规则,默认是找到符合的规则后就不在往下继续匹配。

2.4 receivers 配置

告警接收人配置,每个receiver都有一个名字,经过route分组并且路由后需要指定一个receiver,可以配置不同类型的接收者

receivers:
  - name: 'default'
    webhook_configs:
      - url: 'http://webhook.example.com/alert'

  • name
    :定义接收器的名称。
  • webhook_configs
    :使用 Webhook 发送告警。
  - name: 'email'
    email_configs:
      - to: 'admin@example.com'
        send_resolved: true

  • to
    :邮件接收者。
  • send_resolved
    :是否发送恢复通知。
  - name: 'slack'
    slack_configs:
      - channel: '#alerts'
        api_url: 'https://hooks.slack.com/services/XXX/YYY/ZZZ'

  • channel
    :告警发送的 Slack 频道。
  • api_url
    :Slack Webhook URL。

2.5 inhibit_rules 配置

告警抑制,主要用于减少告警的次数,防止“告警轰炸

inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['instance']

  • source_match
    :定义高优先级的告警。
  • target_match
    :当 source_match
     触发时,抑制 target_match
  • equal
    :只有在相同 instance
     触发时才应用抑制规则。

当一个 severity 为 critical 的告警触发时,所有 severity 为 warning 且 alertname 和 instance 标签与 critical 告警相同的告警将被抑制,不会发送通知。

完整的配置文件应该还有个Templates配置:用于放置自定义模板的位置,这里不展开讲

3. Prometheus 告警规则详解

3.1 告警规则文件

告警配置文件可以自定义名称,在prometheus配置文件同步就行。

alert-rules.yml

groups:
  - name: 主机状态监控
    rules:
      - alert: 主机宕机
        expr: up == 0
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "实例 {{ $labels.instance }} 宕机"
          description: "实例 {{ $labels.instance }} 已经宕机超过 1 分钟。请检查服务状态。"

  • expr
    :告警触发条件。
  • for
    :持续多长时间才触发告警。
  • labels
    :告警标签,可用于匹配规则。
  • annotations
    :告警的详细描述。

本次分享到这里,感谢点赞和关注!




往期精彩文章:


K8S命令详解汇总【自用珍藏版】K8S集群部署 | K8S存储实战案例 |
K8S证书续签十年 | K8S部署Prometheus | Rancher部署并接管K8S |
Jenkins安装部署 | Gitlab安装部署 | 服务网格Istio安装及实战 |
搭建企业级Harbor仓库 | K8S对接Harbor仓库 | Docker常用命令汇总 |
Docker无法下载镜像解决办法 | 安装Docker的三种方法 | Docker基础概念汇总 |

Oralce19C RAC集群搭建 | Oracle集群管理命令汇总 | MySQL集群安装部署 |
MySQL一键备份脚本 | MySQL集群目录迁移 | Redis三主三从集群部署 |

150个Linux常用命令 |  8个有趣的Linux命令 | Vim编辑器的常用操作汇总  |
Firewalld防火墙详细讲解 |  构建内部Yum源 | 超全的磁盘扩容方法 | 服务器带外管理知识 |





文章转载自运维李哥不背锅,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论