用Harbor实现容器镜像仓库的管理和运维<三>

harbor 安装(HTTPS模式)

问题1:
Error response from daemon: Get https://harbor1.test.com/v1/users/: x509: certificate signed by unknown authority
用命令行登陆harbor时报错,具体如下:

1
2
3
4
[root@harbor1 harbor]# docker login harbor1.test.com
Username (admin): admin
Password:
Error response from daemon: Get https://harbor1.test.com/v1/users/: x509: certificate signed by unknown authority

解决方法:
由于自创的证书还没有加入系统级别信任,因此需要将证书加入系统级别信任

1
2
[root@harbor1 harbor]# cp /data/cert/server.crt /etc/pki/ca-trust/source/anchors/reg.test.com.crt
[root@harbor1 harbor]# update-ca-trust

在加入了根证书后用浏览器访问出现证书错误,于是用脚本再次生成新的证书并导入依旧证书错误,终于在harbor.cfg中找到了原因(没有仔细查看配置说明,走了好多弯路),配置文件中标注了如果使用自签名将核实远程证书设为off

1
2
3
4
5
[root@harbor1 harbor]# vi harbor.cfg
...
#Determine whether the job service should verify the ssl cert when it connects to a remote registry.
#Set this flag to off when the remote registry uses a self-signed or untrusted certificate.
verify_remote_cert = on <!--将这里的on改为off-->

在加入了根证书后用浏览器访问出现证书错误,于是用脚本再次生成新的证书并导入依旧证书错误,终于在harbor.cfg中找到了原因(没有仔细查看配置说明,走了好多弯路),配置文件中标注了如果使用自签名将核实远程证书设为off

1
2
3
4
5
[root@harbor1 harbor]# vi harbor.cfg
...
#Determine whether the job service should verify the ssl cert when it connects to a remote registry.
#Set this flag to off when the remote registry uses a self-signed or untrusted certificate.
verify_remote_cert = on <!--将这里的on改为off-->

harbor镜像同步

为什么需要镜像同步


由于对镜像的访问是一个核心的容器概念,在实际使用过程中,一个镜像库可能是不够用的,下例情况下,我们可能会需要部署多个镜像仓库:

国外的公有镜像下载过慢,需要一个中转仓库进行加速
容器规模较大,一个镜像仓库不堪重负
对系统稳定性要求高,需要多个仓库保证高可用性
镜像仓库有多级规划,下级仓库依赖上级仓库
更常用的场景是,在企业级软件环境中,会在软件开发的不同阶段存在不同的镜像仓库,

在开发环境库,开发人员频繁修改镜像,一旦代码完成,生成稳定的镜像即需要同步到测试环境。
在测试环境库,测试人员对镜像是只读操作,测试完成后,将镜像同步到预上线环境库。
在预上线环境库,运维人员对镜像也是只读操作,一旦运行正常,即将镜像同步到生产环境库。
在这个流程中,各环境的镜像库之间都需要镜像的同步和复制。


Harbor的镜像同步机制


有了多个镜像仓库,在多个仓库之间进行镜像同步马上就成为了一个普遍的需求。比较传统的镜像同步方式,有两种:

第一种方案,使用Linux提供的RSYNC服务来定义两个仓库之间的镜像数据同步。
第二种方案,对于使用IaaS服务进行镜像存储的场景,利用IaaS的配置工具来对镜像的同步进行配置。
这两种方案都依赖于仓库所在的存储环境,而需要采用不同的工具策略。Harbor则提供了更加灵活的方案来处理镜像的同步,其核心是三个概念:

用Harbor自己的API来进行镜像下载和传输,作到与底层存储环境解耦。
利用任务调度和监控机制进行复制任务的管理,保障复制任务的健壮性。在同步过程中,如果源镜像已删除,Harbor会自动同步删除远端的镜像。在镜像同步复制的过程中,Harbor会监控整个复制过程,遇到网络等错误,会自动重试。
提供复制策略机制保证项目级的复制需求。在Harbor中,可以在项目中创建复制策略,来实现对镜像的同步。与Docker Registry的不同之处在于,Harbor的复制是推(PUSH)的策略,由源端发起,而Docker Registry的复制是拉(PULL)的策略,由目标端发起。


harbor_replication-1

Harbor的多级部署


在实际的企业级生产运维场景,往往需要跨地域,跨层级进行镜像的同步复制,比如集团企业从总部到省公司,由省公司再市公司的场景。
这一部署场景可简化如下图:


harbor_replication-2
更复杂的部署场景如下图:
harbor_replication-3

镜像同步配置

环境介绍
Master:192.168.10.19 —> harbor1.test.com
Slave:192.168.10.21 —> harbor2.test.com
两个harbor节点都采用的HTTP模式
首先登陆到harbor的网页,进入到系统管理->复制管理
harbor_sync_1
点击“+目标”,创建复制的目标,并测试连接。
harbor_sync_2
如下为创建好一个复制目标
harbor_sync_3
目标创建好之后,返回到项目并且进入需要同步的项目中,并进入复制规则页面
harbor_sync_4
点击“+复制规则”,创建镜像同步的规则并且再次测试连接是否成功。
harbor_sync_5
待测试连接成功后,点击确定。创建好的复制规则如下:
harbor_sync_6
可以看到在页面下方的复制任务栏里会将需要同步的镜像作为任务显示出来,,这里可以看到所有任务的状态都变为finished,说明同步已经完成。
登陆到Slave节点harbor2.test.com,可以看到在原本只有一个library项目的基础上,自动增加了一个cqf项目,并且里面已经包含了相关的镜像,同步成功。
harbor_sync_7


问题1


在创建复制目标测试连接时一直报“测试连接失败”。
解决方法:
由于harbor创建时使用的域名访问的方式,因此经检查发现时harbor运行的container无法解析harbor2.test.com,导致无法将相应的请求送达的原因。
因此需要登陆到UI所在container,修改/etc/resolv.conf和/etc/hosts

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
[root@harbor2 ui]# docker exec -it 2f30038f9592 /bin/bash <!--连接上UI所在的container-->
root [ /harbor ]# vi /etc/resolv.conf
search localdomain test.com
nameserver 127.0.0.11
options ndots:0
nameserver 192.168.10.2 <!--添加192.168.10.2-->
"/etc/resolv.conf" 4L, 90C written
root [ /harbor ]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.6 2f30038f9592
root [ /harbor ]# echo "192.168.10.19 harbor1.test.com" >> /etc/hosts
root [ /harbor ]# echo "192.168.10.21 harbor2.test.com" >> /etc/hosts
root [ /harbor ]# curl harbor1.test.com <!--测试解析效果-->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Harbor</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico?v=2">
</head>
<body style="overflow-y: hidden;">
<harbor-app>
<div class="spinner spinner-lg app-loading">
Loading...
</div>
</harbor-app>
<link rel="stylesheet" href="/static/clarity-ui.min.css">
<link rel="stylesheet" href="/static/clarity-icons.min.css">
<link rel="stylesheet" href="/static/styles.css">
<script src="/static/mutationobserver.min.js"></script>
<script src="/static/custom-elements.min.js"></script>
<script src="/static/clarity-icons.min.js"></script>
<script src="/static/build.min.js"></script>
</body>
</html>
root [ /harbor ]# curl harbor2.test.com
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Harbor</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico?v=2">
</head>
<body style="overflow-y: hidden;">
<harbor-app>
<div class="spinner spinner-lg app-loading">
Loading...
</div>
</harbor-app>
<link rel="stylesheet" href="/static/clarity-ui.min.css">
<link rel="stylesheet" href="/static/clarity-icons.min.css">
<link rel="stylesheet" href="/static/styles.css">
<script src="/static/mutationobserver.min.js"></script>
<script src="/static/custom-elements.min.js"></script>
<script src="/static/clarity-icons.min.js"></script>
<script src="/static/build.min.js"></script>
</body>
</html>

修改后,测试连接可以通过


问题2


在创建好复制规则后,在复制任务中所有任务的状态都变为error,镜像复制失败

解决方法:
检查harbor的日志发现有如下报错信息:

1
2
3
4
5
6
7
[root@harbor1 2017-12-11]# pwd
/var/log/harbor/2017-12-11
[root@harbor1 2017-12-11]# cat registry.log
...
Dec 11 14:35:01 172.18.0.1 registry[10164]: time="2017-12-11T06:35:01.800397146Z" level=debug msg="authorizing request" go.version=go1.7.3 http.request.host="registry:5000" http.request.id=ca0deea4-ccf1-4c0c-b33d-290a099faa40 http.request.method=GET http.request.remoteaddr="172.18.0.8:47324" http.request.uri="/v2/" http.request.useragent="Go-http-client/1.1" instance.id=3f8229fe-cb32-464c-b9a4-9a20ae9aef4a service=registry version=v2.6.2
Dec 11 14:35:01 172.18.0.1 registry[10164]: time="2017-12-11T06:35:01.800720441Z" level=warning msg="error authorizing context: authorization token required" go.version=go1.7.3 http.request.host="registry:5000" http.request.id=ca0deea4-ccf1-4c0c-b33d-290a099faa40 http.request.method=GET http.request.remoteaddr="172.18.0.8:47324" http.request.uri="/v2/" http.request.useragent="Go-http-client/1.1" instance.id=3f8229fe-cb32-464c-b9a4-9a20ae9aef4a service=registry version=v2.6.2
...

同时点击复制任务的日志查看,有如下报错信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
2017-12-11T05:19:33Z [INFO] initializing: repository: cqf/centos, tags: [], source URL: http://registry:5000, destination URL: http://harbor2.test.com, insecure: false, destination user: admin
2017-12-11T05:19:33Z [INFO] initialization completed: project: cqf, repository: cqf/centos, tags: [1.0], source URL: http://registry:5000, destination URL: http://harbor2.test.com, insecure: false, destination user: admin
2017-12-11T05:19:35Z [ERROR] [transfer.go:204]: an error occurred while creating project cqf on http://harbor2.test.com with user admin : failed to create project cqf on http://harbor2.test.com with user admin: 462 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>462 Forbidden Region - DOSarrest Internet Security</title>
<link href="/DOAError/assets/css/core.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="/DOAError/assets/images/favicon.ico" /><link rel="apple-touch-icon" href="/DOAError/assets/images/icon-protection.png" />
<script>
function id_process(n){for(var t=n+"=",r=document.cookie.split(";"),e=0;e<r.length;e++){for(var g=r[e];" "==g.charAt(0);)g=g.substring(1,g.length);if(0==g.indexOf(t))return g.substring(t.length,g.length)}return null};
</script>
</head>

报错信息与前面的测试连接失败比较类似,因此将Master节点上的所有harbor容器全都修改下/etc/resolv.conf和/etc/hosts两个文件,后验证成功,同步正常。
PS
为什么会出现这个无法解析的问题,待后续再解决。

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