ArgoCD App of Apps 多应用管理教程

ArgoCD 是 Kubernetes 生态中最流行的 GitOps 持续交付工具,它以 Git 仓库作为应用配置的唯一真实来源,自动将 Git 中的声明式配置同步到 Kubernetes 集群。App of Apps 模式是 ArgoCD 管理大量应用的高级模式,通过一个父应用来管理所有子应用的创建和配置。

一、安装 ArgoCD

# 创建命名空间
kubectl create namespace argocd

# 安装 ArgoCD
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

# 等待 Pod 就绪
kubectl wait --for=condition=Ready pods --all -n argocd --timeout=300s

# 获取初始管理员密码
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d

1.1 访问 ArgoCD UI

# 方法一:端口转发
kubectl port-forward svc/argocd-server -n argocd 8080:443

# 方法二:修改为 NodePort
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "NodePort"}}'

# 方法三:使用 Ingress(推荐生产环境)
cat > argocd-ingress.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: argocd-server
  namespace: argocd
  annotations:
    nginx.ingress.kubernetes.io/ssl-passthrough: "true"
spec:
  rules:
  - host: argocd.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: argocd-server
            port:
              number: 443
EOF
kubectl apply -f argocd-ingress.yaml

1.2 安装 ArgoCD CLI

curl -sSL -o /usr/local/bin/argocd https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
chmod +x /usr/local/bin/argocd

# 登录
argocd login localhost:8080 --username admin --password 初始密码 --insecure

# 修改密码
argocd account update-password

二、创建应用

# 通过 CLI 创建应用
argocd app create nginx-app \
  --repo https://github.com/user/k8s-configs.git \
  --path nginx \
  --dest-server https://kubernetes.default.svc \
  --dest-namespace default \
  --sync-policy automated

# 通过 YAML 声明创建
cat > nginx-app.yaml <<'EOF'
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: nginx-app
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/user/k8s-configs.git
    targetRevision: HEAD
    path: nginx
  destination:
    server: https://kubernetes.default.svc
    namespace: default
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
EOF
kubectl apply -f nginx-app.yaml

三、App of Apps 模式

App of Apps 是一种通过一个父应用来管理所有子应用的模式:

# Git 仓库结构:
# apps/
# ├── Chart.yaml(或 kustomization.yaml)
# └── templates/
#     ├── nginx.yaml
#     ├── redis.yaml
#     └── monitoring.yaml

# 父应用定义
cat > root-app.yaml <<'EOF'
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: root-app
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/user/k8s-configs.git
    targetRevision: HEAD
    path: apps
  destination:
    server: https://kubernetes.default.svc
    namespace: argocd
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
EOF

# 子应用模板示例(apps/templates/nginx.yaml)
cat > apps/templates/nginx.yaml <<'EOF'
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: nginx
  namespace: argocd
  finalizers:
    - resources-finalizer.argocd.argoproj.io
spec:
  project: default
  source:
    repoURL: https://github.com/user/k8s-configs.git
    path: services/nginx
    targetRevision: HEAD
  destination:
    server: https://kubernetes.default.svc
    namespace: web
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
EOF

四、同步策略

# 手动同步应用
argocd app sync nginx-app

# 查看应用状态
argocd app get nginx-app

# 查看所有应用
argocd app list

# 查看同步差异
argocd app diff nginx-app

# 回滚到上一个版本
argocd app rollback nginx-app

五、多集群管理

# 添加远程集群
argocd cluster add remote-cluster-context

# 查看已注册的集群
argocd cluster list

# 在子应用中指定目标集群
# destination:
#   server: https://remote-cluster-api:6443
#   namespace: default

六、常见问题

应用同步失败

# 查看同步详情
argocd app get myapp --show-operation

# 查看事件
kubectl get events -n argocd --sort-by='.lastTimestamp'

# 强制同步
argocd app sync myapp --force

总结

ArgoCD 的 App of Apps 模式为大规模 Kubernetes 应用管理提供了优雅的解决方案。结合 Kustomize 管理多环境配置,可以实现完整的 GitOps 工作流。另一个流行的 GitOps 方案是 Flux。选购搬瓦工 VPS 请访问 bwh81.net,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。