「Harbor」- 服务部署

Harbor 1.7.5, HTTPS

Use vmware Harbor to build Mirror Registry – Programmer Sought
Installation and Configuration Guide
Configuring Harbor with HTTPS Access

环境要求

Python,Docker,Docker Compose

安装 Harbor 服务

1)安装 Docker 环境;

2)安装 Docker Compose 工具;

3)安装 Certbot 命令,申请证书(1),配置自动续期及服务重载(2)

4)安装 Harobor 服务:

# 下载离线安装包
# https://github.com/goharbor/harbor/releases/tag/v1.7.5

# 修改 harbor.cfg 文件

# 执行 install.sh 进行安装

################################################################################
## 如果需要修改配置文件
################################################################################
docker-compose down -v
vim ./harbor.cfg
./prepare
docker-compose up -d

关于 Certbot 问题

1)在配置文件 ./harbor.cfg 中设置证书路径,使用 certbot 申请的证书(位于 /etc/letsencrypt/ 目录);
2)执行 ./prepare 命令将证书复制到 /path/to/harboar/common/config/nginx/cert/ 目录
3)Harbor 的 Nginx 服务使用在 /path/to/harboar/common/config/nginx/cert/ 中的证书

也就是说,在 cert renew 后,如果想要使用新的证书,需要进行“配置文件修改”步骤:

certbot renew
./prepare
docker-compose restart proxy #  Nginx 服务

Harbor 2.1.5, HTTPS

GitHub/goharbor/harbor
Installation and Configuration Guide
Use vmware Harbor to build Mirror Registry – Programmer Sought
harbor/README.md at master · goharbor/harbor
Harbor docs | Configure HTTPS Access to Harbor
Harbor docs | Run the Installer Script
Release v2.3.0 · goharbor/harbor

第一步、检查系统要求

On a Linux host,2 CPU 4 GB 40 GB(4 CPU,8 GB,160 GB)
docker 17.06.0-ce+
docker-compose 1.18.0+
Openssl Latest is preferred:我们不使用自签名,因此不依赖该工具。

网络端口:
1)443 HTTPS,80 HTTP,服务接口用于接收请求;
2)4443 HTTPS,在启用 Notary 时需要该端口,而我们无需使用;

第二步、下载安装包

我们使用在线安装包:

wget https://github.com/goharbor/harbor/releases/download/v2.1.5/harbor-online-installer-v2.1.5.tgz

tar xvf harbor-online-installer-v2.1.5.tgz

cd harbor

cp -v harbor.yml.tmpl harbor.yml # 后续步骤,多以修改 harbor.yml 为主

注意事项,我们这里未使用 .asc 进行安装校验。

第三步、申请 HTTPS 证书

通过 Certbot DNS Chanllenge 特性,来实现证书申请,相关细节不再详述;

然后,配置 Harboar 使用 HTTPS 证书:

# vim harbor.yml
...
# https related config
https:
  # https port for harbor, default is 443
  port: 443
  # The path of cert and key files for nginx
  certificate: /your/certificate/path
  private_key: /your/private/key/path
...

Harbor docs | Configure HTTPS Access to Harbor
执行 prepare 命令,来开启 nginx HTTPS 配置。

第四步、安装 Harbor 服务

修改其他必要配置:

...
hostname: reg.mydomain.com
...
harbor_admin_password: Harbor12345
...
data_volume: /data
...

启动服务:

./install.sh --with-notary --with-clair --with-chartmuseum

# 如果无需 notary clair Chart 服务,仅作为 Registry 服务,则:
./install.sh

# 补充说明:
# chartmuseum 是支持作为 Helm 仓库;
# clair 是容器的漏洞静态分析工具;
# notray 是镜像的签名工具,用来保证镜像在 pull,push 和传输过程中的一致性和完整性。避免中间人攻击,避免非法的镜像更新和运行。

第五步、访问验证

打开浏览器,访问验证 Harbor 服务正常运行。

[Sol.] the protocol must be https when Harbor is deployed with Notary

[Step 2]: preparing environment ...

[Step 3]: preparing harbor configs ...
prepare base dir is set to /data/harbor/application/registry
ERROR:root:Error: the protocol must be https when Harbor is deployed with Notary
Clearing the configuration file: /config/portal/nginx.conf

如果启用 Notary 服务,则必须使用 HTTPS 服务。

关于 Nginx 反向代理

默认情况,Harbor 占用 80 443 端口。如果修改为其他端口,并使用 Nginx 方向代理,有以下两种配置方法:

方法一、通过 proxy_pass http 代理

类似以往 proxy_pass 反向代理:nginx1 (https, letsencrypt) ⇒ harbor proxy (nginx, http) ⇒ harbor ui/registry

但是,在 Harbor 中,这种拓扑需要进行如下配置:
1)Running Harbor with HTTP behind a HTTPS Reverse Proxy (nginx) · Issue #3114 · goharbor/harbor
2)Harbor docs / Using nginx or Load Balancing

但是,这种部署方案会给日后的维护带来困难,并且需要在文档中清楚明了的记录(我们未采用该方案)。

此外,如果使用 Harbor 的 Notary 特性,则 Harbor 必须以开启 HTTPS 监听。

如果 Harboar 继续监听 HTTPS 协议,反向代理到 HTTPS 端口,则需要维护两套 TLS 证书:1)Harbor 使用的 TLS 证书;2)前端 Nginx 使用的 TLS 证书。当证书续期时,需要同时重启 Harbor 与 Nginx 服务。

方法二、通过 steam 负载均衡

修改 /etc/nginx/nginx.conf 文件,添加如下配置:

stream {
    upstream harbor_registry {
        server 127.0.0.1:29443;
    }
    server {
        listen 0.0.0.0:443;
        proxy_pass harbor_registry;
    }
}

// 注意事项:
// 指令 steam 在 main 中,即直接写入 /etc/nginx/nginx.conf 文件。不要写入 http {} 中

但是,这种方法会丢失客户端的真实 IP 地址。

使用 Harbor 作为 Registry Mirror 服务(WIP)

根据 harbor/Configure_mirror.md at v2.1.5 文档,虽然 Harobr 支持作为 Registry Mirror 服务,但是我们实在是没有找到 Deploy/templates/registry/config.yml 配置文件的目录。此外作为 Registry Mirror 的 Harobr 服务,无法接收镜像推送请求。

根据 Configure Proxy Cache 文档,但是 Harbor 支持创建“Cache Project”,该 Project 用于第三方缓存镜像。如果某个镜像不存在,将自动访问 DockerHub 拉取镜像(或其他镜像仓库)。该方案并不能替代 Registry Mirror 服务。

因此,我们未使用 Harbor 作为 Registry Mirror 服务,而选择自行搭建;

补充说明:
1)我们对 Cache Project 的理解还有欠缺,比如 Project Name 是否需要和 DockerHub 名称匹配;
2)但是它绝对不像我们理解的那种 Registry Mirror 服务,而是作为中间曾并需要进行配置;