揭开docker的神秘面纱?Harbor---私有仓库的搭建、管理


前言:

Habor是由VMWare公司开源的容器镜像仓库。事实上,Habor是在Docker Registry上进行了相应的企业级扩展,从而获得了更加广泛的应用,这些新的企业级特性包括:管理用户界面,基于角色的访问控制 ,AD/LDAP集成以及审计日志等,足以满足基本企业需求。
官方地址:https://vmware.github.io/harbor/cn/

在这里插入图片描述

一、Harbor简介

  • Harbor是VMware公司开源的企业级Docker Registry项目

  • Harbor的优势

    • 基于角色控制
    • 基于镜像的复制策略
    • 支持LDAP/AD
    • 图像删除和垃圾收集
    • 图像UI
    • 审计
    • RESTful API
  • Harbor的作用
    Docker Harbor有可视化的Web管理界面,可以方便管理Docker镜像,有提供了多个项目的镜像权限管理及控制功能

二、Docker私有仓库架构拓扑

在这里插入图片描述

  • Proxy

    • 通过一个前置的反向代理统一接收浏览器、Docker客户端的请求,并将请求转发给后端不同的服务
  • Registry

    • 负责储存Docker镜像,并处理docker push/pull命令
  • Core services

    • Harbor的核心功能,包括UI、webhook、token服务
  • Database

    • 为core services提供数据库服务
  • Log collector

    • 负责收集其他组件的log,供日后进行分析

三、Harbor构建Docker私有仓库

实验环境

主机 操作系统 主机IP 主要软件及版本
服务端 Centos 7.3-x86_64 192.168.221.10 docker19.03、docker-compose、harbor-offline-v1.1.2
客户端 Centos 7.3-x86_64 192.168.221.20 docker 192.03

安装 Docker-Compose 并查看版本判断安装是否成功

cp docker-compose /usr/local/bin/
chmod +x /usr/local/bin/docker-compose
docker-compose -v

关闭所有节点的防火墙

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

(一)、部署 Harbor 服务

Harbor 被部署为多个 Docker 容器,因此可以部署在任何支持 Docker 的 Linux 发行版 上。
服务端主机需要安装 Python、Docker 和 Docker Compose。

1、下载 Harbor 安装程序

wget http:// harbor.orientsoft.cn/harbor-1.2.2/harbor-offline-installer-v1.2.2.tgz

tar zxvf harbor-offline-installer-v1.2.2.tgz -C /usr/local/

2、配置 Harbor 参数文件

vim /usr/local/harbor/harbor.cfg

hostname = 192.168.221.10     #第五行修改为主机IP

在这里插入图片描述

关于 Harbor.cfg 配置文件中有两类参数:所需参数和可选参数
①、所需参数:这些参数需要在配置文件 Harbor.cfg 中设置。
如果用户更新它们并运行 install.sh脚本重新安装 Harbour,参数将生效。具体参数如下:

hostname:用于访问用户界面和 register 服务。它应该是目标机器的 IP 地址或完全限定的域名(FQDN)
例如 192.168.200.60 或 hub.gcc.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 时才应用。

ssl_cert_key:SSL 密钥的路径,仅当协议设置为 https 时才应用。

secretkey_path:用于在复制策略中加密或解密远程 register 密码的密钥路径。

②、可选参数

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

注意:如果选择通过UI设置这些参数,请确保在启动Harbour后立即执行此操作。具体来说,必须在注册或在 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

sh /usr/local/harbor/install.sh

在这里插入图片描述

4、查看 Harbor 启动镜像

#查看镜像
[root@localhost ~]# docker images

REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
vmware/harbor-log           v1.2.2              36ef78ae27df        2 years ago         200MB
vmware/harbor-jobservice    v1.2.2              e2af366cba44        2 years ago         164MB
vmware/harbor-ui            v1.2.2              39efb472c253        2 years ago         178MB
vmware/harbor-adminserver   v1.2.2              c75963ec543f        2 years ago         142MB
vmware/harbor-db            v1.2.2              ee7b9fa37c5d        2 years ago         329MB
vmware/nginx-photon         1.11.13             6cc5c831fc7f        2 years ago         144MB
vmware/registry             2.6.2-photon        5d9100e4350e        2 years ago         173MB
vmware/postgresql           9.6.4-photon        c562762cbd12        2 years ago         225MB
vmware/clair                v2.0.1-photon       f04966b4af6c        2 years ago         297MB
vmware/harbor-notary-db     mariadb-10.1.10     64ed814665c6        2 years ago         324MB
vmware/notary-photon        signer-0.5.0        b1eda7d10640        2 years ago         156MB
vmware/notary-photon        server-0.5.0        6e2646682e3c        2 years ago         157MB
photon                      1.0                 e6e4e4a2ba1b        3 years ago         128MB
#查看容器

[root@localhost ~]# docker ps -a

CONTAINER ID        IMAGE                              COMMAND                  CREATED              STATUS              PORTS                                                              NAMES
aee770c69872        vmware/nginx-photon:1.11.13        "nginx -g 'daemon of…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp   nginx
74e21da53cdd        vmware/harbor-jobservice:v1.2.2    "/harbor/harbor_jobs…"   About a minute ago   Up About a minute                                                                      harbor-jobservice
0a37b29881e9        vmware/harbor-ui:v1.2.2            "/harbor/harbor_ui"      About a minute ago   Up About a minute                                                                      harbor-ui
f77d9c7cf595        vmware/harbor-adminserver:v1.2.2   "/harbor/harbor_admi…"   About a minute ago   Up About a minute                                                                      harbor-adminserver
5dad74ff8d31        vmware/harbor-db:v1.2.2            "docker-entrypoint.s…"   About a minute ago   Up About a minute   3306/tcp                                                           harbor-db
d09a85bb8da1        vmware/registry:2.6.2-photon       "/entrypoint.sh serv…"   About a minute ago   Up About a minute   5000/tcp                                                           registry
041184a34344        vmware/harbor-log:v1.2.2           "/bin/sh -c 'crond &…"   About a minute ago   Up About a minute   127.0.0.1:1514->514/tcp                                            harbor-log
[root@localhost ~]# cd /usr/local/harbor/
[root@localhost ~]# 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              

如果一切都正常,应该可以打开浏览器访问 http://192.168.221.10 的管理页面,默认 的管理员用户名和密码是 admin/Harbor12345。

在这里插入图片描述
在这里插入图片描述
#添加项目并且填写项目名称
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
此时可使用 Docker 命令在本地通过 127.0.0.1 来登录和推送镜像。默认情况下,
Register 服务器在端口 80 上侦听。

//登录
docker login -u admin -p Harbor12345 http://127.0.0.1

//下载镜像进行测试
docker pull cirros


//镜像打标签
docker tag cirros 127.0.0.1/myproject-gcc/cirros:v1

//上传镜像到Harbor
docker push 127.0.0.1/myproject-gcc/cirros:v1

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

[root@client ~]# docker login  -u admin -p Harbor12345 http://192.168.221.10
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: Get https://192.168.221.10/v2/: EOF

#解决:

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

ExecStart=/usr/bin/dockerd -H fd:// --insecure-registry 192.168.221.10 --containerd=/run/containerd/containerd.sock

[root@client ~]# systemctl daemon-reload 
[root@client ~]# systemctl restart docker
[root@client ~]# docker login  -u admin -p Harbor12345 http://192.168.221.10
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@client ~]# docker pull cirros
Using default tag: latest
latest: Pulling from library/cirros
3d6427f49fe3: Pull complete 
1915bfe8159b: Pull complete 
d0ec9ef25b96: Pull complete 
Digest: sha256:8654d33ecbcdc8fd65c80325c3ec3b1bc938dfad9f20d1a2e3cf21e521ab70e6
Status: Downloaded newer image for cirros:latest
docker.io/library/cirros:latest
[root@client ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
cirros              latest              bc94bceaae77        11 months ago       10.3MB

[root@client ~]# docker tag cirros 192.168.221.10/myproject-gcc/cirros:v2


[root@client ~]# docker push 192.168.221.10/myproject-gcc/cirros:v2
The push refers to repository [192.168.221.10/myproject-kgc/cirros]
abbd6d6ac643: Layer already exists 
75b99987219d: Layer already exists 
0cc237193a30: Layer already exists 
v2: digest: sha256:96137d51e0e46006243fa2403723eb47f67818802d1175b5cde7eaa7f19446bd size: 943

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、维护管理Harbor

可以使用 docker-compose 来管理 Harbor。一些有用的命令如下所示,必须在与
docker-compose.yml 相同的目录中运行。

1、修改 Harbor.cfg 配置文件

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

[root@Harbor ~]# cd /usr/local/harbor/
[root@Harbor ~]# docker-compose down -v

Stopping nginx              ... done
Stopping harbor-jobservice  ... done
Stopping harbor-ui          ... done
Stopping harbor-adminserver ... done
Stopping harbor-db          ... done
Stopping registry           ... done
Stopping harbor-log         ... done
Removing nginx              ... done
Removing harbor-jobservice  ... done
Removing harbor-ui          ... done
Removing harbor-adminserver ... done
Removing harbor-db          ... done
Removing registry           ... done
Removing harbor-log         ... done
Removing network harbor_harbor
[root@Harbor ~]# cd /usr/local/harbor/
[root@Harbor ~]# ./prepare

Clearing the configuration file: ./common/config/adminserver/env
Clearing the configuration file: ./common/config/ui/env
Clearing the configuration file: ./common/config/ui/app.conf
Clearing the configuration file: ./common/config/ui/private_key.pem
Clearing the configuration file: ./common/config/db/env
Clearing the configuration file: ./common/config/jobservice/env
Clearing the configuration file: ./common/config/jobservice/app.conf
Clearing the configuration file: ./common/config/registry/config.yml
Clearing the configuration file: ./common/config/registry/root.crt
Clearing the configuration file: ./common/config/nginx/nginx.conf
loaded secret from file: /data/secretkey
Generated configuration file: ./common/config/nginx/nginx.conf
Generated configuration file: ./common/config/adminserver/env
Generated configuration file: ./common/config/ui/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/app.conf
Generated configuration file: ./common/config/ui/app.conf
Generated certificate, key file: ./common/config/ui/private_key.pem, cert file: ./common/config/registry/root.crt
The configuration files are ready, please use docker-compose to start the service.

#报错:

[root@Harbor ~]# docker-compose up -d
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-25094fc09b3c -j RETURN: iptables: No chain/target/match by that name.
 (exit status 1))

#解决:关闭防火墙后,docker需要重启

[root@Harbor ~]#systemctl restart docker
[root@Harbor ~]# docker-compose up -d

Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating harbor-db          ... done
Creating harbor-adminserver ... done
Creating registry           ... done
Creating harbor-ui          ... done
Creating harbor-jobservice  ... done
Creating nginx              ... done

2、创建 Harbor 用户

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

3、创建项目开发人员

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述#在客户端上操作

[root@client ~]# docker rmi 192.168.221.10/myproject-gcc/cirros:v2
Untagged: 192.168.221.10/myproject-gcc/cirros:v2
Untagged: 192.168.22.10/myproject-gcc/cirros@sha256:96137d51e0e46006243fa2403723eb47f67818802d1175b5cde7eaa7f19446bd
//注销登录

[root@client ~]# docker logout 192.168.221.10
Removing login credentials for 192.168.221.10

[root@client ~]# docker login 192.168.22.10
Username:   #填写自己创建的新的用户
Password:    //填写Harbor1234
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@client ~]# docker pull 192.168.221.10/myproject-gcc/cirros:v1
v1: Pulling from myproject-gcc/cirros
Digest: sha256:96137d51e0e46006243fa2403723eb47f67818802d1175b5cde7eaa7f19446bd
Status: Downloaded newer image for 192.168.221.10/myproject-gcc/cirros:v1
192.168.200.60/myproject-gcc/cirros:v1

[root@client ~]# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
192.168.221.10/myproject-gcc/cirros   v1                  bc94bceaae77        11 months ago       10.3MB
cirros                                 latest              bc94bceaae77        11 months ago       10.3MB

移除 Harbor 服务容器同时保留镜像数据/数据库

//在Harbor服务器上操作
docker-compose down -v

Stopping nginx              ... done
Stopping harbor-jobservice  ... done
Stopping harbor-ui          ... done
Stopping registry           ... done
Stopping harbor-db          ... done
Stopping harbor-adminserver ... done
Stopping harbor-log         ... done
Removing nginx              ... done
Removing harbor-jobservice  ... done
Removing harbor-ui          ... done
Removing registry           ... done
Removing harbor-db          ... done
Removing harbor-adminserver ... done
Removing harbor-log         ... done
Removing network harbor_harbor

如需重新部署,需要移除 Harbor 服务容器全部数据
持久数据,如镜像,数据库等在宿主机的/data/目录下,日志在宿主机的
/var/log/Harbor/目录下。

rm -rf /data/database/
rm -rf /data/registry/

总结:

简单来说Harbor私有仓库进行简单的部署还是比较简单的,主要思路如下:

① 环境:docker-ce

② 准备软件: Harbor、docker-compose

③ 修改harbor.cfg 中的hostname

④ 安装启动harbor(install.sh)

⑤ 搭建完成

在客户端,如果想要登入harbor,需要修改/usr/lib/systemd/system/docker.server 配置文件,在准启动(Exstart)配置项中的-H fd:// 后添加 –insecure-registry [harbor服务器地址],后重载进程和docker即可

之后会在Kerbernetes中也使用到私有仓库~~这个之后再介绍啦

猜你喜欢

转载自blog.csdn.net/weixin_51431591/article/details/115300593