name: gitops-workflow description: 使用ArgoCD和Flux实现GitOps工作流,用于自动化、声明式的Kubernetes部署,具有持续协调功能。在实施GitOps实践、自动化Kubernetes部署或设置声明式基础设施管理时使用。
GitOps工作流
使用ArgoCD和Flux实现自动化Kubernetes部署的GitOps工作流完整指南。
目的
使用ArgoCD或Flux CD,遵循OpenGitOps原则,为Kubernetes实现声明式、基于Git的持续交付。
何时使用此技能
- 为Kubernetes集群设置GitOps
- 从Git自动化应用程序部署
- 实施渐进式交付策略
- 管理多集群部署
- 配置自动化同步策略
- 在GitOps中设置秘密管理
OpenGitOps原则
- 声明式 - 整个系统以声明式描述
- 版本化和不可变 - 期望状态存储在Git中
- 自动拉取 - 软件代理拉取期望状态
- 持续协调 - 代理协调实际状态与期望状态
ArgoCD设置
1. 安装
# 创建命名空间
kubectl create namespace argocd
# 安装ArgoCD
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# 获取管理员密码
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. 安装
# 安装Flux CLI
curl -s https://fluxcd.io/install.sh | sudo bash
# 引导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
密封秘密
# 加密秘密
kubeseal --format yaml < secret.yaml > sealed-secret.yaml
# 将sealed-secret.yaml提交到Git
最佳实践
- 使用独立的仓库或分支 用于不同环境
- 实施RBAC 用于Git仓库
- 启用通知 用于同步失败
- 使用健康检查 用于自定义资源
- 实施批准门 用于生产
- 将秘密保留在Git之外(使用外部秘密)
- 使用应用程序的应用程序模式 用于组织
- 标记发布 用于轻松回滚
- 监控同步状态 带有警报
- 首先在暂存环境中测试更改
故障排除
同步失败:
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- 用于打包应用程序