如何在ArgoCD ApplicationSet的Git Generator中配置多目标集群?
Git Directory Generator 配置多K8s集群目标方法
要实现和List Generator类似的多集群目标配置,Git Directory Generator通常通过以下两种方式完成:
方法1:矩阵生成器组合Git与集群/列表生成器
借助矩阵生成器,将Git Directory Generator的目录参数和Cluster Generator(或List Generator)的集群参数组合,让每个Git目录对应所有目标集群生成应用实例。
示例配置(结合Cluster Generator)
apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: multi-cluster-apps spec: generators: - matrix: generators: # 定义Git目录源 - git: repoURL: https://git.example.com/apps.git revision: HEAD directories: - path: apps/* # 定义集群源(通过标签筛选集群) - clusters: selector: matchLabels: environment: prod template: metadata: name: '{{path.basename}}-{{name}}' spec: project: default source: repoURL: https://git.example.com/apps.git targetRevision: HEAD path: '{{path}}' destination: server: '{{server}}' namespace: '{{path.basename}}'
替换为List Generator定义集群
如果要像List Generator那样手动指定集群,把上面的Cluster Generator换成List Generator即可:
- list: elements: - cluster: prod-cluster-1 url: https://prod-cluster-1.example.com - cluster: prod-cluster-2 url: https://prod-cluster-2.example.com
模板里对应使用{{cluster}}和{{url}}引用参数即可。
方法2:Git目录内存储集群专属配置
如果每个Git目录需要绑定特定集群,可以在目录下存放集群配置文件,通过Git Generator读取该文件的参数来指定目标集群。
示例配置
apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: app-per-cluster spec: generators: - git: repoURL: https://git.example.com/apps.git revision: HEAD directories: - path: apps/* # 读取每个目录下的cluster-config.yaml文件 files: - path: '*/cluster-config.yaml' template: metadata: name: '{{path.basename}}' spec: project: default source: repoURL: https://git.example.com/apps.git targetRevision: HEAD path: '{{path}}' destination: server: '{{cluster.url}}' namespace: '{{cluster.namespace}}'
目录内的集群配置文件示例
每个apps/[app-name]/cluster-config.yaml内容如下:
cluster: url: https://prod-cluster-1.example.com namespace: app-demo
内容的提问来源于stack exchange,提问作者devops_dummy
相关产品推荐
相关产品推荐

