关于 gitops-workflows
GitOps Workflows 是用于部署和运维 ArgoCD 与 Flux 的指南及资源集,包含部署模式、模板、脚本和故障排查内容,面向管理 Kubernetes 应用的团队。
主要功能
- 01ArgoCD 与 Flux 配置指南
- 02仓库结构模式
- 03多集群部署模板
- 04密钥管理方案
- 05渐进式交付示例
- 06故障排查与漂移检查
适用场景
- 01为部署选择 ArgoCD 或 Flux
- 02按环境组织 GitOps 仓库
- 03生成多集群 ApplicationSet
- 04排查同步与配置漂移问题
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ ahmedasmar/ devops-claude-skills --skill gitops-workflows安装后在 Claude Code 中输入/gitops-workflows确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
assets/applicationsets/cluster-generator.yamlassets/argocd/install-argocd-3.x.yamlassets/flux/flux-bootstrap-github.shoci-helmrelease.yamlassets/progressive-delivery/argo-rollouts-canary.yamlassets/secrets/sops-age-config.yamlreferences/argocd_vs_flux.mdbest_practices.mdmulti_cluster.mdoci_artifacts.mdprogressive_delivery.mdrepo_patterns.mdsecret_management.mdtroubleshooting.mdscripts/applicationset_generator.pysecret_audit.pyvalidate_gitops_repo.py详细介绍
GitOps 工作流
核心工作流:GitOps 实施
使用此决策树确定起点:
Do you have GitOps installed?
├─ NO → Need to choose a tool
│ └─ Want UI + easy onboarding? → ArgoCD (Workflow 1)
│ └─ Want modularity + platform engineering? → Flux (Workflow 2)
└─ YES → What's your goal?
├─ Sync issues / troubleshooting → Workflow 7
├─ Multi-cluster deployment → Workflow 4
├─ Secrets management → Workflow 5
├─ Progressive delivery → Workflow 6
├─ Repository structure → Workflow 3
└─ Tool comparison → Read references/argocd_vs_flux.md
1. 初始设置:ArgoCD 3.x
最新版本:v3.1.9(稳定版)、v3.2.0-rc4(2025 年 10 月)
快速安装
# Create namespace
kubectl create namespace argocd
# Install ArgoCD 3.x
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/v3.1.9/manifests/install.yaml
# Get admin password
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
# Port forward to access UI
kubectl port-forward svc/argocd-server -n argocd 8080:443
# Access: https://localhost:8080
→ 模板:assets/argocd/install-argocd-3.x.yaml
ArgoCD 3.x 的主要变更
- 重大变更:基于注解的跟踪(默认方式,以前使用标签)、启用 RBAC 日志强制执行、移除旧版指标
- 新增:细粒度 RBAC(按资源授权)、更合理的默认设置(资源排除)、支持密钥操作器
部署首个应用
# CLI method
argocd app create guestbook \
--repo https://github.com/argoproj/argocd-example-apps.git \
--path guestbook \
--dest-server https://kubernetes.default.svc \
--dest-namespace default
# Sync application
argocd app sync guestbook
健康检查
# List all applications and their sync/health status
argocd app list
# Get detailed status for a specific application
argocd app get <app-name>
# Check applications via kubectl (no ArgoCD CLI needed)
kubectl get applications.argoproj.io -A
2. 初始设置:Flux 2.7
最新版本:v2.7.1(2025 年 10 月)
快速安装
# Install Flux CLI
brew install fluxcd/tap/flux # macOS
# or: curl -s https://fluxcd.io/install.sh | sudo bash
# Check prerequisites
flux check --pre
# Bootstrap Flux (GitHub)
export GITHUB_TOKEN=<your-token>
flux bootstrap github \
--owner=<org> \
--repository=fleet-infra \
--branch=main \
--path=clusters/production \
--personal
# Enable source-watcher (Flux 2.7+)
flux install --components-extra=source-watcher
→ 模板:assets/flux/flux-bootstrap-github.sh
Flux 2.7 新功能
- ✅ 镜像自动化正式发布(GA)
- ✅ ExternalArtifact 和 ArtifactGenerator API
- ✅ Source-watcher 组件,性能更佳
- ✅ 支持 OpenTelemetry 跟踪
- ✅ 用于就绪状态评估的 CEL 表达式
部署首个应用
# gitrepository.yaml
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
name: podinfo
namespace: flux-system
spec:
interval: 1m
url: https://github.com/stefanprodan/podinfo
ref:
branch: master
---
# kustomization.yaml
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: podinfo
namespace: flux-system
spec:
interval: 5m
path: "./kustomize"
prune: true
sourceRef:
kind: GitRepository
name: podinfo
健康检查
# Check all Flux resources across namespaces
flux get all -A
# Check Git sources
flux get sources git
# Check kustomization status
flux get kustomizations
3. 仓库结构设计
如何选择:单体仓库还是多仓库?
单体仓库模式
适用于:初创公司、小型团队(< 20 个应用)、单一团队
gitops-repo/
├── apps/
│ ├── frontend/
│ ├── backend/
│ └── database/
├── infrastructure/
│ ├── ingress/
│ ├── monitoring/
│ └── secrets/
└── clusters/
├── dev/
├── staging/
└── production/
多仓库模式
适用于:大型组织、多个团队、边界清晰的场景
infrastructure-repo/ (Platform team)
app-team-1-repo/ (Team 1)
app-team-2-repo/ (Team 2)
环境结构(Kustomize)
app/
├── base/
│ ├── deployment.yaml
│ ├── service.yaml
│ └── kustomization.yaml
└── overlays/
├── dev/
│ ├── kustomization.yaml
│ └── replica-patch.yaml
├── staging/
└── production/
→ 参考:references/repo_patterns.md | → 脚本:python3 scripts/validate_gitops_repo.py /path/to/repo
4. 多集群部署
ArgoCD ApplicationSets
集群生成器(部署到所有集群):
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: cluster-apps
spec:
generators:
- cluster:
selector:
matchLabels:
environment: production
template:
metadata:
name: '{{name}}-myapp'
spec:
source:
repoURL: https://github.com/org/apps
path: myapp
destination:
server: '{{server}}'
→ 模板:assets/applicationsets/cluster-generator.yaml
性能优势:部署速度提升 83%(30 分钟 → 5 分钟)
生成 ApplicationSets
# Cluster generator
python3 scripts/applicationset_generator.py cluster \
--name my-apps \
--repo-url https://github.com/org/repo \
--output appset.yaml
# Matrix generator (cluster x apps)
python3 scripts/applicationset_generator.py matrix \
--name my-apps \
--cluster-label production \
--directories app1,app2,app3 \
--output appset.yaml
→ 脚本:scripts/applicationset_generator.py
Flux 多集群
中心辐射式:管理集群管理所有集群
# Bootstrap each cluster
flux bootstrap github --context prod-cluster --path clusters/production
flux bootstrap github --context staging-cluster --path clusters/staging
→ 参考:references/multi_cluster.md
5. 密钥管理
切勿将明文密钥提交到 Git。 请选择一种解决方案:
决策矩阵
| 解决方案 | 复杂度 | 最适用于 | 2025 年趋势 |
|---|---|---|---|
| SOPS + age | 中 | 以 Git 为中心、灵活 | ↗️ 首选 |
| External Secrets Operator | 中 | 云原生、动态密钥 | ↗️ 持续增长 |
| Sealed Secrets | 低 | 简单、优先采用 GitOps | → 稳定 |
选项 1:SOPS + age(2025 年推荐)
设置:
# Generate age key
age-keygen -o key.txt
# Public key: age1...
# Create .sops.yaml
cat <<EOF > .sops.yaml
creation_rules:
- path_regex: .*.yaml
encrypted_regex: ^(data|stringData)$
age: age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p
EOF
# Encrypt secret
kubectl create secret generic my-secret --dry-run=client -o yaml \
--from-literal=password=supersecret > secret.yaml
sops -e secret.yaml > secret.enc.yaml
# Commit encrypted version
git add secret.enc.yaml .sops.yaml
→ 模板:assets/secrets/sops-age-config.yaml
选项 2:External Secrets Operator(v0.20+)
适用于:云原生应用、动态密钥、自动轮换
选项 3:Sealed Secrets
适用于:简单设置、静态密钥、无需外部依赖
→ 参考:references/secret_management.md
审计密钥
python3 scripts/secret_audit.py /path/to/repo
6. 渐进式交付
Argo Rollouts(与 ArgoCD 配合使用)
金丝雀部署:
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
name: my-app
spec:
strategy:
canary:
steps:
- setWeight: 20
- pause: {duration: 2m}
- setWeight: 50
- pause: {duration: 2m}
- setWeight: 100
→ 模板:assets/progressive-delivery/argo-rollouts-canary.yaml
Flagger(与 Flux 配合使用)
基于指标分析的金丝雀部署:
apiVersion: flagger.app/v1beta1
kind: Canary
metadata:
name: my-app
spec:
analysis:
interval: 1m
threshold: 5
maxWeight: 50
stepWeight: 10
metrics:
- name: request-success-rate
thresholdRange:
min: 99
→ 参考:references/progressive_delivery.md
7. 故障排查
常见问题
ArgoCD 不同步(OutOfSync):
# Check differences
argocd app diff my-app
# Sync application
argocd app sync my-app
# Check health
argocd app list
argocd app get my-app
Flux 未进行协调:
# Check resources
flux get all
# Check specific kustomization
flux get kustomizations
kubectl describe kustomization my-app -n flux-system
# Force reconcile
flux reconcile kustomization my-app
检测偏移:
# ArgoCD drift detection
argocd app diff my-app
# Kubernetes manifest drift detection
kubectl diff -f <manifest.yaml>
→ 参考:references/troubleshooting.md
8. OCI 制品(Flux 2.6+,2025 年 6 月正式发布)
对 Helm Chart 使用 OCIRepository
apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: OCIRepository
metadata:
name: podinfo-oci
spec:
interval: 5m
url: oci://ghcr.io/stefanprodan/charts/podinfo
ref:
semver: ">=6.0.0"
verify:
provider: cosign
→ 模板:assets/flux/oci-helmrelease.yaml
验证 OCI 制品
# Check OCI sources managed by Flux
flux get sources oci
# Get detailed OCI repository status via kubectl
kubectl get ocirepository -A -o json
→ 参考文档:references/oci_artifacts.md
快速参考命令
ArgoCD
argocd app list # List applications
argocd app get <app-name> # Get application details
argocd app sync <app-name> # Sync application
argocd app diff <app-name> # View diff
argocd app delete <app-name> # Delete application
Flux
flux check # Check Flux status
flux get all # Get all resources
flux reconcile source git <name> # Reconcile immediately
flux reconcile kustomization <name> # Reconcile kustomization
flux suspend kustomization <name> # Suspend
flux resume kustomization <name> # Resume
flux export source git --all > sources.yaml # Export resources
资源摘要
脚本:applicationset_generator.py | secret_audit.py | validate_gitops_repo.py
参考文档:argocd_vs_flux.md | repo_patterns.md | secret_management.md | progressive_delivery.md | multi_cluster.md | troubleshooting.md | best_practices.md | oci_artifacts.md
模板:argocd/install-argocd-3.x.yaml | applicationsets/cluster-generator.yaml | flux/flux-bootstrap-github.sh | flux/oci-helmrelease.yaml | secrets/sops-age-config.yaml | progressive-delivery/argo-rollouts-canary.yaml