Chapter 09

通知与可观测性

为 ArgoCD 建立完整的可观测体系:argocd-notifications 实时推送部署事件、Prometheus 采集核心指标、Grafana 可视化监控面板、审计日志追溯操作历史。

argocd-notifications 简介

argocd-notifications 是 ArgoCD 的通知系统(ArgoCD 2.3+ 已内置),基于"触发器(Trigger)+ 模板(Template)+ 订阅(Subscription)"三要素工作:

Trigger(触发器)
定义什么情况下发送通知,基于 Application 状态变化判断。内置触发器如 on-sync-failed(同步失败)、on-health-degraded(健康状态变差)、on-deployed(成功部署)等。
Template(模板)
定义通知的内容格式,支持 Go 模板语法,可引用 Application 的各种字段(名称、状态、revision 等)。不同渠道(Slack/邮件)可使用不同模板。
Subscription(订阅)
将触发器、模板和通知渠道绑定,可以在 Application 的注解中声明,也可以通过 argocd-notifications-cm 全局配置。

配置 Slack 通知

1. 创建 Slack Bot Token Secret

kubectl create secret generic argocd-notifications-secret \
  -n argocd \
  --from-literal=slack-token=xoxb-your-slack-bot-token

2. 配置通知服务和模板

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
  namespace: argocd
data:
  # ── 服务配置 ──
  service.slack: |
    token: $slack-token              # 引用 Secret 中的 key
    username: ArgoCD Bot
    icon: :argo_cd:

  # ── 通知模板 ──
  template.app-deployed: |
    slack:
      attachments: |
        [{
          "color": "#18be52",
          "title": "✅ 部署成功: {{ .app.metadata.name }}",
          "text": "已部署到 {{ .app.spec.destination.namespace }} \n版本: {{ .app.status.sync.revision }}",
          "footer": "ArgoCD | {{ .app.metadata.namespace }}"
        }]

  template.app-sync-failed: |
    slack:
      attachments: |
        [{
          "color": "#E96D76",
          "title": "❌ 同步失败: {{ .app.metadata.name }}",
          "text": "{{ range .app.status.conditions }}{{ .message }}{{ end }}",
          "actions": [{
            "type": "button",
            "text": "查看详情",
            "url": "{{ .context.argocdUrl }}/applications/{{ .app.metadata.name }}"
          }]
        }]

  template.app-health-degraded: |
    slack:
      attachments: |
        [{
          "color": "#f4c030",
          "title": "⚠️ 健康状态异常: {{ .app.metadata.name }}",
          "text": "当前状态: {{ .app.status.health.status }}"
        }]

  # ── 触发器 ──
  trigger.on-deployed: |
    - when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
      send: [app-deployed]

  trigger.on-sync-failed: |
    - when: app.status.operationState.phase in ['Error', 'Failed']
      send: [app-sync-failed]

  trigger.on-health-degraded: |
    - when: app.status.health.status == 'Degraded'
      send: [app-health-degraded]

  # ── 全局默认订阅(所有应用)──
  subscriptions: |
    - recipients:
      - slack:#deployments
      triggers:
      - on-deployed
      - on-sync-failed
      - on-health-degraded

3. 在 Application 上订阅通知

# Application 注解中订阅(覆盖全局配置)
metadata:
  annotations:
    notifications.argoproj.io/subscribe.on-deployed.slack: #prod-deployments
    notifications.argoproj.io/subscribe.on-sync-failed.slack: #prod-alerts

Prometheus 指标

ArgoCD 的各组件暴露 Prometheus 指标端点,核心指标包括:

指标名 类型 描述
argocd_app_info Gauge 应用基础信息,包含 name/project/health/sync 状态标签
argocd_app_sync_total Counter 同步操作总次数,按 app/project/dest/phase 细分
argocd_app_reconcile_count Counter 调和循环次数
argocd_app_k8s_request_total Counter 发往 K8s API Server 的请求总数
argocd_cluster_connection_status Gauge 集群连接状态(1=成功,0=失败)
argocd_git_request_total Counter Git 仓库请求次数(fetch/ls-remote)
argocd_redis_request_total Counter Redis 请求次数

配置 Prometheus 采集

# ServiceMonitor(Prometheus Operator)
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: argocd-metrics
  namespace: argocd
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: argocd-metrics
  endpoints:
  - port: metrics
    interval: 30s

Grafana ArgoCD 仪表盘

社区提供了成熟的 ArgoCD Grafana Dashboard,可直接导入(Dashboard ID: 14584)。关键面板包括:

应用健康概览

展示所有 Application 的 Health Status 分布(Healthy/Degraded/Progressing),快速发现异常应用。通过 argocd_app_info{health_status!="Healthy"} 实现。

同步成功率

统计一段时间内的同步成功/失败比例,通过 rate(argocd_app_sync_total{phase="Succeeded"}[5m]) 计算,用于 SLO 追踪。

集群连接状态

多集群场景下监控各集群连接健康状态,argocd_cluster_connection_status == 0 时触发告警。

仓库同步延迟

从 Git commit 到 ArgoCD 检测到变更的延迟,评估轮询间隔是否合理(默认 3 分钟)。

审计日志

ArgoCD 所有操作都记录在 Kubernetes 事件和 argocd-server 日志中,可用于安全审计:

# 查看 Application 的 K8s Events(操作历史)
kubectl get events -n argocd \
  --field-selector involvedObject.name=my-app \
  --sort-by=.lastTimestamp

# 查看 argocd-server 审计日志
kubectl logs -n argocd -l app.kubernetes.io/name=argocd-server \
  | grep "audit"

# argocd CLI 查看 app 操作历史
argocd app history my-app
生产监控建议

至少要为以下情况配置告警:① argocd_cluster_connection_status == 0(集群连接断开);② argocd_app_info{health_status="Degraded"}(有应用 Degraded);③ argocd_app_sync_total{phase="Error"} > 0(同步失败)。这三个告警能覆盖 ArgoCD 最关键的故障场景。

本章小结

argocd-notifications 通过 Trigger + Template + Subscription 三要素将部署事件实时推送到 Slack/钉钉/邮件/Webhook。Prometheus + Grafana 提供全面的指标监控,重点关注应用健康分布、同步成功率和集群连接状态。审计日志通过 K8s Events 持久化所有操作记录。完整的可观测体系是 GitOps 生产落地的重要保障。