我们已经快速安装和启动了Harbor,接下来我们进一步探索一些定制化的配置:

  • 安全上我们要启用https
  • 默认安装的Harbor将数据Volume挂载到主机的/data目录,我们需要做一些定制,本篇我们将其挂载到主机的其他目录。后边为了实现Harbor的高可用,再继续探索将Registry的存储驱动切换为CephFS或S3。
  • 将Harbor默认安装启动的mysql docker容器切换为外部的数据库, 我们的环境中已经有高可用的MySQL GR集群。后边我们再继续探索多个节点Harbor使用相同的外部高可用MySQL。

1.配置https访问

1.1 使用自签名证书

这里还是使用cfssl这个工具生成SSL证书和秘钥。

下载cfss工具:

1mkdir /data/cert/
2cd /data/cert/
3curl -sSL -o cfssl "https://pkg.cfssl.org/R1.2/cfssl_linux-amd64"
4curl -sSL -o cfssl "https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64"
5chmod +x cfssl cfssljson

创建ca-config.json文件:

 1{
 2    "signing": {
 3        "default": {
 4            "expiry": "87600h"
 5        },
 6        "profiles": {
 7            "harbor": {
 8                "expiry": "87600h",
 9                "usages": [
10                    "signing",
11                    "key encipherment",
12                    "server auth",
13                    "client auth"
14                ]
15            }
16        }
17    }
18}

创建文件ca-csr.json:

 1{
 2    "CN": "harbor",
 3    "key": {
 4        "algo": "rsa",
 5        "size": 2048
 6    },
 7    "names": [
 8        {
 9            "O": "harbor",
10            "OU": "harbor",
11            "L": "the internet"
12        }
13    ]
14}

生成CA证书和私钥:

1export PATH="$PATH:/data/cert"
2cfssl gencert -initca ca-csr.json | cfssljson -bare ca

harbor-csr.json

 1{
 2    "CN": "harbor",
 3    "hosts": [
 4      "127.0.0.1",
 5      "192.168.61.11"
 6    ],
 7    "key": {
 8        "algo": "rsa",
 9        "size": 2048
10    },
11    "names": [
12        {
13            "O": "harbor",
14            "OU": "harbor",
15            "L": "the internet"
16        }
17    ]
18}
1cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=harbor harbor-csr.json | cfssljson -bare harbor

1.2 在Harbor上启用https

我们的建议是不要在Harbor上启用https,而是在将Harbor放置到一个LB的后边,配置LB的SSL访问,并在LB上做SSL termination。 因此本节只是简单试验一下。

修改harbor.cfg:

1ssl_cert = /data/cert/harbor.pem
2ssl_cert_key = /data/cert/harbor-key.pem

修改docker-compose.yml文件:

 1proxy:
 2    image: vmware/nginx:1.11.5-patched
 3    container_name: nginx
 4    restart: always
 5    volumes:
 6      - ./common/config/nginx:/etc/nginx:z
 7    networks:
 8      - harbor
 9    ports:
10      - 8090:443

使用--with-notary参数安装Harbor:

1./install.sh --with-notary
2
3......
4✔ ----Harbor has been installed and started successfully.----
5Now you should be able to visit the admin portal at https://192.168.61.11:8090.

因为我们配置使用的是自签名的证书,因此需要将前面生成的CA证书拷贝到需要访问Harbor仓库的每个docker主机的/etc/docker/certs.d/registry-hostname/下。

1mkdir -p /etc/docker/certs.d/192.168.61.11:8090

拷贝到ca.pem到 /etc/docker/certs.d/192.168.61.11:8090/ca.crt

测试:

1docker login -u admin -p Harbor12345 192.168.61.11:8090
2Login Succeeded

1.3 在前置LB上做SSL termination

我们在Harbor前面再放置一个nginx作为接入层,在这个nginx上配置SSL,可以使用自签名证书,如果绑定了域名和固定IP,可以申请使用 Let’s Encrypt的免费HTTPS证书

下面的是前置nginx上做负载均衡和SSL termination的nginx配置文件片段:

 1upstream harbor {
 2    server 192.168.61.11:8090 weight=1 max_fails=2 fail_timeout=30s;
 3    server 192.168.61.12:8090 weight=1 max_fails=2 fail_timeout=30s;
 4}
 5
 6server {
 7    listen       443 ssl;
 8    server_name  harbor.frognew.com;
 9    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
10    ssl_certificate /etc/letsencrypt/live/harbor.frognew.com/fullchain.pem;
11    ssl_certificate_key /etc/letsencrypt/live/harbor.frognew.com/privkey.pem;
12    location / {
13        proxy_pass   http://harbor;
14        proxy_set_header   Host             $host;
15        proxy_set_header   X-Real-IP        $remote_addr;
16        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
17        proxy_set_header   X-Forwarded-Proto https;
18        client_max_body_size 300M;
19    }
20}
21
22server {
23    listen 80;
24    server_name harbor.frognew.com;
25    rewrite ^(.*)$  https://$host$1 permanent;
26}

接下来还需要对Harbor中的配置模板做一些修改,修改common/templates/nginx/nginx.http.conf,找到location /, location /v2/ and location /service/这3个配置块, 将这三个配置块中的proxy_set_header X-Forwarded-Proto $scheme;配置移除。

修改common/templates/registry/config.yml,修改auth.token.realm的地址:

1auth:
2  token:
3    issuer: harbor-token-issuer
4    realm: https://harbor.frognew.com/service/token
5    # realm: $ui_url/service/token
6    rootcertbundle: /etc/registry/root.crt
7    service: harbor-registry
1docker-compose down -v
2./prepare
3docker-compose up -d

2.挂载data Volume到其他目录

使用docker-compose安装的Harbor默认将数据Volume挂载到主机的/data目录下。 根据主机实际的分区我们可以将其挂载到其他目录下,如/home/harbor/data。

修改harbor.cfg:

1#The path of cert and key files for nginx, they are applied only the protocol is set to https
2ssl_cert = /home/harbor/data/cert/harbor.pem
3ssl_cert_key = /home/harbor/data/cert/harbor-key.pem
4
5#The path of secretkey storage
6secretkey_path = /home/harbor/data

再修改docker-compose.yml中相关volume中的对应路径到/home/harbor/data。

1docker-compose down -v
2./prepare
3docker-compose up -d

3.切换到外部的MySQL

首先在外部的MySQL中创建registry数据库和harbor用户:

1create database registry;
2
3grant all on registry.* to harbor@'%' identified by 'harbor';

将组件harbor-db中的registry的表和数据导入到外部新建的这个数据库。

修改harbor/common/templates/adminserver/env,修改下面的内容:

1DATABASE_TYPE=mysql
2MYSQL_HOST=192.168.61.11
3MYSQL_PORT=3306
4MYSQL_USR=harbor
5MYSQL_PWD=harbor
6MYSQL_DATABASE=registry
7
8RESET=true 
1docker-compose down -v

移除 docker-compose.yml中的mysql组件。

1./prepare
2docker-compose up -d

最后使用浏览器打开Harbor的Web UI确认访问正常。

参考