Myluzh Blog

Alertmanager 邮件告警配置

发布时间: 2024-11-18 文章作者: myluzh 分类名称: Kubernetes 朗读文章


0x01 修改alertmanager.yaml配置文件
我的alertmanager是kube-prometheus部署,alertmanager.yaml是通过secrets挂载到alertmanager的。
# 创建一个带有邮件告警信息的配置
root@iZbp1bddlmdujngbc11omnZ:~/prom# vi alertmanager.yaml
global:
  resolve_timeout: 5m
  smtp_smarthost: 'smtp.exmail.qq.com:465'  
  smtp_hello: 'itho.cn'
  smtp_from: 'support@itho.cn'
  smtp_auth_username: 'support@itho.cn'      
  smtp_auth_password: 'xxxxxxxxx'     
  smtp_require_tls: true                    

inhibit_rules:
- equal:
  - namespace
  - alertname
  source_match:
    severity: critical
  target_match_re:
    severity: warning|info
- equal:
  - namespace
  - alertname
  source_match:
    severity: warning
  target_match_re:
    severity: info

receivers:
- name: email_myluzh
  email_configs:
  - to: 'myluzh@qq.com'
    send_resolved: true
- name: null_receiver  

route:
  group_by:
  - namespace
  group_interval: 5m
  group_wait: 30s
  receiver: email_myluzh
  repeat_interval: 12h
  routes:
  - match:
      alertname: Watchdog
    receiver: null_receiver
  - match:
      severity: critical
    receiver: null_receiver

# 从 alertmanager.yaml 文件生成一个 Kubernetes Secret 资源的 YAML 文件,并将其内容保存到 alertmanager-main-generated.yaml 文件中
root@iZbp1bddlmdujngbc11omnZ:~/prom# kubectl create secret generic alertmanager-main-generated --from-file=alertmanager.yaml --dry-run=client -o yaml > alertmanager-main-generated.yaml

# 删除原来有alertmanager-main-generated配置
root@iZbp1bddlmdujngbc11omnZ:~/prom# kubectl delete secrets alertmanager-main-generated   -n monitoring
secret "alertmanager-main-generated" deleted

# 应用新的alertmanager-main-generated配置
root@iZbp1bddlmdujngbc11omnZ:~/prom# kubectl apply -f alertmanager-main-generated.yaml -n monitoring
secret/alertmanager-main-generated created

如果要配置只有符合 namespace="dev-test" 的告警会发送到邮箱,可以如下配置:
1、将 route 的默认 receiver 设置为 null_receiver,以便丢弃未匹配的告警。
2、在 routes 中,新增 namespace: dev-test 的匹配条件,并指定接收器为 email_myluzh,这样只有 namespace="dev-test" 的告警会发送到 myluzh@qq.com。
route:
  group_by:
  - namespace
  group_interval: 5m
  group_wait: 30s
  receiver: null_receiver  # 默认接收器,不发送告警
  repeat_interval: 12h
  routes:
  - match:
      namespace: dev-test  # 仅匹配 namespace="dev-test"
    receiver: email_myluzh

receivers:
- name: email_myluzh
  email_configs:
  - send_resolved: true
    to: myluzh@qq.com
- name: null_receiver  # 空接收器,不发送告警

0x02 测试验证
如果告警后没有成功发送邮件,可以查看日志 kubectl logs alertmanager-main-0 -n monitoring -c alertmanager



参考链接: https://prometheus.io/docs/alerting/latest/configuration/

标签: 邮件告警 Prometheus Alertmanager

发表评论