ArgoCD 安装与高可用部署
这篇讲三件事:非高可用(standard)安装、高可用(HA)安装、以及生产环境真正要关心的问题——ingress、TLS、初始密码、仓库凭据、版本升级。命令以 Argo CD 2.x 主流版本为准,装之前先去 GitHub releases 确认当前稳定版号。
一、非高可用安装:先跑起来
# 1. 建命名空间并安装(官方 stable 清单)
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# 2. 确认所有 Pod 就绪
kubectl get pods -n argocd -w
# 3. 拿到初始 admin 密码(存在 argocd-initial-admin-secret 里,base64 编码)
kubectl -n argocd get secret argocd-initial-admin-secret \
-o jsonpath="{.data.password}" | base64 -d
# 4. 本地访问 UI(argocd-server 默认是 ClusterIP,需要转发)
kubectl port-forward svc/argocd-server -n argocd 8080:443
# 浏览器打开 https://localhost:8080,用户名 admin装完立刻要做的一件事:改密码或接入 SSO。admin 的初始密码是明文存在 Secret 里的,任何有 namespace 读权限的人都能拿到。命令行方式:
argocd login localhost:8080 --username admin
argocd account update-password如果团队已经有 OIDC(Keycloak/Okta/Azure AD),优先配 SSO 直接关掉本地 admin——见 RBAC 与 SSO 篇。
二、暴露服务:三种方式选一个
argocd-server 默认 ClusterIP,生产必须暴露。三种方案按安全等级排序:
方案 A:Ingress + 自有域名(推荐)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: argocd-server
namespace: argocd
annotations:
nginx.ingress.kubernetes.io/ssl-passthrough: "true" # gRPC 需要 SSL 直通
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
spec:
ingressClassName: nginx
rules:
- host: argocd.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: argocd-server
port:
number: 443关键点:Argo CD 的 UI 和 CLI 走 gRPC,普通七层反代会把 gRPC 请求当普通 HTTP 处理然后 502。Nginx 用 ssl-passthrough(TCP 层直通)最稳;如果用 HTTP 层反代,必须开 nginx.ingress.kubernetes.io/backend-protocol: "HTTPS" 且反代要支持 HTTP/2。
方案 B:LoadBalancer + 云证书:云环境直接 kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}',再挂云厂商的 TLS 证书。简单但 ArgoCD 服务直接暴露在公网(如果 LB 是公网型)——务必配 IP 白名单或内网 LB。
方案 C:--insecure 模式:kubectl patch deploy argocd-server -n argocd -p '[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--insecure"}]',让 server 监听 80 端口纯 HTTP,前面挂反代终结 TLS。适合前置有统一网关的场景,注意此时 server 认为自己是 HTTP,反代配 backend-protocol: HTTP。
三、高可用安装
标准安装里每个组件单副本、Redis 单机——controller 挂了期间没人同步、Redis 丢了应用状态缓存全失效。生产环境用 HA 清单:
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/ha/install.yamlHA 模式和标准模式的差异:
| 组件 | 标准 | HA |
|---|---|---|
| application-controller | 1 副本 | 3 副本(默认分片运行) |
| repo-server | 1 副本 | 2-3 副本 |
| argocd-server | 1 副本 | 2-3 副本 |
| redis | 单机 | Redis HA(哨兵模式) |
| notifications-controller | 1 副本 | 2 副本 |
HA 的两个注意点:
- 控制器分片是默认行为:多个 controller 实例通过 Redis 锁瓜分 Application(按名字 hash)。好处是单集群能管几千个 App;代价是某个 App 的同步失败日志只在持有它的那个实例上——排障时
kubectl logs要看对 Pod。规模不大(几百 App 以内)也可以显式关掉分片:设环境变量ARGOCD_CONTROLLER_REPLICAS=1让所有实例都跑全量。 - Redis 哨兵:HA 清单里的 Redis 需要 3 个 Redis + 3 个 Sentinel 实例,
argocd-redisService 后面挂着哨兵集群。如果集群规模小(开发测试集群),HA 的收益可能抵不过多出来的 6 个 Pod 的资源开销——按 App 数量决定:小于 50 个 App 的标准模式完全够用。
四、Git 仓库凭据:私有仓库必配
同步私有仓库的 YAML,必须先把仓库凭据告诉 Argo CD,否则 Application 报 Unable to load target state: authentication required。
CLI 方式(HTTPS):
argocd repo add https://github.com/org/gitops-repo.git \
--username git-bot \
--password ghp_xxxxxxxxTokenSSH 方式:先准备 known_hosts 和私钥,然后:
argocd repo add git@github.com:org/gitops-repo.git \
--ssh-private-key-path ~/.ssh/id_ed25519声明式方式(GitOps 管 GitOps):把凭据存成 Secret 打进 argocd 命名空间, label 是识别关键:
apiVersion: v1
kind: Secret
metadata:
name: repo-gitops
namespace: argocd
labels:
argocd.argoproj.io/secret-type: repo-creds # 凭据类型标记
stringData:
type: git
url: https://github.com/org
password: ghp_xxxx
username: git-bot注意 url 写 https://github.com/org 这种前缀,可以匹配该组织下所有仓库(凭据复用);写具体仓库 URL 则只对这一个仓库生效。GitHub 现在不再支持账户密码,必须用 PAT 或 GitHub App——PAT 要勾 repo 权限,且建议用 fine-grained token 只给需要的仓库。
五、镜像仓库凭据(可选)
如果 GitOps 仓库里放了拉取镜像用的 imagePullSecret,那是应用层的事,ArgoCD 本身不需要镜像仓库凭据。但如果你用了 Argo CD Image Updater(自动更新镜像 tag 的工具),它要配 registry 凭据——走 argocd-image-updater 自己的 Secret 机制,不在本文范围。
六、命令行客户端与日常操作
装 CLI(macOS/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 argocd.example.com --grpc-web
# 常用命令速查
argocd app list # 列出所有应用
argocd app get myapp-prod # 看某个应用状态
argocd app sync myapp-prod # 手动同步
argocd app diff myapp-prod # 看 Git 与集群差异
argocd app history myapp-prod # 部署历史
argocd app create -f app.yaml # 声明式创建应用
argocd cluster add my-context # 纳管外部集群--grpc-web 参数多数时候要加:CLI 默认走纯 gRPC,前面如果有反代只支持 gRPC-Web(Envoy 网关常见)就得切换模式。
七、升级策略
Argo CD 升级要谨慎——它管着你的生产发布通道,自己挂了对面的 CI/CD 全停。原则:
- 看 release notes 的 breaking changes:CRD schema 变更(1.x → 2.x 时 tracking 机制从 label 改 annotation)、最低 K8s 版本要求变化,这些不处理直接升会出事
- 先升 CRD 再升组件:
kubectl apply -k https://github.com/argoproj/argo-cd/manifests/crds?ref=stable(先应用 CRD,再 apply 完整清单)——避免"新 controller 读不懂旧 CRD schema"的窗口期 - 灰度:可以跑两套 Argo CD(新老版本各一套命名空间),新版本先接管非生产 App,稳定后迁移。重装场景多、资源充足的团队值得这么做
- 备份:Argo CD 的状态主要在 K8s 的 Application/AppProject CRD 和 Redis 里。CRD 就是真相源的一部分——如果 Application 本身就是 Git 里声明式管理的,恢复 = 重新装 + 重新指向 Git(app of apps 模式自动重建一切)。Redis 丢了只是缓存失效,控制器会重建
八、安装后的检查清单
跑完安装,逐项确认:
- [ ]
kubectl get pods -n argocd全部 Running - [ ] 改了 admin 密码或已配 SSO
- [ ] ingress 配了 ssl-passthrough 或 insecure 模式,浏览器能开 UI、CLI 能 login
- [ ] 私有仓库凭据已配,UI 里 Settings → Repositories 显示 Connected
- [ ] 决定了标准/HA 模式(HA 清单覆盖安装前先删旧的,或直接用
kubectl apply覆盖) - [ ] 版本升级流程写进团队 runbook(CRD 先行 + release notes 审查)
九、常见安装问题
Pod 一直 CrashLoopBackOff:看日志,常见是 Redis 连不上(HA 模式下 Sentinel 配置变更期)或资源不足(repo-server 跑 helm template 是内存大户,集群太小会被 OOMKilled)。
UI 能开但点 Sync 没反应:检查反代是否支持 gRPC-Web——Nginx 不加 ssl-passthrough 或 backend-protocol: HTTPS,请求根本到不了 server。
authentication required:仓库凭据没配或 token 过期——UI 的 Settings → Repositories 里看连接状态。
升级后 App 全部 Orphaned:CRD 没升或 tracking annotation 版本不一致,按第七节的顺序重来。
下一篇第一个应用,从建仓库到 UI 同步,跑通完整闭环。
