docker容器應用及開發和運行離不開可靠的鏡像管理,在前面的文章咱們提到了咱們在拉取鏡像的時候使用的是docker官方提供的公共鏡像倉庫,可是不管從安全仍是效率等其餘方面考慮,咱們仍是須要部署在私有環境中的Registry。mysql
而本文將介紹的是一款企業級docker鏡像倉庫Harbor的部署和使用,並且在以後的Kubernetes的集羣中,也推薦使用Harbor倉庫環境。nginx
Harbor是由VMware公司開源的企業級的Docker Registry管理項目,相比docker官方擁有更豐富的權限權利及完善的架構設計,適用於爲大規模的docker集羣部署提供倉庫服務。其主要提供 了Dcoker Registry 管理界面UI,可基於角色訪問控制、鏡像複製、AD/LDAP 集成、日誌審覈等功能,而且徹底支持中文。git
先來看看harbor總體的架構圖github
其實Harbor自有組件是上圖中的Core Service所提供的核心功能,Replication Job Services所提供的多個Harbor實例之間的鏡像同步(複製)功能以及Log collector提供的監控以及日誌分析功能web
其中核心服務主要是三個方面:sql
UI:提供圖形化界面,幫助用戶管理registry上的鏡像(image), 並對用戶進行受權。docker
webhook:爲了及時獲取registry 上image狀態變化的狀況, 在Registry上配置webhook,把狀態變化傳遞給UI模塊。shell
Auth服務:負責根據用戶權限給每一個docker push/pull命令簽發token. Docker 客戶端向Regiøstry服務發起的請求,若是不包含token,會被重定向到這裏,得到token後再從新向Registry進行請求。數據庫
API: 提供Harbor 的RESTful API接口json
而上圖中其餘的組件都是Harbor所依賴的外部組件,例如Nginx(作代理)、Registry v2(鏡像倉庫,官方負責存儲鏡像的位置)以及數據庫等等
準備環境:一臺部署好docker的虛擬機Centos7做爲Harbor部署服務器,另外一臺也須要部署docker環境來做爲客戶端進行相關測試
具體規劃:
服務端docker-harbor:192.168.0.135,Centos7操做系統,docker-ce(即docker環境)
、docker-compose、harbor
客戶端(測試端):192.168.0.129;Centos7操做系統,docker-ce
下面開始進行部署配置
首先在harbor端安裝配置docker-compose和harbor軟件安裝
能夠經過curl命令下載(考驗網速的時候到了~~):
下載docker-compose工具
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s-
uname -m` -o /usr/local/bin/docker-compose
下載harbor軟件包
wget http://harbor.orientsoft.cn/harbor-1.2.2/harbor-offline-installer-v1.2.2.tgz
[root@localhost opt]# ls containerd docker-compose docker.sh harbor-offline-installer-v1.2.2.tgz rh [root@localhost opt]# chmod +x docker-compose [root@localhost opt]# cp -p docker-compose /usr/local/bin/ [root@localhost opt]# ls containerd docker-compose docker.sh harbor-offline-installer-v1.2.2.tgz rh [root@localhost opt]# tar zxf harbor-offline-installer-v1.2.2.tgz -C /usr/local/ [root@localhost opt]# cd /usr/local/ [root@localhost local]# ls bin etc games harbor include lib lib64 libexec sbin share src [root@localhost local]# cd harbor/ [root@localhost harbor]# ll 總用量 527664 drwxr-xr-x. 3 root root 23 4月 6 09:02 common -rw-r--r--. 1 root root 1163 10月 20 2017 docker-compose.clair.yml -rw-r--r--. 1 root root 1988 10月 20 2017 docker-compose.notary.yml -rw-r--r--. 1 root root 3191 10月 20 2017 docker-compose.yml -rw-r--r--. 1 root root 4304 10月 20 2017 harbor_1_1_0_template -rw-r--r--. 1 root root 4345 10月 20 2017 harbor.cfg -rw-r--r--. 1 root root 539885476 10月 20 2017 harbor.v1.2.2.tar.gz -rwxr-xr-x. 1 root root 5332 10月 20 2017 install.sh -rw-r--r--. 1 root root 371640 10月 20 2017 LICENSE -rw-r--r--. 1 root root 482 10月 20 2017 NOTICE -rwxr-xr-x. 1 root root 17592 10月 20 2017 prepare -rwxr-xr-x. 1 root root 4550 10月 20 2017 upgrade
修改harbor配置文件harbor.cfg、使用給出的腳本啓動harbor
[root@localhost harbor]# vim harbor.cfg
在該過程當中,應該不難發現是會下載鏡像和運行相關的容器的,咱們能夠查看一下
[root@localhost harbor]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE vmware/harbor-log v1.2.2 36ef78ae27df 2 years ago 200MB vmware/harbor-jobservice v1.2.2 e2af366cba44 2 years ago 164MB vmware/harbor-ui v1.2.2 39efb472c253 2 years ago 178MB vmware/harbor-adminserver v1.2.2 c75963ec543f 2 years ago 142MB vmware/harbor-db v1.2.2 ee7b9fa37c5d 2 years ago 329MB vmware/nginx-photon 1.11.13 6cc5c831fc7f 2 years ago 144MB vmware/registry 2.6.2-photon 5d9100e4350e 2 years ago 173MB vmware/postgresql 9.6.4-photon c562762cbd12 2 years ago 225MB vmware/clair v2.0.1-photon f04966b4af6c 2 years ago 297MB vmware/harbor-notary-db mariadb-10.1.10 64ed814665c6 2 years ago 324MB vmware/notary-photon signer-0.5.0 b1eda7d10640 3 years ago 156MB vmware/notary-photon server-0.5.0 6e2646682e3c 3 years ago 157MB photon 1.0 e6e4e4a2ba1b 3 years ago 128MB [root@localhost harbor]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2bc676837f83 vmware/nginx-photon:1.11.13 "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp nginx d1bb681c1bde vmware/harbor-jobservice:v1.2.2 "/harbor/harbor_jobs…" 3 minutes ago Up 3 minutes harbor-jobservice da75599518b4 vmware/harbor-ui:v1.2.2 "/harbor/harbor_ui" 3 minutes ago Up 3 minutes harbor-ui 55da84f35f22 vmware/registry:2.6.2-photon "/entrypoint.sh serv…" 3 minutes ago Up 3 minutes 5000/tcp registry 9143d4b35f5a vmware/harbor-db:v1.2.2 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 3306/tcp harbor-db fbf66bc6ea28 vmware/harbor-adminserver:v1.2.2 "/harbor/harbor_admi…" 3 minutes ago Up 3 minutes harbor-adminserver e2ef481df1c7 vmware/harbor-log:v1.2.2 "/bin/sh -c 'crond &…" 3 minutes ago Up 3 minutes 127.0.0.1:1514->514/tcp harbor-log
可見下載了13個鏡像而且運行了7個容器,其實這個時候harbor服務已經搭建完了,是否是簡單到難以想象了呢?
既然上文說到harbor倉庫不只部署簡單,並且能夠經過web UI界面進行登陸管理,那麼咱們如何登陸呢?
這就仍是須要回到harbor的配置文件中看了,咱們使用cat harbor.cfg 命令查看一下結果以下圖:
固然這個密碼是初始密碼,能夠修改的
好了,咱們如今使用瀏覽器輸入IP地址進行登陸驗證
)
登陸結果:
以上,Harbor的倉庫就構建完成了,下面咱們在web ui界面上進行一下操做,而後在命令行終端進行相關的測試驗證。
咱們建立一個私有項目myproject,用於測試上傳和下載鏡像
在Harbor服務器節點上登陸倉庫,先登進去才能夠進行操做的哈!
[root@localhost harbor]# docker login -u admin -p Harbor12345 http://127.0.0.1/ WARNING! Using --password via the CLI is insecure. Use --password-stdin. 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
如今測試拉取一個nginx鏡像而後使用tag命令設置一個副本進行上傳測試
[root@localhost harbor]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx 54fec2fa59d0: Pull complete 4ede6f09aefe: Pull complete f9dc69acb465: Pull complete Digest: sha256:86ae264c3f4acb99b2dee4d0098c40cb8c46dcf9e1148f05d3a51c4df6758c12 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@localhost harbor]# docker tag nginx:latest 127.0.0.1/myproject/nginx:v1 [root@localhost harbor]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 127.0.0.1/myproject/nginx v1 602e111c06b6 Less than a second ago 127MB nginx latest 602e111c06b6 Less than a second ago 127MB ...//省略多餘內容 #上傳命令執行 [root@localhost harbor]# docker push 127.0.0.1/myproject/nginx The push refers to repository [127.0.0.1/myproject/nginx] b3003aac411c: Pushed 216cf33c0a28: Pushed c2adabaecedb: Pushed v1: digest: sha256:cccef6d6bdea671c394956e24b0d0c44cd82dbe83f543a47fdc790fadea48422 size: 948
驗證結果
點擊myproject以後能夠查看剛剛創建的項目中對應的鏡像、成員、日誌等信息
以上對於服務端的測試就到這裏,有興趣的朋友能夠本身再繼續嘗試其餘操做
下面測試經過客戶端登陸,及遠程訪問harbor,畢竟企業中須要你們共享該倉庫,並且有對於不一樣的部門和不一樣人的身份都有着各自的權限(由領導或者老闆決定的)
客戶機當前環境
[root@localhost opt]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [root@localhost opt]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
第一步先要指定私有倉庫harbor服務器的位置,IP地址
修改docker服務配置文件、重載服務
~~~shell、
[root@localhost opt]# vim /usr/lib/systemd/system/docker.service
![](https://s4.51cto.com/images/blog/202004/28/15c4b1b71282c5a09b8e15a1fb9f1c2c.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=) ~~~shell [root@localhost opt]# systemctl daemon-reload [root@localhost opt]# systemctl restart docker.service
此時咱們進行登陸
[root@localhost opt]# docker login -u admin -p Harbor12345 http://192.168.0.135 WARNING! Using --password via the CLI is insecure. Use --password-stdin. 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
此時咱們將剛剛建立的私有倉庫的一個鏡像拉取測試一下(結果對好比下)
[root@localhost opt]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [root@localhost opt]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost opt]# docker pull 192.168.0.135/myproject/nginx:v1 v1: Pulling from myproject/nginx 54fec2fa59d0: Pull complete 4ede6f09aefe: Pull complete f9dc69acb465: Pull complete Digest: sha256:cccef6d6bdea671c394956e24b0d0c44cd82dbe83f543a47fdc790fadea48422 Status: Downloaded newer image for 192.168.0.135/myproject/nginx:v1 192.168.0.135/myproject/nginx:v1 [root@localhost opt]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.0.135/myproject/nginx v1 602e111c06b6 5 days ago 127MB
此時你也能夠在web ui界面中查看對應的日誌記錄,必然有對應的操做記錄(上文說過的「審計」特性)。
拉取測試完成了,接下來進行客戶端上傳測試
先退出登陸,拉取測試鏡像
[root@localhost opt]# docker logout http://192.168.0.135 Removing login credentials for 192.168.0.135 [root@localhost opt]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.0.135/myproject/nginx v1 602e111c06b6 5 days ago 127MB [root@localhost opt]# docker pull cirros ...//省略部份內容
tag標籤後登陸harbor倉庫,進行上傳測試
[root@localhost opt]# docker tag cirros:latest 192.168.0.135/myproject/cirros:v1 [root@localhost opt]# docker login -u admin -p Harbor12345 http://192.168.0.135 WARNING! Using --password via the CLI is insecure. Use --password-stdin. 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 [root@localhost opt]# docker push 192.168.0.135/myproject/cirros:v1 The push refers to repository [192.168.0.135/myproject/cirros] 858d98ac4893: Pushed aa107a407592: Pushed b993cfcfd8fd: Pushed v1: digest: sha256:c7d58d6d463247a2540b8c10ff012c34fd443426462e891b13119a9c66dfd28a size: 943
咱們直接經過日誌驗證,順便查看以前的操做是否留有記錄
至此,部署安裝,服務端以及客戶端的上傳下載測試都順利完成了,最後補充一下有關harbor的管理與維護的操做
修改harbor.cfg配置文件須要先中止全部的harbor實例而且更新配置文件後,再運行prepare腳本進行從新的加載配置,以後進行從新建立和啓動harbor實例
一、關閉全部容器(對應結果就是中止和移除的現象)
[root@localhost harbor]# docker-compose down -v Stopping nginx ... done Stopping harbor-jobservice ... done Stopping harbor-ui ... done Stopping registry ... done Stopping harbor-db ... done Stopping harbor-adminserver ... done Stopping harbor-log ... done Removing nginx ... done Removing harbor-jobservice ... done Removing harbor-ui ... done Removing registry ... done Removing harbor-db ... done Removing harbor-adminserver ... done Removing harbor-log ... done Removing network harbor_harbor [root@localhost harbor]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost harbor]# docker-compose ps Name Command State Ports ------------------------------
二、根據需求更改配置文件以後執行prepare腳本
[root@localhost harbor]# vim harbor.cfg [root@localhost harbor]# ls common docker-compose.notary.yml harbor_1_1_0_template harbor.v1.2.2.tar.gz LICENSE prepare docker-compose.clair.yml docker-compose.yml harbor.cfg install.sh NOTICE upgrade [root@localhost harbor]# ./prepare Clearing the configuration file: ./common/config/adminserver/env Clearing the configuration file: ./common/config/ui/env Clearing the configuration file: ./common/config/ui/app.conf Clearing the configuration file: ./common/config/ui/private_key.pem Clearing the configuration file: ./common/config/db/env Clearing the configuration file: ./common/config/jobservice/env Clearing the configuration file: ./common/config/jobservice/app.conf Clearing the configuration file: ./common/config/registry/config.yml Clearing the configuration file: ./common/config/registry/root.crt Clearing the configuration file: ./common/config/nginx/nginx.conf loaded secret from file: /data/secretkey Generated configuration file: ./common/config/nginx/nginx.conf Generated configuration file: ./common/config/adminserver/env Generated configuration file: ./common/config/ui/env Generated configuration file: ./common/config/registry/config.yml Generated configuration file: ./common/config/db/env Generated configuration file: ./common/config/jobservice/env Generated configuration file: ./common/config/jobservice/app.conf Generated configuration file: ./common/config/ui/app.conf Generated certificate, key file: ./common/config/ui/private_key.pem, cert file: ./common/config/registry/root.crt The configuration files are ready, please use docker-compose to start the service.
三、重啓docker服務和容器服務
[root@localhost harbor]# systemctl restart docker [root@localhost harbor]# docker-compose up -d Creating network "harbor_harbor" with the default driver Creating harbor-log ... done Creating harbor-adminserver ... done Creating registry ... done Creating harbor-db ... done Creating harbor-ui ... done Creating nginx ... done Creating harbor-jobservice ... done
結果:
[root@localhost harbor]# docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------------------------------------------- harbor-adminserver /harbor/harbor_adminserver Up harbor-db docker-entrypoint.sh mysqld Up 3306/tcp harbor-jobservice /harbor/harbor_jobservice Up harbor-log /bin/sh -c crond && rm -f ... Up 127.0.0.1:1514->514/tcp harbor-ui /harbor/harbor_ui Up nginx nginx -g daemon off; Up 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp, 0.0.0.0:80->80/tcp registry /entrypoint.sh serve /etc/ ... Up 5000/tcp
主要是進行用戶建立及測試
一、建立用戶操做
二、爲項目添加新用戶
咱們使用新用戶在client端登陸
以上就是簡單的harbor倉庫管理和維護介紹了。
謝謝閱讀!