企业级私人仓库Harbor搭建!

一、 Harbor概述

Harbor是VMware公司的开源级的企业级DockerRegistry(仓库)项目,项目地址为 https://github.com/vmware/harbor.

Harbor的目标是帮助用户迅速搭建一个企业级的DockerRegistry服务。

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

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

1.1 harbor的特性

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

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

3、审计管理:所有这怒地镜像仓库的错都可以被记录追溯,用于审计管理

4、基于镜像的复制策略:镜像可以在多个Harbor实例之间进行复制。

5、支持LDAP认证:Harbor的用户授权可以使用已经存在的用户。

6、镜像删除和垃圾回收:image可以被删除并且回收image占用的空间。

7、简单的部署功能:harbor提供了online、offline安装,此外还提供了virtualappliance安装

8、harbor和docker registry的关系:harbor实质上是对docker registry做了封装,扩展了自己的业务模板

1.2 Harbor 架构组件

在这里插入图片描述

  • Proxy:对应启动组件nginx。它是一个nginx反向代理,代理Notary client(镜像认证)、Docker client(镜像上传下载等)和浏览器的访问请求(Core Service)给后端的各服务;

  • UI(Core Service)

    对应启动组件harbor-ui。底层数据存储使用mysql数据库,主要提供了四个子功能:

    • UI:一个web管理页面ui;
    • API:Harbor暴露的API服务;
    • Auth:用户认证服务,decode后的token中的用户信息在这里进行认证;auth后端可以接db、ldap、uaa三种认证实现;
    • Token服务(上图中未体现):负责根据用户在每个project中的role来为每一个docker push/pull命令issuing一个token,如果从docker client发送给registry的请求没有带token,registry会重定向请求到token服务创建token。
  • Registry:对应启动组件registry。负责存储镜像文件,和处理镜像的pull/push命令。Harbor对镜像进行强制的访问控制,Registry会将客户端的每个pull、push请求转发到token服务来获取有效的token。

  • Admin Service:对应启动组件harbor-adminserver。是系统的配置管理中心附带检查存储用量,ui和jobserver启动时候需要加载adminserver的配置;

  • Job Sevice:对应启动组件harbor-jobservice。负责镜像复制工作的,他和registry通信,从一个registry pull镜像然后push到另一个registry,并记录job_log;

  • Log Collector:对应启动组件harbor-log。日志汇总组件,通过docker的log-driver把日志汇总到一起;

  • Volnerability Scanning:对应启动组件clair。负责镜像扫描

  • Notary:对应启动组件notary。负责镜像认证

  • DB:对应启动组件harbor-db,负责存储project、 user、 role、replication、image_scan、access等的metadata数据。

1.3 Harbor配置文件参数

  • 所需参数

这些参数需要在配置文件 Harbor.cfg 中设置。

如果用户更新它们并运行 install.sh脚本重新安装 Harbour,

参数将生效。具体参数如下:

  1. hostname:用于访问用户界面和 register 服务。它应该是目标机器的 IP 地址或完全限 定的域名(FQDN)
    例如 192.168.195.128 或 hub.kgc.cn。不要使用 localhost 或 127.0.0.1 为主机名。

  2. ui_url_protocol:(http 或 https,默认为 http)用于访问 UI 和令牌/通知服务的协议。如果公证处于启用状态,则此参数必须为 https。

  3. max_job_workers:镜像复制作业线程。

  4. db_password:用于db_auth 的MySQL数据库root 用户的密码。

  5. customize_crt:该属性可设置为打开或关闭,默认打开。打开此属性时,准备脚本创建私钥和根证书,用于生成/验证注册表令牌。
    当由外部来源提供密钥和根证书时,将此属性设置为 off。

  6. ssl_cert:SSL 证书的路径,仅当协议设置为 https 时才应用。

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

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

  • 可选参数

这些参数对于更新是可选的,即用户可以将其保留为默认值,并在启动 Harbor 后在 Web UI 上进行更新。

如果进入 Harbor.cfg,只会在第一次启动 Harbor 时生效,随后对这些参数 的更新,Harbor.cfg 将被忽略。

注意:如果选择通过UI设置这些参数,请确保在启动Harbour后立即执行此操作。具体来说,必须在注册或在 Harbor 中创建任何新用户之前设置所需的

  1. auth_mode。当系统中有用户时(除了默认的 admin 用户),auth_mode 不能被修改。

  2. Email:Harbor需要该参数才能向用户发送“密码重置”电子邮件,并且只有在需要该功能时才需要。
    请注意,在默认情况下SSL连接时没有启用。如果SMTP服务器需要SSL,但不支持STARTTLS,那么应该通过设置启用SSL email_ssl = TRUE。

  3. harbour_admin_password:管理员的初始密码,只在Harbour第一次启动时生效。之后,此设置将被忽略,并且应 UI中设置管理员的密码。
    请注意,默认的用户名/密码是 admin/Harbor12345。

  4. auth_mode:使用的认证类型,默认情况下,它是 db_auth,即凭据存储在数据库中。对于LDAP身份验证,请将其设置为 ldap_auth。

  5. self_registration:启用/禁用用户注册功能。禁用时,新用户只能由 Admin 用户创建,只有管理员用户可以在 Harbour中创建新用户。
    注意:当 auth_mode 设置为 ldap_auth 时,自注册功能将始终处于禁用状态,并且该标志被忽略。

  6. Token_expiration:由令牌服务创建的令牌的到期时间(分钟),默认为 30 分钟。

  7. project_creation_restriction:用于控制哪些用户有权创建项目的标志。默认情况下, 每个人都可以创建一个项目。
    如果将其值设置为“adminonly”,那么只有 admin 可以创建项目。

  8. verify_remote_cert:打开或关闭,默认打开。此标志决定了当Harbor与远程 register 实例通信时是否验证 SSL/TLS 证书。
    将此属性设置为 off 将绕过 SSL/TLS 验证,这在远程实例具有自签名或不可信证书时经常使用。

另外,默认情况下,Harbour 将镜像存储在本地文件系统上。在生产环境中,可以考虑 使用其他存储后端而不是本地文件系统,

如 S3、Openstack Swif、Ceph 等。但需要更新 common/templates/registry/config.yml 文件。

二、Harbor 部署

2.1 Harbor搭建

# 安装compose包以及harbor
[root@harbor ~]# curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
[root@harbor ~]# tar zxf harbor-offline-installer-v1.2.2.tgz -C /usr/local/
[root@harbor ~]# vi /usr/local/harbor/harbor.cfg 
hostname = 10.0.0.101		# 第5行修改
[root@harbor ~]# sh /usr/local/harbor/install.sh		# 启动harbor,install.sh会直接调用docker-compose.yml,进行编排容器服务
[root@harbor ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
mysql                       5.6                 2548f5cf1d6e        18 hours ago        4.92GB
compose_nginx_nginx         latest              72a0276d43c0        30 hours ago        537MB
centos                      7                   8652b9f0cb4c        2 weeks ago         204MB
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@harbor 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 

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2.2 上传和下载镜像

2.2.1本地上传及下载

# 登录
[root@harbor ~]# 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@harbor ~]# docker pull nginx

# 镜像打标签
[root@harbor ~]# docker tag nginx 10.0.0.101/public-web/nginx:v1

# 上传镜像
[root@harbor ~]# docker push 10.0.0.101/public-web/nginx:v1
The push refers to repository [10.0.0.101/public-web/nginx]
7e914612e366: Pushed 
f790aed835ee: Pushed
850c2400ea4d: Pushed 
7ccabd267c9f: Pushed 
f5600c6330da: Pushed 
v1: digest: sha256:99d0a53e3718cef59443558607d1e100b325d6a2b678cd2a48b05e5e22ffeb49 size: 1362

# 删除镜像重新下载
[root@harbor ~]# docker rmi 10.0.0.101/public-web/nginx:v1
[root@harbor ~]# docker rmi nginx
[root@harbor ~]# docker pull 10.0.0.101/public-web/nginx:v1

在这里插入图片描述

在这里插入图片描述

2.2.2 客户端上传及下载

# 指明docker仓库地址
[root@client ~]# vi /usr/lib/systemd/system/docker.service 
ExecStart=/usr/bin/dockerd -H fd:// --insecure-registry 10.0.0.101 --containerd=/run/containerd/containerd.sock
[root@client ~]# systemctl daemon-reload
[root@client ~]# systemctl restart docker

在这里插入图片描述

# 登录并下载镜像
[root@client ~]# docker login -u admin -p Harbor12345 http://10.0.0.101
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 10.0.0.101/public-web/nginx:v1
v1: Pulling from public-web/nginx
852e50cd189d: Pull complete 
571d7e852307: Pull complete 
addb10abd9cb: Pull complete 
d20aa7ccdb77: Pull complete 
8b03f1e11359: Pull complete 
Digest: sha256:99d0a53e3718cef59443558607d1e100b325d6a2b678cd2a48b05e5e22ffeb49
Status: Downloaded newer image for 10.0.0.101/public-web/nginx:v1
10.0.0.101/public-web/nginx:v1

# 打便签并上传镜像
[root@client ~]# docker tag httpd 10.0.0.101/public-web/httpd:v1
[root@client ~]# docker push 10.0.0.101/public-web/httpd:v1
The push refers to repository [10.0.0.101/public-web/httpd]
c74375f55aa8: Pushed 
211b9be55a20: Pushed 
aa0b3e4b6d3b: Pushed 
540171a10c83: Pushed 
f5600c6330da: Mounted from public-web/nginx 
v1: digest: sha256:4c7c70926e2f2e10a9f78b63f344c83ae97a22c7fefa96afed46c63e4e607c51 size: 1366

在这里插入图片描述

三、Harbor管理

3.1 修改 Harbor.cfg 配置文件

  • 修改harbor.cfg配置文件,先停止现有的Harbor实例并更新harbor.cfg中的配置,然后再运行prepare脚重新加载配置,最后重新创建并且启动harbor的实例。
[root@harbor harbor]# pwd
/usr/local/harbor
[root@harbor harbor]# docker-compose down -v
Stopping nginx              ... done
Stopping harbor-jobservice  ... done
Stopping harbor-ui          ... done
Stopping registry           ... done
Stopping harbor-adminserver ... done
Stopping harbor-db          ... done
Stopping harbor-log         ... done
Removing nginx              ... done
Removing harbor-jobservice  ... 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@harbor harbor]# vi harbor.cfg 
[root@harbor 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 harbor]# systemctl restart docker
[root@harbor harbor]# docker-compose up -d
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating registry           ... done
Creating harbor-adminserver ... done
Creating harbor-db          ... done
Creating harbor-ui          ... done
Creating harbor-jobservice  ... done
Creating nginx              ... done

3.2 迁移harbor数据

1、先关闭harbor仓库:docker-compose down -v
2、拷贝harbor数据:
'//持久数据,如镜像,数据库等在宿主机的/data/目录下,日志在宿主机的/var/log/Harbor/目录下'
/data/database/ 数据库内容,比如身份验证数据

/data/registry/ 镜像文件内容
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值