Argo CD ApplicationSet
Argo CD ApplicationSet
ApplicationSet
介绍
ApplicationSet
控制器是一个 Kubernetes
控制器,添加了对 ApplicationSet
CustomResourceDefinition (CRD)
的支持。该控制器实现了跨大量集群和 monorepos 内管理 Argo CD
应用程序的自动化和更大的灵活性,此外,它还使多租户 Kubernetes
集群上的自助服务使用成为可能。
ApplicationSet
控制器与现有的 Argo CD
安装一起工作。Argo CD
是一种声明式 GitOps
持续交付工具,允许开发人员从现有的 Git
工作流程中定义和控制 Kubernetes
应用程序资源的部署。
ApplicationSet
控制器通过添加支持以集群管理员为中心的场景的附加功能来补充 Argo CD
。控制器 ApplicationSet
提供:
- 能够使用单个
Kubernetes
清单通过Argo CD
定位多个Kubernetes
集群 - 能够使用单个
Kubernetes
清单通过Argo CD
从一个或多个Git
存储库部署多个应用程序 - 改进了对
monorepos
的支持:在Argo CD
的上下文中,monorepo
是在单个Git
存储库中定义的多个Argo CD
应用程序资源 - 在多租户集群内,提高单个集群租户使用
Argo CD
部署应用程序的能力(无需特权集群管理员参与启用目标集群/命名空间)
ApplicationSet
安装
从 Argo CD
v2.3
开始,ApplicationSet
控制器与 Argo CD
捆绑在一起,查看 ApplicationSet
[root@node argocd]# kubectl -n argocd get pods | grep applicationset
argocd-applicationset-controller-57bbb6bcdf-zzh6p 1/1 Running 0 7d23h
[root@node argocd]# kubectl api-resources | grep ApplicationSet
applicationsets appset,appsets argoproj.io/v1alpha1 true ApplicationSet
如果 Argo CD
版本低于 v2.3
则需要安装服务
$ kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/applicationset/v0.4.0/manifests/install.yaml
ApplicationSet
使用
Generators
生成器:https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Generators/
List Generator
官方文档:https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Generators-List/
列表生成器根据任意 key/value
键值对生成参数(只要值是字符串)
配置文件:https://gitee.com/evescn/argocd-example-apps/blob/main/applicationsets/generators/demo-list.yaml
## demo-list.yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: demo1-application-set
namespace: argocd
spec:
generators:
- list:
elements:
- namespace: dev
- namespace: test
- namespace: stg
- namespace: prod
template:
metadata:
name: '{{namespace}}-app'
spec:
project: default
source:
path: guestbook
repoURL: https://gitee.com/evescn/argocd-example-apps.git
targetRevision: main
destination:
server: https://kubernetes.default.svc
namespace: '{{namespace}}' # 动态值
syncPolicy:
syncOptions:
- CreateNamespace=true
# automated:
# prune: true
在此示例中,列表生成器将 namespace
字段作为参数传递到模板中。
创建服务
[root@node argocd]# kubectl apply -f demo-list.yaml
applicationset.argoproj.io/demo1-application-set created
查看 Web UI
界面,自动创建了 4
个服务,
点击 Sync
同步按钮
查看 K8S
集群信息
[root@node argocd]# kubectl get ns
NAME STATUS AGE
dev Active 6d5h
prod Active 15s
stg Active 12s
test Active 10s
[root@node argocd]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
guestbook-ui-56f57db4f-txbjv 1/1 Running 0 20s
[root@node argocd]# kubectl get pods -n test
NAME READY STATUS RESTARTS AGE
guestbook-ui-56f57db4f-gzxds 1/1 Running 0 22s
[root@node argocd]# kubectl get pods -n stg
NAME READY STATUS RESTARTS AGE
guestbook-ui-56f57db4f-55tc8 1/1 Running 0 25s
[root@node argocd]# kubectl get pods -n prod
NAME READY STATUS RESTARTS AGE
guestbook-ui-56f57db4f-8jrzn 1/1 Running 0 28s
Cluster Generator
官方文档:https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Generators-Cluster/
在 Argo CD
中,托管集群存储在 Argo CD
命名空间中的 Secrets
中。ApplicationSet
控制器使用这些相同的 Secret
来生成参数来识别和定位可用集群。
对于使用 Argo CD 注册的每个集群,集群生成器会根据集群 secret
信息找到的项目列表生成参数。
它会自动向每个集群的应用程序模板提供以下参数值:
- name
- nameNormalized (“名称”,但标准化为仅包含小写字母数字字符、“-”或“.”)
- server
- metadata.labels.
(对于 Secret 中的每个标签) - metadata.annotations.
(对于 Secret 中的每个注释)
配置文件:https://gitee.com/evescn/argocd-example-apps/blob/main/applicationsets/generators/demo-cluster.yaml
## demo-cluster.yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: multi-cluster-application-set
namespace: argocd
spec:
generators:
- clusters: {} #all clusters
# selector:
# matchLabels:
# test: "true"
# values:
# name: "test"
template:
metadata:
name: '{{name}}-app' ## name == clusterName (cluster1, cluster2)
spec:
project: default
source:
path: guestbook
repoURL: https://gitee.com/evescn/argocd-example-apps.git
targetRevision: main
destination:
server: '{{server}}' # cluster URL
namespace: '{{name}}-demoapp'
# Sync policy
syncPolicy:
syncOptions:
- CreateNamespace=true
# automated: # automated sync by default retries failed attempts 5 times with following delays between attempts ( 5s, 10s, 20s, 40s, 80s ); retry controlled using `retry` field.
# prune: true # Specifies if resources should be pruned during auto-syncing ( false by default ).
# selfHeal: true # Specifies if partial app sync should be executed when resources are changed only in target Kubernetes cluster and no git change detected ( false by default ).
测试 Cluster Generator
需要多集群支持,需要向 Argo CD
中添加集群
[root@node argocd]# kubectl config get-contexts -o name
context-cluster1
[root@node ~]# argocd cluster add context-cluster1
WARNING: This will create a service account `argocd-manager` on the cluster referenced by context `context-dev` with full cluster level privileges. Do you want to continue [y/N]? y
INFO[0001] ServiceAccount "argocd-manager" already exists in namespace "kube-system"
INFO[0001] ClusterRole "argocd-manager-role" updated
INFO[0001] ClusterRoleBinding "argocd-manager-role-binding" updated
Cluster 'https://192.168.0.101:6443' added
创建服务
[root@node argocd]# kubectl apply -f demo-cluster.yaml
applicationset.argoproj.io/multi-cluster-application-set configured
查看 Web UI
界面,自动创建了 2
个服务,当前存在 2
个集群(虽然这 2
个集群是同一个集群)
点击 Sync
同步按钮
查看 K8S
集群信息
[root@node argocd]# kubectl get ns
NAME STATUS AGE
context-cluster1-demoapp Active 49s
in-cluster-demoapp Active 46s
[root@node argocd]# kubectl get pods -n context-cluster1-demoapp
NAME READY STATUS RESTARTS AGE
guestbook-ui-56f57db4f-dsdgb 1/1 Running 0 64s
[root@node argocd]# kubectl get pods -n in-cluster-demoapp
NAME READY STATUS RESTARTS AGE
guestbook-ui-56f57db4f-9jpw2 1/1 Running 0 69s
Git Generator
官方文档:https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Generators-Git/
Git 生成器包含两个子类型:Git 目录生成器和 Git 文件生成器
配置文件:https://gitee.com/evescn/argocd-example-apps/blob/main/applicationsets/generators/demo-git.yaml
## demo-git.yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: many-apps-application-set
namespace: argocd
spec:
generators:
- git:
repoURL: https://gitee.com/evescn/argocd-example-apps.git
revision: HEAD
directories:
- path: applicationsets/demoapps/*
template:
metadata:
name: '{{path.basename}}' ## 目录名称(demo1,demo2,demo3)
spec:
project: default
source:
repoURL: https://gitee.com/evescn/argocd-example-apps.git
targetRevision: main
path: '{{path}}' ## 完整的路径 applicationsets/demoapps/demo1
destination:
server: https://kubernetes.default.svc
namespace: '{{path.basename}}' ## 目录名称
# Sync policy
syncPolicy:
syncOptions:
- CreateNamespace=true
# automated:
# prune: true
在此示例中,GIT
生成器将 path
字段作为参数传递到模板中。
创建服务
[root@node argocd]# kubectl apply -f demo-git.yaml
applicationset.argoproj.io/many-apps-application-set created
查看 Web UI
界面,自动创建了 3
个服务,因为 applicationsets/demoapps/*
目录下定义了 3
个 APP
点击 Sync
同步按钮
查看 K8S
集群信息
[root@node argocd]# kubectl get ns
NAME STATUS AGE
demo1 Active 3m14s
demo2 Active 3m10s
demo3 Active 3m8s
[root@node argocd]# kubectl get pods -n demo1
NAME READY STATUS RESTARTS AGE
demo1-guestbook-ui-6cd8c8f8f7-c5d6g 1/1 Running 0 3m20s
demo1-guestbook-ui-6cd8c8f8f7-z6fw7 1/1 Running 0 3m20s
[root@node argocd]# kubectl get pods -n demo2
NAME READY STATUS RESTARTS AGE
demo2-guestbook-ui-85799f7689-hr2wq 1/1 Running 0 3m17s
demo2-guestbook-ui-85799f7689-qk2xg 1/1 Running 0 3m17s
[root@node argocd]# kubectl get pods -n demo3
NAME READY STATUS RESTARTS AGE
demo3-guestbook-ui-597fdf4f49-ktfh2 1/1 Running 0 3m16s
demo3-guestbook-ui-597fdf4f49-tgxm9 1/1 Running 0 3m16s
Matrix Generator
官方文档:https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Generators-Matrix/
Matrix 生成器组合了两个子生成器生成的参数,迭代每个生成器生成的参数的每个组合。
通过组合两个生成器参数来生成每种可能的组合,这使您能够获得两个生成器的内在属性。例如,许多可能用例的一小部分包括:
- SCM Provider Generator + Cluster Generator:扫描 GitHub 组织的存储库以获取应用程序资源,并将这些资源定位到所有可用集群。
- Git File Generator + List Generator:提供要通过配置文件部署的应用程序列表,以及可选的配置选项,并将它们部署到固定的集群列表。
- Git Directory Generator + Cluster Decision Resource Generator:找到 Git 存储库的文件夹中包含的应用程序资源,并将它们部署到通过外部自定义资源提供的集群列表。
配置文件:https://gitee.com/evescn/argocd-example-apps/blob/main/applicationsets/generators/demo-matrix.yaml
## demo-matrix.yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: cluster-git
namespace: argocd
spec:
generators:
# matrix 'parent' generator
- matrix:
generators:
# git generator, 'child' #1
- git:
repoURL: https://gitee.com/evescn/argocd-example-apps.git
revision: main
directories:
- path: applicationsets/demoapps/*
# cluster generator, 'child' #2
- clusters: {}
template:
metadata:
name: '{{path.basename}}-{{name}}'
spec:
project: default
source:
repoURL: https://gitee.com/evescn/argocd-example-apps.git
targetRevision: main
path: '{{path}}'
destination:
server: '{{server}}'
namespace: '{{name}}-{{path.basename}}'
syncPolicy:
syncOptions:
- CreateNamespace=true
# automated:
# prune: true
在此示例中,使用了 Matrix Generator
组合了 git generator
+ cluster generator
,完成了在不同的集群中,基于 git 仓库中定义的 APP
配置,完成服务创建,确保每个集群中都的 APP
信息相同。
创建服务
[root@node argocd]# kubectl apply -f demo-matrix.yaml
applicationset.argoproj.io/cluster-git created
查看 Web UI
界面,自动创建了多个服务
点击 Sync
同步按钮
查看 K8S
集群信息
[root@node argocd]# kubectl get ns
NAME STATUS AGE
context-cluster1-demo1 Active 53s
context-cluster1-demo2 Active 48s
context-cluster1-demo3 Active 41s
in-cluster-demo1 Active 96s
in-cluster-demo2 Active 44s
in-cluster-demo3 Active 39s
[root@node argocd]# kubectl get pods -n context-cluster1-demo1
NAME READY STATUS RESTARTS AGE
demo1-guestbook-ui-6cd8c8f8f7-bh7cq 1/1 Running 0 77s
demo1-guestbook-ui-6cd8c8f8f7-x2c5l 1/1 Running 0 77s
[root@node argocd]# kubectl get pods -n in-cluster-demo1
NAME READY STATUS RESTARTS AGE
demo1-guestbook-ui-6cd8c8f8f7-j79ng 1/1 Running 0 82s
demo1-guestbook-ui-6cd8c8f8f7-zl97z 1/1 Running 0 82s
Other Generator
其他更多生成器使用,可以查看官方文档:https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Generators/