4. Argo CD 安装与配置:在 K8s 集群中安装 Argo CD,配置 SSH/HTTPS 仓库连接,Web UI 初探
4.1 安装前提与版本选择
在开始安装 Argo CD 之前,需要确认 Kubernetes 集群满足以下最低要求:
| 组件 | 最低版本 | 推荐版本 | 说明 |
|---|---|---|---|
| Kubernetes | v1.20 | v1.27+ | 需支持 RBAC 与 CRD |
| kubectl | v1.20 | v1.27+ | 与集群版本偏差 ≤ 2 个小版本 |
| 集群资源 | 2 核 CPU / 4 GB 内存 | 4 核 CPU / 8 GB 内存 | 生产环境建议更高配置 |
| Ingress Controller | — | nginx-ingress / traefik | 用于 Web UI 外部访问 |
版本策略: 建议始终使用 Argo CD 官方发布的稳定版本(非 RC/alpha)。生产环境锁定大版本,通过
argocd version --client 和 argocd version --server 确认客户端与服务端版本一致。
4.2 安装 Argo CD 到 K8s 集群
Argo CD 提供两种安装方式:快速安装(install.yaml)和高可用安装(ha/install.yaml)。本课程采用快速安装方式,适合实验与中小规模生产环境。
# 创建命名空间
kubectl create namespace argocd
# 安装 Argo CD 核心组件
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# 验证所有 Pod 处于 Running 状态
kubectl get pods -n argocd -w
安装完成后,集群中会运行以下核心 Pod:
| Pod 名称 | 组件角色 | 副本数 | 功能简述 |
|---|---|---|---|
| argocd-server | API Server + Web UI | 1 | 提供 gRPC/REST API 与用户界面 |
| argocd-repo-server | 仓库服务 | 1 | 克隆、缓存、渲染 Git 仓库清单 |
| argocd-application-controller | 应用控制器 | 1 | 持续对比期望状态与集群实际状态 |
| argocd-redis | 缓存 | 1 | 存储应用状态与缓存数据 |
| argocd-dex-server | 身份认证(可选) | 1 | 集成 OIDC / LDAP 认证 |
架构要点: Argo CD 采用 Pull 模式——
argocd-repo-server 从 Git 仓库拉取清单,application-controller 对比集群状态并触发同步,argocd-server 对外暴露 API 与 UI。三个组件解耦,便于独立扩缩容。
4.3 获取初始管理员密码与 CLI 配置
安装完成后,Argo CD 自动生成管理员密码,存储在 Secret 中:
# 获取初始密码
kubectl -n argocd get secret argocd-initial-admin-secret \
-o jsonpath="{.data.password}" | base64 -d
# 下载并安装 Argo CD CLI
curl -sSL -o /usr/local/bin/argocd \
https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
chmod +x /usr/local/bin/argocd
# 登录 Argo CD(先暴露服务,见 4.4 节)
argocd login <ARGOCD_SERVER>:443 --username admin --password <PASSWORD> --insecure
安全提醒: 首次登录后立即修改默认密码:
argocd account update-password。生产环境务必禁用 --insecure 并配置 TLS 证书。
4.4 配置 Web UI 外部访问
Argo CD Server 默认以 ClusterIP 方式暴露。本课程提供三种访问方式,推荐使用 Ingress 方案:
| 方式 | 命令 / 配置 | 适用场景 |
|---|---|---|
| Port Forward | kubectl port-forward svc/argocd-server -n argocd 8080:443 |
本地开发 / 快速验证 |
| NodePort | kubectl patch svc argocd-server -n argocd -p '{"spec":{"type":"NodePort"}}' |
内网测试环境 |
| Ingress | 配置 Ingress 规则指向 argocd-server:443 |
生产环境 / 团队协作 |
Ingress 配置示例(nginx-ingress):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: argocd-ingress
namespace: argocd
annotations:
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
nginx.ingress.kubernetes.io/ssl-passthrough: "true"
spec:
ingressClassName: nginx
rules:
- host: argocd.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: argocd-server
port:
number: 443
SSL Passthrough: Argo CD 自身管理 TLS 证书,Ingress 应启用
ssl-passthrough 避免二次加密。如果使用 cert-manager,可关闭 ssl-passthrough 并配置 Ingress TLS 段。
4.5 配置 Git 仓库连接(SSH / HTTPS)
Argo CD 通过 argocd repo add 命令或 Web UI 添加 Git 仓库。本课程演示两种协议配置:
4.5.1 HTTPS 仓库连接(推荐用于快速开始)
# 添加公开 HTTPS 仓库(无需认证)
argocd repo add https://github.com/example/gitops-manifests.git \
--name my-helm-repo \
--type helm
# 添加私有 HTTPS 仓库(使用 Personal Access )
argocd repo add https://github.com/example/private-repo.git \
--username gitops-bot \
--password <GITHUB_PAT> \
--insecure-skip-server-verification
4.5.2 SSH 仓库连接(生产环境推荐)
# 生成专用 SSH 密钥(无密码短语)
ssh-keygen -t ed25519 -C "argocd@example.com" -f /tmp/argocd-ssh-key
# 将公钥添加到 Git 仓库的 Deploy Keys 中(GitHub / GitLab / Bitbucket)
# 添加 SSH 仓库
argocd repo add git@github.com:example/gitops-manifests.git \
--ssh-private-key-path /tmp/argocd-ssh-key \
--name production-repo
# 验证仓库连接状态
argocd repo list
| 对比维度 | HTTPS + PAT | SSH + Deploy Key |
|---|---|---|
| 认证方式 | Personal Access | SSH 密钥对 |
| 密钥轮换 | 需更新 Secret | 更新 Deploy Key 或密钥文件 |
| 网络限制 | 需开放 443 端口 | 需开放 22 端口 |
| 审计粒度 | 可绑定用户 | Deploy Key 只读仓库 |
| 推荐场景 | CI/CD 集成 / 快速原型 | 生产环境 / 安全合规 |
最佳实践: 生产环境使用 SSH + 专用 Deploy Key,密钥文件通过 Kubernetes Secret 挂载到
argocd-repo-server,避免明文存储。HTTPS 方式适合临时仓库或与 CI 系统集成。
4.6 Web UI 初探与核心界面导航
访问 https://argocd.example.com(或 Port Forward 地址),使用 admin 与初始密码登录。Web UI 主要功能区如下:
Web UI 核心操作路径:
- 应用列表:左侧面板展示所有 Application,通过颜色/图标快速识别同步状态(Synced / OutOfSync / Unknown)。
- 同步状态卡片:顶部展示当前选中应用的 Sync Status 与 Health Status,一目了然。
- 资源树:可视化展示 K8s 资源层级关系,支持点击查看 YAML 详情与事件日志。
- 操作按钮:SYNC 触发手动同步,ROLLBACK 进入回滚选择界面,REFRESH 强制重新拉取 Git 仓库。
- 设置入口:右上角 Settings 可管理仓库连接、项目 RBAC、通知配置等。
初探建议: 登录后先进入 Settings → Repositories 确认仓库连接状态为
Successful。然后创建一个测试 Application,观察从 Git 仓库到 K8s 集群的同步流程,理解 期望状态 → 实际状态 → 同步 的核心循环。
4.7 验证安装与仓库连接
完成上述步骤后,执行以下验证清单:
| 验证项 | 命令 / 操作 | 预期结果 |
|---|---|---|
| Pod 状态 | kubectl get pods -n argocd |
所有 Pod 处于 Running / Ready 状态 |
| CLI 登录 | argocd login <SERVER> --username admin |
返回 logged in successfully |
| 仓库列表 | argocd repo list |
显示已添加的仓库,STATUS 为 Successful |
| Web UI 访问 | 浏览器打开 https://argocd.example.com |
正常显示登录页面,登录后可见应用列表 |
| 仓库连接测试 | Web UI → Settings → Repositories → 点击仓库 → Test Connection | 返回 Connection successful |
排障提示: 如果仓库连接失败,首先检查
argocd-repo-server Pod 日志:kubectl logs -n argocd -l app.kubernetes.io/name=argocd-repo-server --tail=50。常见原因包括 SSH 密钥权限错误、Git 仓库地址拼写错误、网络策略阻止出站连接。