mcpField
G
gitops-workflow

gitops-workflow

github.com/sickn33云与 DevOps48.8k223 次安装今天更新
代码仓库

关于 gitops-workflow

面向 Kubernetes 部署的 GitOps 工作流指南,介绍使用 ArgoCD 或 Flux CD 以 Git 中的声明式状态持续交付。适合管理集群部署、同步策略和渐进式发布,并强调权限审查、回滚验证及密钥保护。

主要功能

  1. 01规划 GitOps 仓库结构
  2. 02配置 ArgoCD 或 Flux CD
  3. 03设置同步策略与环境晋级
  4. 04演示 Canary 和蓝绿发布
  5. 05管理密钥与外部密钥
  6. 06检查同步状态并规划回滚

适用场景

  1. 01为 Kubernetes 集群建立 GitOps
  2. 02自动从 Git 部署应用
  3. 03管理多集群发布和环境晋级
  4. 04配置密钥管理与生产审批

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill gitops-workflow

安装后在 Claude Code 中输入/gitops-workflow确认已加载。

附带文件references/argocd-setup.mdreferences/sync-policies.md

详细介绍

GitOps 工作流

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

目的

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

在以下情况下使用此技能

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

在以下情况下不要使用此技能

  • 只需进行一次手动部署
  • 无法管理集群访问权限或仓库权限
  • 不会部署到 Kubernetes

说明

  1. 定义仓库布局和期望状态约定。
  2. 检查不可变、固定版本的 ArgoCD 或 Flux 安装,并且只连接已授权的集群。
  3. 配置同步策略、环境和晋级流程。
  4. 验证回滚和密钥处理。

安全

  • 未经批准,不要对生产环境启用自动同步。
  • 不要将密钥存入 Git,应使用 Sealed Secrets 或外部密钥管理器。
  • 在写入前确认 kube context、命名空间、受影响资源和回滚方案。以非执行方式审查下载的清单,并固定已审查的提交或摘要;切勿直接应用可变的远程 YAML。自动同步、修剪和强制操作可能会删除或重新创建资源。以下代码片段是彼此独立的示例,并非可在生产环境中执行的命令序列。

OpenGitOps 原则

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

ArgoCD 设置

1. 安装
# Create namespace
kubectl create namespace argocd

# Install ArgoCD
# Obtain the selected release manifest, review its complete content, then apply
# the reviewed local file to the authorized context:
kubectl apply -n argocd -f ./reviewed-argocd-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. App of Apps 模式
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
brew install fluxcd/tap/flux

# Alternative: download the official installer, inspect it, then execute it
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
curl -fsSLo "$tmpdir/flux-install.sh" https://fluxcd.io/install.sh
cat "$tmpdir/flux-install.sh"  # review the full installer before sudo
sudo bash "$tmpdir/flux-install.sh"

# 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      # Delete resources not in Git
    selfHeal: true   # Reconcile manual changes
    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

密钥管理

External Secrets Operator
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
Sealed Secrets
# Encrypt secret
kubeseal --format yaml < secret.yaml > sealed-secret.yaml

# Commit sealed-secret.yaml to Git

最佳实践

  1. 为不同环境使用独立的仓库或分支
  2. 为 Git 仓库实施 RBAC
  3. 启用同步失败通知
  4. 为自定义资源使用健康检查
  5. 为生产环境设置审批关卡
  6. 不要将密钥存入 Git(使用 External Secrets)
  7. 使用 App of Apps 模式进行组织管理
  8. 标记发布版本以便轻松回滚
  9. 使用告警监控同步状态
  10. 先在预发布环境测试变更

故障排除

同步失败:

argocd app get my-app
# Inspect diff/health first; pruning is a separately authorized deletion action.

状态不同步:

argocd app diff my-app
# Force/replacement sync is not a routine fix; inspect the cause and disruption first.

相关技能

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

限制

  • 仅当任务明确符合上述范围时才使用此技能。
  • 不要将输出视为可替代针对具体环境进行验证、测试或专家审查的方案。
  • 如果缺少必需的输入、权限、安全边界或成功标准,请暂停并请求澄清。

评论

支持 Markdown