595_Argo CD GitOps
一句话概述:Argo CD 是 Kubernetes 的声明式 GitOps 持续部署工具,以 Git 仓库为唯一真实来源(Single Source of Truth),自动同步 Git 中的 K8s 配置到集群,实现"改代码即部署",是 CNCF 毕业项目和企业 GitOps 的事实标准。
核心知识点表
| 概念 | 白话解释 |
|---|
| GitOps | Git 即运维,所有基础设施和应用配置都存在 Git 中 |
| Application | 应用,Argo CD 中的核心对象,关联 Git 源和目标集群 |
| Sync | 同步,把 Git 中的配置应用到 K8s 集群 |
| Health Status | 健康状态,应用在集群中的运行状态 |
| Sync Status | 同步状态,Git 配置和集群实际状态是否一致 |
| Project | 项目,用于组织和限制应用的权限范围 |
| ApplicationSet | 应用集,用模板批量生成多个 Application |
安装配置
安装 Argo CD
# 1. 创建命名空间并安装
kubectl create namespace argocd # 创建命名空间
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# 安装完整版(含 HA 支持)
# 2. 等待所有 Pod 就绪
kubectl -n argocd get pods -w # 监控 Pod 状态
# 等待所有 Pod 变为 Running
# 3. 获取初始管理员密码
kubectl -n argocd get secret argocd-initial-admin-secret \
-o jsonpath="{.data.password}" | base64 -d # 解码密码
# 用户名:admin
# 4. 暴露 Web UI
# 方法1:端口转发(测试用)
kubectl port-forward svc/argocd-server -n argocd 8080:443
# 访问 https://localhost:8080
# 方法2:LoadBalancer(生产用)
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}'
# 方法3:Ingress(推荐)
# 创建 Ingress 资源指向 argocd-server
安装 CLI
# macOS
brew install argocd # Homebrew
# 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 login localhost:8080 # 登录 Argo CD
# 首次登录后修改密码
argocd account update-password # 修改密码
基本使用
创建 Application
# === 方法1:CLI 创建 ===
argocd app create my-app \
--repo https://github.com/user/k8s-manifests.git \ # Git 仓库地址
--path deploy/production \ # 仓库中 K8s 配置的路径
--dest-server https://kubernetes.default.svc \ # 目标集群(当前集群)
--dest-namespace production \ # 目标命名空间
--sync-policy automated \ # 自动同步
--auto-prune \ # 自动删除 Git 中已移除的资源
--self-heal # 自动修复手动修改的资源
# === 方法2:YAML 声明式创建(推荐) ===
# application.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app # 应用名
namespace: argocd # 必须在 argocd 命名空间
spec:
project: default # 所属项目
source:
repoURL: https://github.com/user/k8s-manifests.git # Git 仓库
targetRevision: main # 分支/Tag/Commit
path: deploy/production # 配置文件路径
destination:
server: https://kubernetes.default.svc # 目标集群
namespace: production # 目标命名空间
syncPolicy:
automated: # 自动同步策略
prune: true # 删除 Git 中已移除的资源
selfHeal: true # 自动恢复手动修改
syncOptions:
- CreateNamespace=true # 自动创建命名空间
retry:
limit: 5 # 失败重试次数
backoff:
duration: 5s # 初始重试间隔
maxDuration: 3m # 最大重试间隔
kubectl apply -f application.yaml # 创建应用
使用 Helm Chart
# helm-app.yaml — Argo CD 管理 Helm 应用
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: nginx-helm
namespace: argocd
spec:
project: default
source:
repoURL: https://charts.bitnami.com/bitnami # Helm 仓库
chart: nginx # Chart 名称
targetRevision: 18.3.1 # Chart 版本
helm:
values: | # 自定义 values
replicaCount: 3
service:
type: ClusterIP
resources:
requests:
memory: "64Mi"
cpu: "100m"
destination:
server: https://kubernetes.default.svc
namespace: web
syncPolicy:
automated:
prune: true
selfHeal: true
日常操作
# === 应用管理 ===
argocd app list # 列出所有应用
argocd app get my-app # 查看应用详情
argocd app sync my-app # 手动触发同步
argocd app diff my-app # 查看 Git 与集群的差异
argocd app history my-app # 查看同步历史
argocd app rollback my-app 3 # 回滚到版本 3
argocd app delete my-app # 删除应用
# === 集群管理 ===
argocd cluster add CONTEXT_NAME # 添加外部集群
argocd cluster list # 列出集群
# === 仓库管理 ===
argocd repo add https://github.com/user/repo.git \
--username user --password token # 添加私有仓库
argocd repo list # 列出仓库
高级用法
ApplicationSet(批量管理)
# appset.yaml — 用模板批量创建应用
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: multi-env # 应用集名
namespace: argocd
spec:
generators:
- list: # 列表生成器
elements:
- env: dev # 开发环境
namespace: dev
replicas: "1"
- env: staging # 预发环境
namespace: staging
replicas: "2"
- env: production # 生产环境
namespace: production
replicas: "5"
template:
metadata:
name: "my-app-{{env}}" # 应用名:my-app-dev, my-app-staging 等
spec:
project: default
source:
repoURL: https://github.com/user/k8s-manifests.git
targetRevision: main
path: "envs/{{env}}" # 每个环境的配置路径
destination:
server: https://kubernetes.default.svc
namespace: "{{namespace}}" # 目标命名空间
Webhook 自动触发
# Argo CD 支持 Git Webhook 实现即时同步:
# 1. 在 Git 平台(GitHub/GitLab/Gitea)配置 Webhook
# 2. URL: https://argocd.example.com/api/webhook
# 3. 事件:Push events
# 默认 Argo CD 每 3 分钟轮询 Git
# 配置 Webhook 后可实现秒级同步
多集群部署
# 添加远程集群到 Argo CD
argocd cluster add my-remote-cluster \
--kubeconfig ~/.kube/remote-config # 使用远程集群的 kubeconfig
# 创建部署到远程集群的应用
argocd app create remote-app \
--repo https://github.com/user/manifests.git \
--path deploy \
--dest-server https://remote-cluster-api:6443 \ # 远程集群 API
--dest-namespace production
常见报错
| 报错信息 | 原因 | 解决方案 |
|---|
ComparisonError | Git 仓库无法访问 | 检查仓库 URL 和认证凭据 |
SyncError: one or more objects failed | K8s 资源创建失败 | 查看 Sync 详情中的具体错误 |
OutOfSync | Git 和集群状态不一致 | 点击 Sync 或检查 selfHeal 配置 |
Healthy → Degraded | 应用健康检查失败 | 检查 Pod 日志和资源限制 |
Permission denied | RBAC 权限不足 | 检查 Argo CD Project 的权限配置 |
速查表
# === CLI 核心命令 ===
argocd app create NAME ... # 创建应用
argocd app sync NAME # 同步应用
argocd app get NAME # 查看应用
argocd app diff NAME # 查看差异
argocd app delete NAME # 删除应用
argocd app rollback NAME REV # 回滚
argocd app list # 列出应用
# === 同步状态 ===
# Synced → Git 和集群一致
# OutOfSync → Git 和集群不一致
# Unknown → 无法确定状态
# === 健康状态 ===
# Healthy → 应用运行正常
# Progressing → 正在部署中
# Degraded → 应用异常
# Missing → 资源不存在
# === 关键命名空间 ===
# argocd → Argo CD 自身组件
同类对比
| 特性 | Argo CD | Flux CD | Jenkins X | Rancher Fleet |
|---|
| GitOps | 声明式 | 声明式 | CI+CD | 声明式 |
| Web UI | 优秀 | 无(需Weave) | 基本 | 内嵌Rancher |
| 多集群 | 原生 | 原生 | 有限 | 原生 |
| Helm 支持 | 原生 | 原生 | 原生 | 原生 |
| CNCF | 毕业 | 毕业 | 沙箱 | 非CNCF |
| 学习曲线 | 中等 | 中等 | 高 | 低 |
| 社区 | 最大 | 大 | 中 | 中 |
选型建议:K8s GitOps 首选 Argo CD(功能最全、UI 最好、65% 企业采用率);喜欢纯 CLI 方式选 Flux CD;已用 Rancher 则直接用 Fleet。