如何为 ArgoCD 设置 app-of-apps 模式?

解读

在国内云原生面试中,ArgoCD 的 app-of-apps 模式是高频考点。它本质上是“用 Git 仓库里的一个 ArgoCD Application 资源,去一次性声明并管理 N 个子 Application 资源”,实现“声明式管理声明式”,解决多环境、多微服务批量部署与版本一致性问题。
面试官真正想验证的是:

  1. 你是否理解 GitOps 声明式思想;
  2. 能否把 FastAPI 微服务与 ArgoCD 结合落地;
  3. 是否掌握国内常见私有 Git(GitLab/Gitea)与多集群场景下的权限、凭证、回滚、灰度策略。
    回答时要体现“Python/FastAPI 视角”:把 FastAPI 镜像版本、配置、环境变量全部作为 Git 仓库里的 K8s 资源,通过 app-of-apps 统一驱动,而不是手动改 ArgoCD UI。

知识点

  1. ArgoCD 三大核心 CRD:Application、AppProject、ApplicationSet。
  2. app-of-apps 与 ApplicationSet 的区别:前者是“一个 Application 指向一个目录,目录里全是 Application YAML”,后者是“模板化批量生成 Application”。
  3. Git 目录结构设计(国内习惯):
    argocd/
    ├── apps/                 # app-of-apps 根 Application 只指向这里
    │   ├── base/             # 公共 kustomization
    │   ├── overlays/
    │   │   ├── dev/
    │   │   ├── staging/
    │   │   └── prod/
    └── bootstrap/            # 只在 install 时手动 apply 一次
        ├── root-app.yaml     # app-of-apps 的 Application
        ├── projects.yaml     # AppProject 隔离
        └── repos-secret.yaml # 私有 Git 凭证
    
  4. FastAPI 容器化最佳实践:
    • 使用官方 slim 镜像,multi-stage 构建,/app 目录非 root 用户;
    • 把 gunicorn + uvicorn.workers.UvicornWorker 作为生产入口;
    • 配置通过 Pydantic BaseSettings 从环境变量注入,环境变量由 K8s ConfigMap+Secret 渲染,与镜像解耦。
  5. 国内私有 Git 认证:
    • GitLab 私有库通过 deploy token 或 ssh key,Secret 类型为 argocd.argoproj.io/secret-type: repository;
    • 若使用子模块,需同步配置 ssh.knownHosts。
  6. 多集群管理:
    • 在 argocd-cm 中增加集群 kubeconfig,或直接用 argocd cluster add;
    • 通过 AppProject 限制每个 FastAPI 环境只能部署到指定 namespace 与集群。
  7. 回滚与漂移治理:
    • FastAPI 镜像 tag 使用 Git tag 语义化版本,回滚只需回退 Git commit;
    • 开启 syncPolicy.automated.selfHeal=true,防止手动 kubectl set image 导致漂移。
  8. 灰度发布:
    • 在 overlay 里通过 Argo Rollouts 的 AnalysisTemplate 做 canary,FastAPI 暴露 /health 与 /metrics 给 Prometheus;
    • 与 HPA 结合,根据 QPS 自动扩容。

答案

下面给出一条可直接落地的“FastAPI + app-of-apps”最小可验证路径,全部 YAML 均可公开到 GitLab,符合国内私有云安全规范。

步骤 1:准备 Git 仓库
在 GitLab 新建项目 fastapi-gitops,目录如下:

fastapi-gitops/
├── apps/
│   ├── kustomization.yaml
│   ├── fastapi-dev.yaml
│   └── fastapi-prod.yaml
├── manifests/
│   ├── base/
│   │   ├── deployment.yaml
│   │   ├── service.yaml
│   │   ├── configmap.yaml
│   │   └── kustomization.yaml
│   └── overlays/
│       ├── dev/
│       └── prod/
└── bootstrap/
    ├── root-app.yaml
    ├── projects.yaml
    └── repo-secret.yaml

步骤 2:创建私有 Git 凭证(以 deploy token 为例)
文件 bootstrap/repo-secret.yaml:

apiVersion: v1
kind: Secret
metadata:
  name: gitlab-deploy-token
  namespace: argocd
  labels:
    argocd.argoproj.io/secret-type: repository
type: Opaque
stringData:
  url: https://gitlab.xxx.cn/team/fastapi-gitops.git
  username: deploy-token-username
  password: deploy-token-password

步骤 3:创建 AppProject 隔离环境
文件 bootstrap/projects.yaml:

apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
  name: fastapi
  namespace: argocd
spec:
  sourceRepos:
  - 'https://gitlab.xxx.cn/team/fastapi-gitops.git'
  destinations:
  - namespace: 'fastapi-dev'
    server: https://kubernetes.default.svc
  - namespace: 'fastapi-prod'
    server: https://kubernetes.default.svc
  roles:
  - name: ci
    policies:
    - p, proj:fastapi:ci, applications, *, fastapi/*, allow

步骤 4:编写 app-of-apps 根 Application
文件 bootstrap/root-app.yaml:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: apps
  namespace: argocd
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  project: default
  source:
    repoURL: https://gitlab.xxx.cn/team/fastapi-gitops.git
    targetRevision: HEAD
    path: apps
  destination:
    server: https://kubernetes.default.svc
    namespace: argocd
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
    - CreateNamespace=true

步骤 5:在 apps/ 目录里声明子 Application
文件 apps/fastapi-dev.yaml:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: fastapi-dev
  namespace: argocd
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  project: fastapi
  source:
    repoURL: https://gitlab.xxx.cn/team/fastapi-gitops.git
    targetRevision: HEAD
    path: manifests/overlays/dev
  destination:
    server: https://kubernetes.default.svc
    namespace: fastapi-dev
  syncPolicy:
    automated:
      prune: true
      selfHeal: true

步骤 6:manifests/base/deployment.yaml(FastAPI 示例)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: fastapi
spec:
  replicas: 2
  selector:
    matchLabels: {app: fastapi}
  template:
    metadata:
      labels: {app: fastapi}
    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 1000
      containers:
      - name: app
        image: registry.xxx.cn/team/fastapi:1.0.0
        ports:
        - containerPort: 8000
        envFrom:
        - configMapRef: {name: fastapi-cm}
        - secretRef: {name: fastapi-secret}
        readinessProbe:
          httpGet: {path: /health, port: 8000}
        livenessProbe:
          httpGet: {path: /health, port: 8000}

步骤 7:一键安装

# 在任意可访问 K8s 的运维机
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/v2.10.0/manifests/install.yaml
# 等待 argocd-server 启动
kubectl apply -f bootstrap/

ArgoCD 会自动创建 root-app,root-app 再自动创建 fastapi-dev,fastapi-dev 再把真正的 Deployment/Service 同步到 fastapi-dev namespace,完成 GitOps 闭环。
后续只需把 registry.xxx.cn/team/fastapi:1.0.0 改为 1.1.0 并推送到 Git,ArgoCD 会在 30 秒内完成滚动升级;回滚直接 git revert 即可。

拓展思考

  1. 如果微服务数量 >50,建议把 apps/ 目录换成 ApplicationSet,利用 Git 目录生成器,根据 overlays/*/kustomization.yaml 的存在性自动为每个环境生成 Application,减少重复 YAML。
  2. 国内金融客户要求“不可自动同步”,可把 syncPolicy.automated 关闭,改为 CI 调用 ArgoCD API 触发同步,并在 GitLab CI 里使用 argocd app sync --dry-run 做准入检查。
  3. 对 FastAPI 的灰度场景,可引入 Argo Rollouts 的 AnalysisTemplate,结合 Prometheus 查询 rate(http_requests_total{job="fastapi",status=~"5.."}[1m])>0.05 自动回滚,实现“Python 代码变更 → Git push → 金丝雀 → 自动回滚”全链路无人值守。