高级进阶
将gitee 项目导入到 本地Gitlab仓库中-公开仓库
https://gitee.com/mageedu/spring-boot-helloworld-deployment.git
本地仓库路劲:
http://gitlab.gitlab.svc.cluster.local/root/spring-boot-helloworld-deployment.git
#资源定义
[root@xianchaomaster1 07-argocd-basics]# cat 01-application-helloworld.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: spring-boot-helloworld
namespace: argocd
spec:
project: default
source:
repoURL: http://gitlab.gitlab.svc.cluster.local/root/spring-boot-helloworld-deployment.git
targetRevision: HEAD
path: deploy/kubernetes
destination:
server: https://kubernetes.default.svc
namespace: helloworld
syncPolicy:
automated:
prune: true
selfHeal: true
allowEmpty: false
syncOptions:
- Validate=false
- CreateNamespace=true
- PrunePropagationPolicy=foreground
- PruneLast=true
retry:
limit: 5
backoff:
duration: 5s
factor: 2
maxDuration: 3m
#应用创建
[root@xianchaomaster1 07-argocd-basics]# kubectl apply -f 01-application-helloworld.yaml
application.argoproj.io/spring-boot-helloworld created
[root@xianchaomaster1 07-argocd-basics]# argocd app list
NAME CLUSTER NAMESPACE PROJECT STATUS HEALTH SYNCPOLICY CONDITIONS REPO PATH TARGET
argocd/guestbookcli https://kubernetes.default.svc guestbook default Synced Healthy Auto <none> http://gitlab.gitlab.svc.cluster.local/root/argocd-example-apps.git guestbook
argocd/spring-boot-helloworld https://kubernetes.default.svc helloworld default Synced Healthy Auto-Prune <none> http://gitlab.gitlab.svc.cluster.local/root/spring-boot-helloworld-deployment.git deploy/kubernetes HEAD
![]()
![]()
Project CRD Application CRD
#Project CRD
Projects负责为Application提供逻辑分组,它主要实现如下功能
◼ 限制可以部署的内容(指定受信任的Git Source仓库)
◼ 限制Application可以部署到的目标位置(指定目标Cluster和Namespace)
◼ 限制能够及不能够部署的对象类型,例如RBAC、CRD、DeamonSets、NetworkPolicy等
◼ 定义Project Role,从而为Application提供RBAC机制,以绑定到OIDC组或JWT token
ArgoCD的默认项目
◼ default project由ArgoCD自动部署,它允许用户按需修改,但不能被删除
#关于ApplicaionSet CRD
ApplicationSet CRD
◼ ApplicationSet CRD用于定义可自动生成Application的模板,从而能够在monorepo(单个Repo中一义了多个
ArgoCD Application)或多个Repo,以及跨大量Cluster的场景中自动化管理ArgoCD Application
◼ ApplicationSet CRD需要同其专用的ApplicationSet控制器支撑实现
◆ApplicationSet控制器是ArgoCD的独立子项目,会随同ArgoCD一起部署
ApplicationSet可提供如下功能
◼ 目标Cluster的模板化,从而能够在单个资源配置文件中适配部署到多个Kubernetes集群
◼ 源Git配置仓库的模板化
◼ 较好地支持monorepo
ApplicationSet控制器的工作模式-示例
![]()
![]()
ApplicationSet 资源示例
#ApplicationSet控制器的工作模式
ApplicationSet控制器与Application控制器的交互方式
◼ ApplicationSet负责管理Application,因此,ApplicationSet控制器会生成或更新Application资源
◼ 生成或更新的Application资源则由Application控制器确保期望状态与实际状态的一致
◼ 因此,ApplicationSet控制器仅负责确保ApplicationSet资源的期望状态与实际状态的一致
#示例
helloworld ApplicationSet示例
◼ 使用了列表生成器(list generator)
◆有三个元素,分别为environment参数传递不同的值
◆该参数既作为配置文件获取位置,也是目标集群上的名称空间
◼ Application模板
◆给出了模板化的source
◆定义了模板化的destination
◆定义了共用的syncPolicy
◼ ApplicationSet的syncPolicy
◆preserveResourcesOnDeletion:是否在删除当前ApplicationSet资源时,一并删除由其创建的Application,即是否执行级联删除操作
目前,有七种不同的generator可用,常用的有如下4个
◼ List Generator
◼ Cluster Generator
◼ Git Generator
◼ Matrix Generator
[root@xianchaomaster1 07-argocd-basics]# cat 02-applicationset-demo.yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: helloworld
namespace: argocd
spec:
generators:
- list:
elements:
- environment: dev
- environment: staging
- environment: prod
template:
metadata:
name: '{{environment}}-sprint-boot-helloworld-xks'
spec:
project: default
source:
repoURL: http://gitlab.gitlab.svc.cluster.local/root/spring-boot-helloworld-deployment.git
targetRevision: HEAD
path: helloworld/{{environment}}
destination:
server: https://kubernetes.default.svc
namespace: '{{environment}}'
syncPolicy:
automated:
prune: true
selfHeal: true
allowEmpty: false
syncOptions:
- Validate=false
- CreateNamespace=true
- PrunePropagationPolicy=foreground
- PruneLast=true
retry:
limit: 5
backoff:
duration: 5s
factor: 2
maxDuration: 3m
syncPolicy:
preserveResourcesOnDeletion: false
[root@xianchaomaster1 07-argocd-basics]# kubectl apply -f 02-applicationset-demo.yaml
applicationset.argoproj.io/helloworld created
[root@xianchaomaster1 07-argocd-basics]# argocd app list
NAME CLUSTER NAMESPACE PROJECT STATUS HEALTH SYNCPOLICY CONDITIONS REPO PATH TARGET
argocd/dev-sprint-boot-helloworld-xks https://kubernetes.default.svc dev default Synced Healthy Auto-Prune <none> http://gitlab.gitlab.svc.cluster.local/root/spring-boot-helloworld-deployment.git helloworld/dev HEAD
argocd/guestbookcli https://kubernetes.default.svc guestbook default Synced Healthy Auto <none> http://gitlab.gitlab.svc.cluster.local/root/argocd-example-apps.git guestbook
argocd/prod-sprint-boot-helloworld-xks https://kubernetes.default.svc prod default Synced Healthy Auto-Prune <none> http://gitlab.gitlab.svc.cluster.local/root/spring-boot-helloworld-deployment.git helloworld/prod HEAD
argocd/spring-boot-helloworld https://kubernetes.default.svc helloworld default Synced Healthy Auto-Prune <none> http://gitlab.gitlab.svc.cluster.local/root/spring-boot-helloworld-deployment.git deploy/kubernetes HEAD
argocd/staging-sprint-boot-helloworld-xks https://kubernetes.default.svc staging default Synced Healthy Auto-Prune <none> http://gitlab.gitlab.svc.cluster.local/root/spring-boot-helloworld-deployment.git helloworld/staging HEAD
![]()