SuperAPI AI 工具集

gitops-workflow

部署与运维

查看项目

更新于 10-16 10:31

技能介绍

用途

本 Skill 用于实现基于 Git 的声明式持续交付,面向 Kubernetes 环境,通过 ArgoCD 或 Flux CD 落地 OpenGitOps 原则,实现自动化部署与持续协调。

适用场景

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

OpenGitOps 原则

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

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. 创建 Application

# 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
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 # 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

智能体与 MCP 增强

本 Skill 支持并推荐配合特定的智能体角色和 MCP 工具使用,以获得最佳效果。

推荐智能体角色

  • DevOps Engineer:详见 AGENTS.md。该角色专注于 IaC(基础设施即代码)和自动化流水线。启用后,AI 将严格遵循声明式 API 原则,避免命令式操作。

推荐 MCP 工具

  • Kubectl MCP:允许 AI 直接监控集群状态和调试 Pod。
  • Git/GitHub MCP:用于管理 GitOps 仓库的配置变更和 PR 流程。
  • mcp-feedback-enhanced:在配置自动同步策略(Auto-Sync)或处理敏感信息(Secrets)时,使用 ask_followup_question 确认用户的安全偏好和操作边界。

最佳实践

  • 为不同环境使用单独的仓库或分支
  • 为 Git 仓库实施 RBAC
  • 为同步失败启用通知
  • 为自定义资源使用健康检查
  • 为生产环境实施审批门禁
  • 将密钥排除在 Git 之外(使用 External Secrets)
  • 使用 App of Apps 模式进行组织
  • 为发布打标签以便回滚
  • 使用告警监控同步状态
  • 先在 staging 中测试变更

故障排查

同步失败:

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

Out of sync 状态:

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

相关 Skill

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