Docker--harbor 私有仓库部署与管理

一、本地私有仓库

 1、下载registry镜像

[root@localhost ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
6a428f9f83b0: Pull complete 
90cad49de35d: Pull complete 
b215d0b40846: Pull complete 
429305b6c15c: Pull complete 
6f7e10a4e907: Pull complete 
Digest: sha256:265d4a5ed8bf0df27d1107edb00b70e658ee9aa5acb3f37336c5a17db634481e
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest

[root@localhost ~]# docker images
REPOSITORY             TAG       IMAGE ID       CREATED       SIZE
juice1ee/nginx         web       f8f4ffc8092c   4 weeks ago   133MB
nginx                  latest    f8f4ffc8092c   4 weeks ago   133MB
centos                 7         eeb6ee3f44bd   5 weeks ago   204MB
registry               latest    b2cb11db9d3d   8 weeks ago   26.2MB

2、在daemon.json文件中添加私有镜像仓库地址

[root@localhost ~]# vim /etc/docker/daemon.json 
[root@localhost ~]# cat /etc/docker/daemon.json
{
  "registry-mirrors": ["https://31ycpc34.mirror.aliyuncs.com"],
  "insecure-registries": ["20.0.0.6:5000"]
}
[root@localhost ~]# systemctl restart docker.service

3.、运行registry容器

[root@localhost ~]# docker run -itd -v /data/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:latest
d247a82e1985f4718ca9907a36775937ad443eba71faf499576b7407b88f865e
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS         PORTS                                       NAMES
d247a82e1985   registry:latest   "/entrypoint.sh /etc…"   9 seconds ago   Up 8 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry

-itd:在容器中打开一个伪终端进行交互操作,并在后台运行
-v:把宿主机的/data/registry目录(这个目录是registry容器中存放镜像文件的目录),来实现数据的持久化
-p:映射端口,访问宿主机的5000端口就访问到registry容器的服务了
--restart=always:这是重启的策略,在容器退出时总是重启容器
--name registry:创建容器命名为registry
registry:latest:这个是刚才pull下来的镜像

4、Docker容器的重启策略如下

[root@localhost ~]# docker tag centos:7 20.0.0.6:5000/centos:test1
[root@localhost ~]# docker images
REPOSITORY             TAG       IMAGE ID       CREATED       SIZE
juice1ee/nginx         web       f8f4ffc8092c   4 weeks ago   133MB
nginx                  latest    f8f4ffc8092c   4 weeks ago   133MB
20.0.0.6:5000/centos   test1     eeb6ee3f44bd   5 weeks ago   204MB
centos                 7         eeb6ee3f44bd   5 weeks ago   204MB
registry               latest    b2cb11db9d3d   8 weeks ago   26.2MB

5、为镜像打标签

[root@localhost ~]# docker tag centos:7 20.0.0.6:5000/centos:test1
[root@localhost ~]# docker images
REPOSITORY             TAG       IMAGE ID       CREATED       SIZE
juice1ee/nginx         web       f8f4ffc8092c   4 weeks ago   133MB
nginx                  latest    f8f4ffc8092c   4 weeks ago   133MB
20.0.0.6:5000/centos   test1     eeb6ee3f44bd   5 weeks ago   204MB
centos                 7         eeb6ee3f44bd   5 weeks ago   204MB
registry               latest    b2cb11db9d3d   8 weeks ago   26.2MB

6、上传到私有仓库

[root@localhost ~]# docker push 20.0.0.6:5000/centos:test1
The push refers to repository [20.0.0.6:5000/centos]
174f56854903: Pushed 
test1: digest: sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f size: 529

7、列出私有仓库的所有镜像

[root@localhost ~]# curl http://20.0.0.6:5000/v2/_catalog
{"repositories":["centos"]}

8、列出私有仓库的centos镜像有哪些tag

[root@localhost ~]# curl http://20.0.0.6:5000/v2/centos/tags/list
{"name":"centos","tags":["test1"]}

9、先删除原有的centos镜像,再测试私有仓库下载

[root@localhost ~]# docker rmi -f 20.0.0.6:5000/centos:test1 centos:7
Untagged: 20.0.0.6:5000/centos:test1
Untagged: 20.0.0.6:5000/centos@sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f
Untagged: centos:7
Untagged: centos@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Deleted: sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9
Deleted: sha256:174f5685490326fc0a1c0f5570b8663732189b327007e47ff13d2ca59673db02
[root@localhost ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED       SIZE
juice1ee/nginx   web       f8f4ffc8092c   4 weeks ago   133MB
nginx            latest    f8f4ffc8092c   4 weeks ago   133MB
registry         latest    b2cb11db9d3d   8 weeks ago   26.2MB
[root@localhost ~]# docker pull 20.0.0.6:5000/centos:test1
test1: Pulling from centos
2d473b07cdd5: Pull complete 
Digest: sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f
Status: Downloaded newer image for 20.0.0.6:5000/centos:test1
20.0.0.6:5000/centos:test1
[root@localhost ~]# docker images
REPOSITORY             TAG       IMAGE ID       CREATED       SIZE
juice1ee/nginx         web       f8f4ffc8092c   4 weeks ago   133MB
nginx                  latest    f8f4ffc8092c   4 weeks ago   133MB
20.0.0.6:5000/centos   test1     eeb6ee3f44bd   5 weeks ago   204MB
registry               latest    b2cb11db9d3d   8 weeks ago   26.2MB

 

二、Harbor 简介

1、什么是Harbor

Harbor 是 VMware 公司开源的企业级 Docker Registry 项目,其目标是帮助用户迅速搭建一个企业级的 Docker Registry 服务。

Harbor以 Docker 公司开源的 Registry 为基础,提供了图形管理 UI 、基于角色的访问控制(Role Based AccessControl) 、AD/LDAP 集成、以及审计日志(Auditlogging) 等企业用户需求的功能,同时还原生支持中文。

Harbor 的每个组件都是以 Docker 容器的形式构建的,使用 docker-compose 来对它进行部署。用于部署 Harbor 的 docker-compose 模板位于 harbor/docker-compose.yml。

2、Harbor的特性

(1)基于角色控制:用户和仓库都是基于项目进行组织的,而用户在项目中可以拥有不同的权限。
(2)基于镜像的复制策略:镜像可以在多个Harbor实例之间进行复制(同步)。
(3)支持 LDAP/AD:Harbor 可以集成企业内部已有的 AD/LDAP(类似数据库的一张表),用于对已经存在的用户认证和管理。
(4)镜像删除和垃圾回收:镜像可以被删除,也可以回收镜像占用的空间。
(5)图形化用户界面:用户可以通过浏览器来浏览,搜索镜像仓库以及对项目进行管理。
(6)审计管理:所有针对镜像仓库的操作都可以被记录追溯,用于审计管理。
(7)支持 RESTful API:RESTful API 提供给管理员对于 Harbor 更多的操控, 使得与其它管理软件集成变得更容易。
(8)Harbor和docker registry的关系:Harbor实质上是对docker registry做了封装,扩展了自己的业务模板。

3、Harbor的构成

Harbor 在架构上主要有 Proxy、Registry、Core services、Database(Harbor-db)、Log collector(Harbor-log)、Job services 六个组件。


●Proxy:

Harbor 的 Registry、UI、Token 服务等组件,都处在 nginx 反向代理后边。该代理将来自浏览器、docker clients 的请求转发到后端不同的服务上。

●Registry:

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


●Core services:

Harbor的核心功能,主要提供以下3个服务:

1)UI(harbor-ui): 提供图形化界面,帮助用户管理 Registry 上的镜像(image), 并对用户进行授权。
2)WebHook:为了及时获取Registry 上image 状态变化的情况,在Registry 上配置 Webhook,把状态变化传递给 UI 模块。
3)Token 服务:负责根据用户权限给每个 Docker push/pull 命令签发 Token。Docker 客户端向 Registry 服务发起的请求, 如果不包含 Token,会被重定向到 Token 服务,获得 Token 后再重新向 Registry 进行请求。


●Database(harbor-db):

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

●Job services:

主要用于镜像复制,本地镜像可以被同步到远程 Harbor 实例上。


●Log collector(harbor-log):

负责收集其他组件的日志到一个地方。


Harbor 的每个组件都是以 Docker 容器的形式构建的,因此,使用 Docker Compose 来对它进行部署。
总共分为7个容器运行,通过在docker-compose.yml所在目录中执行 docker-compose ps 命令来查看, 名称分别为:nginx、harbor-jobservice、harbor-ui、harbor-db、harbor-adminserver、registry、harbor-log。
其中 harbor-adminserver 主要是作为一个后端的配置数据管理,并没有太多的其他功能。harbor-ui 所要操作的所有数据都通过 harbor-adminserver 这样一个数据配置管理中心来完成。

 

三、Harbor 部署

服务器 IP地址 主要软件
Harbor 服务器 20.0.0.11 docker-ce、docker-compose、harbor-offline-v1.2.2
client 服务器 20.0.0.6 docker-ce

---部署 docker-compose 服务---

1、下载或者上传 Docker-Compose

curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose 

docker-compose -v
docker-compose version 1.21.1, build 5a3f1a3
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
containerd  rh
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
containerd  docker-compose  rh
[root@localhost opt]# ll
总用量 10616
drwx--x--x. 4 root root       28 10月 14 18:48 containerd
-rw-r--r--. 1 root root 10867152 10月 22 15:22 docker-compose
drwxr-xr-x. 2 root root        6 3月  26 2015 rh

[root@localhost opt]# chmod +x docker-compose

[root@localhost opt]# ll
总用量 10616
drwx--x--x. 4 root root       28 10月 14 18:48 containerd
-rwxr-xr-x. 1 root root 10867152 10月 22 15:22 docker-compose
drwxr-xr-x. 2 root root        6 3月  26 2015 rh
[root@localhost opt]# mv docker-compose /usr/local/bin/
[root@localhost opt]# docker-compose -v
docker-compose version 1.21.1, build 5a3f1a3

2、部署 Habor

(1)下载或上传Harbor安装程序

[root@localhost ~]# cd /opt
[root@localhost opt]# ls
containerd  rh
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
containerd  harbor-offline-installer-v1.2.2.tgz  rh
[root@localhost opt]# tar zxvf harbor-offline-installer-v1.2.2.tgz -C /usr/local/

(2)修改harbor安装的配置文件

vim /usr/local/harbor/harbor.cfg

#5行,修改,设置为Harbor服务器的IP地址或者域名
5 hostname = 20.0.0.11 #59行,指定管理员的初始密码,默认的用户名/密码是admin/Harbor12345 59 harbor_admin_password = Harbor12345

(3)harbor.cfg配置文件中的两类参数

关于harbor.cfg配置文件中有两类参数:所需参数和可选参数

<1> 所需参数

这些参数需要在配置文件harbor.cfg中设置。如果用户更新它们并运行install.sh
脚本重新安装harbor,参数将生效。具体参数如下:
● hostname:用于访问用户界面和 register 服务。它应该是目标机器的 IP 地址或完全限定的域名(FQDN),例如 192.168.80.10 或 hub.kgc.cn。不要使用 localhost 或 127.0.0.1 为主机名。
● ui_url_protocol:(http 或 https,默认为 http)用于访问 UI 和令牌/通知服务的协议。如果公证是启用状态,则此参数必须为 https。
● max_job_workers:镜像复制作业线程。
● db_password:用于db_auth 的MySQL数据库root 用户的密码。
● customize_crt:该属性可设置为打开或关闭,默认打开。打开此属性时,准备脚本创建私钥和根证书,用于生成/验证注册表令牌。当由外部来源提供密钥和根证书时,将此属性设置为 off。
● ssl_cert:SSL 证书的路径,仅当协议设置为 https 时才应用。
● secretkey_path:用于在复制策略中加密或解密远程 register 密码的密钥路径。

<2> 可选参数

这些参数对于更新是可选的,即用户可以将其保留为默认值,并在启动 Harbor 后在 Web UI 上进行更新。如果进入 Harbor.cfg,只会在第一次启动 Harbor 时生效,随后对这些参数的更新,Harbor.cfg 将被忽略。

注意:如果选择通过 UI 设置这些参数,请确保在启动 Harbor 后立即执行此操作。具体来说,必须在注册或在 Harbor 中创建任何新用户之前设置所需的 auth_mode。当系统中有用户时(除了默认的 admin 用户), auth_mode 不能被修改。 具体参数如下:
● Email:Harbor 需要该参数才能向用户发送“密码重置”电子邮件,并且只有在需要该功能时才启用。请注意,在默认情况下 SSL 连接时没有启用。如果 SMTP 服务器需要 SSL,但不支持 STARTTLS,那么应该通过设置启用 SSL email_ssl = TRUE。
● harbour_admin_password:管理员的初始密码,只在 Harbour 第一次启动时生效。之后, 此设置将被忽略,并且应在 UI 中设置管理员的密码。请注意,默认的用户名/密码是admin/Harbor12345。
● auth_mode:使用的认证类型,默认情况下,它是 db_auth,即凭据存储在数据库中。对于LDAP身份验证,请将其设置为 ldap_auth。
● self_registration:启用/禁用用户注册功能。禁用时,新用户只能由 Admin 用户创建,只有管理员用户可以在 Harbour 中创建新用户。注意:当 auth_mode 设置为 ldap_auth 时,自注册功能将始终处于禁用状态,并且该标志被忽略。
● Token_expiration:由令牌服务创建的令牌的到期时间(分钟),默认为 30 分钟。
● project_creation_restriction:用于控制哪些用户有权创建项目的标志。默认情况下,每个人都可以创建一个项目。如果将其值设置为“adminonly”,那么只有 admin 可以创建项目。
● verify_remote_cert:打开或关闭,默认打开。此标志决定了当Harbor与远程 register 实例通信时是否验证 SSL/TLS 证书。 将此属性设置为 off 将绕过 SSL/TLS 验证,这在远程实例具有自签名或不可信证书时经常使用。

另外,默认情况下,Harbour 将镜像存储在本地文件系统上。在生产环境中,可以考虑 使用其他存储后端而不是本地文件系统,如 S3、Openstack Swif、Ceph 等对象存储。但需要更新 common/templates/registry/config.yml 文件。

3、启动 harbor

注:启动harbor前需删除registry镜像/容器,否则无法生成新的registry镜像。

[root@localhost opt]# cd /usr/local/harbor/
[root@localhost harbor]# ./install.sh

[root@localhost harbor]# docker images
REPOSITORY                  TAG               IMAGE ID       CREATED       SIZE
centos                      latest            5d0da3dc9764   5 weeks ago   231MB
vmware/harbor-log           v1.2.2            36ef78ae27df   4 years ago   200MB
vmware/harbor-jobservice    v1.2.2            e2af366cba44   4 years ago   164MB
vmware/harbor-ui            v1.2.2            39efb472c253   4 years ago   178MB
vmware/harbor-adminserver   v1.2.2            c75963ec543f   4 years ago   142MB
vmware/harbor-db            v1.2.2            ee7b9fa37c5d   4 years ago   329MB
vmware/nginx-photon         1.11.13           6cc5c831fc7f   4 years ago   144MB
vmware/registry             2.6.2-photon      5d9100e4350e   4 years ago   173MB
vmware/postgresql           9.6.4-photon      c562762cbd12   4 years ago   225MB
vmware/clair                v2.0.1-photon     f04966b4af6c   4 years ago   297MB
vmware/harbor-notary-db     mariadb-10.1.10   64ed814665c6   4 years ago   324MB
vmware/notary-photon        signer-0.5.0      b1eda7d10640   4 years ago   156MB
vmware/notary-photon        server-0.5.0      6e2646682e3c   4 years ago   157MB
photon                      1.0               e6e4e4a2ba1b   5 years ago   128MB
[root@localhost harbor]# docker ps -a
CONTAINER ID   IMAGE                              COMMAND                  CREATED         STATUS         PORTS                                                                                                                 NAMES
fba18e58bf03   vmware/nginx-photon:1.11.13        "nginx -g 'daemon of…"   4 minutes ago   Up 4 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp, 0.0.0.0:4443->4443/tcp, :::4443->4443/tcp   nginx
fc9c86cb5b09   vmware/harbor-jobservice:v1.2.2    "/harbor/harbor_jobs…"   4 minutes ago   Up 4 minutes                                                                                                                         harbor-jobservice
472cc5f94eaf   vmware/harbor-ui:v1.2.2            "/harbor/harbor_ui"      4 minutes ago   Up 4 minutes                                                                                                                         harbor-ui
f63c47e589ed   vmware/harbor-db:v1.2.2            "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes   3306/tcp                                                                                                              harbor-db
642e509b2644   vmware/registry:2.6.2-photon       "/entrypoint.sh serv…"   4 minutes ago   Up 4 minutes   5000/tcp                                                                                                              registry
16d10597ebf4   vmware/harbor-adminserver:v1.2.2   "/harbor/harbor_admi…"   4 minutes ago   Up 4 minutes                                                                                                                         harbor-adminserver
fe50bfa692ab   vmware/harbor-log:v1.2.2           "/bin/sh -c 'crond &…"   4 minutes ago   Up 4 minutes   127.0.0.1:1514->514/tcp                                                                                               harbor-log

4、查看harbor启动镜像

[root@localhost harbor]# 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,:::443->
                                                              443/tcp, 0.0.0.0:4443->4443/t
                                                              cp,:::4443->4443/tcp, 0.0.0.0
                                                              :80->80/tcp,:::80->80/tcp    
registry             /entrypoint.sh serve /etc/ ...   Up      5000/tcp                     

5、创建一个新项目

(1)浏览器访问:http://20.0.0.11  登录harbor web ui界面,默认的管理员用户名和密码是admin/Harbor12345

6、登录 Harbor

## 可使用 Docker 命令在本地通过 127.0.0.1 来登录和推送镜像。默认情况下,Registry 服务器在端口 80 上侦听。

[root@localhost ~]# docker login -u admin -p Harbor12345 http://127.0.0.1
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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@localhost ~]# docker images
REPOSITORY                  TAG               IMAGE ID       CREATED       SIZE
centos                      latest            5d0da3dc9764   6 weeks ago   231MB
vmware/harbor-log           v1.2.2            36ef78ae27df   4 years ago   200MB
vmware/harbor-jobservice    v1.2.2            e2af366cba44   4 years ago   164MB
vmware/harbor-ui            v1.2.2            39efb472c253   4 years ago   178MB
vmware/harbor-adminserver   v1.2.2            c75963ec543f   4 years ago   142MB
vmware/harbor-db            v1.2.2            ee7b9fa37c5d   4 years ago   329MB
vmware/nginx-photon         1.11.13           6cc5c831fc7f   4 years ago   144MB
vmware/registry             2.6.2-photon      5d9100e4350e   4 years ago   173MB
vmware/postgresql           9.6.4-photon      c562762cbd12   4 years ago   225MB
vmware/clair                v2.0.1-photon     f04966b4af6c   4 years ago   297MB
vmware/harbor-notary-db     mariadb-10.1.10   64ed814665c6   4 years ago   324MB
vmware/notary-photon        signer-0.5.0      b1eda7d10640   4 years ago   156MB
vmware/notary-photon        server-0.5.0      6e2646682e3c   4 years ago   157MB
photon                      1.0               e6e4e4a2ba1b   5 years ago   128MB

7、下载镜像进行测试

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
b380bbd43752: Pull complete 
fca7e12d1754: Pull complete 
745ab57616cb: Pull complete 
a4723e260b6f: Pull complete 
1c84ebdff681: Pull complete 
858292fd2e56: Pull complete 
Digest: sha256:644a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY                  TAG               IMAGE ID       CREATED       SIZE
nginx                       latest            87a94228f133   2 weeks ago   133MB
centos                      latest            5d0da3dc9764   6 weeks ago   231MB
vmware/harbor-log           v1.2.2            36ef78ae27df   4 years ago   200MB
vmware/harbor-jobservice    v1.2.2            e2af366cba44   4 years ago   164MB
vmware/harbor-ui            v1.2.2            39efb472c253   4 years ago   178MB
vmware/harbor-adminserver   v1.2.2            c75963ec543f   4 years ago   142MB
vmware/harbor-db            v1.2.2            ee7b9fa37c5d   4 years ago   329MB
vmware/nginx-photon         1.11.13           6cc5c831fc7f   4 years ago   144MB
vmware/registry             2.6.2-photon      5d9100e4350e   4 years ago   173MB
vmware/postgresql           9.6.4-photon      c562762cbd12   4 years ago   225MB
vmware/clair                v2.0.1-photon     f04966b4af6c   4 years ago   297MB
vmware/harbor-notary-db     mariadb-10.1.10   64ed814665c6   4 years ago   324MB
vmware/notary-photon        signer-0.5.0      b1eda7d10640   4 years ago   156MB
vmware/notary-photon        server-0.5.0      6e2646682e3c   4 years ago   157MB
photon                      1.0               e6e4e4a2ba1b   5 years ago   128MB

8、将镜像打标签

[root@localhost ~]# docker tag nginx:latest 127.0.0.1/test/nginx:v1
[root@localhost ~]# docker images
REPOSITORY                  TAG               IMAGE ID       CREATED       SIZE
127.0.0.1/test/nginx        v1                87a94228f133   2 weeks ago   133MB
nginx                       latest            87a94228f133   2 weeks ago   133MB
centos                      latest            5d0da3dc9764   6 weeks ago   231MB
vmware/harbor-log           v1.2.2            36ef78ae27df   4 years ago   200MB
vmware/harbor-jobservice    v1.2.2            e2af366cba44   4 years ago   164MB
vmware/harbor-ui            v1.2.2            39efb472c253   4 years ago   178MB
vmware/harbor-adminserver   v1.2.2            c75963ec543f   4 years ago   142MB
vmware/harbor-db            v1.2.2            ee7b9fa37c5d   4 years ago   329MB
vmware/nginx-photon         1.11.13           6cc5c831fc7f   4 years ago   144MB
vmware/registry             2.6.2-photon      5d9100e4350e   4 years ago   173MB
vmware/postgresql           9.6.4-photon      c562762cbd12   4 years ago   225MB
vmware/clair                v2.0.1-photon     f04966b4af6c   4 years ago   297MB
vmware/harbor-notary-db     mariadb-10.1.10   64ed814665c6   4 years ago   324MB
vmware/notary-photon        signer-0.5.0      b1eda7d10640   4 years ago   156MB
vmware/notary-photon        server-0.5.0      6e2646682e3c   4 years ago   157MB
photon                      1.0               e6e4e4a2ba1b   5 years ago   128MB

9、上传镜像到 Harbor

[root@localhost ~]# docker push 127.0.0.1/test/nginx:v1
The push refers to repository [127.0.0.1/test/nginx]
9959a332cf6e: Pushed 
f7e00b807643: Pushed 
f8e880dfc4ef: Pushed 
788e89a4d186: Pushed 
43f4e41372e4: Pushed 
e81bff2725db: Pushed 
v1: digest: sha256:7250923ba3543110040462388756ef099331822c6172a050b12c7a38361ea46f size: 1570

10、Web 查看 project 状态

11、在其他客户端上传镜像

以上操作都是在Harbor服务器本地操作。如果其他客户端登录到Harbor,就会报如下错误。出现这问题的原因为Docker Registry交互默认使用的是HTTPS,但是搭建私有镜像默认使用的是HTTP服务,所以与私有镜像交互时出现以下错误。

[root@localhost ~]# docker login -u admin -p Harbor12345 http://20.0.0.11
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: Get "https://20.0.0.11/v2/": dial tcp 20.0.0.11:443: connect: connection refused

(1)在Docker客户端配置操作

解决办法是在Docker server启动的时候,增加启动参数,默认使用HTTP访问。
方法一:修改/usr/lib/systemd/system/docker.service

[root@localhost ~]# vim /usr/lib/systemd/system/docker.service

#13行修改
13 ExecStart=/usr/bin/dockerd -H fd:// --insecure-registry 20.0.0.11 --containerd=/run/con    tainerd/containerd.sock

#或修改为ExecStart=/usr/bin/dockerd --insecure-registry 20.0.0.11

方法二:修改/ets/docker/daemon.json

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

{
  "registry-mirrors": ["https://31ycpc34.mirror.aliyuncs.com"],
  "insecure-registries": ["20.0.0.11"]

12、重启 Docker 服务,再次登录 Harbor

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker login -u admin -p Harbor12345 http://20.0.0.11
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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

13、下载镜像进行测试

[root@localhost ~]# docker pull 20.0.0.11/test/nginx:v1
v1: Pulling from test/nginx
Digest: sha256:7250923ba3543110040462388756ef099331822c6172a050b12c7a38361ea46f
Status: Downloaded newer image for 20.0.0.11/test/nginx:v1
20.0.0.11/test/nginx:v1
[root@localhost ~]# docker images
REPOSITORY                  TAG               IMAGE ID       CREATED       SIZE
127.0.0.1/test/nginx        v1                87a94228f133   2 weeks ago   133MB
20.0.0.11/test/nginx        v1                87a94228f133   2 weeks ago   133MB
nginx                       latest            87a94228f133   2 weeks ago   133MB
centos                      latest            5d0da3dc9764   6 weeks ago   231MB

14、上传镜像进行测试,刷新网页进行查看

[root@localhost ~]# docker pull cirros
Using default tag: latest
latest: Pulling from library/cirros
d0b405be7a32: Pull complete 
bd054094a037: Pull complete 
c6a00de1ec8a: Pull complete 
Digest: sha256:1e695eb2772a2b511ccab70091962d1efb9501fdca804eb1d52d21c0933e7f47
Status: Downloaded newer image for cirros:latest
docker.io/library/cirros:latest
......
[root@localhost ~]# docker images
REPOSITORY                  TAG               IMAGE ID       CREATED        SIZE
nginx                       latest            87a94228f133   2 weeks ago    133MB
127.0.0.1/test/nginx        v1                87a94228f133   2 weeks ago    133MB
20.0.0.11/test/nginx        v1                87a94228f133   2 weeks ago    133MB
centos                      latest            5d0da3dc9764   6 weeks ago    231MB
cirros                      latest            f9cae1daf5f6   7 months ago   12.6MB
......
[root@localhost ~]# docker tag cirros:latest 20.0.0.11/test/cirros:v2
[root@localhost ~]# docker push 20.0.0.11/test/cirros:v2
The push refers to repository [20.0.0.11/test/cirros]
984ad441ec3d: Pushed 
f0a496d92efa: Pushed 
e52d19c3bee2: Pushed 
v2: digest: sha256:483f15ac97d03dc3d4dcf79cf71ded2e099cf76c340f3fdd0b3670a40a198a22 size: 943

 

四、维护管理 Harbor

1、通过 Harbor Web 创建项目

在 Harbor 仓库中,任何镜像在被 push 到 regsitry 之前都必须有一个自己所属的项目。
单击“+项目”,填写项目名称,项目级别若设置为"私有",则不勾选。如果设置为公共仓库,则所有人对此项目下的镜像拥有读权限,命令行中不需要执行"Docker login"即可下载镜像,镜像操作与 Docker Hub 一致。

2、创建 Harbor 用户

(1)创建用户并分配权限

在 Web 管理界面中单击系统管理 -> 用户管理 -> +用户,
填写用户名为“test”,邮箱为“xxx@test.com”,全名为“test”,密码为“Test12345”(需大小写英文,数字以及至少8位字符),注释为“test”(可省略)。
附:用户创建成功后,单击左侧“...”按钮可将上述创建的用户设置为管理员角色或进行删除操作,本例不作任何设置。

(2)添加项目成员

单击项目 -> test -> 成员 -> + 成员,填写上述创建的用户 test 并分配角色为“开发人员”。

 

附:此时单击左侧“...”按钮仍然可对成员角色进行变更或者删除操作

3、在客户端上使用普通账户操作镜像

---删除上述打标签的本地镜像---

[root@localhost harbor]# docker rmi 20.0.0.11/test/cirros:v2 
Untagged: 20.0.0.11/test/cirros:v2
Untagged: 20.0.0.11/test/cirros@sha256:483f15ac97d03dc3d4dcf79cf71ded2e099cf76c340f3fdd0b3670a40a198a22
[root@localhost harbor]# docker images
REPOSITORY                  TAG               IMAGE ID       CREATED        SIZE
20.0.0.11/test/nginx        v1                87a94228f133   2 weeks ago    133MB
nginx                       latest            87a94228f133   2 weeks ago    133MB
127.0.0.1/test/nginx        v1                87a94228f133   2 weeks ago    133MB
centos                      latest            5d0da3dc9764   6 weeks ago    231MB
cirros                      latest            f9cae1daf5f6   7 months ago   12.6MB

 

---退出当前用户,然后使用上述创建的账户 test 登录---

docker logout 20.0.0.11    #退出登录

docker login 20.0.0.11
或
docker login -u test -p Harbor12345 http://20.0.0.11   

---下载和上传镜像进行测试---

docker pull 20.0.0.11/test/cirros:v2    
docker tag cirros:latest 20.0.0.11/test/cirros:v3 docker push 20.0.0.11/test/cirros:v3

4、查看日志

Web 界面日志,操作日志按时间顺序记录用户相关操作

5、修改 Harbor.cfg 配置文件

要更改 Harbour的配置文件中的可选参数时,请先停止现有的 Harbor实例并更新 Harbor.cfg;然后运行 prepare 脚本来填充配置; 最后重新创建并启动 Harbour 的实例。

使用 docker-compose 管理 Harbor 时,必须在与 docker-compose.yml 相同的目录中运行。

cd /usr/local/harbor
docker-compose down -v
 
vim harbor.cfg  #只能修改可选参数
 
./prepare
 
//如果有以下报错,需要开启防火墙 firewalld 服务解决
Creating network "harbor_harbor" with the default driver
ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed: iptables --wait -t nat -I DOCKER -i br-b53c314f45e8 -j RETURN: iptables: No chain/target/match by that name.
(exit status 1))
 
systemctl restart firewalld.service
docker-compose up -d

 

vim harbor.cfg			#只能修改可选参数

./prepare

docker-compose up -d

docker-compose ps

6、移除 Harbor 服务容器同时保留镜像数据/数据库,并进行迁移

---在Harbor服务器上操作---

(1)移除 Harbor 服务容器

cd /usr/local/harbor
docker-compose down -v

(2)把项目中的镜像数据进行打包

---持久数据,如镜像,数据库等在宿主机的/data/目录下,日志在宿主机的/var/log/harbor/目录下---

ls /data/registry/docker/registry/v2/repositories/test
cd /data/registry/docker/registry/v2/repositories/test
tar zcvf test.tar.gz ./*

7、如需重新部署,需要移除 Harbor 服务容器全部数据

cd /usr/local/harbor
docker-compose down -v
rm -r /data/database rm -r /data/registry

 

posted @ 2021-10-22 21:25  DoYouWantMore  阅读(577)  评论(0编辑  收藏  举报