前言:
Harbor是一個用於存儲和分發Docker鏡像的企業級Registry服務器,由VMware中國研發團隊負責開發的開源企業級Registry項目。作爲一個企業級私有Registry服務器,Harbor提供了更好的性能和安全,提升用戶使用Registry構建和運行環境傳輸鏡像的效率。Harbor支持安裝在多個Registry節點的鏡像資源複製,鏡像全部保存在私有Registry中,確保數據和知識產權在公司內部網絡中管控。另外,Harbor也提供了高級的安全特性,諸如用戶管理,訪問控制和活動審計等。
環境說明:
主機名 | 操作系統版本 | IP地址 | docker版本 | docker-compose版本 | harbor版本 | 說明 |
ubuntu1604 | Ubuntu 16.04.5 | 172.27.9.31 | 18.09.2 | 1.23.2 | v1.7.4-ce6a6237 | harbor服務器 |
docker02 | Ubuntu 16.04.5 | 172.27.9.38 | 18.09.2 | / | / | docker主機 |
centos7 | centos7 | 172.27.9.181 | / | / | / | NFS服務器 |
ubuntu安裝詳見:Ubuntu16.04.5以lvm方式安裝全記錄
docker安裝詳見:Ubuntu16.04安裝Docker
1.Harbor安裝前準備
安裝docker-compose
root@ubuntu1604:/# sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose root@ubuntu1604:/# chmod +x /usr/local/bin/docker-compose
Docker Compose是Docker官方編排(Orchestration)項目之一,負責快速在集羣中部署分佈式應用。
測試docker-compose
root@ubuntu1604:~# docker-compose --version
介質下載
離線安裝:https://storage.googleapis.com/harbor-releases/release-1.7.0/harbor-offline-installer-v1.7.4.tgz
在線安裝:https://storage.googleapis.com/harbor-releases/release-1.7.0/harbor-online-installer-v1.7.4.tgz
解壓
root@ubuntu1604:/opt# tar -zxvf harbor-offline-installer-v1.7.4.tgz
將介質上傳至/opt目錄並解壓
2.NFS配置
安裝nfs軟件
root@ubuntu1604:~# apt-get install nfs-common -y
掛載
root@ubuntu1604:~# showmount -e 172.27.9.181 root@ubuntu1604:~# mkdir /data root@ubuntu1604:~# mount -t nfs 172.27.9.181:/nfs /data/
新建掛載點/data並掛載nfs共享目錄/nfs,/data爲harbor默認的數據文件路徑,所有鏡像文件都保存在該文件中。
NFS配置詳見:Centos7下NFS服務器搭建及客戶端連接配置
3.生成證書
爲實現https方式訪問,需生成證書
創建證書存放目錄
root@ubuntu1604:~# mkdir -p /data/cert && cd /data/cert
創建 CA 根證書
root@ubuntu1604:/data/cert# openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 365 -out ca.crt -subj "/C=CN/L=zhejiang/O=lisea/CN=harbor-registry"
生成證書籤名
root@ubuntu1604:/data/cert# openssl req -newkey rsa:4096 -nodes -sha256 -keyout loong576.com.key -out server.csr -subj "/C=CN/L=zhejiang/O=lisea/CN=loong576.com"
訪問域名設置爲loong576.com
生成主機證書
root@ubuntu1604:/data/cert# openssl x509 -req -days 365 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out loong576.com.crt
查看證書
拷貝證書
將證書拷貝至所有需要登錄Harbor的docker主機
root@ubuntu1604:~# mkdir -p /etc/docker/certs.d/loong576.com root@ubuntu1604:~# cp /data/cert/loong576.com.crt /etc/docker/certs.d/loong576.com/ca.crt
新建目錄/etc/docker/certs.d/loong576.com,將證書loong576.com.crt拷貝並重命名爲ca.crt
4.Harbor安裝
配置Harbor
修改harbor.cfg文件
root@ubuntu1604:~# view /opt/harbor/harbor.cfg hostname = loong576.com ui_url_protocol = https ssl_cert = /data/cert/loong576.com.crt ssl_cert_key = /data/cert/loong576.com.key
設置域名爲loong576.com(/etc/hosts文件已新增配置:172.27.9.31 loong576.com),訪問方式爲https,證書爲之前生成的證書
安裝Harbor
root@ubuntu1604:/opt/harbor# ./install.sh
安裝共有4步,最後會提示安裝成功並告知訪問地址。
若以後想修改配置文件harbor.cfg,可按如下步驟執行:
root@ubuntu1604:/opt/harbor# docker-compose down -v root@ubuntu1604:/opt/harbor# vim harbor.cfg root@ubuntu1604:/opt/harbor# ./prepare root@ubuntu1604:/opt/harbor# docker-compose up -d
訪問Harbor
訪問地址:https://loong576.com
瀏覽器會提示不安全的證書,因爲我們是自己當CA機構,所以瀏覽器會不信任,添加信任即可。默認密碼是admin/Harbor12345。瀏覽器所在電腦記得編輯host文件,新增:172.27.9.31 loong576.com
5.Harbor測試
新建項目myproject
新建項目myproject並設置爲公開
拉取鏡像
從Docker Hub拉取鏡像centos:7
root@ubuntu1604:~# docker pull centos:7
標記鏡像
root@ubuntu1604:~# docker tag centos:7 loong576.com/myproject/centos7
將鏡像標記爲loong576.com/myproject,推送時Docker會將其解釋爲倉庫的位置
登陸倉庫
root@ubuntu1604:/etc/docker/certs.d/loong576.com# docker login loong576.com
密碼會被保存,下次會免密登陸。
推送鏡像
root@ubuntu1604:~# docker push loong576.com/myproject/centos7
頁面查看鏡像
拉取鏡像
在docker02(172.27.9.38)拉取鏡像centos7,在做此操作前需進行前文的“拷貝證書”操作。
root@docker02:~# docker pull loong576.com/myproject/centos7
查看拉取的鏡像
root@docker02:~# docker image ls
刪除鏡像
直接web頁面刪除鏡像
日誌查看
至此完成Harbor私有倉庫搭建。
6.後續
可安裝Harborclient工具,用命令行方式進行管理。
Harbor通過Web界面可以方便地管理用戶、租戶以及鏡像倉庫等資源,但是缺乏開發人員更喜愛的命令行管理工具,Harborclient是Harbor的第三方擴展開源工具,正彌補Harbor不足,它適合開發和運維人員管理鏡像倉庫、項目等資源。
安裝及使用:https://github.com/int32bit/python-harborclient/blob/master/README.zh.md
本文參考:
https://github.com/goharbor/harbor/blob/master/docs/installation_guide.md
https://docs.docker.com/registry/insecure/