一、harbor介紹
Harbor是一個用於存儲和分發Docker鏡像的企業級Registry服務器。
由下面幾個組件組成:
- proxy:nginx前端代理,主要是分發前端頁面ui訪問和鏡像上傳和下載流量
- registry:鏡像倉庫,負責存儲鏡像文件
- 核心服務:提供web ui,數據庫,token認證,webhook等功能
- 日志服務
- database:用來存儲核心服務的一些數據
因為是vmware出品的,所以支持下面幾種部署方式
- 在線安裝
- 離線安裝
- ova安裝,這個直接在vcenter上導入就可以了
官方最小配置
- 2個cpu
- 4g內存
- 40g硬盤,因為是存儲鏡像的所以推薦硬盤大點
二、搭建過程
Install Docker CE
root@localhost:~# apt-get update root@localhost:~# apt-get install apt-transport-https ca-certificates software-properties-common curl root@localhost:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg |sudo apt-key add - root@localhost:~# apt-key fingerprint 0EBFCD88 root@localhost:~# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" root@localhost:~# apt-get update root@localhost:~# apt-get install docker-ce
Install Docker-compose
root@localhost:~# curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose root@localhost:~# chmod +x /usr/local/bin/docker-compose #查看版本 root@localhost:~# docker --version Docker version 18.09.0, build 4d60db4 root@localhost:~# docker-compose --version docker-compose version 1.22.0, build f46880fe
Install harbor
root@localhost:~# wget https://storage.googleapis.com/harbor-releases/release-1.6.0/harbor-offline-installer-v1.6.0.tgz root@localhost:~# tar -zxvf harbor-offline-installer-v1.6.0.tgz #編輯配置文件 root@localhost:~# cd harbor/ root@localhost:~/harbor# vim harbor.cfg hostname = 114.112.34.27 project_creation_restriction = adminonly #Python環境 root@localhost:~/harbor# apt-get install python root@localhost:~/harbor# export LC_ALL=C root@localhost:~/harbor# ln -s /usr/bin/python3 /usr/bin/python #安裝服務 root@localhost:~/harbor# ./install.sh
看到以下內容,說明安裝成功:
啟動服務:
root@localhost:~/harbor# docker-compose start Starting log ... done Starting registry ... done Starting postgresql ... done Starting adminserver ... done Starting ui ... done Starting redis ... done Starting jobservice ... done Starting proxy ... done root@localhost:~/harbor# docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------------------------------------------------------- harbor-adminserver /harbor/start.sh Up (healthy) harbor-db /entrypoint.sh postgres Up (healthy) 5432/tcp harbor-jobservice /harbor/start.sh Up harbor-log /bin/sh -c /usr/local/bin/ ... Up (healthy) 127.0.0.1:1514->10514/tcp harbor-ui /harbor/start.sh Up (healthy) nginx nginx -g daemon off; Up (healthy) 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp, 0.0.0.0:80->80/tcp redis docker-entrypoint.sh redis ... Up 6379/tcp registry /entrypoint.sh /etc/regist ... Up (healthy) 5000/t
配置HTTPS
##如果不做HTTPS,只需將hostname設置為IP,protocol改為HTTP即可,也不必生成CA root@localhost:~/harbor# vi harbor.cfg
#創建目錄 root@localhost:~/harbor# mkdir -p /data/cert/ #生成ca證書和簽名 root@localhost:~/harbor# openssl genrsa -out /data/cert/ca.key 2048 root@localhost:~/harbor# openssl req -x509 -new -nodes -key /data/cert/ca.key -subj "/CN=test.harbor.com" -days 5000 -out /data/cert/ca.crt #啟動服務 root@localhost:~/harbor# ./prepare root@localhost:~/harbor# docker-compose down root@localhost:~/harbor# docker-compose up -d
測試
修改本地host,添加一行內容 114.112.34.27 test.harbor.com 在瀏覽器中訪問 http://test.harbor.com/ 默認用戶名/密碼:admin/Harbor12345
登錄成功后會看到如下頁面:
添加項目:
進入此項目,可以看到推送鏡像的命令:
刪除項目:
確認項目不再使用時,選中項目,點擊刪除
點擊刪除:
在服務器進行測試
修改本地Docker默認倉庫,指向Harbor
root@localhost:/data/cert# cp ca.crt /usr/local/share/ca-certificates/ root@localhost:/data/cert# update-ca-certificates #修改docker配置文件,指向Harbor root@localhost:~/harbor# vi /etc/default/docker #添加一行內容 DOCKER_OPTS="--insecure-registry=test.harbor.com" #重啟docker服務 root@localhost:~# service docker restart
將本地Ubuntu鏡像上傳至Harbor
#將本地鏡像打一個tag root@localhost:~# docker tag ubuntu:16.04 test.harbor.com/paas/ubuntu:v1.0 #推送到Harbor上 root@localhost:~/harbor# docker push test.harbor.com/paas/ubuntu:v1.0
下載Harbor鏡像至本地
root@localhost:~/harbor# docker pull test.harbor.com/paas/ubuntu:v1.0 v1.0: Pulling from paas/ubuntu Digest: sha256:078d30763ae6697b7d55e49f4cb9e61407abd2137cafb5625f131aa47c1a47af Status: Downloaded newer image for test.harbor.com/paas/ubuntu:v1.0