docker私有registry和harbor简单使用 - Go语言中文社区

docker私有registry和harbor简单使用


前言介绍

Registry用于保存docker镜像,包括镜像的层次结构和元数据

用户可自建Registry,也可使用官方的Docker Hub

Docker Registry,它是所有仓库(包括共有和私有)以及工作流的中央Registry。在深入Docker Registry之前,让我们先去看看一些常见的术语和与仓库相关的概念。

  • Repositories(仓库)可以被标记为喜欢或者像书签一样标记起来
  • 用户可以在仓库下评论。
  • 私有仓库和共有仓库类似,不同之处在于前者不会在搜索结果中显示,也没有访问它的权限。只有用户设置为合作者才能访问私有仓库。
  • 成功推送之后配置webhooks。

Docker Registry角色

Docker Registry有三个角色,分别是index、registry和registry client。

角色 1 -- Index

index 负责并维护有关用户帐户、镜像的校验以及公共命名空间的信息。它使用以下组件维护这些信息:

  • Web UI
  • 元数据存储
  • 认证服务
  • 符号化

这也分解了较长的URL,以方便使用和验证用户存储库。

角色 2 --Registry

registry是镜像和图表的仓库。然而,它没有一个本地数据库,也不提供用户的身份认证,由S3、云文件和本地文件系统提供数据库支持。此外,通过Index Auth service的Token方式进行身份认证。Registries可以有不同的类型。现在让我们来分析其中的几种类型:

  • Sponsor Registry:第三方的registry,供客户和Docker社区使用。

  • Mirror Registry:第三方的registry,只让客户使用。

  • Vendor Registry:由发布Docker镜像的供应商提供的registry。

  • Private Registry:通过设有防火墙和额外的安全层的私有实体提供的registry。

角色 3 --Registry Client

Docker充当registry客户端来负责维护推送和拉取的任务,以及客户端的授权。

Docker Registry工作流程详解

现在,让我们讨论五种情景模式,以便更好地理解Docker Registry。

情景A:用户要获取并下载镜像。所涉及的步骤如下:

  1. 用户发送请求到index来下载镜像。
  2. index 发出响应,返回三个相关部分信息:
    • 该镜像所处的registry
    • 该镜像包括所有层的校验
    • 以授权为目的的Token

    注意:当请求header里有X-Docker-Token时才会返回Token。而私人仓库需要基本的身份验证,对于公有仓库这一点不是强制性的。

  3. 用户通过响应后返回的Token和registry沟通,registry全权负责镜像,它用来存储基本的镜像和继承的层。
  4. registry现在要与index证实该token是被授权的。
  5. index会发送“true”或者“false”给registry,由此判定是否允许用户下载所需要的镜像。

情景A流程图

情景B:用户想要将镜像推送到registry中。其中涉及的步骤如下:

  1. 用户发送附带证书的请求到index要求分配库名。
  2. 在认证成功,命名空间可用之后,库名也被分配。index发出响应返回临时的token。
  3. 镜像连带token,一起被推送到registry中。
  4. registry与index证实token被授权,然后在index验证之后开始读取推送流。
  5. 该index由Docker校验的镜像更新。

情景B

情景C:用户想要从index或registry中删除镜像:

  1. index接收来自Docker一个删除库的信号。
  2. 如果index对库验证成功,它将删除该库,并返回一个临时的token。
  3. registry现在接收到带有该token的删除信号。
  4. registry与index核实该token,然后删除库以及所有与其相关的信息。
  5. Docker现在通知有关删除的index,然后index移除库的所有记录。

情景C

情景D:用户希望在没有index的独立模式中使用registry。
使用没有index的registry,这完全由Docker控制,它最适合于在私有网络中存储镜像。registry运行在一个特殊的模式里,此模式限制了registry与Docker index的通信。所有有关安全性和身份验证的信息需要用户自己注意。

情景E:用户想要在有index的独立模式中使用registry。
在这种情况下,一个自定义的index会被创建在私有网络里来存储和访问镜像的问题。然而,通知Docker有关定制的index是耗时的。 Docker提供一个有趣的概念chaining registries,从而,实现负载均衡和为具体请求而指定的registry分配。在接下来的Docker教程系列中,我们将讨论如何在上述每个情景中使用Docker Registry API ,以及深入了解Docker Security。

搭建registry

[root@node2 ~]# yum info docker-registry

版本 :0.9.1 //版本过老

发布 :7.el7

大小 :123 k

源 :extras/7/x86_64

[root@node2 ~]# yum install docker-registry

========================================================================================

Package 架构 版本 源 大小

========================================================================================

正在安装:

docker-distribution x86_64 2.6.2-2.git48294d9.el7 extras 3.5 M

[root@node2 ~]# rpm -ql docker-distribution

/etc/docker-distribution/registry/config.yml

/usr/bin/registry //主程序

上传镜像

[root@node2 ~]# cd /etc/docker-distribution/registry/

[root@node2 registry]# cat config.yml

version: 0.1

log:

fields:

service: registry

storage:

cache:

layerinfo: inmemory

filesystem:

rootdirectory: /var/lib/registry

http:

addr: :5000 //默认所有主机的 5000端口

[root@node2 registry]# systemctl start docker-distribution.service

[root@node2 registry]# ss -tnl

State Recv-Q Send-Q Local Address:Port Peer Address:Port

LISTEN 0 128 *:22 *:*

LISTEN 0 128 :::5000 :::*

LISTEN 0 128 :::22 :::*

[root@node1 ~]# docker tag myweb:v0.3-8 node2.zisefeizhu.com:5000/myweb:v0.3-8

[root@node1 ~]# docker image ls

node2.zisefeizhu.com:5000/myweb v0.3-8 05f10f0430cb 18 hours ago 17.4MB

[root@node1 ~]# cat /etc/hosts          //做好域名解析准备

10.0.0.220 node2.zisefeizhu.com

[root@node1 ~]# docker push node2.zisefeizhu.com:5000/myweb:v0.3-8

The push refers to repository [node2.zisefeizhu.com:5000/myweb]

Get https://node2.zisefeizhu.com:5000/v2/: http: server gave HTTP response to HTTPS client

[root@node1 ~]# vim /etc/docker/daemon.json

{

"registry-mirrors": ["https://llpuz83z.mirror.aliyuncs.com","https://registry.docker-cn.com"],

"insecure-registries": ["node2.zisefeizhu.com:5000"]   //修改使其可以通过http方式push到私有仓库

}

[root@node1 ~]# systemctl restart docker

上传成功

[root@node1 ~]# docker push node2.zisefeizhu.com:5000/myweb:v0.3-8

The push refers to repository [node2.zisefeizhu.com:5000/myweb]

edcda3959450: Pushed

8154ed0fadea: Pushed

9a07ffbe3d7d: Pushed

955e7d7f7300: Pushed

95bb4e754f2d: Pushed

ebf12965380b: Pushed

v0.3-8: digest: sha256:3b70494d5bae8bdc49c6d0b93b3ed7ea4522bb854dd77d672c7252ed4887bf4b size: 1568

查看上传镜像信息

[root@node2 ~]# cd /var/lib/registry/

[root@node2 registry]# ls

docker

[root@node2 registry]# cd docker/

[root@node2 docker]# ls

registry

[root@node2 docker]# cd registry/

[root@node2 registry]# ls

v2

[root@node2 registry]# cd v2/

[root@node2 v2]# ls

blobs repositories

[root@node2 v2]# cd repositories/

[root@node2 repositories]# ls

myweb

[root@node2 repositories]# ls myweb/

_layers _manifests _uploads

[root@node2 repositories]# ls myweb/_layers/sha256/

05f10f0430cbdd4e77759dec3f075a88a9211f70665b69eb0e6e63e24696612b

428de5b8d58a2547bec1c51d249f7c99dd0eadc454a44205def2e5e826992896

4f98fc8b751c0da0efe6133a9df21d68cd713de0ad00c2016e2fa5afbb8c191a

61a56b170416bea473afcb041812027f39024d5e890e5874c971e32380d9f32b

7efd417f3e288119f4aea8cf4b4f804ffecef91559fe683ceada620a09bf9407

8654768780c4b660118a2604f5c4e8b54f7658dbe26287a43622a80a0cef3022

c67f3896b22c1378881cbbb9c9d1edfe881fd07f713371835ef46d93c649684d

[root@node2 repositories]# ls myweb/_layers/sha256/05f10f0430cbdd4e77759dec3f075a88a9211f70665b69eb0e6e63e24696612b/

link

下载镜像

[root@node2 ~]# vim /etc/docker/daemon.json

{

"registry-mirrors": ["https://llpuz83z.mirror.aliyuncs.com","https://registry.docker-cn.com"],

"insecure-registries": ["node2.zisefeizhu.com:5000"]   //修改使其可以通过http方式私有仓库pull镜像

}

[root@node2 ~]# systemctl restart docker

[root@node2 ~]# cat /etc/docker

docker/ docker-distribution/

拖取成功

[root@node2 ~]# docker pull node2.zisefeizhu.com:5000/myweb:v0.3-8

v0.3-8: Pulling from myweb

c67f3896b22c: Pull complete

428de5b8d58a: Pull complete

7efd417f3e28: Pull complete

61a56b170416: Pull complete

8654768780c4: Pull complete

4f98fc8b751c: Pull complete

Digest: sha256:3b70494d5bae8bdc49c6d0b93b3ed7ea4522bb854dd77d672c7252ed4887bf4b

Status: Downloaded newer image for node2.zisefeizhu.com:5000/myweb:v0.3-8

查看镜像

[root@node2 ~]# docker image ls

REPOSITORY TAG IMAGE ID CREATED SIZE

node2.zisefeizhu.com:5000/myweb v0.3-8 05f10f0430cb 18 hours ago 17.4MB

缺点:太丑陋了。。。

distribution没有web页面,管理起来不方便,还有一个解决办法是vmware harbor,路径为https://github.com/vmware/harbor  关于harbor的安装文档,可以见链接 https://github.com/vmware/harbor/blob/master/docs/installation_guide.md

Harbor简介

Harbor介绍

Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全、标识和管理等,扩展了开源Docker Distribution。作为一个企业级私有Registry服务器,Harbor提供了更好的性能和安全。提升用户使用Registry构建和运行环境传输镜像的效率。Harbor支持安装在多个Registry节点的镜像资源复制,镜像全部保存在私有Registry中, 确保数据和知识产权在公司内部网络中管控。另外,Harbor也提供了高级的安全特性,诸如用户管理,访问控制和活动审计等。

Harbor特性

基于角色的访问控制 :用户与Docker镜像仓库通过“项目”进行组织管理,一个用户可以对多个镜像仓库在同一命名空间(project)里有不同的权限。

镜像复制 : 镜像可以在多个Registry实例中复制(同步)。尤其适合于负载均衡,高可用,混合云和多云的场景。

图形化用户界面 : 用户可以通过浏览器来浏览,检索当前Docker镜像仓库,管理项目和命名空间。

AD/LDAP 支持 : Harbor可以集成企业内部已有的AD/LDAP,用于鉴权认证管理。

审计管理 : 所有针对镜像仓库的操作都可以被记录追溯,用于审计管理。

国际化 : 已拥有英文、中文、德文、日文和俄文的本地化版本。更多的语言将会添加进来。

RESTful API : RESTful API 提供给管理员对于Harbor更多的操控, 使得与其它管理软件集成变得更容易。

部署简单 : 提供在线和离线两种安装工具, 也可以安装到vSphere平台(OVA方式)虚拟设备。

3.Harbor组件

Harbor在架构上主要由6个组件构成:

Proxy:Harbor的registry, UI, token等服务,通过一个前置的反向代理统一接收浏览器、Docker客户端的请求,并将请求转发给后端不同的服务。

Registry: 负责储存Docker镜像,并处理docker push/pull 命令。由于我们要对用户进行访问控制,即不同用户对Docker image有不同的读写权限,Registry会指向一个token服务,强制用户的每次docker pull/push请求都要携带一个合法的token, Registry会通过公钥对token 进行解密验证。

Core services: 这是Harbor的核心功能,主要提供以下服务:

UI:提供图形化界面,帮助用户管理registry上的镜像(image), 并对用户进行授权。

webhook:为了及时获取registry 上image状态变化的情况, 在Registry上配置webhook,把状态变化传递给UI模块。

token 服务:负责根据用户权限给每个docker push/pull命令签发token. Docker 客户端向Regiøstry服务发起的请求,如果不包含token,会被重定向到这里,获得token后再重新向Registry进行请求。

Database:为core services提供数据库服务,负责储存用户权限、审计日志、Docker image分组信息等数据。

Job Services:提供镜像远程复制功能,可以把本地镜像同步到其他Harbor实例中。

Log collector:为了帮助监控Harbor运行,负责收集其他组件的log,供日后进行分析。

各个组件之间的关系如下图所示:

.Harbor实现

Harbor的每个组件都是以Docker容器的形式构建的,官方也是使用Docker Compose来对它进行部署。用于部署Harbor的Docker Compose模板位于 harbor/docker-compose.yml,打开这个模板文件,发现Harbor是由7个容器组成的;

# docker-compose ps
       Name                     Command               State                                Ports                              
------------------------------------------------------------------------------------------------------------------------------
harbor-adminserver   /harbor/harbor_adminserver       Up                                                                      
harbor-db            docker-entrypoint.sh mysqld      Up      3306/tcp                                                        
harbor-jobservice    /harbor/harbor_jobservice        Up                                                                      
harbor-log           /bin/sh -c crond && rm -f  ...   Up      127.0.0.1:1514->514/tcp                                         
harbor-ui            /harbor/harbor_ui                Up                                                                      
nginx                nginx -g daemon off;             Up      0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp, 0.0.0.0:80->80/tcp
registry             /entrypoint.sh serve /etc/ ...   Up      5000/tcp 

nginx:nginx负责流量转发和安全验证,对外提供的流量都是从nginx中转,所以开放https的443端口,它将流量分发到后端的ui和正在docker镜像存储的docker registry。
harbor-jobservice:harbor-jobservice 是harbor的job管理模块,job在harbor里面主要是为了镜像仓库之前同步使用的;
harbor-ui:harbor-ui是web管理页面,主要是前端的页面和后端CURD的接口;
registry:registry就是docker原生的仓库,负责保存镜像。
harbor-adminserver:harbor-adminserver是harbor系统管理接口,可以修改系统配置以及获取系统信息。
这几个容器通过Docker link的形式连接在一起,在容器之间通过容器名字互相访问。对终端用户而言,只需要暴露proxy (即Nginx)的服务端口。
harbor-db:harbor-db是harbor的数据库,这里保存了系统的job以及项目、人员权限管理。由于本harbor的认证也是通过数据,在生产环节大多对接到企业的ldap中;
harbor-log:harbor-log是harbor的日志服务,统一管理harbor的日志。通过inspect可以看出容器统一将日志输出的syslog。

这几个容器通过Docker link的形式连接在一起,这样,在容器之间可以通过容器名字互相访问。对终端用户而言,只需要暴露proxy (即Nginx)的服务端口。

实例:

下载harbor

https://github.com/goharbor/harbor/releases   这里有关于harbor的详细说明

我这里以1.2.0为例演示

[root@node2 ~]#wget http://harbor.orientsoft.cn/harbor-v1.5.0/harbor-offline-installer-v1.2.0.tgz

[root@node2 ~]# tar xf harbor-online-installer-v1.2.0.tgz -C /usr/local

[root@node2 ~]# cd /usr/local/

[root@node2 local]# ls

bin etc games harbor include lib lib64 libexec sbin share src

[root@node2 local]# cd harbor/

[root@node2 harbor]# ls

common docker-compose.yml install.sh prepare

docker-compose.clair.yml harbor_1_1_0_template LICENSE upgrade

docker-compose.notary.yml harbor.cfg NOTICE

//运行install.sh前先编辑docker-compose.yml

//需要仔细阅读docker-compose.yml 和 harbor.cfg

[root@node2 ~]# systemctl stop docker-distribution.service //先停掉服务再编辑

[root@node2 harbor]# vim harbor.cfg

hostname = node2.zisefeizhu.com

db_password = root123

#管理登录密码

harbor_admin_password = Harbor12345

#The path of cert and key files for nginx, they are applied only the protocol is set to

https

ssl_cert = /data/cert/server.crt

ssl_cert_key = /data/cert/server.key

运行脚本

[root@node2 harbor]# ./install.sh 过程会很慢 还显示版本过低

安装过程,[Step 1]: loading Harbor images … 这一步比较慢,需要下载很多镜像,来实现相关的服务

停止docker harbor服务,通过docker-compose来实现,所有harbor服务会被停止,命令如下

注意,命令必须在/usr/local/harbor/路径下执行

docker-compose stop

启动服务

yum install -y docker-compose

[root@node2 ~]#docker-compose start

[root@node2 harbor]# ./install.sh

[root@node2 ~]# ss -tnl

State Recv-Q Send-Q Local Address:Port Peer Address:Port

LISTEN 0 128 127.0.0.1:1514 *:*

LISTEN 0 128 *:22 *:*

LISTEN 0 128 :::5000 :::*

LISTEN 0 128 :::80 :::*

LISTEN 0 128 :::22 :::*

LISTEN 0 128 :::443 :::*

LISTEN 0 128 :::4443 :::*

创建一个用户:zhujingxing

创建目标

关闭证书(也可以自己搭建个CA认证)

普通用户登录

自定一个项目,项目里可以有很多仓库

验证推node1推镜像到仓库

[root@node1 ~]# vim /etc/docker/daemon.json     //node2也做相应修改

{
  "registry-mirrors": ["https://llpuz83z.mirror.aliyuncs.com","https://registry.docker-cn.com"],
  "insecure-registries": ["node2.zisefeizhu.com"]   //默认就是PORT:80
}
[root@node1 ~]# systemctl restart docker

重新打标(多打几个)

[root@node1 ~]# docker  tag  myweb:v0.3-1 node2.zisefeizhu.com/devel/myweb:v0.3-1
[root@node1 ~]# docker  tag  myweb:v0.3-2 node2.zisefeizhu.com/devel/myweb:v0.3-2
[root@node1 ~]# docker  tag  myweb:v0.3-4 node2.zisefeizhu.com/devel/myweb:v0.3-4

登陆

[root@node1 ~]# docker login node2.zisefeizhu.com
Username: zhujingxing     //普通用户
Password:                        //对应用户设置的密码
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
推到仓库

[root@node1 ~]# docker push node2.zisefeizhu.com/devel/myweb
The push refers to repository [node2.zisefeizhu.com/devel/myweb]
cc61e03b98b5: Pushed 
9a07ffbe3d7d: Pushed 
955e7d7f7300: Pushed 
95bb4e754f2d: Pushed 
ebf12965380b: Pushed 
v0.3-1: digest: sha256:fbfb1113ee9b4d467415f1149c0f4b0f1523018cd46b5ae6884ddc52d9b0293e size: 1360
6f41b1930405: Pushed 
9a07ffbe3d7d: Layer already exists 
955e7d7f7300: Layer already exists 
95bb4e754f2d: Layer already exists 
ebf12965380b: Layer already exists 
v0.3-2: digest: sha256:9945293ffcd1db79922d78209c884e25b7cc153e8364c07d881c5499862daa3c size: 1360
6f41b1930405: Layer already exists 
9a07ffbe3d7d: Layer already exists 
955e7d7f7300: Layer already exists 
95bb4e754f2d: Layer already exists 
ebf12965380b: Layer already exists 
v0.3-4: digest: sha256:c2f07dcc0319d75c83e6661cd48708c62d4bc05c097a93ff59b78e82f8dbb895 size: 1...

页面验证

成功!!!

但是还有一定问题的:上传的镜像放到了那个路径下了 vim docker-compose.yml

我们可以改一改,改成我们真正准备的一个磁盘空间,空间较大的,IO较强的分区或者目录

停止docker-compose

[root@node2 harbor]# docker-compose pause    //暂停 ,访问不到了
Pausing harbor-log         ... done
Pausing harbor-adminserver ... done
Pausing harbor-db          ... done
Pausing registry           ... done
Pausing harbor-ui          ... done
Pausing harbor-jobservice  ... done
Pausing nginx              ... done
[root@node2 harbor]# docker-compose unpause  //继续运行
Unpausing nginx              ... done
Unpausing harbor-jobservice  ... done
Unpausing harbor-ui          ... done
Unpausing registry           ... done
Unpausing harbor-db          ... done
Unpausing harbor-adminserver ... done
Unpausing harbor-log         ... done

[root@node2 harbor]# docker-compose --help
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name (default: directory name)
  --verbose                   Show more output
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the name specified
                              in the client certificate (for example if your docker host
                              is an IP address)
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)

Commands:
  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

将来我们可以把harbor也托管到Kubernetes上,所以这里我们会简单应用即可!!!

参考:

https://www.cnblogs.com/pangguoping/p/7650014.html

https://www.jianshu.com/p/6ebd82086754

http://blog.51cto.com/ghbsunny/2155048

 

 

 

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zisefeizhu/article/details/83511586
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 21:56:43
  • 阅读 ( 1119 )
  • 分类:docker

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢