Harbor Installation and Configuration

版本 2.8.0

https://goharbor.io/docs/2.0.0/install-config/

harbor 安装条件

硬件条件 Hardware

下面列出了 需要的硬件资源, cpu ,内存,磁盘等:

Resource 	Minimum 	Recommended
CPU 	    2 CPU 	    4 CPU
Mem 	    4 GB 	    8 GB
Disk 	    40 GB 	    160 GB

软件条件 Software

安装harbor前需要提前安装,准备的软件环境,例如docker,docker-compose, openssl

Software 	    Version 	                    Description
Docker engine 	Version 17.06.0-ce+ or higher 	参考文档 https://docs.docker.com/engine/installation/
Docker Compose 	Version 1.18.0 or higher 	    参考文档 https://docs.docker.com/compose/install/
Openssl 	    Latest is preferred 	        Used to generate certificate and keys for Harbor

网络端口条件 Network ports

harbor 服务 需要如下的几个 网络端口,端口都是可以改的,不过默认,习惯 用下面的几个端口。

Port 	Protocol 	Description
443 	HTTPS 	    Harbor portal and core API accept HTTPS requests on this port. You can change this port in the configuration file.
80 	    HTTP 	    Harbor portal and core API accept HTTP  requests on this port. You can change this port in the configuration file.
4443 	HTTPS 	    Connections to the Docker Content Trust service for Harbor. Only required if Notary is enabled. You can change this port in the configuration file.

下载 Harbor Installer

可以从官方发布页面下载Harbor安装程序。下载 联机安装程序 或 脱机安装程序。

Online installer: 在线安装包很小,就是一个执行脚本加上几个配置文件 .

Offline installer: 离线安装包很多,其中多了镜像文件.

下面可以看到 安装 包大小,

1
2
3
4
5
6
7
8

harbor-offline-installer-v2.8.2.tgz 579 MB

harbor-offline-installer-v2.8.2.tgz.asc 833 Bytes 这个文件是 验证安装包文件是否是官方的,类似签名验证 ,检测文件真假的,是否是被篡改过的。

harbor-online-installer-v2.8.2.tgz 10.8 KB

harbor-online-installer-v2.8.2.tgz.asc 833 Bytes

然后选择需要的包,从 github 上下载 https://github.com/goharbor/harbor/releases

利用 wget 命令下载 文件:

1
2
3
4
5
wget https://github.com/goharbor/harbor/releases/download/v2.8.2/harbor-offline-installer-v2.8.2.tgz


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

文件正版验证:

1
2
3
gpg -v --keyserver hkps://keyserver.ubuntu.com --verify harbor-online-installer-version.tgz.asc

gpg -v --keyserver hkps://keyserver.ubuntu.com --verify harbor-offline-installer-version.tgz.asc

解压文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ tar -xvzf harbor-online-installer-v2.8.2.tgz
harbor/prepare
harbor/LICENSE
harbor/install.sh
harbor/common.sh
harbor/harbor.yml.tmpl

# 可以看到解压后文件内容:
tree
├── harbor
│   ├── common.sh
│   ├── harbor.yml.tmpl
│   ├── install.sh
│   ├── LICENSE
│   └── prepare
├── harbor-offline-installer-v2.8.2.tgz
└── harbor-online-installer-v2.8.2.tgz



配置 HTTPS 去访问 Harbor

若要配置HTTPS,必须创建SSL证书。您可以使用由受信任的第三方CA签名的证书,也可以使用自签名证书。本节介绍如何使用OpenSSL创建CA,以及如何使用CA对服务器证书和客户端证书进行签名。您可以使用其他CA提供程序,例如Let’s Encrypt。

生成证书

在生产环境中,您应该从CA获得证书。在测试或开发环境中,可以生成自己的CA。若要生成CA证书,请运行以下命令。 

Generate a CA certificate private key.

1
openssl genrsa -out ca.key 4096

Generate the CA certificate.

1
2
3
4
openssl req -x509 -new -nodes -sha512 -days 3650 \
-subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=yourdomain.com" \
-key ca.key \
-out ca.crt

生成服务器证书

 证书通常包含.crt文件和.key文件,例如yourdomain.com.crt和yourdomain.com.key。 

生成私钥文件 Generate a private key.

1
2
openssl genrsa -out yourdomain.com.key 4096

生成 csr 文件 Generate a certificate signing request (CSR).

1
2
3
4
5
openssl req -sha512 -new \
-subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=yourdomain.com" \
-key yourdomain.com.key \
-out yourdomain.com.csr

生成 x509 v3 文件

Generate an x509 v3 extension file.

1
2
3
4
5
6
7
8
9
10
11
12
cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1=yourdomain.com
DNS.2=yourdomain
DNS.3=hostname
EOF

生成 crt 文件

1
2
3
4
5
6
openssl x509 -req -sha512 -days 3650 \
-extfile v3.ext \
-CA ca.crt -CAkey ca.key -CAcreateserial \
-in yourdomain.com.csr \
-out yourdomain.com.crt

配置证书到服务器上

需要 ca.crt, yourdomain.com.crt, and yourdomain.com.key 文件到 harbor,docker 等上

复制 crt, key 文件到 harbor 服务器上

1
2
cp yourdomain.com.crt /data/cert/
cp yourdomain.com.key /data/cert/

转换生成 cert 文件

1
2
openssl x509 -inform PEM -in yourdomain.com.crt -out yourdomain.com.cert

将您的domain.com.crt转换为yourdomain.com.cert,供Docker使用。
Docker守护进程将.crt文件解释为CA证书,将.cert文件解释为客户端证书。

复制 cert, key, ca.crt 文件

1
2
3
cp yourdomain.com.cert /etc/docker/certs.d/yourdomain.com/
cp yourdomain.com.key /etc/docker/certs.d/yourdomain.com/
cp ca.crt /etc/docker/certs.d/yourdomain.com/
如果 默认的nginx端口443映射到不同的端口,
请创建文件夹/etc/docker/certs.d/yourdomain.com:port
或/etc/docker/certs.d/harbor_IP:port。 

重启 docker 服务

1
2
systemctl restart docker

1
2
3
4
5
/etc/docker/certs.d/
└── yourdomain.com:port
├── yourdomain.com.cert <-- Server certificate signed by CA
├── yourdomain.com.key <-- Server key signed by CA
└── ca.crt <-- Certificate authority that signed the registry certificate

重新部署或者重新配置 harbor的情况

如果你已经部署了harbor,但是采用的http协议的,需要如下操作

重新执行 prepare 脚本,去开启 https 配置

1
2
./prepare

如果harbor 正在运行,需要关闭掉,移除掉

1
docker-compose down -v

重启 harbor

1
docker-compose up -d

确认https是否可以正常访问

浏览器 打开 https://yourdomain.com,如果能正常访问说明配置没问题了。

查看其他docker实例机器上/etc/docker/daemon.json文件 没有配置上 -insecure-registry https://yourdomain.com., 并且 docker login yourdomain.com 可以正常登录,这样说明配置没问题了。

如果你的https端口默认不是 443 ,使用 docker login yourdomain.com:port 登录。

配置 Harbor YML 文件

主要配置 如下 几个地方

1
2
3
4
5
6
7
8
9
10
11
12
13
14
域名
hostname: reg.mydomain.com


证书
certificate: /your/certificate/path
private_key: /your/private/key/path


管理员密码,
harbor_admin_password

数据存储的位置
data_volume: /data

配置harbor内部组件之间通信使用 tls

执行安装脚本

1
2
3
4
5
6
7
8
9
# Harbor安装环境预处理
./prepare

# 安装并启动Harbor
./install.sh

# 检查是否安装成功(应该是启动9个容器)要在harbor目录下操作,否则docker-compose会又问题;
docker-compose ps

其他参考

https://www.cnblogs.com/kevingrace/p/6547616.html

https://www.qikqiak.com/post/harbor-code-analysis/

https://www.cnblogs.com/leffss/p/15621165.html

https://xie.infoq.cn/article/faa9ee456452891828cc080b8