Hub-Spoke 架构
ArgoCD 的多集群管理采用 Hub-Spoke(中心辐射)架构:ArgoCD 安装在一个专用的"管理集群"(Hub),向外辐射管理多个"工作集群"(Spoke)。Hub 集群不运行业务应用,只运行 ArgoCD 控制面。
Hub-Spoke 多集群架构 ┌──────────────────────────────┐ │ Hub 集群(管理面) │ │ │ │ ┌────────────────────────┐ │ │ │ ArgoCD Control Plane │ │ │ │ API Server │ │ │ │ Repo Server │ │ │ │ App Controller │ │ │ └────────────────────────┘ │ └──────────────┬───────────────┘ ┌───────────────┼───────────────┐ ↓ ↓ ↓ ┌────────────────┐ ┌────────────────┐ ┌────────────────┐ │ Dev 集群 │ │ Staging 集群 │ │ Prod 集群 │ │ 业务应用 │ │ 业务应用 │ │ 业务应用 │ │ (Spoke) │ │ (Spoke) │ │ (Spoke) │ └────────────────┘ └────────────────┘ └────────────────┘
Hub 集群专门运行 ArgoCD,不部署业务应用,这样即使某个 Spoke 集群出现问题,也不影响 ArgoCD 的管理能力。Hub 集群本身应具备高可用(ArgoCD server/controller 多副本)。
注册外部集群
方式一:argocd CLI(推荐)
# 确保 kubeconfig 中有目标集群的 context
kubectl config get-contexts
# 注册 dev 集群
argocd cluster add my-dev-cluster-context \
--name dev-cluster \
--label environment=dev \
--label region=cn-hangzhou
# 注册 prod 集群
argocd cluster add my-prod-cluster-context \
--name prod-cluster \
--label environment=production \
--label region=cn-hangzhou
# 查看所有已注册集群
argocd cluster list
SERVER NAME VERSION STATUS
https://kubernetes.default.svc in-cluster 1.29 Successful
https://dev-cluster.example.com:6443 dev-cluster 1.29 Successful
https://prod-cluster.example.com:6443 prod-cluster 1.30 Successful
注册过程发生了什么
argocd cluster add 执行时,会在目标集群中创建一个 ServiceAccount(argocd-manager),并绑定 ClusterAdmin 权限(或自定义权限),然后将该 ServiceAccount 的 token 存储到 Hub 集群的 Secret 中(argocd namespace),供 Application Controller 使用。
# ArgoCD 自动创建的集群凭证 Secret(自动管理,不需要手动创建)
apiVersion: v1
kind: Secret
metadata:
name: prod-cluster-creds
namespace: argocd
labels:
argocd.argoproj.io/secret-type: cluster # 必须有此 label
environment: production # 自定义标签(用于 Cluster Generator)
stringData:
name: prod-cluster
server: https://prod-cluster.example.com:6443
config: |
{
"bearerToken": "<service-account-token>",
"tlsClientConfig": {
"caData": "<base64-ca-cert>"
}
}
集群标签选择器
给集群打标签后,ApplicationSet 的 Cluster Generator 可以按标签筛选目标集群:
# 只部署到生产集群
generators:
- clusters:
selector:
matchLabels:
environment: production
# 部署到杭州区所有集群
generators:
- clusters:
selector:
matchLabels:
region: cn-hangzhou
# 使用 matchExpressions 更灵活的筛选
generators:
- clusters:
selector:
matchExpressions:
- key: environment
operator: In
values: [staging, production]
实战:两集群(dev/prod)统一管理
完整示例:用一个 ApplicationSet 同时管理 dev 和 prod 两个集群的应用部署,通过集群标签路由不同配置:
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: my-app-multi-cluster
namespace: argocd
spec:
goTemplate: true
generators:
- clusters:
selector:
matchExpressions:
- key: environment
operator: In
values: [dev, production]
template:
metadata:
name: 'my-app-{{.metadata.labels.environment}}'
spec:
project: default
source:
repoURL: https://github.com/my-org/config
targetRevision: main
path: 'apps/my-app/overlays/{{.metadata.labels.environment}}'
destination:
server: '{{.server}}' # 集群 Server URL(由 Generator 注入)
namespace: my-app
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
灾备:主备集群自动切换
使用 ArgoCD + ApplicationSet 实现基本的主备灾备切换:
正常状态
主集群(primary)运行所有应用,备集群(standby)同样部署了相同配置但副本数为 0(通过 overlay 控制),确保镜像已预热。
切换步骤
主集群故障时:① 修改备集群 overlay 的 replicas 为正常值;② 将 DNS/负载均衡切换到备集群 IP;③ ArgoCD 检测到 Git 变更后自动同步备集群扩容。整个过程在 5 分钟内完成(GitOps 自动同步间隔)。
主集群恢复
主集群修复后:① DNS 切回主集群;② 将主集群 overlay replicas 改回正常值;③ ArgoCD 自动同步主集群恢复服务;④ 将备集群 replicas 改回 0 节约资源。
ArgoCD 的多集群管理只负责应用层的声明式部署,数据库、消息队列等有状态服务的跨集群同步需要额外方案(如数据库主从复制、对象存储跨地域同步)。真正的灾备需要应用层和数据层协同设计。
Hub-Spoke 架构让一个 ArgoCD 实例管理多个集群,ArgoCD 运行在专用 Hub 集群中,通过 ServiceAccount token 访问各 Spoke 集群。集群标签(Label)是路由的核心,ApplicationSet + Cluster Generator 实现多集群批量部署,每个集群对应各自的 Kustomize overlay。灾备切换通过修改 Git 配置触发,整个流程完全 GitOps 化。