实战指南:在Ubuntu上部署Nexus并配置为Docker私有镜像中心
1. 为什么需要私有Docker镜像仓库
当你所在团队规模超过5人时,就会开始感受到公共镜像仓库的种种不便。我经历过最夸张的情况是:全团队在上午10点同时执行CI构建时,因为大量拉取Docker Hub镜像导致IP被临时封禁。更不用说那些动辄500MB的生产镜像,跨国传输时经常中断重试的糟糕体验。
私有镜像仓库就像在公司内部搭建的"超市货架",它能带来三个核心价值:
- 速度飞跃:千兆内网传输比外网快10倍不止,一个1GB的镜像从拉取到完成只需20秒
- 安全管控:敏感镜像不再暴露在公网,结合企业AD域账号实现权限精细化管理
- 成本优化:避免因频繁拉取公有镜像产生云服务费用(Docker Hub匿名用户限流200次/6小时)
Nexus Repository作为老牌制品库工具,其Docker仓库功能经过我们三年生产环境验证,单节点可稳定支持:
- 日均3000+次镜像推送/拉取操作
- 50TB+二进制存储容量
- 完善的垃圾回收机制
2. 部署前的环境准备
2.1 硬件配置建议
根据我们为20+企业部署的经验,推荐以下配置基准线:
| 团队规模 | CPU核心 | 内存 | 存储空间 | 预期承载量 |
|---|---|---|---|---|
| 10人以下 | 4核 | 8GB | 500GB | 日均500次操作 |
| 30人团队 | 8核 | 16GB | 2TB | 日均2000次操作 |
| 100人以上 | 16核 | 32GB | 5TB+ | 需集群部署 |
关键提示:存储务必选用SSD!机械硬盘在并发上传时会成为性能瓶颈。我们曾有个客户使用HDD,推送镜像时经常超时,换成NVMe SSD后速度提升8倍。
2.2 系统环境配置
以Ubuntu 22.04 LTS为例,执行以下初始化操作:
# 更新系统
sudo apt update && sudo apt upgrade -y
# 安装必要工具
sudo apt install -y openjdk-17-jre-headless docker.io
# 配置Docker加速器(国内用户建议)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://registry.cn-hangzhou.aliyuncs.com"]
}
EOF
sudo systemctl restart docker
避坑指南:遇到过三次因为系统locale未设置导致Nexus启动异常的情况。务必执行:
sudo locale-gen en_US.UTF-8
export LANG=en_US.UTF-8
3. 两种部署方式详解
3.1 原生安装方案
下载最新版Nexus(当前为3.87.1):
cd /opt
sudo wget https://download.sonatype.com/repository/downloads-prod-group/3/nexus-3.87.1-01-linux-x86_64.tar.gz
sudo tar xvzf nexus-3.87.1-01-linux-x86_64.tar.gz
sudo ln -s nexus-3.87.1-01 nexus
创建专用系统服务:
sudo useradd -r -s /bin/false nexus
sudo chown -R nexus:nexus /opt/nexus /opt/sonatype-work
# 创建systemd服务
sudo tee /etc/systemd/system/nexus.service <<-'EOF'
[Unit]
Description=Nexus Service
After=network.target
[Service]
Type=forking
User=nexus
ExecStart=/opt/nexus/bin/nexus start
ExecStop=/opt/nexus/bin/nexus stop
Restart=on-abort
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable --now nexus
性能调优:编辑/opt/nexus/bin/nexus.vmoptions,根据服务器内存调整:
-Xms4g
-Xmx4g
-XX:MaxDirectMemorySize=2g
3.2 Docker容器化部署
更适合快速试用的方案:
# 创建数据卷
sudo mkdir -p /data/nexus-data && sudo chown -R 200:200 /data/nexus-data
# 启动容器(注意端口映射)
docker run -d \
-p 8081:8081 \
-p 18444:18444 \
--name nexus \
-v /data/nexus-data:/nexus-data \
sonatype/nexus3:3.87.1
重要安全配置:默认admin密码位于容器内/nexus-data/admin.password,获取方式:
docker exec nexus cat /nexus-data/admin.password
4. 仓库配置实战技巧
4.1 三大仓库类型深度解析
通过实际案例理解不同仓库类型的组合用法:
-
Hosted仓库(私有仓库)
- 命名规范:建议按环境划分如
docker-dev/docker-prod - 配置关键参数:
{ "httpPort": 18444, "allowAnonymousPull": false, "storage": { "blobStoreName": "default", "strictContentTypeValidation": true } }
- 命名规范:建议按环境划分如
-
Proxy仓库(代理仓库)
- 国内推荐代理源:
- 阿里云:
https://registry.cn-hangzhou.aliyuncs.com - 华为云:
https://swr.cn-east-2.myhuaweicloud.com
- 阿里云:
- 缓存策略设置:
"proxy": { "remoteUrl": "https://registry-1.docker.io", "contentMaxAge": 1440, "metadataMaxAge": 1440 }
- 国内推荐代理源:
-
Group仓库(聚合仓库)
- 典型组合方案:
graph LR A[docker-all] --> B[docker-internal] A --> C[aliyun-proxy] A --> D[dockerhub-proxy] - 成员顺序原则:
- 自有Hosted仓库
- 稳定代理源
- 备用代理源
- 典型组合方案:
4.2 权限控制最佳实践
基于RBAC的权限方案示例:
-
创建角色:
- 开发者:push/pull权限
- 运维:cleanup权限
- 只读用户:pull权限
-
关联企业AD/LDAP(关键步骤):
"ldap": { "host": "ldap.yourcompany.com", "port": 389, "searchBase": "ou=users,dc=yourcompany,dc=com", "authScheme": "simple" }
5. Docker客户端集成
5.1 全局配置方案
修改/etc/docker/daemon.json:
{
"insecure-registries": ["nexus.yourcompany.com:18444"],
"registry-mirrors": ["https://nexus.yourcompany.com:18444"]
}
重启服务:
sudo systemctl restart docker
5.2 项目级配置
在CI/CD管道中推荐使用登录方式:
docker login nexus.yourcompany.com:18444 \
-u ${NEXUS_USER} \
-p ${NEXUS_PASSWORD}
镜像命名规范:
# 推送示例
docker tag nginx:alpine nexus.yourcompany.com:18444/base/nginx:1.23-alpine
docker push nexus.yourcompany.com:18444/base/nginx:1.23-alpine
# 拉取示例
docker pull nexus.yourcompany.com:18444/base/nginx:1.23-alpine
6. 维护与监控
6.1 日常维护命令
查看存储状态:
curl -u admin:yourpassword \
-X GET \
"http://localhost:8081/service/rest/v1/blobstores"
手动触发清理任务:
curl -u admin:yourpassword \
-X POST \
"http://localhost:8081/service/rest/v1/tasks/docker-cleanup/run"
6.2 性能监控指标
关键监控项:
blobstore.blob.count:存储对象数量docker.requests:API请求量jvm.memory.used:JVM内存使用
Prometheus监控配置示例:
scrape_configs:
- job_name: 'nexus'
metrics_path: '/service/metrics/prometheus'
static_configs:
- targets: ['nexus.yourcompany.com:8081']
basic_auth:
username: admin
password: yourpassword
7. 故障排查经验
常见问题1:推送镜像时报413 Request Entity Too Large
- 解决方案:调整Nginx代理配置
client_max_body_size 2G; proxy_read_timeout 600s;
常见问题2:拉取镜像时认证失败
- 检查步骤:
- 确认
docker login凭证存在~/.docker/config.json - 验证Nexus用户是否有对应仓库的pull权限
- 检查仓库是否配置了
Docker Bearer Token Realm
- 确认
性能问题:镜像推送速度慢
- 优化方向:
- 检查网络带宽(推荐内网万兆)
- 调整JVM参数(增加Xmx值)
- 检查存储IO性能(使用
fio工具测试)
在最后分享一个真实案例:某金融客户因为未配置存储清理策略,导致2TB磁盘三个月被占满。建议设置自动清理策略:
- 创建Cleanup Policy
- 设置保留最近10个版本
- 排除
latest标签 - 每周日凌晨2点执行
更多推荐
所有评论(0)