个人文章记录

心情不好就去打两把游戏,你会发现刚刚心情还挺好的

0%

Docker 镜像仓库 Harbor 1.9.3 版本的安装和使用

Harbor 的 GitHub 地址:goharbor/harbor
Harbor 的官方文档地址:harbor-docs

一、环境要求

下面所列的都是基于截至当前版本和当前时间(2019-12),官方文档对于安装所列出的要求,不同版本还需要到官方网站查看历史或实时信息

1. 硬件要求

用于部署 Harbor 的最低和建议的硬件配置如下

资源 最低要求 推荐配置
cpu 2 个 cpu 4 个 cpu
内存 4G 8G
硬盘 40G 160G

因为 Harbor 的模块比较多,镜像通常也比较大,所以对资源的要求相对较高,建议选择一台单独的起码符合最低配置要求的主机来部署 Harbor。如果不能符合要求,比如硬盘的 40G 最低要求,则安装脚本在运行过程中会抛出 no space left on device 的错误,当然,你可以想办法绕过脚本检查,但不建议这么做。

2. 软件要求

软件 版本 说明
Docker 17.06.0-ce 版本或更高 可以查看我的 Docker 安装文章
Docker compose 1.18.0 版本或更高 可以查看我的 Docker compose 安装文章

3. 端口要求

端口 协议 描述
443 HTTPS 在使用 https 时,Harbor 出入口与核心 API 默认使用这个端口,可以在配置文件中修改
4443 HTTPS 连接到 Harbor 的 Docker 内容信任服务,只有在 Notary 可用时才需要,可以在配置文件中修改
80 HTTP Harbor 出入口与核心 API 默认使用这个端口,可以在配置文件中修改

二、使用离线方式安装 Harbor

本文的 Harbor 使用 Docker compose 来安装。Docker compose 等文件和各种相应配置在 1.9.3 版本中会在执行 install 脚本时自动生成。安装有两种方式,一种是在线安装,一种是离线安装。离线安装使用下载好的安装包进行,后续安装就不再需要下载。而在线安装程序从 Docker hub 下载 Harbor 映像。因此,安装程序的尺寸比较小。因为主机可能是离线状态,或者因为墙的因素,我这里选择的是离线安装方式进行安装。主要的安装过程是【下载安装包 –> 解压并配置 harbor.yml 文件 –> 按需使用合适的 options 选项运行 install.sh 脚本来安装和启动 Harbor】

1. 下载

先到 GitHub 上找到你想要安装的版本,然后下载 tgz 的包,如此处我下载的是 harbor-offline-installer-v1.9.3.tgz。注:如果因为网络问题下载不了这个安装包的,可以留言我发送备份的包

2. 解压并查看所有 Harbor 文件

使用 tar 提取安装程序包并进入解压的目录查看文件信息,可以看到,相比较之前比如 1.7.0 的版本,初始配置文件相当精简,我们只需要配置 harbor.xml 文件,后续的包括 Docker compose,common 目录及其下的所有配置文件,都会由 install 脚本自动生成,离线安装的方式中,install 也包含了 prepare 的步骤,他会到你完整安装包中查询并使用所需镜像。所以可以不用再运行 prepare 脚本,反而执行 prepare 还可能导致无法从 Docker hub 下载镜像或者下载很慢的问题(除非你本地已经存在了所需的 images)

1
2
3
4
5
6
$ tar xf harbor-offline-installer-v1.9.3.tgz
$ cd harbor
$ ls
LICENSE harbor.yml prepare
harbor.v1.9.3.tar.gz install.sh
$

3. 配置 harbor.yml

关于 harbor.yml 文件

harbor.yml 可以在安装程序包所包含的文件中,为 Harbor 设置系统级别参数。这些参数在运行 install.sh 脚本以安装或重新配置 Harbor 时生效。

关于 harbor.xml,根据官方文档的提示,下表列出了部署 Harbor 时必须设置的参数。默认情况下,所有必需的参数都不会在 harbor.yml 文件中注释。可选参数用注释 #。你不一定需要从提供的默认值中更改所需参数的值,但是这些参数必须保持注释。至少,你必须更新 hostname 参数。

重要信息

Harbor 不附带任何证书。在 1.9.x(含) 以下的版本中,默认情况下,Harbour 使用 HTTP 服务于注册表请求。这仅在测试或开发环境中可接受。在生产环境中,请始终使用 HTTPS。如果启用 Content Trust with Notary 来正确签名所有镜像,那么就必须使用 HTTPS。你可以使用由受信任的第三方 CA 签名的证书,也可以使用自签名证书。

开始配置

1)配置用于Harbour的服务器证书和密钥
此处就不赘述证书的申请了,这一步可以参考我关于 CA 证书申请的文章
申请证书后,你会获得如下目录的证书文件

1
2
3
4
5
|---cert
|---cert.pem
|---chain.pem
|---fullchain.pem
|---privkey.pem

获取 fullchain.pem 和 privkey.pem 文件后,将它们放入目录,例如 /data/cert

2)为 Docker 配置服务器证书,密钥和CA
为了安全,决定使用 https,不使用 http,所以注释 80 端口的部分。取消对 https 块的注释,并更新属性 certificateprivate_key,取消注释的时候注意 https 这里要顶格,前面不能有空格,快捷键解开注释的时候前面会留个空格,不注意根本发现不了,下面的字段也要对齐,不然后面脚本会执行错误。不要问我为什么知道…

1
2
3
4
https:
port: 443
certificate: /data/cert/fullchain.pem
private_key: /data/cert/privkey.pem

2)修改 hostname
这里使用你自己的域名(除非你是学习测试用途,不然最好不要使用 IP)

1
hostname: reg.mydomain.com

3)其他
其他的包括默认根目录,自定义端口号的使用,额外的数据库,日志配置等等都不是必要修改项。而且 harbor.yml 中各项各字段的注释也都比较清楚明白,我们按需配置即可。另外,默认的管理员密码在这里也不用修改,因为你在安装部署成功,并登录 Harbor 之后,第一个动作就应该是在 web 修改管理员密码。当然,第一个动作之后也别忘了配置邮件服务器,这对日后的维护工作,比如事件提醒和找回密码有奇效

4. 开始安装

运行 install.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
$ ./install.sh

[Step 0]: checking installation environment ...

Note: docker version: 19.03.5

Note: docker-compose version: 1.22.0

[Step 1]: loading Harbor images ...
47a4bb1cfbc7: Loading layer 34.26MB/34.26MB
f93c083be5bc: Loading layer 9.009MB/9.009MB
bf1b03029526: Loading layer 44.41MB/44.41MB
3395eb0db37a: Loading layer 2.048kB/2.048kB
630606b67737: Loading layer 3.072kB/3.072kB
b59ccb3639e4: Loading layer 44.41MB/44.41MB
Loaded image: goharbor/chartmuseum-photon:v0.9.0-v1.9.3
0801a4e2ebe9: Loading layer 2.56kB/2.56kB
e2b8ec162f1b: Loading layer 1.536kB/1.536kB
b68f30fd6125: Loading layer 73.35MB/73.35MB
fbe2002fa9e6: Loading layer 42.63MB/42.63MB
105f3dfa58eb: Loading layer 156.7kB/156.7kB
2395dbeaea9f: Loading layer 3.006MB/3.006MB
Loaded image: goharbor/prepare:v1.9.3
e80aab7b0662: Loading layer 63.49MB/63.49MB
f8b3d119e8a9: Loading layer 54.42MB/54.42MB
5f496dd4633b: Loading layer 5.632kB/5.632kB
9a2858fea72d: Loading layer 2.048kB/2.048kB
b7501a88cd95: Loading layer 2.56kB/2.56kB
c82e3969a2e2: Loading layer 2.56kB/2.56kB
7cac869555ad: Loading layer 2.56kB/2.56kB
cca577be763d: Loading layer 10.24kB/10.24kB
Loaded image: goharbor/harbor-db:v1.9.3
9a508de7d2b7: Loading layer 9.005MB/9.005MB
508382d944be: Loading layer 3.072kB/3.072kB
6e9e3cb5d33e: Loading layer 2.56kB/2.56kB
73ddaf5bf2d0: Loading layer 21.76MB/21.76MB
9a276490655f: Loading layer 21.76MB/21.76MB
Loaded image: goharbor/registry-photon:v2.7.1-patch-2819-2553-v1.9.3
4a0251807cc9: Loading layer 9.004MB/9.004MB
897af7ff7b63: Loading layer 6.239MB/6.239MB
3f4123d93010: Loading layer 14.9MB/14.9MB
4e0bdb09cba5: Loading layer 29.21MB/29.21MB
29c5283b24ee: Loading layer 22.02kB/22.02kB
6f09075fda1a: Loading layer 50.34MB/50.34MB
Loaded image: goharbor/notary-signer-photon:v0.6.1-v1.9.3
756ffb58fa91: Loading layer 7.037MB/7.037MB
733009a88a23: Loading layer 196.6kB/196.6kB
5cc05e9a6a60: Loading layer 172kB/172kB
eabe1b3a567b: Loading layer 15.36kB/15.36kB
dd83cfe76436: Loading layer 3.584kB/3.584kB
28bde9e732c9: Loading layer 10.84MB/10.84MB
Loaded image: goharbor/harbor-portal:v1.9.3
0eb10921ee80: Loading layer 78.25MB/78.25MB
74a7ce2b0571: Loading layer 3.072kB/3.072kB
7d193a1c54ab: Loading layer 59.9kB/59.9kB
d45413cea5ea: Loading layer 61.95kB/61.95kB
Loaded image: goharbor/redis-photon:v1.9.3
7c53cafac35a: Loading layer 337.9MB/337.9MB
c6bad7449208: Loading layer 119.8kB/119.8kB
Loaded image: goharbor/harbor-migrator:v1.9.3
4bb3c8da2619: Loading layer 50.3MB/50.3MB
fa60bb5fba7f: Loading layer 3.584kB/3.584kB
12a81f321c68: Loading layer 3.072kB/3.072kB
a94dcd551900: Loading layer 2.56kB/2.56kB
c684117da188: Loading layer 3.072kB/3.072kB
f14e11ea2c25: Loading layer 3.584kB/3.584kB
2e2e439cb618: Loading layer 12.29kB/12.29kB
Loaded image: goharbor/harbor-log:v1.9.3
99ce145e19e6: Loading layer 10.84MB/10.84MB
Loaded image: goharbor/nginx-photon:v1.9.3
da7ad744c6fd: Loading layer 16.4MB/16.4MB
a03cee9d5ed3: Loading layer 29.21MB/29.21MB
f5f37ba098b1: Loading layer 22.02kB/22.02kB
c6e237d0effb: Loading layer 51.85MB/51.85MB
Loaded image: goharbor/notary-server-photon:v0.6.1-v1.9.3
01d39dbfbd65: Loading layer 115.7MB/115.7MB
6d1186763503: Loading layer 12.31MB/12.31MB
293c8dc3d8c6: Loading layer 2.048kB/2.048kB
d0ffe0bb8b6b: Loading layer 48.13kB/48.13kB
c9f0ab7d8bbb: Loading layer 3.072kB/3.072kB
75c4044a879b: Loading layer 12.36MB/12.36MB
Loaded image: goharbor/clair-photon:v2.1.0-v1.9.3
b2329d5f99cf: Loading layer 12.77MB/12.77MB
17c27eb4f7f8: Loading layer 55.4MB/55.4MB
3c10f4815fc0: Loading layer 5.632kB/5.632kB
5fb810768754: Loading layer 36.35kB/36.35kB
376871497fae: Loading layer 55.4MB/55.4MB
Loaded image: goharbor/harbor-core:v1.9.3
2c38bb39e841: Loading layer 12.77MB/12.77MB
20ef2473aa7f: Loading layer 48.13MB/48.13MB
Loaded image: goharbor/harbor-jobservice:v1.9.3
bc526ee3d965: Loading layer 9.005MB/9.005MB
d880cff24505: Loading layer 3.072kB/3.072kB
adc7407dd64d: Loading layer 21.76MB/21.76MB
aa1f551082be: Loading layer 3.072kB/3.072kB
97bd8288c3d1: Loading layer 8.661MB/8.661MB
f184a9da0594: Loading layer 30.42MB/30.42MB
Loaded image: goharbor/harbor-registryctl:v1.9.3

[Step 2]: preparing environment ...
prepare base dir is set to /data/harbor
WARNING: IPv4 forwarding is disabled. Networking will not work.
Clearing the configuration file: /config/log/logrotate.conf
Clearing the configuration file: /config/log/rsyslog_docker.conf
Generated configuration file: /config/log/logrotate.conf
Generated configuration file: /config/log/rsyslog_docker.conf
Generated configuration file: /config/nginx/nginx.conf
Generated configuration file: /config/core/env
Generated configuration file: /config/core/app.conf
Generated configuration file: /config/registry/config.yml
Generated configuration file: /config/registryctl/env
Generated configuration file: /config/db/env
Generated configuration file: /config/jobservice/env
Generated configuration file: /config/jobservice/config.yml
Generated and saved secret to file: /secret/keys/secretkey
Generated certificate, key file: /secret/core/private_key.pem, cert file: /secret/registry/root.crt
Generated configuration file: /compose_location/docker-compose.yml
Clean up the input dir



[Step 3]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating registryctl ... done
Creating redis ... done
Creating registry ... done
Creating harbor-portal ... done
Creating harbor-db ... done
Creating harbor-core ... done
Creating nginx ... done
Creating harbor-jobservice ... done

✔ ----Harbor has been installed and started successfully.----

Now you should be able to visit the admin portal at https://reg.yourdomain.com.
For more details, please visit https://github.com/goharbor/harbor .

$

从输出日志来简要分析一下 install 脚本的执行过程

  1. 检查安装环境是否符合我们前面所说的环境要求
  2. 拉取相关镜像。可以看到,镜像非常多,如果不是完整的离线安装包而使用在线方式进行安装的,在国内环境当前的情况下,这个过程可能会让你抓狂
  3. 进行一系列包括 Docker compose 在内的各种配置、文件目录等环境准备工作
  4. 使用 Docker compose 启动 Harbor
  5. 安装成功

5. 配置外部访问

如果你的主机设置有防火墙或者安全组规则的话,注意要打开 harbor.yml 中配置的端口,我这里是 443

6. 登录 web

现在,设置好 Nginx 转发,就可以使用上面配置的域名,加上 admin 账号和配置文件中配置的默认 admin 密码就可以登录 Harbor 了。这里需要注意的是设置 Nginx 转发的时候要设置最大上传文档大小大一点,一般镜像几百兆是比较正常的,Nginx 默认的大小限制会让你上传失败

f9421dbac48a30336ba79185369cef49.png

三、Harbor 的使用

先在 web 上创建一个项目 test,再创建一个用户 test-user-name,密码为 Test123,再把这个用户设置为这个项目的管理员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ docker login -u test-user-name -p Test123 https://reg.yourdomain.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded
$ docker tag java:8 reg.yourdomain.com/test/java:my8
$ docker push reg.yourdomain.com/test/java:my8
35c20f26d188: Pushed
c3fe59dd9556: Pushed
6ed1a81ba5b6: Pushed
a3483ce177ce: Pushed
ce6c8756685b: Pushed
30339f20ced0: Pushed
0eb22bfb707d: Pushed
a2ae92ffcd29: Pushed
my8: digest: sha256:f841a2abd0422364ec94bb633a56707a38c330179f2bbccebd95f9aff4a36808 size: 2000
$ docker pull reg.yourdomain.com/test/java:my8

登录到 web 就可以看到这个上传到镜像了
而关于使用 maven 插件打包应用上传到 harbor,可以参考我的另外一篇文章

四、总结

19.x 版本的 Harbor 做了很多更友好的改变,非常值得推荐

-------------本文结束感谢您的阅读-------------

欢迎关注我的其它发布渠道