ArgoCD ApplicationSet核心机制解析
ArgoCD ApplicationSet控制器扩展了ArgoCD的声明式交付能力,支持通过模板化方式批量生成Application资源。与传统手动创建单个Application不同,ApplicationSet通过Generator自动发现目标集群或Git目录,动态生成对应的Application,实现”一套配置交付多集群”的GitOps模式。
ApplicationSet的CRD结构包含两个核心字段:spec.generators 定义生成策略,spec.template 定义Application模板。Generator每次运行会生成一组参数替换模板中的占位符,每个参数组合对应一个Application实例。
Cluster Generator实现多集群自动发现
Cluster Generator是最常用的生成器,自动从ArgoCD的集群列表中匹配目标集群:
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: webapp-multi-cluster
namespace: argocd
spec:
generators:
- clusters:
selector:
matchLabels:
env: production
template:
metadata:
name: "{{name}}-webapp"
spec:
project: default
source:
repoURL: https://git.example.com/apps/webapp.git
targetRevision: main
path: overlays/{{name}}
destination:
server: "{{server}}"
namespace: webapp
使用前需为集群注册标签。在ArgoCD中添加集群时指定标签:
argocd cluster add prod-east-1 \
--label env=production \
--label region=east
Generator遍历所有带 env: production 标签的集群,为每个集群生成一个Application。{{name}} 替换为集群名,{{server}} 替换为集群API Server地址。
Git Directory Generator实现多环境目录交付
Git Directory Generator扫描仓库中的目录结构,每个目录生成一个Application:
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: microservices
namespace: argocd
spec:
generators:
- git:
repoURL: https://git.example.com/platform.git
revision: main
directories:
- path: services/*
- exclude:
- path: services/test-*
template:
metadata:
name: "{{path.basename}}"
spec:
project: default
source:
repoURL: https://git.example.com/platform.git
targetRevision: main
path: "{{path}}"
destination:
server: https://kubernetes.default.svc
namespace: "{{path.basename}}"
目录排除规则 exclude 支持glob模式,适合在开发阶段排除测试服务。每个子目录需要包含独立的Kustomize或Helm配置,Generator只负责发现目录并生成Application,不关心目录内的具体内容。
Matrix Generator组合多维度生成策略
当需要”集群 x 环境 x 服务”的笛卡尔积部署时,使用Matrix Generator:
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: platform-matrix
namespace: argocd
spec:
generators:
- matrix:
generators:
- clusters:
selector:
matchLabels:
tier: frontend
- list:
elements:
- service: api-gateway
path: services/api-gateway
- service: auth-service
path: services/auth
- service: user-service
path: services/user
template:
metadata:
name: "{{name}}-{{service}}"
spec:
project: default
source:
repoURL: https://git.example.com/platform.git
targetRevision: main
path: "{{path}}"
destination:
server: "{{server}}"
namespace: "{{service}}"
Matrix Generator将两个Generator的输出做笛卡尔积。上述配置中,3个frontend集群 x 3个服务 = 9个Application实例,每个集群部署全部3个服务。
Progressive Delivery与Rollout策略
ApplicationSet配合Argo Rollouts可实现渐进式交付。在template中引入Rollout资源替代Deployment:
template:
spec:
source:
helm:
valueFiles:
- values.yaml
- "overlays/{{name}}/values.yaml"
parameters:
- name: image.tag
value: "{{imageTag}}"
关键实践:利用ApplicationSet的 syncPolicy 控制自动同步行为,结合Argo Rollouts的canary策略,实现”代码合并到自动生成Application到金丝雀发布”的全自动化链路。生产环境建议设置 automated.prune: false,避免Generator变更导致正在运行的Application被误删。
多集群场景下的权限与安全配置
跨集群部署需要正确配置RBAC权限。ArgoCD的Project资源限制Application可部署的目标集群和资源类型:
apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
name: platform
namespace: argocd
spec:
sourceRepos:
- "https://git.example.com/*"
destinations:
- namespace: "webapp-*"
server: "https://prod-east.k8s.local"
- namespace: "webapp-*"
server: "https://prod-west.k8s.local"
clusterResourceWhitelist:
- group: ""
kind: Namespace
建议为不同环境创建独立Project,限制每个Project只能部署到指定集群的指定命名空间。ApplicationSet生成的Application必须属于某个Project,超出Project权限范围的Application会进入Error状态。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/argocdapplicationset-duo-ji-qun-gitops-dong-tai-jiao-fu-shi/