帮助改进此页面
要帮助改进本用户指南,请选择位于每个页面右侧窗格中的在 GitHub 上编辑此页面链接。
使用 ApplicationSets
ApplicationSet 通过模板生成多个应用程序,使您能够通过单个资源定义,跨多个集群、环境或命名空间部署相同的应用程序。
先决条件
ApplicationSets 的工作原理
ApplicationSets 使用生成器来生成参数,然后将这些参数应用于应用程序模板。每组生成的参数都会创建一个应用程序。
适用于 EKS 部署的常用生成器:
-
列表生成器:为每个环境明确定义集群和参数
-
集群生成器:自动部署到所有已注册的集群
-
Git 生成器:基于存储库结构生成应用程序
-
矩阵生成器:组合多个生成器以实现多维部署
有关完整的生成器参考,请参阅 ApplicationSet 文档
列表生成器
通过明确配置部署到多个集群:
apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: guestbook-all-clusters namespace: argocd spec: generators: - list: elements: - cluster: arn:aws:eks:us-west-2:111122223333:cluster/dev-cluster environment: dev replicas: "2" - cluster: arn:aws:eks:us-west-2:111122223333:cluster/staging-cluster environment: staging replicas: "3" - cluster: arn:aws:eks:us-west-2:111122223333:cluster/prod-cluster environment: prod replicas: "5" template: metadata: name: 'guestbook-{{environment}}' spec: project: default source: repoURL: https://github.com/example/guestbook targetRevision: HEAD path: 'overlays/{{environment}}' destination: server: '{{cluster}}' namespace: guestbook syncPolicy: automated: prune: true selfHeal: true
注意
将已注册的 EKS 集群设为目标时,请在 server 字段中使用 EKS 集群 ARN。您也可以使用带有 destination.name 的集群名称,而非 destination.server。
这将创建三个应用程序:guestbook-dev、guestbook-staging 和 guestbook-prod。
集群生成器
自动部署到所有已注册的集群:
apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: cluster-addons namespace: argocd spec: generators: - clusters: {} template: metadata: name: '{{name}}-addons' spec: project: default source: repoURL: https://github.com/example/cluster-addons targetRevision: HEAD path: addons destination: server: '{{server}}' namespace: kube-system syncPolicy: automated: prune: true selfHeal: true
这将自动为每个已注册的集群创建应用程序。
筛选集群:
spec: generators: - clusters: selector: matchLabels: environment: production
Git 生成器
Git 生成器基于存储库结构创建应用程序:
-
目录生成器:将每个目录作为单独的应用程序部署(适用于微服务)
-
文件生成器:根据参数文件生成应用程序(适用于多租户部署)
示例:微服务部署
apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: microservices namespace: argocd spec: generators: - git: repoURL: https://github.com/example/microservices revision: HEAD directories: - path: services/* template: metadata: name: '{{path.basename}}' spec: project: default source: repoURL: https://github.com/example/microservices targetRevision: HEAD path: '{{path}}' destination: server: arn:aws:eks:us-west-2:111122223333:cluster/my-cluster namespace: '{{path.basename}}' syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=true
有关 Git 生成器和基于文件的配置的详细信息,请参阅 Argo CD 文档中的 Git Generator
矩阵生成器
组合多个生成器,以实现跨多个维度(环境 × 集群)的部署:
apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: multi-env-multi-cluster namespace: argocd spec: generators: - matrix: generators: - list: elements: - environment: dev - environment: staging - environment: prod - clusters: selector: matchLabels: region: us-west-2 template: metadata: name: 'app-{{environment}}-{{name}}' spec: project: default source: repoURL: https://github.com/example/app targetRevision: HEAD path: 'overlays/{{environment}}' destination: server: '{{server}}' namespace: 'app-{{environment}}'
有关组合生成器的详细信息,请参阅 Argo CD 文档中的 Matrix Generator
逐步部署
使用不同的同步策略,按顺序部署到环境:
apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: progressive-rollout namespace: argocd spec: generators: - list: elements: - environment: dev autoSync: "true" - environment: staging autoSync: "true" - environment: prod autoSync: "false" template: metadata: name: 'app-{{environment}}' spec: project: default source: repoURL: https://github.com/example/app targetRevision: HEAD path: 'overlays/{{environment}}' destination: server: arn:aws:eks:us-west-2:111122223333:cluster/{{environment}}-cluster namespace: app syncPolicy: automated: prune: true selfHeal: '{{autoSync}}'
开发和暂存采用自动同步,而生产则需手动批准。
多区域部署
跨多个区域部署到集群:
apiVersion: argoproj.io/v1alpha1 kind: ApplicationSet metadata: name: global-app namespace: argocd spec: generators: - list: elements: - cluster: arn:aws:eks:us-west-2:111122223333:cluster/prod-us-west region: us-west-2 - cluster: arn:aws:eks:us-east-1:111122223333:cluster/prod-us-east region: us-east-1 - cluster: arn:aws:eks:eu-west-1:111122223333:cluster/prod-eu-west region: eu-west-1 template: metadata: name: 'app-{{region}}' spec: project: default source: repoURL: https://github.com/example/app targetRevision: HEAD path: kubernetes helm: parameters: - name: region value: '{{region}}' destination: server: '{{cluster}}' namespace: app syncPolicy: automated: prune: true selfHeal: true
管理 ApplicationSets
查看 ApplicationSets 及生成的应用程序:
kubectl get applicationsets -n argocd kubectl get applications -n argocd -l argocd.argoproj.io/application-set-name=<applicationset-name>
更新 ApplicationSet:
修改 ApplicationSet 规范并重新应用。Argo CD 会自动更新所有生成的应用程序:
kubectl apply -f applicationset.yaml
删除 ApplicationSet:
kubectl delete applicationset <name> -n argocd
警告
删除 ApplicationSet 会同时删除所有生成的应用程序。如果这些应用程序设置了 prune: true,其资源也将从目标集群中删除。
其他资源
-
使用 Argo CD 项目:通过项目整理 ApplicationSets
-
创建应用程序:了解应用程序配置
-
ApplicationSet Documentation
:完整的生成器参考和模式 -
Generator Reference
:详细的生成器规范