搭建Harbor倉庫
一、安裝 docker
rm -rf /etc/yum.repos.d/* #CentOS 7 安裝docker依賴三個yum源:Base,Extras,docker-ce wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum clean all yum -y install docker-ce #啟動Docker服務並設置開機自啟 systemctl enable --now docker #查看docker版本 docker version
二、安裝docker compose
通過pip安裝
yum -y install python3 pip3 install --upgrade pip pip3 install docker-compose
三、下載Harbor安裝包並解壓縮
wget https://storage.googleapis.com/harbor-releases/release-1.7.0/harbor-offline-installer-v1.7.6.tgz mkdir /apps tar xvf harbor-offline-installer-v1.7.6.tgz -C /apps
四、修改harbor配置文件
[root@Harbor1 ~]#vim /apps/harbor/harbor.cfg #只需要修改下面兩行 hostname = 192.168.18.71 #修改此行,指向當前主機IP 或 FQDN harbor_admin_password = 123456 #修改此行指定harbor登錄用戶admin的密碼,默認用戶/密 碼:admin/Harbor12345
五、運行harbor安裝腳本
[root@Harbor1 ~]# /apps/harbor/install.sh
安裝harbor后會自動開啟很多相關容器
[root@Harbor1 ~] # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e03e58e5dc0 goharbor/nginx-photon:v1.7.6 "nginx -g 'daemon of…" 13 seconds ago Up 12 seconds (health: starting) 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp, 0.0.0.0:4443->4443/tcp, :::4443->4443/tcp nginx
2364ac8789ae goharbor/harbor-portal:v1.7.6 "nginx -g 'daemon of…" 14 seconds ago Up 12 seconds (health: starting) 80/tcp harbor-portal
f440ce09e7c1 goharbor/harbor-jobservice:v1.7.6 "/harbor/start.sh" 14 seconds ago Up 12 seconds harbor-jobservice
fede4fceed84 goharbor/harbor-core:v1.7.6 "/harbor/start.sh" 15 seconds ago Up 13 seconds (health: starting) harbor-core
dff6b5c68d3c goharbor/harbor-adminserver:v1.7.6 "/harbor/start.sh" 16 seconds ago Up 14 seconds (health: starting) harbor-adminserver
01f192ad44d0 goharbor/harbor-db:v1.7.6 "/entrypoint.sh post…" 16 seconds ago Up 14 seconds (health: starting) 5432/tcp harbor-db
16e252af4e21 goharbor/redis-photon:v1.7.6 "docker-entrypoint.s…" 16 seconds ago Up 14 seconds 6379/tcp redis
1b863e398f01 goharbor/harbor-registryctl:v1.7.6 "/harbor/start.sh" 16 seconds ago Up 14 seconds (health: starting) registryctl
e0a62d3e51f8 goharbor/registry-photon:v2.6.2-v1.7.6 "/entrypoint.sh /etc…" 16 seconds ago Up 14 seconds (health: starting) 5000/tcp registry
77f844cb252b goharbor/harbor-log:v1.7.6 "/bin/sh -c /usr/loc…" 17 seconds ago Up 16 seconds (health: starting) 127.0.0.1:1514->10514/tcp harbor-log
六、登陸harbor的web頁面,並創建項目
- 用瀏覽器訪問 http://192.168.18.71/
- 用戶名: admin
- 密碼: 123456 即前面harbor.cfg中指定的密碼
七、命令行登錄harbor並上傳鏡像到倉庫
編輯/etc/docker/daemon.json,添加 "insecure-registries":["http://192.168.18.71"]
[root@Harbor1 ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://kroknwmp.mirror.aliyuncs.com"], "insecure-registries":["http://192.168.18.71"] }
[root@Harbor1 ~]# systemctl restart docker
命令行登陸harbor
[root@Harbor1 ~]# docker login 192.168.18.71 Username: admin Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
給本地鏡像打標簽並上傳到harbor
修改 images 的名稱,不修改成指定格式無法將鏡像上傳到 harbor 倉庫
[root@centos7 ~]# docker tag alpine:latest 192.168.18.71/test/alpine:latest [root@centos7 ~]# docker push 192.168.18.71/test/alpine:latest The push refers to repository [192.168.18.71/test/alpine] e2eb06d8af82: Pushed latest: digest: sha256:69704ef328d05a9f806b6b8502915e6a0a4faa4d72018dc42343f511490daf8a size: 528
訪問harbor網站驗證上傳鏡像成功
實現Harbor的高可用
Harbor支持基於策略的Docker鏡像復制功能,這類似於MySQL的主從同步,其可以實現不同的數據中
心、不同的運行環境之間同步鏡像,並提供友好的管理界面,大大簡化了實際運維中的鏡像管理工作,
已經有用很多互聯網公司使用harbor搭建內網docker倉庫的案例,並且還有實現了雙向復制功能
一、在另一台主機上搭建Harbor
參照上述步驟,在第二台主機上搭建Harbor
注意:
- harbor.cfg中配置文件中 hostname 修改為本機地址
[root@Harbor2 ~]#vim /apps/harbor/harbor.cfg hostname = 192.168.18.72 harbor_admin_password = 123456
- 編輯/etc/docker/daemon.json,添加 "insecure-registries":["http://192.168.18.72"]
[root@Harbor2 ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://kroknwmp.mirror.aliyuncs.com"], "insecure-registries":["http://192.168.18.72"] }
二、第二台harbor上新建項目
參考第一台harbor服務器的項目名稱,在第二台harbor服務器上新建與之同名的項目
三、第二台harbor上倉庫管理中新建目標
參考第一台主機信息,新建復制(同步)目標信息,將第一台主機設為復制的目標
輸入第一台harbor服務器上的主機和用戶信息
四、第二台harbor上新建復制規則實現到第一台harbor的單向復制
在第二台harbor上建立復制的目標主機,即第一台harbor主機,將第二台harbor上面的鏡像復制到第一台harbor上
五、在第一台harbor主機上重復上面操作
以上操作,只是實現了從第二台harbor主機192.168.18.72到第一台harbor主機192.168.18.71的單向同步
在第一台harbor上再執行下面操作,才實現雙向同步
六、確認同步成功
在第二台harbor主機上可以查看到從第一台主機同步過來的鏡像
七、在第二台harbor上傳鏡像驗證是否可以雙向同步
[root@Harbor2 ~]# docker login 192.168.18.72 [root@Harbor2 ~]# docker tag httpd-busybox:v1.0 192.168.18.72/test/httpd-busybox:v1.0 [root@Harbor2 ~]# docker push 192.168.18.72/test/httpd-busybox:v1.0 The push refers to repository [192.168.18.72/test/httpd-busybox] e5adc4f4e456: Pushed 5b8c72934dfc: Pushed v1.0: digest: sha256:885a1ebcb2d75b3981abbe0167ba278705e4a1ba755db5ffef1c4dddf07ad999 size: 734
兩台Harbor已實現雙向同步