Harbor新建目标时测试连接失败(no such host)
环境是下面这样,一共两台主机:
a.harbor.com(172.16.20.80) -----> b.harbor.com(172.16.20.81)
harbor版本:v2.5.5
搭建了两台harbor,准备实现双向复制,但是在a.harbor.com上新建目标时无法连接到b.harbor.com:
查看harbor-core的日志,日志显示无法解析主机:
$ docker logs -f harbor-core
2023-02-02T04:09:11Z [ERROR] [/lib/http/error.go:54]: {"errors":[{"code":"UNKNOWN","message":"unknown: Get \"https://b.harbor.com/api/version\": dial tcp: lookup b.harbor.com on 127.0.0.11:53: no such host"}]}
2023-02-02T04:11:05Z [ERROR] [/lib/http/error.go:54]: {"errors":[{"code":"UNKNOWN","message":"unknown: Get \"https://b.harbor.com/api/version\": dial tcp: lookup b.harbor.com on 127.0.0.11:53: no such host"}]}
原因就是harbor-core容器无法解析主机名。
$ docker exec -u root -it harbor-core ping b.harbor.com
ping: b.harbor.com: Name or service not known
在主机a.harbor.com的harbor-core容器中添加主机记录:
$ docker exec -u root -it harbor-core bash
root [ /harbor ]# echo "172.16.20.81 b.harbor.com" >> /etc/hosts
root [ /harbor ]# exit
同样,在主机b.harbor.com的harbor-core容器中也添加主机记录:
$ docker exec -u root -it harbor-core bash
root [ /harbor ]# echo "172.16.20.80 a.harbor.com" >> /etc/hosts
root [ /harbor ]# exit
再次在a.harbor.com上测试连接:
上面的方法只能临时解决问题,当容器重新启动后主机记录就会丢失。所以需要修改docker-compose持久化保存。
修改a.harbor.com的docker-compose.yml。extra_hosts的作用就是在容器的/etc/hosts中添加记录。
# 在harbor的工作目录下
# 停止容器
$ docker-compose down
# 修改docker-compose.yml文件,在core和jobservice的最后添加extra_hosts。格式和/etc/hosts是相反的。
# core对应harbor-core容器,jobservice对应harbor-jobservice容器
# harbor-jobservice负责镜像复制工作,如果不修改后续的复制会失败。
$ vim docker-compose.yml
core:
image: goharbor/harbor-core:v2.5.5
container_name: harbor-core
... ...
extra_hosts:
- "a.harbor.com:172.16.20.80"
- "b.harbor.com:172.16.20.81"
jobservice:
image: goharbor/harbor-jobservice:v2.5.5
container_name: harbor-jobservice
... ...
extra_hosts:
- "a.harbor.com:172.16.20.80"
- "b.harbor.com:172.16.20.81"
# 启动容器
$ docker-compose up -d
# 确认有刚才添加的主机记录
$ docker exec -u root -it harbor-core 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.16.20.80 a.harbor.com
172.16.20.81 b.harbor.com
172.20.0.8 09f16e15e9be
在b.harbor.com上也修改docker-compose.yml:
$ vim docker-compose.yml
core:
image: goharbor/harbor-core:v2.5.5
container_name: harbor-core
... ...
extra_hosts:
- "a.harbor.com:172.16.20.80"
- "b.harbor.com:172.16.20.81"
jobservice:
image: goharbor/harbor-jobservice:v2.5.5
container_name: harbor-jobservice
... ...
extra_hosts:
- "a.harbor.com:172.16.20.80"
- "b.harbor.com:172.16.20.81"
# 确认有刚才添加的主机记录
$ docker exec -u root -it harbor-core 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.16.20.80 a.harbor.com
172.16.20.81 b.harbor.com
172.18.0.8 de168afb1bac