GitOps将Git仓库作为系统状态的唯一可信源,通过声明式配置驱动Kubernetes集群的应用部署。ArgoCD是CNCF毕业的GitOps持续交付工具,以Controller模式运行在集群内,持续比对Git仓库中的期望状态与集群实际状态,自动或半自动地消除差异。本文从安装配置到自动同步策略,完整演示ArgoCD的实战部署流程。
ArgoCD架构与核心概念
ArgoCD由几个核心组件构成:API Server提供gRPC/REST接口和Web UI;Repository Server负责缓存Git仓库并生成渲染后的清单;Application Controller是核心控制器,持续比较应用的实际状态与期望状态,并执行同步操作。三个组件均以Deployment形式运行在argocd命名空间中。
ArgoCD的部署模型中,Application是最小管理单元,定义了一个Kubernetes资源集合与其对应的Git仓库路径的映射关系。Project用于权限隔离和资源约束,限制Application可以部署到哪些命名空间、可以使用哪些资源类型。Sync策略控制状态同步的行为,包括自动同步、自愈和修剪策略。
ArgoCD安装与初始配置
# 安装ArgoCD
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# 获取初始密码
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
# 端口转发访问Web UI
kubectl port-forward svc/argocd-server -n argocd 8080:443
# 安装ArgoCD CLI
curl -sSL -o argocd https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
chmod +x argocd && mv argocd /usr/local/bin/
# 登录
argocd login localhost:8080 --username admin --password $(kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d)
Git仓库结构与应用清单组织
GitOps工作流要求所有Kubernetes清单以声明式YAML存放在Git仓库中。推荐的多环境仓库结构如下:
gitops-repo/
├── apps/
│ ├── frontend/
│ │ ├── deployment.yaml
│ │ ├── service.yaml
│ │ └── ingress.yaml
│ ├── backend/
│ │ ├── deployment.yaml
│ │ └── service.yaml
│ └── database/
│ └── statefulset.yaml
├── overlays/
│ ├── dev/
│ │ └── kustomization.yaml
│ ├── staging/
│ │ └── kustomization.yaml
│ └── production/
│ └── kustomization.yaml
└── argocd/
├── frontend-app.yaml
├── backend-app.yaml
└── project.yaml
使用Kustomize管理多环境差异,base目录存放通用配置,overlays目录存放环境特定配置。ArgoCD原生支持Kustomize渲染,无需额外插件。
Application创建与同步策略配置
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: frontend
namespace: argocd
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
source:
repoURL: https://github.com/example/gitops-repo.git
targetRevision: main
path: overlays/production
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true # 自动删除Git中已移除的资源
selfHeal: true # 自动恢复手动修改(自愈)
allowEmpty: false # 禁止同步到空状态
syncOptions:
- CreateNamespace=true
- PrunePropagationPolicy=foreground
- PruneLast=true
retry:
limit: 5
backoff:
duration: 5s
factor: 2
maxDuration: 3m
revisionHistoryLimit: 10
关键参数说明:prune: true确保Git仓库中删除的资源在集群中也被清理;selfHeal: true阻止开发者通过kubectl直接修改集群资源——任何手动更改都会被自动回滚到Git定义的状态;PruneLast确保依赖资源先于被引用资源删除,避免删除顺序错误导致卡住。
多环境部署与Progressive Delivery
生产环境的持续交付通常需要渐进式发布。ArgoCD Rollouts扩展组件支持蓝绿部署和金丝雀发布,在同步过程中逐步切换流量并基于指标自动决定是否继续。
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
name: backend
spec:
replicas: 10
strategy:
canary:
steps:
- setWeight: 20
- pause: { duration: 5m }
- analysis:
templates:
- templateName: success-rate
- setWeight: 50
- pause: { duration: 5m }
- setWeight: 100
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend
image: registry.example.com/backend:v2.0
ports:
- containerPort: 8080
AnalysisTemplate定义指标检查规则,通常对接Prometheus:
apiVersion: argoproj.io/v1alpha1
kind: AnalysisTemplate
metadata:
name: success-rate
spec:
args:
- name: service-name
metrics:
- name: success-rate
interval: 1m
successCondition: result[0] >= 0.95
failureLimit: 3
provider:
prometheus:
address: http://prometheus.monitoring:9090
query: |
sum(rate(http_requests_total{service="{{args.service-name}}",status!~"5.."}[2m]))
/
sum(rate(http_requests_total{service="{{args.service-name}}"}[2m]))
SSO集成与RBAC权限管理
团队协作场景下,ArgoCD需要对接SSO实现细粒度权限控制。支持GitHub、GitLab、OIDC等多种认证方式。以GitHub为例,在argocd-cm ConfigMap中配置:
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-cm
namespace: argocd
data:
url: https://argocd.example.com
dex.config: |
connectors:
- type: github
id: github
name: GitHub
config:
clientID: your-client-id
clientSecret: your-client-secret
orgs:
- name: your-org
rbac.policy.default: readonly
---
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-rbac-cm
namespace: argocd
data:
policy.csv: |
p, role:dev-team, applications, sync, dev/*, allow
p, role:dev-team, applications, get, dev/*, allow
p, role:ops-team, applications, *, */*, allow
g, your-org:dev-team, role:dev-team
g, your-org:ops-team, role:ops-team
配置完成后,开发者通过GitHub登录ArgoCD,权限自动根据所在Team分配。dev-team成员只能同步dev命名空间的应用,ops-team成员可以管理所有环境。所有同步操作记录在ArgoCD的审计日志中,可追溯每次部署的触发者、变更内容和结果。
故障排查与运维要点
ArgoCD同步失败的常见原因包括:Git仓库认证过期、清单渲染错误、资源冲突和健康检查超时。使用CLI查看同步状态和事件:
# 查看应用状态
argocd app get frontend
# 查看同步差异
argocd app diff frontend --local=./overlays/production
# 手动触发同步
argocd app sync frontend --prune
# 查看操作日志
argocd app logs frontend
# 强制刷新Git仓库缓存
argocd app get frontend --refresh
资源健康检查通过argocd-cm中的resource.customizations配置自定义。对于StatefulSet等有序资源,默认的健康检查可能不够精确,需要自定义Lua脚本判断所有Pod就绪后才认为健康。定期检查ArgoCD自身组件的资源占用和日志,确保Repository Server的Git仓库缓存不耗尽磁盘空间。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/argocdgitops-chi-xu-jiao-fu-shi-zhan-sheng-ming-shi-bu-shu/