使用Harbor构建docker私有仓库

网友投稿 267 2022-10-17

使用Harbor构建docker私有仓库

一、概述

1、什么是Harbor

Harbor 是VMware公司开源的云本地 registry 仓库,有可视化的Web管理界面,可以方便的管理和储存 Docker 镜像。Harbor 支持在多个仓库直接进行复制镜像,提供用户管理和访问控制和活动审计。

2、Harbor的优势

基于角色控制基于镜像的复制策略支持LDAP/AD域,通过VPN连接域使用图像删除和垃圾收集图像UI,方便审计,此功能使用较少,一般企业中用ELK收集、分析日志RESTful API

3、Docker私有仓库架构

图解所有的请求都经过proxy代理,proxy代理转发给Core services和Registry,其中Core services包括UI界面、token令牌和webhook网页服务功能,Registry主要提供镜像存储功能。如果要进行下载上传镜像,要经过token令牌验证然后从Registry获取或上传镜像,每一次下载或上传都会生成日志记录,会记入Log collector,而用户身份权限及一些镜像语言信息会被存储在Database中。

4、Harbor 配置文件以及相关参数

Harbor的配置文件是:/usr/local/harbor/harbor.cfg关于 Harbor.cfg 配置文件中有两类参数:所需参数和可选参数

(1)所需参数

修改此参数后,需要运行 install.sh脚本重新安装 Harbour,参数才会生效。

hostname用于访问用户界面和 register 服务。它应该是目标机器的 IP 地址或完全限定的域名(FQDN)例如 192.168.74.74 或test.com。不要使用 localhost 或 127.0.0.1 为主机名。ui_url_protocol(参数选项:或 UI 和令牌/通知服务的协议。如果公证处于启用状态,则此参数必须为 的MySQL数据库root 用户的密码。customize_crt该属性可设置为打开或关闭,默认打开。打开此属性时,准备脚本创建私钥和根证书,用于生成/验证注册表令牌。当由外部来源提供密钥和根证书时,将此属性设置为 off。ssl_certSSL 证书的路径,仅当协议设置为 时才应用。ssl_cert_keySSL 密钥的路径,仅当协议设置为 时才应用。secretkey_path用于在复制策略中加密或解密远程 register 密码的密钥路径。不建议配置,有很大的安全隐患

(2)可选参数

这些参数对于更新是可选的,即用户可以将其保留为默认值,并在启动 Harbor 后在 Web UI 上进行更新。如果进入 Harbor.cfg,只会在第一次启动 Harbor 时生效,随后对这些参数 的更新,Harbor.cfg 将被忽略。注意:如果选择通过UI设置这些参数,请确保在启动Harbour后立即执行此操作。具体来说,必须在注册或在 Harbor 中创建任何新用户之前设置所需的auth_mode当系统中有用户时(除了默认的 admin 用户),auth_mode 不能被修改。EmailHarbor需要该参数才能向用户发送“密码重置”电子邮件,并且只有在需要该功能时才需要。请注意,在默认情况下SSL连接时没有启用。如果SMTP服务器需要SSL,但不支持STARTTLS,那么应该通过设置启用SSL email_ssl = TRUE。harbour_admin_password管理员的初始密码,只在Harbour第一次启动时生效。之后,此设置将被忽略,并且应 UI中设置管理员的密码。默认的用户名/密码是 admin/Harbor12345。auth_mode使用的认证类型默认情况下,它是 db_auth,即凭据存储在数据库中。对于LDAP身份验证,请将其设置为 ldap_authself_registration启用/禁用用户注册功能。禁用时,新用户只能由 Admin 用户创建,只有管理员用户可以在 Harbour中创建新用户。注意:当 auth_mode 设置为 ldap_auth 时,自注册功能将始终处于禁用状态,并且该标志被忽略。Token_expiration由令牌服务创建的令牌的到期时间(分钟),默认为 30 分钟。即登录–退出后,30分钟内可以不输入用户名和密码登录,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 文件。

二、使用Harbor构建docker私有仓库

案例环境client(客户端)192.168.74.74Harbor(服务端)192.168.74.75、案例需求通过Harbor创建Docker私有仓库使用图形化管理Docker私有仓库镜像实验准备在服务端安装docker和docker-compose客户端上安装docker关闭防火墙修改主机名

1、部署 Harbor 服务

Harbor 被部署为多个 Docker 容器,因此可以部署在任何支持 Docker 的 Linux 发行版上。服务端主机需要安装Docker 和 Docker Compose。安装Docker:Docker的第一次接触安装Docker Compose如下放入命令压缩包```html/xml放入命令压缩包cd /usr/localrz docker-composechmod +x docker-composemv docker-compose /usr/bin

#### (1)下载 Harbor 安装程序 Harbor:192.168.74.75 ```html/xml //在线下载 wget 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 参数文件

Harbor:192.168.74.75

vim /usr/local/harbor/harbor.cfg //第5行 hostname = 192.168.74.75

(3)启动 Harbor

Harbor:192.168.74.75```html/xmlsh /usr/local/harbor/install.sh

![image.png](https://s2./images/20220721/1658395983971991.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) #### (4)查看 Harbor 启动镜像和容器 Harbor:192.168.74.75 ![image.png](https://s2./images/20220721/1658396110709834.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![image.png](https://s2./images/20220721/1658396165141535.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 检查服务容器是否开启 ![image.png](https://s2./images/20220721/1658396459412834.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) #### (5)在UI界面创建项目 真机 上图检查一切正常,此时可以打开浏览器访问 的管理页面,默认的管理员用户名和密码是 admin/Harbor12345。 ![image.png](https://s2./images/20220722/1658471611328056.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![image.png](https://s2./images/20220722/1658471698203896.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![image.png](https://s2./images/20220722/1658471881699898.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![image.png](https://s2./images/20220722/1658472229628808.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) #### (6)在本地测试仓库功能 Harbor:192.168.74.75 此时可使用 Docker 命令在本地通过 127.0.0.1 来登录和推送镜像。默认情况下, Register 服务器在端口 80 上侦听。 登录 ```html/xml docker login -u admin -p Harbor12345 http://127.0.0.1

![image.png](https://s2./images/20220722/1658473426811796.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ```html/xml docker tag nginx 127.0.0.1/testproject/nginx:v1

(7)在客户端上传镜像

client:192.168.74.74上述操作都是在 Harbor 服务器本地操作。如果其他客户端上传镜像到 Harbor,就会报如下错误。出现这问题的原因 Docker Registry 交互默认使用的是 HTTPS,但是搭建私有镜像默认使用的是 HTTP 服务,所以与私有镜像交互时出现以下错误。```html/xmldocker login -u admin -p Harbor12345 http://192.168.74.75#-------------------输出内容------------------------------------------

![image.png](https://s2./images/20220723/1658586243469780.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 解决方法 ```html/xml vim /usr/lib/systemd/system/docker.service #--------------------修改内容----------------------------------- ExecStart=/usr/bin/dockerd -H fd:// --insecure-registry 192.168.74.75 --containerd=/run/containerd/containerd.sock #--------------------------------------------------------------- //重启服务 systemctl daemon-reload systemctl restart docker //再次登录 docker login -u admin -p Harbor12345 http://192.168.74.75 #----------------------输出内容-------------------------------------------- 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 #--------------------------------------------------------------------

docker images

docker tag tomcat 192.168.74.75/testproject/tomcat:v2

docker push 192.168.74.75/testproject/tomcat:v2

![image.png](https://s2./images/20220725/1658733116846830.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![image.png](https://s2./images/20220725/1658733139788795.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ### 2、维护管理Harbor ```html/xml 可以使用 docker-compose 来管理 Harbor。一些有用的命令如下所示,必须在与 docker-compose.yml 相同的目录中运行。

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

(1)停止现有的 Harbor 实例

```html/xmldocker-compose down -v#---------------输出内容--------------------Stopping nginx ... doneStopping harbor-jobservice ... doneStopping harbor-ui ... doneStopping registry ... doneStopping harbor-db ... doneStopping harbor-adminserver ... doneStopping harbor-log ... doneRemoving nginx ... doneRemoving harbor-jobservice ... doneRemoving harbor-ui ... doneRemoving registry ... doneRemoving harbor-db ... doneRemoving harbor-adminserver ... doneRemoving harbor-log ... doneRemoving network harbor_harbor

![image.png](https://s2./images/20220725/1658733639465255.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) #### (2)修改配置文件 Harbor.cfg ![image.png](https://s2./images/20220725/1658733691152477.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) #### (3)运行 prepare 脚本来填充配置 ```html/xml ./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.

(4)重启服务

```html/xmldocker-compose up -d#-----------输出内容-----------------------------------Creating network "harbor_harbor" with the default driverCreating harbor-log ... doneCreating registry ... doneCreating harbor-adminserver ... doneCreating harbor-db ... doneCreating harbor-ui ... doneCreating harbor-jobservice ... doneCreating nginx ... done

![image.png](https://s2./images/20220725/1658733890639590.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 如果启动时报错(此报错比较少见,毕竟防火墙已开始就关闭了) ```html/xml 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需要重启```html/xmlsystemctl stop firewalldsystemctl restart dockerdocker-compose up -d

### 3、创建 Harbor 用户 ![image.png](https://s2./images/20220725/1658734048143780.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![image.png](https://s2./images/20220725/1658734168787093.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![image.png](https://s2./images/20220725/1658734210981940.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![image.png](https://s2./images/20220725/1658734269737690.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![image.png](https://s2./images/20220725/1658734394945469.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 在客户端上操作 删除之前打标签的镜像 ```html/xml docker rmi -f 07eee1a00569

//使用新创建的用户和密码登录私有仓库docker login 192.168.74.75#------------------输出内容-------------------------Username: xgPassword: //输入密码WARNING! Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning. SeeSucceeded#--------------------------------------------------

//从私有仓库下载镜像docker pull 192.168.74.75/testproject/nginx:v1#-----------------------输出内容-----------------------------v1: Pulling from testproject/nginxa076a628af6f: Pull complete 0732ab25fa22: Pull complete d7f36f6fe38f: Pull complete f72584a26f32: Pull complete 7125e4df9063: Pull complete Digest: sha256:0b159cd1ee1203dad901967ac55eee18c24da84ba3be384690304be93538bea8Status: Downloaded newer image for 192.168.163.100/testproject/nginx:v1192.168.163.100/testproject/nginx:v1

//查看镜像docker images#-------------------输出内容---------------------------------REPOSITORY TAG IMAGE ID CREATED SIZEtomcat latest 040bdb29ab37 2 months ago 649MB192.168.163.100/testproject/nginx v1 f6d0b4767a6c 2 months ago 133MB

![image.png](https://s2./images/20220725/1658735095183086.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ### 3、移除 Harbor 服务容器以及全部数据 移除 Harbor 服务容器同时保留镜像数据/数据库 //在Harbor服务器上操作 ```html/xml 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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot 枚举类型的自动转换的实现
下一篇:【docker专栏7】容器自启动与守护进程停止后容器保活
相关文章

 发表评论

暂时没有评论,来抢沙发吧~