扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章将为大家详细讲解有关docker私有仓库如何搭建,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
成都创新互联是一家专注于成都网站设计、网站制作与策划设计,离石网站建设哪家好?成都创新互联做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:离石等地区。离石做网站价格咨询:13518219792查看docker-registry版本信息:
[root@docker-registry ~]# yum info docker-registry Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.bit.edu.cn * extras: mirror.bit.edu.cn * updates: mirror.bit.edu.cn Available Packages Name : docker-registry Arch : x86_64 Version : 0.9.1 Release : 7.el7 Size : 123 k Repo : extras/7/x86_64 Summary : Registry server for Docker URL : https://github.com/docker/docker-registry License : ASL 2.0 Description : Registry server for Docker (hosting/delivering of repositories and images).
安装docker-registry:
[root@docker-registry ~]# yum -y install docker-registry
查看安装后docker-distribution的的配置文件位置
[root@docker-registry ~]# rpm -ql docker-distribution /etc/docker-distribution/registry/config.yml /usr/bin/registry /usr/lib/systemd/system/docker-distribution.service /usr/share/doc/docker-distribution-2.6.2 /usr/share/doc/docker-distribution-2.6.2/AUTHORS /usr/share/doc/docker-distribution-2.6.2/CONTRIBUTING.md /usr/share/doc/docker-distribution-2.6.2/LICENSE /usr/share/doc/docker-distribution-2.6.2/MAINTAINERS /usr/share/doc/docker-distribution-2.6.2/README.md /var/lib/registry #数据放在这个目录下,可以修改/etc/docker-distribution/registry/config.yml改这个路径
查看配置文件:
[root@docker-registry ~]# cat /etc/docker-distribution/registry/config.yml version: 0.1 log: fields: service: registry storage: cache: layerinfo: inmemory filesystem: rootdirectory: /var/lib/registry #镜像存储位置,可以修改成自己的 http: addr: :5000 #监听端口为5000
启动服务:
[root@docker-registry ~]# systemctl start docker-distribution [root@docker-registry ~]# systemctl enable docker-distribution Created symlink from /etc/systemd/system/multi-user.target.wants/docker-distribution.service to /usr/lib/systemd/system/docker-distribution.service.
这样我们就装好了docker-registry。
下面我们测试,把node3机器上的镜像推到docker-registry机器上。
[root@k8s-node3 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7.22 6bb891430fb6 3 months ago 372 MB
我们准备把node3上的mysql:5.7.22镜像推到docker-registry机器上,需要先给node3机器上的镜像mysql打标签:
[root@k8s-node3 ~]# docker tag mysql:5.7.22 docker-registry:5000/mysql:5.7.22
[root@k8s-node3 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker-registry.com:5000/mysql 5.7.22 6bb891430fb6 3 months ago 372 MB
注意,docker-registry是主机名,要用hosts文件解析到对应的registryip地址上。
[root@k8s-node3 ~]# docker push docker-registry:5000/mysql:5.7.22 The push refers to a repository [docker-registry:5000/mysql] Get https://docker-registry:5000/v1/_ping: http: server gave HTTP response to HTTPS client
注意:如果上面写作docker-registry:5000/mysql,说明推送的是mysql顶级仓库下所有的镜像版本。
上面看到,我们docker push 时报错了,这是因为docker 客户端默认使用的https形式的,但是dockr registry server端是http形式的。
如果我们实在就用http的,那就需要对docker 客户端做如下修改:
[root@k8s-node3 ~]# vim /etc/docker/daemon.json { "registry-mirrors": ["https://registry.docker-cn.com"], "insecure-registries": ["docker-registry:5000"] }
注意,上面的dokcer-registry是主机名。
[root@k8s-node3 ~]# systemctl restart docker
然后再推就能推上去了。
[root@k8s-node3 ~]# docker push docker-registry:5000/mysql:5.7.22 The push refers to a repository [docker-registry:5000/mysql] a968f24d4187: Pushed f8cb294d5d80: Pushed 489bddb9c55e: Pushed 22b402e93939: Pushed 8aeebb3964c1: Pushed 94f8d8f5acbf: Pushed c0c26734fb83: Pushed 4801a487d51a: Pushed aae63f31dee9: Pushed 6f8d38b0e2b6: Pushed cdb3f9544e4c: Pushed 5.7.22: digest: sha256:1d3119703eb04855c971a9ec24646184444fa1bd889b201de3ce8904c35eb627 size: 2621
然后,我们登录到docker registry服务器,就能看到推送过来的镜像了:
[root@docker-registry ~]# ll /var/lib/registry/docker/registry/v2/repositories/mysql/ total 0 drwxr-xr-x. 3 root root 20 Oct 25 05:13 _layers drwxr-xr-x. 4 root root 35 Oct 25 05:14 _manifests drwxr-xr-x. 2 root root 6 Oct 25 05:14 _uploads
下面我们就让其他服务器从docker-registry服务器上下载镜像。
首先也需要在其他服务器上更改docker配置,加个"insecure-registries"参数,如下:
[root@k8s-node1 ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://registry.docker-cn.com"], "insecure-registries": ["docker-registry:5000"] }
[root@k8s-node1 ~]# systemctl restart docker
然后在这个机器上下载docker-registry机器上的镜像:
[root@k8s-node1 ~]# docker pull docker-registry:5000/mysql:5.7.22 5.7.22: Pulling from mysql 2da35ff30a7d: Pull complete 46459f75a599: Pull complete fe071c86fe94: Pull complete 75457c650197: Pull complete 6506db22c932: Pull complete a6e0a2acd728: Pull complete 3182738b1913: Pull complete ea75bfdf07be: Pull complete 6b85e8810885: Pull complete 5dca51ac89bd: Pull complete b3400d337f49: Pull complete Digest: sha256:1d3119703eb04855c971a9ec24646184444fa1bd889b201de3ce8904c35eb627 Status: Downloaded newer image for docker-registry:5000/mysql:5.7.22
[root@k8s-node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 05af71dd9251 8 days ago 463 MB docker-registry:5000/mysql 5.7.22 6bb891430fb6 3 months ago 372 MB
看到下载的镜像就是我们私有仓库里面的。
我们看到上面搭建的docker私有仓库是命令行界面的,很丑陋。不过,好消息是,目前有个开源项目叫harbor,是在docker registry基础上做的,并带了个漂亮的web界面,还支持冗余等。是个非常不错的项目。另外,CNCF组织也非常青睐harbor,可见harbor的前景非常不错。
可是,harbor的部署是非常麻烦的。还好,现在可以用docker compose(单机编排工具)来做harbor的安装。
下面我们准备安装harbor试一下。
官方项目地址是: https://github.com/goharbor/harbor
官方安装文档:https://github.com/goharbor/harbor/blob/master/docs/installation_guide.md
harbor官方要求配置为:
Software | Version | Description |
---|---|---|
Python | version 2.7 or higher | Note that you may have to install Python on Linux distributions (Gentoo, Arch) that do not come with a Python interpreter installed by default |
Docker engine | version 1.10 or higher | For installation instructions, please refer to: https://docs.docker.com/engine/installation/ |
Docker Compose | version 1.6.0 or higher | For installation instructions, please refer to: https://docs.docker.com/compose/install/ |
Openssl | latest is preferred | Generate certificate and keys for Harbor |
先安装epel源。
[root@harbor yum.repos.d]# cd /etc/yum.repos.d/ [root@harbor yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
看epel里面的docker-compose版本情况:
[root@docker-registry yum.repos.d]# yum info docker-compose epel 12742/12742 Available Packages Name : docker-compose Arch : noarch Version : 1.18.0 Release : 1.el7 Size : 226 k Repo : epel/x86_64 Summary : Multi-container orchestration for Docker URL : https://github.com/docker/compose License : ASL 2.0
登录 https://github.com/goharbor/harbor/releases下载harbor。
我们下载二进制的安装包:
[root@harbor ~]# wget https://storage.googleapis.com/harbor-releases/harbor-offline-installer-v1.6.1.tgz
[root@harbor ~]# tar -xvf harbor-offline-installer-v1.6.1.tgz -C /usr/local/
编辑配置文件:
[root@harbor ~]# cd /usr/local/harbor/
[root@docker-registry harbor]# vim harbor.cfg hostname = 172.16.22.196 #harbor服务器的名称,可以是IP地址(本机ip),或者是完整的域名,不要使用localhost或者127.0.0.1,因为服务需要被其他的机器访问 ui_url_protocol = http max_job_workers = 10 #小于操作系统的cpu个数 harbor_admin_password = Harbor12345 #admin的密码
停止我们前面安装的docker-distribution
[root@harbor harbor]# systemctl stop docker-distribution
安装docker-compose
[root@docker-registry harbor]# yum install docker-compose
安装docker:
[root@harbor harbor]#yum intall docker-ce [root@harbor harbor]# systemctl start docker
安装harbor:
[root@harbor harbor]# ./prepare [root@harbor harbor]# ./install.sh [Step 0]: checking installation environment ... Note: docker version: 18.06.1 Note: docker-compose version: 1.18.0
安装过程时间比较长,原因是它需要展开harbor.v1.6.1.tar.gz镜像,并安装这些镜像。
安装完后,访问 http://172.16.22.196,默认用户名admin,密码Harbor12345
登录后,我们建立一个普通用户:
创建一个项目:
备注:上面的复制管理就是用来给harbor做主从复制的。
然后以普通用户登录,并创建一个项目:
我们登录另外一个机器node03当做客户端,然后在这个机器上修改客户端以http方式访问harbor server(我这里ip是172.16.22.196):
[root@k8s-node3 ~]# vim /etc/docker/daemon.json { "registry-mirrors": ["https://registry.docker-cn.com"], "insecure-registries": ["172.16.22.196"] }
[root@k8s-node3 ~]# systemctl daemon-reload [root@k8s-node3 ~]# systemctl restart docker
然后给node3的镜像打标签,打成harbor主机名形式:
[root@k8s-node3 ~]# docker tag mysql:5.7.22 172.16.22.196/dev/mysql:5.7.22
在node3机器上,登录docker-registry(172.16.22.196)
[root@k8s-node3 ~]# docker login 172.16.22.196 Username: chenzhixin Password: Login Succeeded
把node3上的镜像推送到harbor server172.16.22.196上:
[root@k8s-node3 ~]# docker push 172.16.22.196/dev/mysql #不加标签标示把mysql下所有标签的镜像都推送到harbor上 The push refers to a repository [172.16.22.196/dev/mysql] a968f24d4187: Pushed f8cb294d5d80: Pushed 489bddb9c55e: Pushed 22b402e93939: Pushed 8aeebb3964c1: Pushed 94f8d8f5acbf: Pushed c0c26734fb83: Pushed 4801a487d51a: Pushed aae63f31dee9: Pushed 6f8d38b0e2b6: Pushed cdb3f9544e4c: Pushed 5.7.22: digest: sha256:1d3119703eb04855c971a9ec24646184444fa1bd889b201de3ce8904c35eb627 size: 2621
然后我们就能在harbor server上看到我们推送的镜像了:
Harbor容器的stop与start:
进入Harbor目录执行如下命令即可: cd /usr/local/harbor docker-compose stop/start
其他补充知识:
/usr/local/harbor/docker-compose.yml :这个文件里面定义了数据存放的目录是:/data
[root@harbor harbor]# ls /data/ ca_download config database job_logs psc redis registry secretkey
关于“docker私有仓库如何搭建”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流