Skip to content

ArgoCD 监控与可观测

ArgoCD 自己也是跑在 K8s 里的服务,会崩、会慢、会因为 Git 仓库抽风而全线瘫痪。这篇讲怎么用 Prometheus 指标 + 日志把 ArgoCD 的健康管起来,重点在三件事:组件自身指标、应用交付指标(DORA 视角)、告警规则。

一、指标暴露点

ArgoCD 各组件都暴露了 Prometheus 指标(默认在 pod 的 8082 端口,路径 /metrics):

组件关键指标前缀关注点
argocd-serverargocd_.../http_...API 延迟、错误率
argocd-application-controllerargocd_app_...应用状态、同步队列深度
argocd-repo-serverargocd_git_...、argocd_kubectl_...Git 请求延迟、manifest 生成耗时
argocd-applicationset-controllerargocd_applicationset_...ApplicationSet 协调状态
argocd-redisredis_...缓存命中、连接数

用 ServiceMonitor 抓取(假设集群里已有 Prometheus Operator):

yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: argocd-metrics
  namespace: argocd
  labels:
    release: prometheus           # 匹配 Prometheus 实例的 serviceMonitorSelector
spec:
  namespaceSelector:
    matchNames: [argocd]
  selector:
    matchExpressions:
      - key: app.kubernetes.io/part-of
        operator: Exists
  endpoints:
    - port: metrics
      interval: 30s

装 ArgoCD 时其实自带指标 Service(argocd-metrics),只要集群的 Prometheus 能发现它就行——检查 kubectl get svc -n argocd | grep metrics。

二、最该盯的五个指标

1. 不同步的应用数量(交付健康度)

promql
count(argocd_app_info{sync_status="OutOfSync"}) by (project)

长期 > 0 说明有 Git 提交没同步——可能是自动化没开、可能是持续失败。这个指标持续上涨 = GitOps 在腐烂。

2. 健康异常的应用(业务健康度)

promql
count(argocd_app_info{health_status="Degraded"}) by (project)

配合按 namespace 拆分,一眼看出哪个环境出问题。

3. 同步耗时(交付速度)

promql
histogram_quantile(0.95, rate(argocd_app_sync_total_bucket[5m]))

P95 同步耗时突然变长——repo-server 压力大(Kustomize/Helm 渲染慢)、还是集群 API 慢。这是 DORA "部署频率"的前置指标。

4. 控制器协调滞后(同步实时性)

promql
argocd_app_reconcile_duration_seconds / argocd_app_reconcile_interval_seconds

实际协调间隔 / 期望间隔。接近或超过 1 说明 controller 处理不过来——该扩容或开分片了。

5. Git 请求错误率(真相源可用性)

promql
rate(argocd_git_request_errors_total[5m]) / rate(argocd_git_request_total[5m])

突然飙升:Git 仓库限流、token 过期、网络分区。ArgoCD 的全部真相系于 Git 可用性,这个指标是系统级风险预警。

三、告警规则示例

yaml
groups:
  - name: argocd
    rules:
      # 生产应用 Degraded 超过 10 分钟 → 立即告警
      - alert: ArgoAppDegraded
        expr: argocd_app_info{health_status="Degraded", namespace=~"prod.*"} == 1
        for: 10m
        labels: {severity: critical}
        annotations:
          summary: "ArgoCD 应用 {{ $labels.name }} 健康异常"

      # 大量应用长期不同步 → GitOps 腐烂
      - alert: ArgoAppsOutOfSyncTooMany
        expr: count(argocd_app_info{sync_status="OutOfSync"}) > 20
        for: 30m
        labels: {severity: warning}

      # 同步失败持续发生
      - alert: ArgoSyncFailing
        expr: increase(argocd_app_sync_failed_total[15m]) > 0
        for: 5m
        labels: {severity: critical}

      # Git 仓库不可用
      - alert: ArgoGitUnhealthy
        expr: rate(argocd_git_request_errors_total[5m]) / rate(argocd_git_request_total[5m]) > 0.5
        for: 5m
        labels: {severity: critical}

      # ArgoCD 组件本身宕了
      - alert: ArgoComponentDown
        expr: up{namespace="argocd", job="argocd-metrics"} == 0
        for: 5m
        labels: {severity: critical}

告警设计原则:Degraded 给 10 分钟 for 时长(瞬时的滚动更新误报太多)、Git 错误率给 5 分钟(等自愈)、组件 down 立即发。别把告警当日志用——每个告警都必须有明确的响应动作,否则就是狼来了。

四、UI 的"扩展信息":把业务指标挂到应用上

ArgoCD 的 UI 应用详情页可以嵌入外部信息面板(Extensions),把 Grafana 面板、日志查询、业务指标直接挂在应用卡片上——这是 ArgoCD 可观测性最被低估的功能。

配置在 argocd-cm:

yaml
data:
  application.resource.customizations: |
    # 略(健康检查用,见其他篇)
  # UI 扩展:给 Deployment 资源加 Grafana 链接
  ui.button.applications: |
    - title: 查看 Grafana
      icon: src
      href: https://grafana.example.com/d/app-overview?var-app={{.app.metadata.name}}

配合 Deployment 的 annotation 挂具体面板:

yaml
metadata:
  annotations:
    grafana-url: https://grafana.example.com/d/deployment?var-deployment=myapp

效果:点开应用的 Pod 树,每个资源旁直接跳它的监控面板——排查 Degraded 时不用跳出 ArgoCD 满系统找链接。

五、日志:排障的最后一道防线

指标告诉你"出事了",日志告诉你"为什么":

bash
# application-controller:同步决策的完整心路
kubectl logs -n argocd deploy/argocd-application-controller -f | grep myapp-prod

# repo-server:manifest 生成失败(kustomize build 报错在这)
kubectl logs -n argocd deploy/argocd-repo-server -f

# server:API 请求审计(谁在什么时候 sync 了什么)
kubectl logs -n argocd deploy/argocd-server -f

日志级别调优:argocd-cm 里 server.log.level: debug 可看更细的 API 请求,但调试完记得调回 info——debug 级别下 repo-server 会把每个 Git 操作都打出来,量很大。

集中式日志:生产环境把 argocd 命名空间的日志接入 Loki/ES——三个价值:① 跨 Pod 查一个 App 的完整时间线(HA 模式下 controller 分片,单 Pod 日志不全)② 留存审计 ③ 同步失败时按时间窗捞上下文。

六、把交付指标接到 DORA

ArgoCD 的数据天然适合算 DORA 四指标:

DORA 指标ArgoCD 数据源
部署频率argocd_app_sync_total 按天计数
变更前置时间Git commit 时间 → operationState.finishedAt 的差
变更失败率sync 失败次数 / 总 sync 次数
恢复时间Degraded → Healthy 的时长(用 argocd_app_info 的状态变化序列)

用 recording rule 落成 Grafana 面板,月度复盘直接看——这是给管理层证明 GitOps 价值的最佳素材,比"感觉部署快了"有说服力得多。

七、ArgoCD 自身的容量规划

监控数据指导扩容:

  • controller:argocd_app_reconcile_duration_seconds 持续高位 → 应用太多,加分片(HA 模式默认 3 副本已分片);单实例分片太少浪费
  • repo-server:argocd_git_request_duration_seconds 高 → 大仓库 clone 慢,考虑仓库拆分或 repo-server 加副本(无状态,随便扩)
  • redis:内存使用率高 → 应用数量多、清单缓存大,Redis HA 模式下扩内存节点
  • 集群总应用数参考:单 controller 分片实例轻松管 500+ App;超过 2000 App 必须评估分片策略和 API Server 压力

自查清单

  • [ ] 配好 ServiceMonitor,五个核心指标有面板
  • [ ] 告警规则覆盖:Degraded / OutOfSync 堆积 / sync 失败 / Git 不可用 / 组件 down
  • [ ] 会用日志三件套定位同步问题
  • [ ] 知道 UI Extensions 能把 Grafana 挂到应用上
  • [ ] 能说出 ArgoCD 指标到 DORA 四指标的映射

下一篇多集群管理:从"管一个集群"到"管所有集群"。

CTO Plus技术栈 Version 1.0.20261024-12