Docker Private Registry

Docker Registry

网上有很多的 Registry 服务器都支持第三方用户注册,而后基于用户名去做自己的仓库,但是使用互联网上的 Registry 有一个缺陷,那就是我们去推送和下载镜像时都不会很快,而在生产环境中很可能并行启动的容器将达到几十、上百个,而且很有可能每个服务器本地是没有镜像的,此时如果通过互联网去下载镜像会有很多问题,比如下载速度会很慢、带宽会用很多等等,如果带宽不够的话,下载至启动这个过程可能要持续个几十分钟,这已然违背了使用容器会更加轻量、快速的初衷和目的。因此,很多时候我们很有可能需要去做自己的私有 Registry。

Registry 用于保存 docker 镜像,包括镜像的层次结构和元数据。用户可以自建 Registry,也可以使用官方的 Docker Hub。

Docker Registry 分类:

  • Sponsor Registry:第三方的 Registry,供客户和 Docker 社区使用
  • Mirror Registry:第三方的 Registry,只让客户使用
  • Vendor Registry:由发布 docker 镜像的供应商提供的 registry
  • Private Registry:通过设有防火墙和额外的安全层的私有实体提供的 registry

事实上,如果运维的系统环境托管在云计算服务上,比如阿里云,那么用阿里云的 Registry 则是最好的选择。很多时候我们的生产环境不会在本地,而是托管在数据中心机房里,如果我们在数据中心机房里的某台主机上部署 Registry,因为都在同一机房,所以属于同一局域网,此时数据传输走内网,效率会极大的提升。

所有的 Registry 默认情况下都是基于 https 工作的,这是 Docker 的基本要求,而我自建 Registry 时很可能是基于 http 工作的,但是 Docker 默认是拒绝使用 http 提供 Registry 服务的,除非明确的告诉它,我们就是要用 http 协议的 Registry。

Docker Private Registry

为了帮助我们快速创建私有 Registry,Docker 专门提供了一个名为 Docker Distribution 的软件包,我们可以通过安装这个软件包快速构建私有仓库。

问:既然 Docker 是为了运行程序的,Docker Distribution 能否运行在容器中?

容器时代,任何程序都应该运行在容器中,除了 Kernel 和 init。而为了能够做 Docker Private Registry,Docker Hub 官方直接把 Registry 做成了镜像,我们可以直接将其 pull 到本地并启动为容器即可快速实现私有 Registry。

Registry 的主要作用是托管镜像,Registry 运行在容器中,而容器自己的文件系统是随着容器的生命周期终止和删除而被删除的,所以当我们把 Registry 运行在容器中时,客户端上传了很多镜像,随着 Registry 容器的终止并删除,所有镜像都将化为乌有,因此这些镜像应该放在存储卷上,而且这个存储卷最好不要放在 Docker 主机本地,而应该放在一个网络共享存储上,比如 NFS。不过,镜像文件自己定义的存储卷,还是一个放在 Docker 本地、Docker 管理的卷,我们可以手动的将其改成使用其它文件系统的存储卷。

这就是使用容器来运行 Registry 的一种简单方式。自建 Registry 的另一种方式,就是直接安装 docker-distribution 软件。

使用 docker-distribution 自建 Registry

搭建 Registry:

[root@localhost ~]# yum install -y  http://mirror.centos.org/centos/7/extras/x86_64/Packages/docker-distribution-2.6.2-2.git48294d9.el7.x86_64.rpm

[root@localhost ~]# 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
    
[root@localhost ~]# systemctl start docker-distribution.service 

[root@localhost ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port     Peer Address:Port  Process  
LISTEN  0       128            0.0.0.0:22            0.0.0.0:*              
LISTEN  0       128               [::]:22               [::]:*              
LISTEN  0       128                  *:2375                *:*              
LISTEN  0       128                  *:5000                *:*              

使用自建的 Registry 去上传镜像:

[root@localhost ~]# vim /etc/docker/daemon.json 
......
  "insecure-registries":["192.168.91.128:5000"]

[root@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl restart docker

[root@localhost ~]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
centos-httpd   v2        40d9de9e74c7   4 days ago      678MB
centos-httpd   v1        3b5b771c3f29   4 days ago      678MB
busybox        latest    beae173ccac6   7 months ago    1.24MB
httpd          latest    dabbfbe0c57b   7 months ago    144MB
centos         latest    5d0da3dc9764   10 months ago   231MB

[root@localhost ~]# docker tag busybox:latest 192.168.91.128:5000/busybox:v1[root@localhost ~]# docker images
REPOSITORY                    TAG       IMAGE ID       CREATED         SIZE
centos-httpd                  v2        40d9de9e74c7   4 days ago      678MB
centos-httpd                  v1        3b5b771c3f29   4 days ago      678MB
192.168.91.128:5000/busybox   v1        beae173ccac6   7 months ago    1.24MB
busybox                       latest    beae173ccac6   7 months ago    1.24MB
httpd                         latest    dabbfbe0c57b   7 months ago    144MB
centos                        latest    5d0da3dc9764   10 months ago   231MB

[root@localhost ~]# docker push 192.168.91.128:5000/busybox:v1 
The push refers to repository [192.168.91.128:5000/busybox]
01fd6df81c8e: Pushed 
v1: digest: sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee size: 527

[root@localhost ~]# docker rmi 192.168.91.128:5000/busybox:v1 
Untagged: 192.168.91.128:5000/busybox:v1
Untagged: 192.168.91.128:5000/busybox@sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee
[root@localhost ~]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
centos-httpd   v2        40d9de9e74c7   4 days ago      678MB
centos-httpd   v1        3b5b771c3f29   4 days ago      678MB
busybox        latest    beae173ccac6   7 months ago    1.24MB
httpd          latest    dabbfbe0c57b   7 months ago    144MB
centos         latest    5d0da3dc9764   10 months ago   231MB

[root@localhost ~]# docker pull 192.168.91.128:5000/busybox:v1 
v1: Pulling from busybox
Digest: sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee
Status: Downloaded newer image for 192.168.91.128:5000/busybox:v1
192.168.91.128:5000/busybox:v1
[root@localhost ~]# docker images
REPOSITORY                    TAG       IMAGE ID       CREATED         SIZE
centos-httpd                  v2        40d9de9e74c7   4 days ago      678MB
centos-httpd                  v1        3b5b771c3f29   4 days ago      678MB
busybox                       latest    beae173ccac6   7 months ago    1.24MB
192.168.91.128:5000/busybox   v1        beae173ccac6   7 months ago    1.24MB
httpd                         latest    dabbfbe0c57b   7 months ago    144MB
centos                        latest    5d0da3dc9764   10 months ago   231MB

[root@localhost ~]# curl http://192.168.91.128:5000/v2/_catalog
{"repositories":["busybox"]}
[root@localhost ~]# curl http://192.168.91.128:5000/v2/busybox/tags/list
{"name":"busybox","tags":["v1"]}

使用官方镜像自建 Registry

先停止 docker-distribution 服务

[root@localhost ~]# vim /etc/docker/daemon.json 
......
  "insecure-registries":["192.168.91.128:5000"]

[root@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl restart docker

[root@localhost ~]# docker search registry
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
registry                        The Docker Registry 2.0 implementation for s…   3649      [OK]       

[root@localhost ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
79e9f2f55bf5: Pull complete 
0d96da54f60b: Pull complete 
5b27040df4a2: Pull complete 
e2ead8259a04: Pull complete 
3790aef225b9: Pull complete 
Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest

[root@localhost ~]# docker run -dit --name registry -p 5000:5000 -v /opt/registry:/tmp/registry registry
401929553b3e79be0b3b7b969f16edfd6326244eb10f40fb38f330754e29ff26

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                       NAMES
401929553b3e   registry   "/entrypoint.sh /etc…"   19 seconds ago   Up 17 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry
[root@localhost ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port     Peer Address:Port  Process  
LISTEN  0       128            0.0.0.0:22            0.0.0.0:*              
LISTEN  0       128            0.0.0.0:5000          0.0.0.0:*              
LISTEN  0       128               [::]:22               [::]:*              
LISTEN  0       128                  *:2375                *:*              
LISTEN  0       128               [::]:5000             [::]:*              

[root@localhost ~]# docker tag busybox:latest 192.168.91.128:5000/box:v1
[root@localhost ~]# docker images
REPOSITORY                TAG       IMAGE ID       CREATED         SIZE
centos-httpd              v2        40d9de9e74c7   4 days ago      678MB
centos-httpd              v1        3b5b771c3f29   4 days ago      678MB
192.168.91.128:5000/box   v1        beae173ccac6   7 months ago    1.24MB
busybox                   latest    beae173ccac6   7 months ago    1.24MB
httpd                     latest    dabbfbe0c57b   7 months ago    144MB
registry                  latest    b8604a3fe854   9 months ago    26.2MB
centos                    latest    5d0da3dc9764   10 months ago   231MB

[root@localhost ~]# docker push 192.168.91.128:5000/box:v1 
The push refers to repository [192.168.91.128:5000/box]
01fd6df81c8e: Pushed 
v1: digest: sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee size: 527

[root@localhost ~]# curl http://192.168.91.128:5000/v2/_catalog
{"repositories":["box"]}
[root@localhost ~]# curl http://192.168.91.128:5000/v2/box/tags/list
{"name":"box","tags":["v1"]}

Harbor

无论是使用 Docker-distribution 去自建仓库,还是通过官方镜像跑容器的方式去自建仓库,通过前面的演示我们可以发现其是非常的简陋的,还不如直接使用官方的 Docker Hub 去管理镜像来得方便,至少官方的 Docker Hub 能够通过 web 界面来管理镜像,还能在 web 界面执行搜索,还能基于 Dockerfile 利用 Webhooks 和 Automated Builds 实现自动构建镜像的功能,用户不需要在本地执行 docker build,而是把所有 build 上下文的文件作为一个仓库推送到 github 上,让 Docker Hub 可以从 github 上去 pull 这些文件来完成自动构建。

但无论官方的 Docker Hub 有多强大,它毕竟是在国外,所以速度是最大的瓶颈,我们很多时候是不可能去考虑使用官方的仓库的,但是上面说的两种自建仓库方式又十分简陋,不便管理,所以后来就出现了一个被 CNCF 组织青睐的项目,其名为 Harbor。

Harbor 简介

Harbor 是由 VMWare 在 Docker Registry 的基础之上进行了二次封装,加进去了很多额外程序,而且提供了一个非常漂亮的 web 界面。

Project Harbor is an open source trusted cloud native registry project that stores, signs, and scans context.

Harbor extends the open source Docker Distribution by adding the functionalities usually required by users such as security, identity and management.

Harbor supports advanced features such as user management, access control, activity monitoring, and replication between instances.

Harbor 的功能

Feathers:

  • Multi-tenant content signing and validation
  • Security and vulnerability analysis
  • Audit logging
  • Identity integration and role-based access control
  • Image replication between instances
  • Extensible API and graphical UI
  • Internationalization(currently English and Chinese)

Docker compose

Harbor 在物理机上部署是非常难的,而为了简化 Harbor 的应用,Harbor 官方直接把 Harbor 做成了在容器中运行的应用,而且这个容器在 Harbor 中依赖类似 redis、mysql、pgsql 等很多存储系统,所以它需要编排很多容器协同起来工作,因此 VMWare Harbor 在部署和使用时,需要借助于 Docker 的单机编排工具(Docker compose)来实现。

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Docker Compose 官方文档

Harbor 部署

Harbor 官方文档

//开始之前,删除所有容器

[root@localhost ~]# wget https://github.com/goharbor/harbor/releases/download/v2.4.3/harbor-offline-installer-v2.4.3.tgz

[root@localhost ~]# curl -SL https://github.com/docker/compose/releases/download/v2.7.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose

[root@localhost ~]# cd /usr/local/bin/
[root@localhost bin]# ll
total 25188
-rw-r--r-- 1 root root 25792512 Aug 11 16:58 docker-compose
[root@localhost bin]# chmod +x docker-compose 
[root@localhost bin]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
[root@localhost bin]# cd

[root@localhost ~]# which docker-compose 
/usr/local/bin/docker-compose

[root@localhost ~]# docker-compose version
Docker Compose version v2.7.0
[root@localhost ~]# tar xf harbor-offline-installer-v2.4.3.tgz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
bin  etc  games  harbor  include  lib  lib64  libexec  sbin  share  src
[root@localhost local]# cd harbor/
[root@localhost harbor]# ls
common.sh             harbor.yml.tmpl  LICENSE
harbor.v2.4.3.tar.gz  install.sh       prepare

[root@localhost harbor]# mv harbor.yml.tmpl harbor.yml
[root@localhost harbor]# vim harbor.yml
......
hostname: 192.168.91.128		//修改为当前 IP;如果配置了域名映射,也可修改为域名
......
#https:		//注释
  # https port for harbor, default is 443
  # port: 443        //注释
  # The path of cert and key files for nginx
  # certificate: /your/certificate/path        //注释
  # private_key: /your/private/key/path        //注释
......
harbor_admin_password: Harbor12345			//登录密码
......
database:
  # The password for the root user of Harbor DB. Change this before any production use.
  password: root123          //数据库密码
......

[root@localhost harbor]# vim /etc/docker/daemon.json 
......
  "insecure-registries":["192.168.91.128"]

[root@localhost harbor]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port     Peer Address:Port  Process  
LISTEN  0       128            0.0.0.0:80            0.0.0.0:*              
LISTEN  0       128            0.0.0.0:22            0.0.0.0:*              
LISTEN  0       128          127.0.0.1:1514          0.0.0.0:*              
LISTEN  0       128               [::]:80               [::]:*              
LISTEN  0       128               [::]:22               [::]:*              
LISTEN  0       128                  *:2375                *:*              

设置开机自启:

[root@localhost harbor]# cd
[root@localhost ~]# vim /usr/local/harbor/startall.sh
#!/bin/bash
cd /usr/local/harbor/
/usr/local/bin/docker-compose stop
/usr/local/bin/docker-compose start

[root@localhost ~]# vim /etc/rc.d/rc.local 
#!/bin/bash
/bin/bash       /usr/local/harbor/startall.sh

[root@localhost ~]# ll -d /etc/rc.d/rc.local 
-rw-r--r-- 1 root root 514 Aug 11 18:22 /etc/rc.d/rc.local
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# ll -d /etc/rc.d/rc.local 
-rwxr-xr-x 1 root root 514 Aug 11 18:22 /etc/rc.d/rc.local

数据存放路径

[root@localhost ~]# cd /
[root@localhost /]# ls
bin   data  etc   lib    media  opt   root  sbin  sys  usr
boot  dev   home  lib64  mnt    proc  run   srv   tmp  var
[root@localhost /]# cd data/
[root@localhost data]# ls
ca_download  database  job_logs  redis  registry  secret

使用 IP 登录管理 Harbor:
在这里插入图片描述

创建用户、管理员:
在这里插入图片描述

创建项目:
在这里插入图片描述

推送镜像:
只有管理员有权限推送镜像

[root@localhost ~]# docker login 192.168.91.128

[root@localhost ~]# docker tag busybox:latest 192.168.91.128/abc/busybox:v1

[root@localhost ~]# docker push 192.168.91.128/abc/busybox:v1
The push refers to repository [192.168.91.128/abc/busybox]
01fd6df81c8e: Pushed 
v1: digest: sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee size: 527

在这里插入图片描述

使用 Harbor 的注意事项:

  1. 在客户端上传镜像时一定要记得执行 docker login 进行用户认证,否则无法直接 push
  2. 在客户端使用的时候如果不是用的 https 则必须要在客户端的/etc/docker/daemon.json配置文件中配置insecure-registries参数
  3. 数据存放路径应在配置文件中配置到一个容量比较充足的共享存储中
  4. Harbor 是使用docker-compose命令来管理的,如果需要停止 Harbor 也应用docker-compose stop来停止,其他参数请--help
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值