主题
Nexus Repository Manager 3 - K8s 高可用部署
概述
在 RKE2 集群 (192.168.122.31, 6节点) 上部署 Nexus 3,采用 Active-Passive 高可用模式。
⚠️ 重要说明
Nexus OSS 不支持真正的 Active-Active HA (需要 Pro 商业版)。本方案采用 Active-Passive 模式:
- 2 个 Pod 分布在不同节点 (Pod Anti-Affinity)
- 共享 NFS 存储 (ReadWriteMany)
- 使用
flock文件锁:Active 持有锁运行 Nexus,Standby 安静等待(不崩溃) - Active 故障时,Standby 自动获取锁并接管(10-30 秒检测 + 2 分钟启动)
文件清单
| 文件 | 说明 |
|---|---|
nexus-ha-deploy.yaml | K8s HA 部署清单 |
sync-image.sh | 镜像同步脚本 |
deploy.sh | 一键部署脚本 |
README.md | 本文档 |
部署步骤
1. 同步镜像
bash
bash sync-image.sh
# 镜像约 500MB, 预计 5-15 分钟手动同步:
bash
skopeo copy --src-tls-verify=false --dest-tls-verify=false \
--dest-creds "admin:xxx" \
docker://docker.1ms.run/sonatype/nexus3:3.95.2 \
docker://192.168.122.156:30000/sonatype/nexus3:3.95.22. 部署
bash
bash deploy.sh
# 或仅部署 K8s 资源:
KUBECONFIG=/home/xxx/.kube/config-122.31 kubectl apply -f nexus-ha-deploy.yaml3. 验证
bash
KUBECONFIG=/home/xxx/.kube/config-122.31 kubectl -n nexus get pods -o wide
# nexus-0: Ready 1/1 (Active)
# nexus-1: Ready 0/1, Restarts>0 (Standby, 正常现象)访问信息
| 服务 | 地址 |
|---|---|
| Web UI | http://192.168.122.31:30881 |
| Docker 仓库 | 192.168.122.31:30882 |
默认登录凭据:
- 用户名: xxx
- 密码: xxx
⚠️ 重要: 首次登录后请立即修改密码!
(因为设置了 `-Dnexus.security.randompassword=xxx
高可用行为
- 正常运行: nexus-0 Active (Ready 1/1, Restarts 0), nexus-1 Standby (Ready 0/1, Restarts 0, 安静等待)
- 故障切换: Active 故障 → 锁释放 → Standby 检测到锁 → 启动 Nexus (约 2-3 分钟)
- 手动切换:
kubectl -n nexus delete pod nexus-0
常用命令
bash
kubectl -n nexus get pods -o wide # 查看状态
kubectl -n nexus logs -f nexus-0 # Active 日志
kubectl -n nexus logs -f nexus-1 # Standby 日志
kubectl -n nexus delete pod nexus-0 # 触发切换
kubectl -n nexus get pvc # 存储状态清理
bash
kubectl delete -f nexus-ha-deploy.yaml
kubectl -n nexus delete pvc/nexus-data-shared配置概要
| 项目 | 值 |
|---|---|
| 镜像 | 192.168.122.156:30000/sonatype/nexus3:3.95.2 |
| JVM | Xms=1.5G, Xmx=1.5G, Direct=1.5G |
| 存储 | 30Gi NFS (ReadWriteMany) |
| CPU | 0.5~2 cores |
| Memory | 2.5~4Gi |
| 健康检查 | /service/rest/v1/status |
| Anti-Affinity | requiredDuringScheduling (强制分布不同节点) |
| PDB | minAvailable: 1 |
FAQ
Q: Standby Pod 显示 Ready 0/1 但 Restarts 0,正常吗? A: 正常。Standby 使用包装脚本安静等待锁,不会崩溃重启。
Q: 如何确认哪个是 Active? A: Ready=1/1 的是 Active,Ready=0/1 的是 Standby。
Q: 故障切换需要多久? A: 通常 2-3 分钟(10-30 秒检测间隔 + 约 2 分钟 Nexus 启动时间)。