ArgoCD是CNCF毕业的GitOps持续交付工具,通过将Git仓库作为应用配置的唯一真实来源,实现Kubernetes集群中应用的自动化部署与同步。本文围绕ArgoCD的核心架构、Application配置、多集群管理、同步策略与回滚机制展开实战配置。
ArgoCD架构与GitOps工作流原理
ArgoCD采用声明式GitOps模型:所有Kubernetes清单(Deployment、Service、ConfigMap等)存储在Git仓库中,ArgoCD持续监控Git仓库与集群实际状态的差异,自动或手动触发同步操作使集群状态收敛到Git定义的期望状态。
核心组件包括:API Server(提供gRPC/REST接口)、Repository Server(缓存Git仓库内容)、Application Controller(协调同步循环)、Redis(缓存加速)、Dex(SSO/OIDC认证)。每个组件以Deployment形式运行在argocd命名空间。
Application资源定义与同步策略配置
ArgoCD通过Application自定义资源定义一个部署单元。Application指定源Git仓库、目标集群和命名空间、同步策略等参数。
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: api-service-prod
namespace: argocd
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
source:
repoURL: https://github.com/myorg/k8s-manifests
targetRevision: main
path: overlays/production/api-service
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true
selfHeal: true
allowEmpty: false
syncOptions:
- CreateNamespace=true
- PruneLast=true
- ApplyOutOfSyncOnly=true
retry:
limit: 5
backoff:
duration: 5s
factor: 2
maxDuration: 3m0s
revisionHistoryLimit: 10
syncPolicy.automated配置自动同步行为:prune=true表示删除Git中已移除的资源,selfHeal=true在检测到手动修改时自动回滚到Git状态。生产环境建议关闭automated,改用手动同步审批流程。syncOptions中的ApplyOutOfSyncOnly=true只同步发生变化的资源,提升大规模部署的同步速度。
Kustomize与Helm集成的多环境配置管理
ArgoCD原生支持Kustomize和Helm两种模板引擎。通过目录结构区分环境,实现单一Git仓库管理多环境部署。
# 项目结构
k8s-manifests/
├── base/
│ ├── deployment.yaml
│ ├── service.yaml
│ └── kustomization.yaml
├── overlays/
│ ├── staging/
│ │ ├── kustomization.yaml
│ │ └── replica-patch.yaml
│ └── production/
│ ├── kustomization.yaml
│ └── replica-patch.yaml
使用Helm时,在Application的source段指定helm字段:
spec:
source:
repoURL: https://github.com/myorg/helm-charts
path: charts/api-service
targetRevision: main
helm:
valueFiles:
- values-production.yaml
parameters:
- name: image.tag
value: "v2.1.0"
- name: replicaCount
value: "6"
releaseName: api-service
namespace: production
AppProject多团队权限隔离与资源配额管理
AppProject是ArgoCD的权限隔离单元,用于限制团队可访问的Git仓库、目标集群和命名空间。生产环境中不同团队应分配独立的AppProject。
apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
name: team-payments
namespace: argocd
spec:
description: Payment team project
sourceRepos:
- https://github.com/myorg/payments-*
destinations:
- server: https://kubernetes.default.svc
namespace: payments-*
clusterResourceWhitelist:
- group: ''
kind: Namespace
namespaceResourceWhitelist:
- group: apps
kind: Deployment
- group: ''
kind: Service
- group: ''
kind: ConfigMap
namespaceResourceBlacklist:
- group: ''
kind: ResourceQuota
roles:
- name: developer
policies:
- p, proj:team-payments:developer, applications, get, team-payments/*, allow
- p, proj:team-payments:developer, applications, sync, team-payments/*, allow
groups:
- myorg:payments-devs
sourceRepos使用通配符限制可关联的Git仓库。destinations限制应用可部署的目标集群和命名空间。通过namespaceResourceWhitelist精确控制团队可创建的Kubernetes资源类型,防止误操作创建集群级资源。
多集群注册与跨集群应用分发配置
ArgoCD支持管理多个Kubernetes集群。通过argocd cluster add命令将外部集群注册到ArgoCD控制平面。注册后,Application的destination.server字段可指定目标集群的API Server地址。
# 注册远程集群
argocd cluster add production-cluster \
--label environment=production \
--label region=us-west-2
# 查看已注册集群
argocd cluster list
# Application指向远程集群
spec:
destination:
server: https://10.0.1.100:6443
namespace: payments
跨集群同步需确保ArgoCD Service Account在目标集群上有足够权限。推荐使用ArgoCD的ApplicationSet控制器实现基于集群标签的自动应用分发,新集群注册后自动创建对应Application。
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: api-service-multi-cluster
namespace: argocd
spec:
generators:
- clusters:
selector:
matchLabels:
environment: production
template:
metadata:
name: '{{name}}-api-service'
spec:
source:
repoURL: https://github.com/myorg/k8s-manifests
path: overlays/production
targetRevision: main
destination:
server: '{{server}}'
namespace: api-service
同步状态监控与回滚操作实战
ArgoCD提供丰富的同步状态检测能力,包括资源是否已同步、是否健康、是否有差异。通过argocd app sync命令手动触发同步,argocd app wait等待同步完成。
# 手动同步并等待完成
argocd app sync api-service-prod --wait --timeout 300
# 查看同步状态与差异
argocd app diff api-service-prod
# 回滚到上一个版本
argocd app rollback api-service-prod
# 回滚到指定历史版本
argocd app rollback api-service-prod 3
# 查看同步历史
argocd app history api-service-prod
回滚操作实际上是将Application的targetRevision指向之前提交的Git commit。revisionHistoryLimit控制保留的历史版本数量,默认10。生产环境建议配合ArgoCD Notifications插件,在同步失败或状态不健康时自动发送告警到Slack、钉钉等渠道。
资源健康检查可通过自定义Lua脚本扩展。对于非标准资源(如ExternalSecret、CertManager Certificate),编写自定义健康检查脚本使ArgoCD能正确判断资源是否真正就绪,避免同步状态显示Healthy但实际运行异常的问题。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/argocdgitops-chi-xu-bu-shu-yu-duo-ji-qun-ying-yong-tong-bu/