GitOps工作流实施Skill gitops-workflow

本技能提供使用ArgoCD和Flux工具实现GitOps工作流的完整指南,适用于自动化Kubernetes部署和声明式基础设施管理。关键词包括GitOps、ArgoCD、Flux、Kubernetes、自动化部署、DevOps、持续协调、渐进式交付。

DevOps 0 次安装 0 次浏览 更新于 3/10/2026

name: gitops-workflow description: 使用 ArgoCD 和 Flux 实现 GitOps 工作流,用于自动化、声明式的 Kubernetes 部署和持续协调。适用于实施 GitOps 实践、自动化 Kubernetes 部署或设置声明式基础设施管理。 version: 1.0.0 model: sonnet invoked_by: [devops] tools: [Read, Write, Edit, Bash, Glob, Grep] verified: false lastVerifiedAt: 2026-02-19T05:29:09.098Z

GitOps 工作流程

使用 ArgoCD 和 Flux 实现 GitOps 工作流的完整指南,用于自动化 Kubernetes 部署。

目的

使用 ArgoCD 或 Flux CD 实现声明式、基于 Git 的 Kubernetes 持续交付,遵循 OpenGitOps 原则。

何时使用此技能

  • 为 Kubernetes 集群设置 GitOps
  • 从 Git 自动化应用程序部署
  • 实施渐进式交付策略
  • 管理多集群部署
  • 配置自动同步策略
  • 在 GitOps 中设置秘密管理

OpenGitOps 原则

  1. 声明式 - 整个系统以声明方式描述
  2. 版本化和不可变 - 期望状态存储在 Git 中
  3. 自动拉取 - 软件代理拉取期望状态
  4. 持续协调 - 代理协调实际状态与期望状态

ArgoCD 设置

1. 安装

# Create namespace
kubectl create namespace argocd

# Install ArgoCD
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

# Get admin password
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d

参考:references/argocd-setup.md 获取详细设置

2. 仓库结构

gitops-repo/
├── apps/
│   ├── production/
│   │   ├── app1/
│   │   │   ├── kustomization.yaml
│   │   │   └── deployment.yaml
│   │   └── app2/
│   └── staging/
├── infrastructure/
│   ├── ingress-nginx/
│   ├── cert-manager/
│   └── monitoring/
└── argocd/
    ├── applications/
    └── projects/

3. 创建应用程序

# argocd/applications/my-app.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-app
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/org/gitops-repo
    targetRevision: main
    path: apps/production/my-app
  destination:
    server: https://kubernetes.default.svc
    namespace: production
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true

4. 应用的应用模式

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: applications
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/org/gitops-repo
    targetRevision: main
    path: argocd/applications
  destination:
    server: https://kubernetes.default.svc
    namespace: argocd
  syncPolicy:
    automated: {}

Flux CD 设置

1. 安装

# Install Flux CLI
curl -s https://fluxcd.io/install.sh | sudo bash

# Bootstrap Flux
flux bootstrap github \
  --owner=org \
  --repository=gitops-repo \
  --branch=main \
  --path=clusters/production \
  --personal

2. 创建 GitRepository

apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
  name: my-app
  namespace: flux-system
spec:
  interval: 1m
  url: https://github.com/org/my-app
  ref:
    branch: main

3. 创建 Kustomization

apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: my-app
  namespace: flux-system
spec:
  interval: 5m
  path: ./deploy
  prune: true
  sourceRef:
    kind: GitRepository
    name: my-app

同步策略

自动同步配置

ArgoCD:

syncPolicy:
  automated:
    prune: true # 删除不在 Git 中的资源
    selfHeal: true # 协调手动更改
    allowEmpty: false
  retry:
    limit: 5
    backoff:
      duration: 5s
      factor: 2
      maxDuration: 3m

Flux:

spec:
  interval: 1m
  prune: true
  wait: true
  timeout: 5m

参考:references/sync-policies.md

渐进式交付

使用 ArgoCD Rollouts 的金丝雀部署

apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
  name: my-app
spec:
  replicas: 5
  strategy:
    canary:
      steps:
        - setWeight: 20
        - pause: { duration: 1m }
        - setWeight: 50
        - pause: { duration: 2m }
        - setWeight: 100

蓝绿部署

strategy:
  blueGreen:
    activeService: my-app
    previewService: my-app-preview
    autoPromotionEnabled: false

秘密管理

外部秘密操作符

apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
  name: db-credentials
spec:
  refreshInterval: 1h
  secretStoreRef:
    name: aws-secrets-manager
    kind: SecretStore
  target:
    name: db-credentials
  data:
    - secretKey: password
      remoteRef:
        key: prod/db/password

密封秘密

# Encrypt secret
kubeseal --format yaml < secret.yaml > sealed-secret.yaml

# Commit sealed-secret.yaml to Git

最佳实践

  1. 使用单独的仓库或分支 用于不同环境
  2. 实施 RBAC 用于 Git 仓库
  3. 启用通知 用于同步失败
  4. 使用健康检查 用于自定义资源
  5. 实施批准门 用于生产
  6. 保持秘密不出 Git(使用外部秘密)
  7. 使用应用的应用模式 用于组织
  8. 标记发布 用于轻松回滚
  9. 监控同步状态 与警报
  10. 测试更改 首先在暂存环境中

故障排除

同步失败:

argocd app get my-app
argocd app sync my-app --prune

不同步状态:

argocd app diff my-app
argocd app sync my-app --force

相关技能

  • k8s-manifest-generator - 用于创建清单
  • helm-chart-scaffolding - 用于打包应用程序

内存协议(强制)

开始前:

cat C:\dev\projects\agent-studio\.claude\context\memory\learnings.md

完成后:

  • 新模式 -> C:\dev\projects\agent-studio\.claude\context\memory\learnings.md
  • 发现的问题 -> C:\dev\projects\agent-studio\.claude\context\memory\issues.md
  • 做出的决策 -> C:\dev\projects\agent-studio\.claude\context\memory\decisions.md

假设中断:如果不在内存中,就没有发生。