
关于
使用 ArgoCD 和 Flux 实现 GitOps 工作流的完整指南,用于自动化 Kubernetes 部署。
name: gitops-workflow description: "使用 ArgoCD 和 Flux 实现 GitOps 工作流的完整指南,用于自动化 Kubernetes 部署。" risk: critical source: community date_added: "2026-02-27"
GitOps 工作流
使用 ArgoCD 和 Flux 实现 GitOps 工作流的完整指南,用于自动化 Kubernetes 部署。
目的
使用 ArgoCD 或 Flux CD 实现声明式、基于 Git 的 Kubernetes 持续交付,遵循 OpenGitOps 原则。
适用场景
- 为 Kubernetes 集群设置 GitOps
- 从 Git 自动化应用部署
- 实现渐进式交付策略
- 管理多集群部署
- 配置自动同步策略
- 在 GitOps 中设置密钥管理
不适用场景
- 需要一次性手动部署
- 无法管理集群访问或仓库权限
- 不是部署到 Kubernetes
操作说明
- 定义仓库布局和期望状态约定。
- 安装 ArgoCD 或 Flux 并连接集群。
- 配置同步策略、环境和晋升流程。
- 验证回滚和密钥处理。
安全注意事项
- 避免在没有审批的情况下自动同步到生产环境。
- 将密钥保持在 Git 之外,使用 sealed secrets 或外部密钥管理器。
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. 创建应用
# 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
curl -fsSLo /tmp/flux-install.sh https://fluxcd.io/install.sh
sed -n '1,160p' /tmp/flux-install.sh
sudo bash /tmp/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-active
previewService: my-app-preview
autoPromotionEnabled: false
兼容工具
Claude CodeCursor
标签
运维部署

