GitOps核心理念与ArgoCD架构
GitOps将Git仓库作为基础设施和应用配置的唯一可信来源,所有部署状态通过Git提交记录来追踪。ArgoCD是CNCF毕业的GitOps持续交付工具,以Kubernetes原生方式运行,通过监听Git仓库变化自动同步部署到集群。
ArgoCD的架构由三个核心组件构成:API Server提供gRPC/REST接口和Web UI,Repository Server负责缓存Git仓库并生成渲染后的清单,Application Controller对比Git期望状态与集群实际状态的差异并驱动同步操作。
与传统的CI/CD流水线相比,ArgoCD采用Pull模式而非Push模式。CI流水线负责构建镜像并推送到Registry,ArgoCD独立运行在集群内部,持续监听Git仓库变化,检测到更新后自动将新版本部署到Kubernetes集群。这种模式不需要在CI系统中配置集群凭据,降低了安全风险。
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
# 访问 https://localhost:8080
# 安装ArgoCD CLI
curl -sSL -o argocd \
https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
chmod +x argocd && sudo mv argocd /usr/local/bin/
# 登录
argocd login localhost:8080 --username admin --password
Application资源定义与部署
ArgoCD通过Application CRD定义一个部署单元。以下是一个完整的Application定义,将Git仓库中的Kubernetes清单同步到目标集群。
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: web-app
namespace: argocd
spec:
source:
repoURL: https://github.com/example/k8s-manifests
targetRevision: main
path: overlays/production
# 支持Kustomize
kustomize:
images:
- example/web-app:1.4.2
# 或使用Helm
# helm:
# valueFiles:
# - values-production.yaml
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true # 自动清理已删除的资源
selfHeal: true # 自动纠正手动修改
syncOptions:
- CreateNamespace=true
- PruneLast=true
# 滚动更新策略
managedNamespaceMetadata:
labels:
app: web-app
env: production
automated配置启用了自动同步:prune确保Git中删除的资源在集群中也被清理,selfHeal检测到有人手动修改了集群中的资源时会自动恢复为Git中的状态。这两个配置是GitOps声明式管理的核心。
Kustomize多环境管理
Kustomize是Kubernetes原生的配置管理工具,通过base和overlay的目录结构管理多环境差异,不需要模板引擎。
# 目录结构
k8s-manifests/
base/
deployment.yaml
service.yaml
configmap.yaml
kustomization.yaml
overlays/
dev/
kustomization.yaml
patches.yaml
staging/
kustomization.yaml
patches.yaml
production/
kustomization.yaml
patches.yaml
# base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
- configmap.yaml
commonLabels:
app: web-app
# overlays/production/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: production
resources:
- ../../base
patches:
- patches.yaml
# 覆盖镜像版本
images:
- name: example/web-app
newName: example/web-app
newTag: 1.4.2
# 副本数覆盖
replicas:
- name: web-app
count: 5
dev环境使用1个副本和最小资源限制,production环境使用5个副本并配置HPA自动伸缩。所有环境共享base目录中的通用配置,仅通过overlay覆盖差异部分。
ApplicationSet多集群部署
当需要在多个集群或多个环境部署同一应用时,ApplicationSet自动生成Application资源,避免手动创建大量重复配置。
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: web-app-multi-cluster
namespace: argocd
spec:
generators:
- list:
elements:
- cluster: dev
url: https://1.2.3.4
- cluster: staging
url: https://5.6.7.8
- cluster: production
url: https://9.10.11.12
template:
metadata:
name: 'web-app-{{cluster}}'
spec:
source:
repoURL: https://github.com/example/k8s-manifests
targetRevision: main
path: 'overlays/{{cluster}}'
destination:
server: '{{url}}'
namespace: web-app
syncPolicy:
automated:
prune: true
selfHeal: true
ApplicationSet的generator列表中的每个元素生成一个Application。新增环境只需在list中添加一条记录,ArgoCD会自动创建对应的Application并开始同步。
同步状态监控与告警
ArgoCD提供丰富的同步状态指标,可以集成Prometheus进行监控告警。
# ArgoCD Prometheus ServiceMonitor
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: argocd-metrics
namespace: argocd
spec:
selector:
matchLabels:
app.kubernetes.io/name: argocd-metrics
endpoints:
- port: metrics
interval: 30s
# 告警规则示例
groups:
- name: argocd
rules:
- alert: ArgoCDSyncFailed
expr: argocd_app_sync_status{sync_status!="Synced"} == 1
for: 5m
labels:
severity: critical
annotations:
summary: "应用 {{ $labels.application }} 同步失败"
- alert: ArgoCDAppOutOfSync
expr: argocd_app_sync_status{sync_status="OutOfSync"} == 1
for: 10m
labels:
severity: warning
annotations:
summary: "应用 {{ $labels.application }} 状态不同步"
ArgoCD还支持Webhook通知,当同步成功或失败时发送消息到Slack、钉钉或企业微信。在ArgoCD的configmap中配置通知模板和触发条件即可启用。
GitOps实践的关键在于建立规范的Git提交流程:所有配置变更通过PR提交,经过Code Review后合并到main分支,ArgoCD自动检测变更并同步到集群。这种流程保证了每次部署都有审计记录,回滚操作只需revert Git提交即可。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/gitops-chi-xu-jiao-fu-shi-zhan-argocd-bu-shu-yu-kubernetes/