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
部署应用程序的能力(无需特权集群管理员参与启用目标集群/命名空间)
安装
从 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
则需要安装服务
官方文档:https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Getting-Started/#installation
$ 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/gmkk/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/gmkk/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
来生成参数来识别和定位可用集群。
配置文件:https://gitee.com/gmkk/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
template:
metadata:
name: '{{name}}-app' ## name == clusterName (cluster1, cluster2)
spec:
project: default
source:
path: guestbook
repoURL: https://gitee.com/gmkk/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