什么是 GitOps
GitOps 是一套由 Weaveworks 于 2017 年提出的运维方法论。核心理念是:将 Git 仓库作为系统基础设施和应用配置的唯一真实来源(Single Source of Truth),通过自动化工具持续同步 Git 中的声明式配置到目标系统(如 Kubernetes 集群)。
当工程师想要修改生产环境时,他不再直接 kubectl apply,而是提交一个 Pull Request 到配置仓库,经过 Code Review 后合并,ArgoCD 自动将变更同步到集群。这意味着所有操作都有完整的 Git 历史记录,回滚只需 git revert。
GitOps 四大原则
OpenGitOps 社区总结了 GitOps 的四大核心原则,ArgoCD 完整实现了这四点:
GitOps vs 传统 CI/CD
传统的 CI/CD(如 Jenkins Pipeline)通常是 Push 模式:CI 系统推送变更到集群。GitOps 是 Pull 模式:集群内的代理主动拉取 Git 配置并同步。这个区别极其重要。
| 对比维度 | 传统 CI/CD(Push) | GitOps(Pull) |
|---|---|---|
| 凭证存储 | CI 系统需持有集群 kubeconfig(高风险) | ArgoCD 在集群内,无需外部持有凭证 |
| 配置漂移 | 无法检测,手动 kubectl 改动无法发现 | 持续检测,自动修复漂移 |
| 回滚 | 需要重新触发 Pipeline | git revert + ArgoCD 自动同步 |
| 可审计性 | 依赖 CI 日志,分散 | Git 历史完整记录所有变更 |
| 多集群 | 需要为每个集群配置凭证和 Pipeline | ArgoCD 统一管理,AppSet 批量部署 |
| 灾备恢复 | 依赖 Pipeline 重新执行 | 新建集群后 ArgoCD 自动从 Git 恢复 |
传统 CI/CD 中,Jenkins 等系统需要持有目标集群的高权限 kubeconfig,一旦 CI 系统被攻破,攻击者可以直接操控生产集群。GitOps 中 ArgoCD 运行在集群内部,外部系统无需直接访问集群,大幅缩小攻击面。
ArgoCD 架构详解
ArgoCD 是一个声明式的、GitOps 持续交付工具,由多个核心组件构成:
ArgoCD 架构图 ┌────────────────────────── argocd namespace ──────────────────────────────┐ │ │ │ ┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐ │ │ │ API Server │ │ Repo Server │ │ App Controller │ │ │ │ gRPC/REST/WS │ │ 克隆仓库/渲染 │ │ 同步调和循环 │ │ │ │ UI 后端+CLI │ │ Helm/Kustomize │ │ 漂移检测 │ │ │ └────────┬────────┘ └────────┬─────────┘ └────────┬────────┘ │ │ │ │ │ │ │ ┌────────▼──────────────────────▼───────────────────────▼────────┐ │ │ │ argocd-redis (状态缓存) │ │ │ └────────────────────────────────────────────────────────────────┘ │ │ │ │ ┌───────────────┐ │ │ │ Dex Server │ SSO 身份提供者 (GitHub/GitLab/LDAP) │ │ └───────────────┘ │ └──────────────────────────────────────────────────────────────────────────┘ │ Watch │ Clone │ Apply/Delete ┌────────▼────────┐ ┌─────────▼──────┐ ┌──────▼──────────────┐ │ K8s API Server │ │ Git Repository │ │ Target K8s Cluster │ └─────────────────┘ └────────────────┘ └─────────────────────┘
安装 ArgoCD
方式一:kubectl apply(快速安装)
# 创建命名空间
kubectl create namespace argocd
# 应用官方安装清单(包含所有 CRD、RBAC、Deployment)
kubectl apply -n argocd -f \
https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# 等待所有 Pod 就绪
kubectl wait --for=condition=Ready pod --all -n argocd --timeout=120s
# 查看安装状态
kubectl get pods -n argocd
NAME READY STATUS RESTARTS
argocd-application-controller-0 1/1 Running 0
argocd-applicationset-controller-xxx 1/1 Running 0
argocd-dex-server-xxx 1/1 Running 0
argocd-notifications-controller-xxx 1/1 Running 0
argocd-redis-xxx 1/1 Running 0
argocd-repo-server-xxx 1/1 Running 0
argocd-server-xxx 1/1 Running 0
方式二:Helm Chart 安装(推荐生产)
# 添加 Argo Helm 仓库
helm repo add argo https://argoproj.github.io/argo-helm
helm repo update
# 查看可用版本
helm search repo argo/argo-cd --versions | head -5
# 安装(自定义 values)
helm install argocd argo/argo-cd \
--namespace argocd \
--create-namespace \
--version 7.x.x \
-f argocd-values.yaml
# argocd-values.yaml 基础配置示例
server:
replicas: 2
ingress:
enabled: true
hostname: argocd.example.com
tls: true
applicationSet:
replicas: 2
controller:
resources:
requests:
cpu: 250m
memory: 256Mi
limits:
cpu: 1000m
memory: 1Gi
首次登录与 CLI 配置
获取初始密码
# 初始 admin 密码存储在 Secret 中
kubectl get secret argocd-initial-admin-secret \
-n argocd -o jsonpath="{.data.password}" | base64 -d
# 临时端口转发访问 UI(本地开发)
kubectl port-forward svc/argocd-server -n argocd 8080:443
# 浏览器访问: https://localhost:8080
安装并配置 argocd CLI
# macOS
brew install argocd
# Linux
curl -sSL -o argocd \
https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
chmod +x argocd && sudo mv argocd /usr/local/bin/
# 登录 ArgoCD(需先端口转发或配置 Ingress)
argocd login localhost:8080 \
--username admin \
--password <initial-password> \
--insecure
# 修改初始密码
argocd account update-password
# 查看 ArgoCD 版本
argocd version
# 列出所有 Application
argocd app list
生产环境务必:① 通过 Ingress + TLS 暴露 ArgoCD UI,而非直接 NodePort;② 登录后立即修改 admin 密码或禁用 admin 账户并启用 SSO;③ 安装后删除 argocd-initial-admin-secret。
核心术语速查
GitOps 的核心是将 Git 作为事实来源,通过四大原则(声明式、版本化、自动应用、持续协调)实现安全可审计的部署。ArgoCD 由 API Server、Repo Server、Application Controller 和 Dex 四大组件构成,以 Pull 模式持续同步集群状态。下一章将深入 Application CRD 的核心配置。