如何基于 Alertmanager 实现微信企业号告警?
解读
在国内生产环境,微信企业号(现称企业微信) 是最轻量、最合规、最省钱的告警通道之一。Couchbase 集群的 N1QL 延迟、内存配额、磁盘写队列、XDCR 断链、搜索节点掉线等关键指标,一旦触发 Prometheus 规则,必须秒级推送到企业微信,否则运维人员无法“秒回”止血。面试官问这道题,核心想看三点:
- 你是否熟悉 Alertmanager 的路由树、抑制、静默机制;
- 能否把企业微信的应用消息接口与 Alertmanager 的 webhook 无缝拼接;
- 是否理解高可用、多租户、灰度场景下的告警治理策略,而不是“能发出去就行”。
知识点
-
企业微信自建应用
- 需管理员在“应用管理”里新建“Couchbase 告警”应用,拿到 AgentId、Secret、CorpID。
- 推送地址统一为
https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=xxx,支持文本、markdown、图文三种格式,单条消息最大 2048 字节,超限需拆分。
-
Alertmanager 高可用部署
- 生产至少 3 实例,使用
--cluster.listen-address组成 Gossip 集群,防止脑裂重复告警。 - 配置
group_by: ['cluster', 'alertname'],把同一 Couchbase 集群、同一名称的告警合并成一条,避免企业微信刷屏。
- 生产至少 3 实例,使用
-
webhook 适配器
- Alertmanager 原生不支持企业微信,必须写轻量化适配器(Go 或 Python Flask,<200 行)。
- 适配器内部做三件事:
a. 用 CorpID+Secret 换 access_token(缓存 7000 s,失败自动重试);
b. 把 Alertmanager JSON 中的status=fire转成 markdown 红色标题,status=resolved转绿色;
c. 若消息超长,按 “集群→节点→桶” 三级维度拆包,保证每包不超过 2048 字节。
-
告警分级与路由
- 在 Prometheus 侧用
severity: critical/warning/info标签;Alertmanager 路由树匹配后,critical 立即@所有人,warning 只发值班群。 - 使用
inhibit_rules抑制策略:当 Couchbase 节点宕机告警已触发时,抑制该节点上的磁盘空间不足告警,防止告警风暴。
- 在 Prometheus 侧用
-
安全合规
- 适配器部署在内网 K8s 独立命名空间,通过
NetworkPolicy仅允许 Alertmanager 访问,防止 Secret 泄露。 - access_token 写入 K8s Secret,并配置
vault-agent自动轮转,避免硬编码。
- 适配器部署在内网 K8s 独立命名空间,通过
答案
步骤一:企业微信侧准备
- 登录企业微信管理后台 → 应用管理 → 创建“Couchbase 告警”自建应用,上传 logo,设置可见范围“运维部”。
- 记录 CorpID、AgentId、Secret;在“接收消息”里填写适配器地址(先占位,后续再改)。
步骤二:编写 webhook 适配器(以 Go 为例,核心片段)
type WechatMsg struct {
ToUser string `json:"touser"` // "@all"
MsgType string `json:"msgtype"`
AgentID int `json:"agentid"`
Text struct {
Content string `json:"content"`
} `json:"text"`
}
func sendWechat(markdown string) error {
token := getCachedToken(corpID, secret)
url := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + token
msg := WechatMsg{ToUser: "@all", MsgType: "markdown", AgentID: agentID}
msg.Text.Content = markdown
_, err := http.Post(url, "application/json", bytes.NewBuffer(json.Marshal(msg)))
return err
}
适配器容器镜像 wechat-webhook:1.0 推送到公司 Harbor,部署为 K8s Deployment,2 副本,配置 HPA 根据 CPU 50% 扩容。
步骤三:Alertmanager 配置(关键段落)
global:
resolve_timeout: 5m
route:
group_by: ['cluster', 'alertname']
group_wait: 10s
group_interval: 1m
repeat_interval: 4h
receiver: 'wechat'
routes:
- match:
severity: critical
receiver: 'wechat-urgent'
continue: true
receivers:
- name: 'wechat'
webhook_configs:
- url: 'http://wechat-webhook.couchbase-monitoring.svc.cluster.local:8080/webhook'
send_resolved: true
http_config:
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
inhibit_rules:
- source_match:
alertname: 'CouchbaseNodeDown'
target_match:
alertname: 'CouchbaseDiskSpaceWarning'
equal: ['instance']
步骤四:验证
- 手动停掉 Couchbase 某一个 data 节点,观察 Prometheus Alert 页面出现
CouchbaseNodeDown; - 企业微信群里秒级收到红色 markdown 卡片,@所有人,并附带 Grafana 链接;
- 节点恢复后,收到绿色 resolved 提示。
步骤五:灰度与回滚
适配器发布采用 Argo Rollout,金丝雀 10% 流量 5 分钟,无错误再全量;若企业微信接口异常,一键回滚,告警自动走备用通道(短信或飞书)。
拓展思考
-
多集群多租户:
当 Couchbase 集群按业务线划分为 A、B、C 三套时,可在 Alertmanager 路由里增加tenant标签,适配器根据标签把消息推到不同企业微信子部门,实现租户隔离,避免 A 集群故障刷屏 B 集群负责人。 -
自适应降噪:
在适配器里增加计数滑动窗口,若 5 分钟内同一集群同类告警超过 20 条,则自动升级为“疑似雪崩”卡片,提示值班人员直接电话会议,而不是继续微信轰炸。 -
双向互动:
企业微信支持“回调模式”,可在卡片里加按钮“已认领”“已处理”,值班人员点击后回调到适配器,调用 Alertmanager API 对该告警组做静默 30 分钟,实现移动端的轻量级闭环,而不是被动接收。