ArgoCD GitOps持续交付流水线与Kubernetes声明式部署实战

ArgoCD 是 CNCF 毕业级别的 GitOps 持续交付工具,通过将 Git 仓库作为应用部署的唯一可信源(Single Source of Truth),实现 Kubernetes 集群状态的自动同步与漂移检测。相较于传统 CI/CD 推送模式,ArgoCD 采用 Pull 模型从集群内部拉取配置变更,解决了多集群部署的权限管理和网络可达性问题。

ArgoCD架构设计与组件职责划分

ArgoCD 由以下核心组件构成:

API Server:暴露 gRPC/REST API,处理 CLI 和 Web UI 请求,管理 RBAC 鉴权
Repository Server:负责 Git 仓库的克隆和缓存,生成 Kubernetes 清单文件的渲染结果
Application Controller:核心控制器,持续对比 Git 仓库中的期望状态与集群中的实际状态,触发同步操作
Dex Server:集成 SSO/OIDC 认证,对接企业身份管理系统
Redis:缓存 Git 仓库信息和同步状态,降低 Repository Server 负载

Application Controller 以 stateful loop 方式运行,默认每 3 分钟从 Git 仓库拉取一次变更。检测到 Git 状态与集群状态不一致时,根据同步策略决定是否自动触发同步。

ArgoCD安装配置与项目初始化

通过 Helm 安装 ArgoCD:

kubectl create namespace argocd
helm repo add argo https://argoproj.github.io/argo-helm
helm install argocd argo/argo-cd \
  --namespace argocd \
  --set server.ingress.enabled=true \
  --set server.ingress.hostname=argocd.example.com

配置 Git 仓库凭证:

apiVersion: v1
kind: Secret
metadata:
  name: repo-credentials
  namespace: argocd
  labels:
    argocd.argoproj.io/secret-type: repository
type: Opaque
stringData:
  type: git
  url: https://github.com/org/app-manifests.git
  username: deploy-token
  password: ghp_xxxxxxxxxxxxxxxxxxxx

创建 AppProject 实现多团队隔离:

apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
  name: production
  namespace: argocd
spec:
  description: Production deployment project
  sourceRepos:
  - https://github.com/org/app-manifests.git
  destinations:
  - server: https://kubernetes.default.svc
    namespace: prod-*
  clusterResourceWhitelist:
  - group: ''
    kind: Namespace
  namespaceResourceWhitelist:
  - group: 'apps'
    kind: Deployment
  - group: ''
    kind: Service
  - group: 'networking.k8s.io'
    kind: Ingress

Application清单编写与自动同步策略配置

定义 ArgoCD Application 资源:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: web-frontend
  namespace: argocd
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  project: production
  source:
    repoURL: https://github.com/org/app-manifests.git
    targetRevision: HEAD
    path: overlays/production
  destination:
    server: https://kubernetes.default.svc
    namespace: prod-web
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
      allowEmpty: false
    syncOptions:
    - CreateNamespace=true
    - PrunePropagationPolicy=foreground
    - ApplyOutOfSyncOnly=true
  revisionHistoryLimit: 10

关键配置项说明:

prune: true:当 Git 中删除资源时,集群中对应资源也会被清理
selfHeal: true:检测到有人手动修改集群资源(kubectl edit)时,自动回滚到 Git 状态
PrunePropagationPolicy=foreground:删除操作在前台执行,确保依赖资源按序清理
ApplyOutOfSyncOnly=true:仅对发生变更的资源执行 apply,大规模集群下显著提升同步速度

ApplicationSet多集群批量部署与模板化

ApplicationSet 支持通过模板生成多个 Application,适用于多环境、多集群场景:

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: multi-cluster-deploy
  namespace: argocd
spec:
  generators:
  - list:
    elements:
    - cluster: cluster-bj
      url: https://1.2.3.4:6443
      env: production
    - cluster: cluster-sh
      url: https://5.6.7.8:6443
      env: production
    - cluster: cluster-gz
      url: https://9.10.11.12:6443
      env: staging
  template:
    metadata:
      name: '{{cluster}}-web-app'
    spec:
      project: '{{env}}'
      source:
        repoURL: https://github.com/org/app-manifests.git
        targetRevision: HEAD
        path: 'overlays/{{env}}'
      destination:
        server: '{{url}}'
        namespace: web-app
      syncPolicy:
        automated:
          prune: true
          selfHeal: true

ArgoCD Notifications与同步状态告警

配置 Slack/钉钉/飞书通知,实现同步失败自动告警:

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
  namespace: argocd
data:
  service.slack: |
    token: $slack-token
  trigger.sync-failed: |
    - when: app.status.sync.status == 'Unknown' || app.status.sync.status == 'Error'
      send: [sync-failed]
  template.sync-failed: |
    message: |
      同步失败: {{.app.metadata.name}}
      状态: {{.app.status.sync.status}}
      错误: {{.app.status.operationState.message}}
      提交: {{.app.status.sync.revision}}

通过 ArgoCD 的 Sync Wave 机制控制资源部署顺序,wave 值越小越先部署。例如设置 CRD wave=-1,确保自定义资源定义先于 CR 实例创建。结合 ArgoCD Rollouts 还能实现金丝雀发布和蓝绿部署,将 GitOps 理念延伸到渐进式交付领域。

原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/argocdgitops-chi-xu-jiao-fu-liu-shui-xian-yu-kubernetes/

(0)
小编小编
上一篇 13小时前
下一篇 13小时前

相关推荐