服务器环境
- centos7 x86_64
服务器软件
nexus
用来搭建内网registry的代理服务器和内网的registry
nginx
用来做HTTPS代理
docker
镜像的下载和私有镜像库的访问(login,pull,push)
nexus部署
下载最新的版本的nexus
,本次使用的是nexus-3.13.0-01-unix.tar.gz
cd /data/software
tar -zxvf nexus-3.13.0-01-unix.tar.gz
useradd nexus
chown -R nexus.nexus nexus-3.13.0-01
mkdir sonatype-work
chown -R nexus.nexus sonatype-work
设置启动服务:
cat >> /lib/systemd/system/nexus.service<<EOF
[Unit]
Description=nexus service
After=network.target
[Service]
Type=forking
ExecStart=/data/software/nexus/bin/nexus start
ExecStop=/data/software/nexus/bin/nexus stop
#User=root
User=nexus
Restart=on-abort
[Install]
WantedBy=multi-user.target
<<EOF
systemctl enable nexus
systemctl start nexus
查询运行状态:
监听端口在8081,如果想要修改监听端口可以在nexus的etc目录下找到nexus-default.properties
访问WEB UI:
出现以下的界面表示启动成功
nexus配置和使用
nexus可以作用yum的私库,maven私库、nuget等,可以参见官方说明,这里只是针对docker的私有镜像进行配置
-
私有镜像库(host repository)的配置和访问
注意blob store需要提供比较大的空间
创建后会在9990端口上提供服务,然后配置NGINX支持HTTPS的访问
-
代理镜像库(proxy repository)的配置和访问
在内网总是有下载hub.docker.com 的需求,可以通过配置nexus的代理模式来实现
使用管理员登陆,默认账号 admin/admin123
- 添加新的repository
图中 "Blob store"建议自己配置为比较大的磁盘分区上,避免空间不足
点击"create repository"后可以查看是否在9991(可以在创建时进行调整)端口进行监听 因为docker访问镜像库使用的是HTTPS,需要在NGXIN服务器上配置HTTPS的方向代理,指向9991端口。
nginx 配置
Nginx的安装可以参见互联网上的其他文章,这里主要是配置如果通过HTTPS的方向代理去访问docker的私有registry,
本例中的docker使用的版本是18.03.1-ce,在进行login登陆时要求后端的registry需要采用HTTPS的方式
- 配置proxy repository 的HTTPS访问
在nginx开启9991端口的HTTPS的监听,将请求指向nexus服务器的9991端口.配置完成后reload Nginx服务器
/usr/local/nginx/sbin/nginx -t #检查配置是否有错误
/usr/local/nginx/sbin/nginx -s reload #重新加载配置
- 配置host repository的HTTPS访问
使用方法
代理(registry)的使用方法
docker login mobile.chinasws.com:9991 #登陆
需要输入创建repository时指定的账号和密码 登陆成功后可以pull远程的镜像
docker pull mobile.chinasws.com:9991/nginx # 拉取DockerHub上的nginx镜像
在使用时需要注意,拉取外网的image时都需要将mobile.chinasws.com:9991追加载镜像前面
私有registry的使用方法
docker login mobile.chinasws.com:9990 #登陆
需要输入创建repository时指定的账号和密码 登陆成功后可以pull私有库的镜像
docker pull mobile.chinasws.com:9990/nginx # 拉取mobile.chinasws.com:9990上的nginx镜像,如果没有则拉取失败
提交本地image到mobile.chinasws.com:9990
docker tag imageid mobile.chinasws.com:9990/imageid:tag
docker push mobile.chinasws.com:9990/imageid:tag
小结:
工欲善其事,必先利其器,nexus提供了比较多的支持,搭建内网的docker私有镜像库和公用镜像的proxy为后续docker的应用部署提供基础。下一篇会介绍基于docker swarm mode搭建swarm的原生集群.会在下一篇中看到如何使用potainer进行docker 集群的管理和监控。potainer的部署就会引用到nexus搭建好的代理镜像库(在内网环境中通过代理镜像库拉去DockerHub上的potainer镜像).