一、前言
当我们制作好了自己的镜像后,为了在其他服务器上可以使用该镜像,我们需要把镜像推送(push)到仓库(Repository),然后在其他服务器上拉取(pull)该镜像。仓库有分公共仓库和私有仓库,如果要在公司中使用Docker,基本上我们不太可能会把商业项目的镜像上传到公共仓库中,所以私有仓库也就有了用武之地,它能避免商业项目暴露出去的风险。
提供仓库服务主要有3种方式:
- Docker Hub - Docker官方维护的一个仓库,默认只提供公开仓库,私人仓库是收费模式,最低是7美元/月提供5个私人仓库
- Registry镜像 - Docker官方镜像,此镜像包含了Docker Registry HTTP API V2的实现,适用于Docker 1.6及1.6以上版本
- Harbor - VMware公司开源的企业级Registry项目
二、Registry镜像
通过Registry镜像来搭建私人仓库,使用了2台服务器来演示,一台作为仓库,一台作为客户端
2.1 在192.168.6.92服务器
拉取镜像
docker pull registry
运行容器
docker run -p 80:5000 -itd --name hub registry
2.2 在192.168.6.91服务器
由于在192.168.6.92服务器运行的不是HTTPS协议,push镜像给92服务器时会出现如下提示:
Get https://192.168.6.92:80/v2/: http: server gave HTTP response to HTTPS client
所以需要修改91服务器的/lib/systemd/system/docker.service文件,增加启动参数--insecure-registry=192.168.6.92:80
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
改为
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry=192.168.6.92:80
然后重启Docker服务
systemctl daemon-reload
systemctl restart docker
现在将《Docker篇 (一) 制作镜像-Tomcat开启APR模式》文章中创建的镜像推送到服务器
#格式 <registry_ip>:<registry_port>/<image_name>:<image_tag>
docker tag anron 192.168.6.92:80/anron:v1.0
docker push 192.168.6.92:80/anron:v1.0
查看镜像是否推送成功
[root@docker1 imagebuilder]# curl http://192.168.6.92:80/v2/_catalog
{"repositories":["anron"]}
[root@docker1 imagebuilder]# curl http://192.168.6.92:80/v2/anron/tags/list
{"name":"anron","tags":["v1.0"]}
推送成功后在其他服务器通过下面的命令就可以拉取镜像了
docker pull 192.168.6.92:80/anron:v1.0
三、Harbor
Harbor是一个用于存储和分发Docker镜像的企业级Registry服务,是VMware公司开源的企业级Registry项目,基于Docker Registry开发的,它除了提供友好的Web管理界面、角色和用户权限管理、用户操作审计、多个Registry节点的镜像复制等功能外,它还可以整合K8s的插件(管理大量的服务器时,手动在每台服务器上去输入命令管理大量的容器是不现实的,K8s提供了更高级更灵活的管理方式)。
3.1 安装Docker-compose
检查是否安装了pip
[root@docker1 ~]# pip --version
pip 19.1.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)
如果没有,就用以下命令安装pip
yum -y install python-pip
安装pip时如果提示:No package python-pip available,需要先安装epel,阿里的镜像默认是有epel的,如果自己下载CentOS.iso安装默认是没有epel,epel (Extra Packages for Enterprise Linux,企业版Linux的额外软件包) 是Fedora小组维护的一个软件仓库项目,为RHEL/CentOS提供他们自带源上默认不提供的软件包。
yum -y install epel-release
然后用pip命令安装Docker-compose
pip install docker-compose==1.25.0
指定1.25.0的版本来安装,从Docker官网上可以看到Docker-compose有时迭代很快,实测结果是最新的版本安装后用docker-composer --version查看版本号时有以下异常信息:
ImportError: No module named configparser
如出现No module提示,也可以尝试下pip uninstall <模块名称> 再pip install <模块名称>
/usr/lib/python2.7/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.25.3) or chardet (2.2.1) doesn't match a supported version!
如果出现以上错误信息,python库中urllib3或chardet的版本不兼容,卸载掉urllib3和chardet后,重新安装就可以了
pip uninstall urllib3 -y
pip uninstall chardet -y
pip install requests
NameError: name 'platform_system' is not defined
如果出现以上异常信息,升级下pip和setuptools
pip install --upgrade pip
pip install --upgrade setuptools
posixsubprocess.c:16:20: fatal error: Python.h: No such file or directory
#include "Python.h"
compilation terminated.
error: command 'gcc' failed with exit status 1
如出现以上错误,无法找到python库,#include "Python.h"出错时需要安装python-devel
yum install python-devel
error: no acceptable C compiler found in $PATH
如果出现以上错误信息,安装C编译器
yum -y install gcc
3.2 安装Harbor
把harbor-offline-installer-v1.7.0.tgz文件下载后上传到服务器,解压后先修改harbor.cfg文件中的hostname和harbor_admin_password,然后执行./install.sh脚本就会自动安装完成
harbor-offline-installer-v1.7.0.tgz可在我的网盘下载,https://pan.baidu.com/s/13R58C1acmxB6koZUA4H2iQ,提取码el6o
[root@docker1 harbor]# ./install.sh
[Step 0]: checking installation environment ...
Note: docker version: 19.03.8
Note: docker-compose version: 1.25.0
[Step 1]: loading Harbor images ...
Loaded image: goharbor/harbor-adminserver:v1.7.0
Loaded image: goharbor/harbor-registryctl:v1.7.0
Loaded image: goharbor/redis-photon:v1.7.0
Loaded image: goharbor/chartmuseum-photon:v0.7.1-v1.7.0
Loaded image: goharbor/harbor-portal:v1.7.0
Loaded image: goharbor/harbor-jobservice:v1.7.0
Loaded image: goharbor/nginx-photon:v1.7.0
Loaded image: goharbor/harbor-db:v1.7.0
Loaded image: goharbor/notary-server-photon:v0.6.1-v1.7.0
Loaded image: goharbor/notary-signer-photon:v0.6.1-v1.7.0
Loaded image: goharbor/clair-photon:v2.0.7-v1.7.0
Loaded image: goharbor/harbor-migrator:v1.7.0
Loaded image: goharbor/harbor-core:v1.7.0
Loaded image: goharbor/harbor-log:v1.7.0
Loaded image: goharbor/registry-photon:v2.6.2-v1.7.0
[Step 2]: preparing environment ...
Generated and saved secret to file: /data/secretkey
Generated configuration file: ./common/config/nginx/nginx.conf
Generated configuration file: ./common/config/adminserver/env
Generated configuration file: ./common/config/core/env
Generated configuration file: ./common/config/registry/config.yml
Generated configuration file: ./common/config/db/env
Generated configuration file: ./common/config/jobservice/env
Generated configuration file: ./common/config/jobservice/config.yml
Generated configuration file: ./common/config/log/logrotate.conf
Generated configuration file: ./common/config/registryctl/env
Generated configuration file: ./common/config/core/app.conf
Generated certificate, key file: ./common/config/core/private_key.pem, cert file: ./common/config/registry/root.crt
The configuration files are ready, please use docker-compose to start the service.
[Step 3]: checking existing instance of Harbor ...
[Step 4]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating redis ... done
Creating registry ... done
Creating registryctl ... done
Creating harbor-db ... done
Creating harbor-adminserver ... done
Creating harbor-core ... done
Creating harbor-jobservice ... done
Creating harbor-portal ... done
Creating nginx ... done
✔ ----Harbor has been installed and started successfully.----
Now you should be able to visit the admin portal at http://47.112.184.165.
For more details, please visit https://github.com/goharbor/harbor .
列出项目中所有的容器
[root@docker1 harbor]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------------------------------------------------------
harbor-adminserver /harbor/start.sh Up (healthy)
harbor-core /harbor/start.sh Up (healthy)
harbor-db /entrypoint.sh postgres Up (healthy) 5432/tcp
harbor-jobservice /harbor/start.sh Up
harbor-log /bin/sh -c /usr/local/bin/ ... Up (healthy) 127.0.0.1:1514->10514/tcp
harbor-portal nginx -g daemon off; Up (healthy) 80/tcp
nginx nginx -g daemon off; Up (healthy) 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp, 0.0.0.0:80->80/tcp
redis docker-entrypoint.sh redis ... Up 6379/tcp
registry /entrypoint.sh /etc/regist ... Up (healthy) 5000/tcp
registryctl /harbor/start.sh Up (healthy)
3.3 使用Harbor
先在Harbor管理界面中创建用户-->新建项目-->在项目中添加成员,然后用该成员在其他服务器推送镜像,推送前先要用以下命令登录
docker login IP或域名 -u 用户名
3.4 使用https协议
当Harbor服务器安装在阿里云的SLB或是华为云的ELB的后端,在负载均衡中配置好443->80和80->80时,其他服务器中的Docker使用https协议push镜像时会出现login成功,但是push还是提示未授权,查看Harbor中的log则显示401
unauthorized: authentication required
解决方式为,修改Harbor服务器下common/config/nginx/nginx.conf文件
proxy_set_header X-Forwarded-Proto $scheme;
改为
proxy_set_header X-Forwarded-Proto https;
然后重启Harbor服务
docker-compose down
docker-compose up -d