基于 ArgoCD 的 GitOps 持续交付实践
GitOps 的核心理念是:Git 仓库是应用部署状态的唯一事实来源。ArgoCD 作为 Kubernetes 原生的持续交付工具,完美践行了这一理念——它持续监控 Git 仓库的变更,并自动将集群状态同步到声明式配置。
ArgoCD 的核心资源是 Application,以下是一个典型的应用定义:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/org/k8s-manifests.git
targetRevision: main
path: overlays/production
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
对于多集群场景,ApplicationSet 提供了强大的模板化能力。我们可以用 Generator 自动为每个集群生成 Application:
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: my-app-clusters
spec:
generators:
- clusters:
selector:
matchLabels:
env: production
template:
metadata:
name: "{{name}}-my-app"
spec:
source:
repoURL: https://github.com/org/k8s-manifests.git
path: overlays/production
destination:
server: "{{server}}"
namespace: production
在我们的实践中,ArgoCD 的自助回滚功能多次拯救了团队。当一次有问题的提交导致集群状态偏离时,ArgoCD 检测到健康检查失败并自动回滚到上一个已知良好的状态。配合 Slack 通知和 ArgoCD Rollouts 的渐进式交付,我们的发布流程既安全又高效。