使用Harbor搭建docker镜像私有仓库

一.Harbor软件简介

Harbor 是由 VMware 公司中国团队为企业用户设计的 Registry server 开源项目,包括了权限管理(RBAC)、LDAP、审计、管理界面、自我注册、HA 等企业必需的功能,同时针对中国用户的特点,设计镜像复制和中文支持等功能。

在这里插入图片描述

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

  • 基于角色的访问控制 - 用户与 Docker 镜像仓库通过“项目”进行组织管理,一个用户可以对多个镜像仓库在同一命名空间(project)里有不同的权限。
  • 镜像复制 - 镜像可以在多个 Registry 实例中复制(同步)。尤其适合于负载均衡,高可用,混合云和多云的场景。
  • 图形化用户界面 - 用户可以通过浏览器来浏览,检索当前 Docker 镜像仓库,管理项目和命名空间。
  • AD/LDAP 支持 - Harbor 可以集成企业内部已有的 AD/LDAP,用于鉴权认证管理。
  • 审计管理 - 所有针对镜像仓库的操作都可以被记录追溯,用于审计管理。
  • 国际化 - 已拥有英文、中文、德文、日文和俄文的本地化版本。更多的语言将会添加进来。
  • RESTful API - RESTful API 提供给管理员对于 Harbor 更多的操控, 使得与其它管理软件集成变得更容易。
  • 部署简单 - 提供在线和离线两种安装工具, 也可以安装到 vSphere 平台(OVA 方式)虚拟设备。

二.部署Harbor服务

1.案例设计

主机操作系统主机名/IP地址主要软件及版本
服务器centos7.4Harbor/192.168.100.128docker-ce、docker-compose、harbor-offline-v1.1.2
客户端centos7.4client/192.168.100.130docker-ce

案例需求

  • 通过Harbor创建Docker私有仓库
  • 图形化管理Docekr私有仓库镜像

2.部署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

//5 hostname = 192.168.100.128			//本地ip地址,不要使用 localhost 或 127.0.0.1 为主机名

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

所需参数

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

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

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.安装docker-compose 容器编排工具

上传dockers-compose工具到opt下,赋予执行权限后交给系统识别
[root@harbor harbor]# chmod +x docker-compose
[root@harbor harbor]# mv docker-compose /usr/bin/

4.启动Harbor

sh /usr/local/harbor/install.sh

5.查看Harbor启动镜像

查看镜像

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

查看容器

[root@localhost harbor]# docker ps -a
CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS              PORTS                                                              NAMES
fce0c2e412de        vmware/harbor-jobservice:v1.2.2    "/harbor/harbor_jobs…"   34 minutes ago      Up 34 minutes                                                                          harbor-jobservice
7181012e6532        vmware/nginx-photon:1.11.13        "nginx -g 'daemon of…"   34 minutes ago      Up 34 minutes       0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp   nginx
03b9ebc9f4f0        vmware/harbor-ui:v1.2.2            "/harbor/harbor_ui"      34 minutes ago      Up 34 minutes                                                                          harbor-ui
6ce45eca3b3e        vmware/registry:2.6.2-photon       "/entrypoint.sh serv…"   34 minutes ago      Up 34 minutes       5000/tcp                                                           registry
4ea1bd2a4d05        vmware/harbor-adminserver:v1.2.2   "/harbor/harbor_admi…"   34 minutes ago      Up 34 minutes                                                                          harbor-adminserver
4f839680f0b2        vmware/harbor-db:v1.2.2            "docker-entrypoint.s…"   34 minutes ago      Up 34 minutes       3306/tcp                                                           harbor-db
49227da45bb3        vmware/harbor-log:v1.2.2           "/bin/sh -c 'crond &…"   34 minutes ago      Up 34 minutes       127.0.0.1:1514->514/tcp    
[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, 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.100.128 的管理页面,默认 的管理员用户名和密码是 admin/Harbor12345。

在这里插入图片描述

登录

在这里插入图片描述

添加项目并且填写项目名称

在这里插入图片描述

此时可使用 Docker 命令在本地通过 127.0.0.1 来登录和推送镜像。默认情况下, Register 服务器在端口 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 pull nginx
Using default tag: latest
latest: Pulling from library/nginx
852e50cd189d: Pull complete 
571d7e852307: Pull complete 
addb10abd9cb: Pull complete 
d20aa7ccdb77: Pull complete 
8b03f1e11359: Pull complete 
Digest: sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

打标签及上传镜像

[root@localhost ~]# docker tag nginx:latest 127.0.0.1/test/nginx:v1
[root@localhost ~]# docker push 127.0.0.1/test/nginx:v1
The push refers to repository [127.0.0.1/test/nginx]
7e914612e366: Pushed 
f790aed835ee: Pushed 
850c2400ea4d: Pushed 
7ccabd267c9f: Pushed 
f5600c6330da: Pushed 
v1: digest: sha256:99d0a53e3718cef59443558607d1e100b325d6a2b678cd2a48b05e5e22ffeb49 size: 1362

在这里插入图片描述

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

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


vi /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --insecure-registry 192.168.195.128 --containerd=/run/containerd/containerd.sock

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker login -u admin -p Harbor12345 http://192.168.100.128
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 pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
756975cb9c7e: Pull complete 
d77915b4e630: Pull complete 
5f37a0a41b6b: Pull complete 
96b2c1e36db5: Pull complete 
27a2d52b526e: Pull complete 
a867dba77389: Pull complete 
0939c055fb79: Pull complete 
0b0694ce0ae2: Pull complete 
81a5f8099e05: Pull complete 
c3d7917d545e: Pull complete 
Digest: sha256:a319b10d8729817c7ce0bcc2343a6f97711c7870395019340d96b6aafd6ccbea
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@localhost ~]# docker tag tomcat:latest 192.168.100.128/test/tomcat:v2
[root@localhost ~]# docker push 192.168.100.128/test/tomcat:v2
The push refers to repository [192.168.100.128/test/tomcat]
3be17d77a93a: Pushed 
a84354b89db3: Pushed 
438ec47051c4: Pushed 
94982bbe98d5: Pushed 
39341dafb261: Pushed 
4b9227ba273c: Pushed 
712264374d24: Pushed 
475b4eb79695: Pushed 
f3be340a54b9: Pushed 
114ca5b7280f: Pushed 
v2: digest: sha256:4527a552568f7d706173d8065278cd1abaa7edce186a149a5a2de251e12e6c3c size: 2421

在这里插入图片描述

3.维护Harbor

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

修改 Harbor.cfg 配置文件 
要更改 Harbour 的配置文件时,请先停止现有的 Harbour 实例并更新 Harbor.cfg;然 后运行 prepare 脚本来填充配置;最后重新创建并启动 Harbour 的实例。
cd /cd /usr/local/harbor/
[root@localhost harbor]# docker-compose down -v
Stopping harbor-jobservice  ... done
Stopping nginx              ... done
Stopping harbor-ui          ... done
Stopping registry           ... done
Stopping harbor-adminserver ... done
Stopping harbor-db          ... done
Stopping harbor-log         ... done
Removing harbor-jobservice  ... done
Removing nginx              ... done
Removing harbor-ui          ... done
Removing registry           ... done
Removing harbor-adminserver ... done
Removing harbor-db          ... done
Removing harbor-log         ... done
Removing network harbor_harbor

[root@localhost harbor]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

vim harbor.cfg

./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.

报错:
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需要重启
systemctl restart docker
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

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

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

/var/log/Harbor/目录下。

rm -rf /data/database/

rm -rf /data/registry/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值