1. 程式人生 > >搭建私有倉庫Harbor 搭建Harbor企業級docker倉庫

搭建私有倉庫Harbor 搭建Harbor企業級docker倉庫

搭建Harbor企業級docker倉庫

 

搭建Harbor企業級docker倉庫

一、Harbor簡介

1.Harbor介紹

Harbor是一個用於儲存和分發Docker映象的企業級Registry伺服器,通過新增一些企業必需的功能特性,例如安全、標識和管理等,擴充套件了開源Docker Distribution。作為一個企業級私有Registry伺服器,Harbor提供了更好的效能和安全。提升使用者使用Registry構建和執行環境傳輸映象的效率。Harbor支援安裝在多個Registry節點的映象資源複製,映象全部儲存在私有Registry中, 確保資料和智慧財產權在公司內部網路中管控。另外,Harbor也提供了高階的安全特性,諸如使用者管理,訪問控制和活動審計等。

2.Harbor特性

  • 基於角色的訪問控制 :使用者與Docker映象倉庫通過“專案”進行組織管理,一個使用者可以對多個映象倉庫在同一名稱空間(project)裡有不同的許可權。
  • 映象複製 : 映象可以在多個Registry例項中複製(同步)。尤其適合於負載均衡,高可用,混合雲和多雲的場景。
  • 圖形化使用者介面 : 使用者可以通過瀏覽器來瀏覽,檢索當前Docker映象倉庫,管理專案和名稱空間。
  • AD/LDAP 支援 : Harbor可以整合企業內部已有的AD/LDAP,用於鑑權認證管理。
  • 審計管理 : 所有針對映象倉庫的操作都可以被記錄追溯,用於審計管理。
  • 國際化 : 已擁有英文、中文、德文、日文和俄文的本地化版本。更多的語言將會新增進來。
  • RESTful API : RESTful API 提供給管理員對於Harbor更多的操控, 使得與其它管理軟體整合變得更容易。
  • 部署簡單 : 提供線上和離線兩種安裝工具, 也可以安裝到vSphere平臺(OVA方式)虛擬裝置。

3.Harbor元件

Harbor在架構上主要由6個元件構成:

  • Proxy:Harbor的registry, UI, token等服務,通過一個前置的反向代理統一接收瀏覽器、Docker客戶端的請求,並將請求轉發給後端不同的服務。

  • Registry: 負責儲存Docker映象,並處理docker push/pull 命令。由於我們要對使用者進行訪問控制,即不同使用者對Docker image有不同的讀寫許可權,Registry會指向一個token服務,強制使用者的每次docker pull/push請求都要攜帶一個合法的token, Registry會通過公鑰對token 進行解密驗證。

  • Core services: 這是Harbor的核心功能,主要提供以下服務:

  • UI:提供圖形化介面,幫助使用者管理registry上的映象(image), 並對使用者進行授權。

  • webhook:為了及時獲取registry 上image狀態變化的情況, 在Registry上配置webhook,把狀態變化傳遞給UI模組。

  • token 服務:負責根據使用者許可權給每個docker push/pull命令簽發token. Docker 客戶端向Regiøstry服務發起的請求,如果不包含token,會被重定向到這裡,獲得token後再重新向Registry進行請求。

  • Database:為core services提供資料庫服務,負責儲存使用者許可權、審計日誌、Docker image分組資訊等資料。

  • Job Services:提供映象遠端複製功能,可以把本地映象同步到其他Harbor例項中。

  • Log collector:為了幫助監控Harbor執行,負責收集其他元件的log,供日後進行分析。

各個元件之間的關係如下圖所示:

4.Harbor實現

Harbor的每個元件都是以Docker容器的形式構建的,官方也是使用Docker Compose來對它進行部署。用於部署Harbor的Docker Compose模板位於 harbor/docker-compose.yml,開啟這個模板檔案,發現Harbor是由7個容器組成的;

# 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 

nginx:nginx負責流量轉發和安全驗證,對外提供的流量都是從nginx中轉,所以開放https的443埠,它將流量分發到後端的ui和正在docker映象儲存的docker registry。
harbor-jobservice:harbor-jobservice 是harbor的job管理模組,job在harbor裡面主要是為了映象倉庫之前同步使用的;
harbor-ui:harbor-ui是web管理頁面,主要是前端的頁面和後端CURD的介面;
registry:registry就是docker原生的倉庫,負責儲存映象。
harbor-adminserver:harbor-adminserver是harbor系統管理介面,可以修改系統配置以及獲取系統資訊。
這幾個容器通過Docker link的形式連線在一起,在容器之間通過容器名字互相訪問。對終端使用者而言,只需要暴露proxy (即Nginx)的服務埠。
harbor-db:harbor-db是harbor的資料庫,這裡儲存了系統的job以及專案、人員許可權管理。由於本harbor的認證也是通過資料,在生產環節大多對接到企業的ldap中;
harbor-log:harbor-log是harbor的日誌服務,統一管理harbor的日誌。通過inspect可以看出容器統一將日誌輸出的syslog。

這幾個容器通過Docker link的形式連線在一起,這樣,在容器之間可以通過容器名字互相訪問。對終端使用者而言,只需要暴露proxy (即Nginx)的服務埠。

二、安裝和配置Harbor

1.環境說明

環境名稱 版本
系統版本 CentOS Linux release 7.2.1511 (Core)
docker-ce 17.03.1-ce
docker-compose 1.16.1
Harbor v1.2.0
安裝方式 線上安裝
安裝位置 /usr/local/harbor

2.首先安裝docker

安裝Harbor需要先安裝docker和docker-compose

# yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
配置repository:
# yum-config-manager \
    --add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

安裝最新版本docker-ce   Docker version 17.03.1-ce
# yum install docker-ce

啟動docker:

# systemctl start docker

如果需要解除安裝docker-ce,操作如下:
yum remove docker-ce
解除安裝後images,containers,volumes,configuration files 是不能自動刪除的,為了刪除all images,containers,and volumes,請執行如下命令:
rm -rf /var/lib/docker

3.安裝docker-compose

必須先安裝docker

方法一:二進位制安裝

a.下載二進位制檔案
# curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

如果需要安裝其他版本的話,請修改上面命令中的版本號。

b.賦予二進位制檔案可執行許可權
# chmod +x /usr/local/bin/docker-compose
c.根據自己的情況決定是否安裝命令補全功能
# yum install bash-completion 
# curl -L https://raw.githubusercontent.com/docker/compose/1.16.1/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

重新登陸後就生效了

# docker-compose 
build    config   down     exec     images   logs     port     pull     restart  run      start    top      up       
bundle   create   events   help     kill     pause    ps       push     rm       scale    stop     unpause  version  
d.測試是否安裝成功
# docker-compose --version
docker-compose version 1.16.1, build 6d1ac21

方法二:pip

# yum install python-pip
# pip install  docker-compose

測試:

# docker-compose --version

解除安裝docker-compose
對應上面兩種安裝方法:
二進位制:

# rm  /usr/local/bin/docker-compose

pip:

# pip uninstall  docker-compose

4.安裝Harbor

安裝方式分為線上安裝和離線安裝兩種方式,我這裡選擇的是線上安裝。

wget -P /usr/loca/src/     https://github.com/vmware/harbor/releases/download/v1.2.0/harbor-online-installer-v1.2.0.tgz

也可以下載下面的檔案進行離線安裝:

https://github.com/vmware/harbor/releases/download/v1.2.0/harbor-offline-installer-v1.2.0.tgz

不過這個檔案,我沒有下載成功。因為在亞馬遜S3上總是連線超時
安裝:

# cd /usr/local/src/
# tar zxf harbor-online-installer-v1.2.0.tgz  -C /usr/local/
# cd /usr/local/harbor/

5.修改配置檔案

配置檔案為:/usr/local/harbor/harbor.cfg
配置的內容為:


# vim /usr/local/harbor/harbor.cfg
hostname = rgs.unixfbi.com
#郵箱配置
email_server = smtp.qq.com
email_server_port = 25
email_username = [email protected]
email_password =12345678
email_from = UnixFBI <[email protected]>
email_ssl = false
#禁止使用者註冊
self_registration = off
#設定只有管理員可以建立專案
project_creation_restriction = adminonly

6.執行安裝指令碼

 # /usr/local/harbor/install.sh

執行過程


[Step 0]: checking installation environment ...

Note: docker version: 17.03.1

Note: docker-compose version: 1.16.1


[Step 1]: preparing environment ...
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.


[Step 2]: checking existing instance of Harbor ...


[Step 3]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Pulling log (vmware/harbor-log:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-log
93b3dcee11d6: Pull complete
07d028a1dbdd: Pull complete
208723cd598a: Pull complete
b876b05989fc: Pull complete
12f0e0ef448a: Pull complete
Digest: sha256:608e10b7aaac07e10a4cd639d8848aef96daa7dd5c7ebaaf6a7ecd47f903e1f8
Status: Downloaded newer image for vmware/harbor-log:v1.2.0
Pulling adminserver (vmware/harbor-adminserver:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-adminserver
93b3dcee11d6: Already exists
6ab21236e58b: Pull complete
f70b0efff900: Pull complete
c5d206b5e184: Pull complete
Digest: sha256:aba66ec90fc12fe0814cecc9f647f5d17b41395199821cc7af69db9c0fbe446c
Status: Downloaded newer image for vmware/harbor-adminserver:v1.2.0
Pulling registry (vmware/registry:2.6.2-photon)...
2.6.2-photon: Pulling from vmware/registry
93b3dcee11d6: Already exists
007fe7635995: Pull complete
20d63c99b572: Pull complete
2026103b2811: Pull complete
ab7a5be79b33: Pull complete
11757dc5c642: Pull complete
69ab3e15edde: Pull complete
Digest: sha256:3cf06cdff00f48e15a5254b6d0370bcb4423dcd158e7f970dc8246893125b032
Status: Downloaded newer image for vmware/registry:2.6.2-photon
Pulling ui (vmware/harbor-ui:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-ui
93b3dcee11d6: Already exists
6ab21236e58b: Already exists
7753f4b55df6: Pull complete
a647b33bdf74: Pull complete
eb30db926101: Pull complete
204d77847826: Pull complete
4910a0b56c59: Pull complete
e880a4b0031f: Pull complete
Digest: sha256:b198d8f2f59515d286bdcf06c7f99c370eb4475e2547495c3e1db8761940646b
Status: Downloaded newer image for vmware/harbor-ui:v1.2.0
Pulling mysql (vmware/harbor-db:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-db
df559435c037: Pull complete
a6310a57af5d: Pull complete
d13d90890144: Pull complete
b694d8967a6c: Pull complete
a34f6cef56a6: Pull complete
3519eec83af5: Pull complete
34bae610e56c: Pull complete
86a867bebd89: Pull complete
3db2d0ac366e: Pull complete
c0d307ee295f: Pull complete
c5b1b404c5ee: Pull complete
14f4a4328366: Pull complete
c7fa77b46ec4: Pull complete
0cbe2b5669b8: Pull complete
Digest: sha256:c10b3555beb6d1c851ae49a4e90ef4296a1ad42bcd1a58ae97e316b034515b6e
Status: Downloaded newer image for vmware/harbor-db:v1.2.0
Pulling jobservice (vmware/harbor-jobservice:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-jobservice
93b3dcee11d6: Already exists
6ab21236e58b: Already exists
64bd0172d071: Pull complete
b4f5382f226f: Pull complete
Digest: sha256:0692648176c1c87379025b0519036b9f3f1a0eceb2646f17dd40eb143c898d5c
Status: Downloaded newer image for vmware/harbor-jobservice:v1.2.0
Pulling proxy (vmware/nginx-photon:1.11.13)...
1.11.13: Pulling from vmware/nginx-photon
93b3dcee11d6: Already exists
d2a110a9296e: Pull complete
Digest: sha256:9ec5644c667e87bf051e581ce74b2933d3ed469b27862534ba60ccf17b4ff57a
Status: Downloaded newer image for vmware/nginx-photon:1.11.13
Creating harbor-log ... 
Creating harbor-log ... done
Creating harbor-db ... 
Creating harbor-adminserver ... 
Creating registry ... 
Creating harbor-db
Creating registry
Creating registry ... done
Creating harbor-ui ... 
Creating harbor-ui ... done
Creating nginx ... 
Creating harbor-jobservice ... 
Creating harbor-jobservice
Creating nginx ... done

✔ ----Harbor has been installed and started successfully.----

Now you should be able to visit the admin portal at http://rgs.unixfbi.com. 
For more details, please visit https://github.com/vmware/harbor .

7.Harbor啟動和停止

Harbor 的日常運維管理是通過docker-compose來完成的,Harbor本身有多個服務程序,都放在docker容器之中執行,我們可以通過docker ps命令檢視。

# docker ps            
CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS              PORTS                                                              NAMES
e6da3450bebe        vmware/harbor-jobservice:v1.2.0    "/harbor/harbor_jo..."   4 days ago          Up 4 days                                                                              harbor-jobservice
6134227d7ed4        vmware/nginx-photon:1.11.13        "nginx -g 'daemon ..."   4 days ago          Up 4 days           0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp   nginx
ef5fe0b856a7        vmware/harbor-ui:v1.2.0            "/harbor/harbor_ui"      4 days ago          Up 4 days                                                                              harbor-ui
a752861c7e92        vmware/registry:2.6.2-photon       "/entrypoint.sh se..."   4 days ago          Up 4 days           5000/tcp                                                           registry
78ea91838582        vmware/harbor-db:v1.2.0            "docker-entrypoint..."   4 days ago          Up 4 days           3306/tcp                                                           harbor-db
024e226ff135        vmware/harbor-adminserver:v1.2.0   "/harbor/harbor_ad..."   4 days ago          Up 4 days                                                                              harbor-adminserver
3fab80444a1d        vmware/harbor-log:v1.2.0           "/bin/sh -c 'crond..."   4 days ago          Up 4 days           127.0.0.1:1514->514/tcp                                            harbor-log

或者docker-compose 來檢視

# cd /usr/local/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            

Harbor的啟動和停止

啟動Harbor
# docker-compose start
停止Harbor
# docker-comose stop
重啟Harbor
# docker-compose restart

8.訪問測試

在瀏覽器輸入rgs.unixfbi.com,因為我配置的域名為rgs.unixfbi.com。請大家根據自己的配置情況輸入訪問的域名;
預設賬號密碼: admin / Harbor12345 登入後修改密碼

四、測試上傳和下載映象

1.修改各docker client配置

# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --insecure-registry rgs.unixfbi.com

增加 --insecure-registry rgs.unixfbi.com 即可。
重啟docker:

# systemctl daemon-reload
# systemctl  restart docker

或者

建立/etc/docker/daemon.json檔案,在檔案中指定倉庫地址
# cat > /etc/docker/daemon.json << EOF
{ "insecure-registries":["rgs.unixfbi.com"] }
EOF
然後重啟docker就可以。

# systemctl  restart docker

這樣設定完成後,就不會提示我們使用https的錯誤了。

2.建立Dockerfile

# vim Dockerfile 
FROM centos:centos7.1.1503
ENV TZ "Asia/Shanghai"

3.建立映象

# docker build -t rgs.unixfbi.com/library/centos7.1:0.1 .

4.把映象push到Harbor

# docker login rgs.unixfbi.com
# docker push rgs.unixfbi.com/library/centos7.1:0.1

如果不是自己建立的映象,記得先執行 docker tags 給映象做tag
例如:

# docker pull busybox
# docker tag busybox:latest rgs.unixfbi.com/library/busybox:latest
# docker push rgs.unixfbi.com/library/busybox:latest

5.登入web頁面檢視映象

6.pull映象

從別的機器上拉一下映象

# docker rmi -f $(docker images -q -a )
# docker pull rgs.unixfbi.com/library/centos7.1:0.1
0.1: Pulling from library/centos7.1
07618ba636d9: Pull complete 
Digest: sha256:7f398052ae0e93ddf96ba476185c7f436b15abd27acd848a24b88ede4bb3c322
Status: Downloaded newer image for rgs.unixfbi.com/library/centos7.1:0.1

# docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
rgs.unixfbi.com/library/centos7.1   0.1                 6c849613a995        5 hours ago         212MB

五、Harbor配置TLS證書

因為我們上面對Harbor的配置都是使用的http協議訪問,但是我們工作中一般都是配置https訪問。所以我給大家演示一下怎麼配置Harbor可以使用https訪問,以及配置TLS證書都需要做哪些工作。

1.修改Harbor配置檔案

因為Harbor預設使用http協議訪問,所以我們這裡在配置檔案中,開啟https配置;
配置harbor.cfg

hostname = rgs.unixfbi.com
ui_url_protocol = https
ssl_cert = /etc/certs/ca.crt
ssl_cert_key = /etc/certs/ca.key

2.建立自簽名證書key檔案(可以申請阿里免費證書)

# mkdir /etc/certs
# openssl genrsa -out /etc/certs/ca.key 2048 
Generating RSA private key, 2048 bit long modulus
....+++
..................................................+++
e is 65537 (0x10001)

3.建立自簽名證書crt檔案

注意命令中/CN=rgs.unixfbi.com欄位中rgs.unixfbi.com修改為你自己的倉庫域名。

# openssl req -x509 -new -nodes -key /etc/certs/ca.key -subj "/CN=rgs.unixfbi.com" -days 5000 -out /etc/certs/ca.crt

4.開始安裝Harbor

# ./install.sh

✔ ----Harbor has been installed and started successfully.----

Now you should be able to visit the admin portal at https://rgs.unixfbi.com. 
For more details, please visit https://github.com/vmware/harbor .

顯示是https了。

5.客戶端配置

客戶端需要建立證書檔案存放的位置,並且把服務端建立的證書拷貝到該目錄下,然後重啟客戶端docker。我們這裡建立目錄為:/etc/docker/certs.d/rgs.unixfbi.com

# mkdir -p /etc/docker/certs.d/rgs.unixfbi.com

把服務端crt證書檔案拷貝到客戶端,例如我這的客戶端為:192.168.199.183

# scp /etc/certs/ca.crt [email protected]:/etc/docker/certs.d/rgs.unixfbi.com/

重啟客戶端docker

# systemctl restart docker

6.測試是否支援https訪問

# docker login rgs.unixfbi.com
Username (admin): 
Password: 
Login Succeeded

搭建Harbor企業級docker倉庫

一、Harbor簡介

1.Harbor介紹

Harbor是一個用於儲存和分發Docker映象的企業級Registry伺服器,通過新增一些企業必需的功能特性,例如安全、標識和管理等,擴充套件了開源Docker Distribution。作為一個企業級私有Registry伺服器,Harbor提供了更好的效能和安全。提升使用者使用Registry構建和執行環境傳輸映象的效率。Harbor支援安裝在多個Registry節點的映象資源複製,映象全部儲存在私有Registry中, 確保資料和智慧財產權在公司內部網路中管控。另外,Harbor也提供了高階的安全特性,諸如使用者管理,訪問控制和活動審計等。

2.Harbor特性

  • 基於角色的訪問控制 :使用者與Docker映象倉庫通過“專案”進行組織管理,一個使用者可以對多個映象倉庫在同一名稱空間(project)裡有不同的許可權。
  • 映象複製 : 映象可以在多個Registry例項中複製(同步)。尤其適合於負載均衡,高可用,混合雲和多雲的場景。
  • 圖形化使用者介面 : 使用者可以通過瀏覽器來瀏覽,檢索當前Docker映象倉庫,管理專案和名稱空間。
  • AD/LDAP 支援 : Harbor可以整合企業內部已有的AD/LDAP,用於鑑權認證管理。
  • 審計管理 : 所有針對映象倉庫的操作都可以被記錄追溯,用於審計管理。
  • 國際化 : 已擁有英文、中文、德文、日文和俄文的本地化版本。更多的語言將會新增進來。
  • RESTful API : RESTful API 提供給管理員對於Harbor更多的操控, 使得與其它管理軟體整合變得更容易。
  • 部署簡單 : 提供線上和離線兩種安裝工具, 也可以安裝到vSphere平臺(OVA方式)虛擬裝置。

3.Harbor元件

Harbor在架構上主要由6個元件構成:

  • Proxy:Harbor的registry, UI, token等服務,通過一個前置的反向代理統一接收瀏覽器、Docker客戶端的請求,並將請求轉發給後端不同的服務。

  • Registry: 負責儲存Docker映象,並處理docker push/pull 命令。由於我們要對使用者進行訪問控制,即不同使用者對Docker image有不同的讀寫許可權,Registry會指向一個token服務,強制使用者的每次docker pull/push請求都要攜帶一個合法的token, Registry會通過公鑰對token 進行解密驗證。

  • Core services: 這是Harbor的核心功能,主要提供以下服務:

  • UI:提供圖形化介面,幫助使用者管理registry上的映象(image), 並對使用者進行授權。

  • webhook:為了及時獲取registry 上image狀態變化的情況, 在Registry上配置webhook,把狀態變化傳遞給UI模組。

  • token 服務:負責根據使用者許可權給每個docker push/pull命令簽發token. Docker 客戶端向Regiøstry服務發起的請求,如果不包含token,會被重定向到這裡,獲得token後再重新向Registry進行請求。

  • Database:為core services提供資料庫服務,負責儲存使用者許可權、審計日誌、Docker image分組資訊等資料。

  • Job Services:提供映象遠端複製功能,可以把本地映象同步到其他Harbor例項中。

  • Log collector:為了幫助監控Harbor執行,負責收集其他元件的log,供日後進行分析。

各個元件之間的關係如下圖所示:

4.Harbor實現

Harbor的每個元件都是以Docker容器的形式構建的,官方也是使用Docker Compose來對它進行部署。用於部署Harbor的Docker Compose模板位於 harbor/docker-compose.yml,開啟這個模板檔案,發現Harbor是由7個容器組成的;

# 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 

nginx:nginx負責流量轉發和安全驗證,對外提供的流量都是從nginx中轉,所以開放https的443埠,它將流量分發到後端的ui和正在docker映象儲存的docker registry。
harbor-jobservice:harbor-jobservice 是harbor的job管理模組,job在harbor裡面主要是為了映象倉庫之前同步使用的;
harbor-ui:harbor-ui是web管理頁面,主要是前端的頁面和後端CURD的介面;
registry:registry就是docker原生的倉庫,負責儲存映象。
harbor-adminserver:harbor-adminserver是harbor系統管理介面,可以修改系統配置以及獲取系統資訊。
這幾個容器通過Docker link的形式連線在一起,在容器之間通過容器名字互相訪問。對終端使用者而言,只需要暴露proxy (即Nginx)的服務埠。
harbor-db:harbor-db是harbor的資料庫,這裡儲存了系統的job以及專案、人員許可權管理。由於本harbor的認證也是通過資料,在生產環節大多對接到企業的ldap中;
harbor-log:harbor-log是harbor的日誌服務,統一管理harbor的日誌。通過inspect可以看出容器統一將日誌輸出的syslog。

這幾個容器通過Docker link的形式連線在一起,這樣,在容器之間可以通過容器名字互相訪問。對終端使用者而言,只需要暴露proxy (即Nginx)的服務埠。

二、安裝和配置Harbor

1.環境說明

環境名稱 版本
系統版本 CentOS Linux release 7.2.1511 (Core)
docker-ce 17.03.1-ce
docker-compose 1.16.1
Harbor v1.2.0
安裝方式 線上安裝
安裝位置 /usr/local/harbor

2.首先安裝docker

安裝Harbor需要先安裝docker和docker-compose

# yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
配置repository:
# yum-config-manager \
    --add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

安裝最新版本docker-ce   Docker version 17.03.1-ce
# yum install docker-ce

啟動docker:

# systemctl start docker

如果需要解除安裝docker-ce,操作如下:
yum remove docker-ce
解除安裝後images,containers,volumes,configuration files 是不能自動刪除的,為了刪除all images,containers,and volumes,請執行如下命令:
rm -rf /var/lib/docker

3.安裝docker-compose

必須先安裝docker

方法一:二進位制安裝

a.下載二進位制檔案
# curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

如果需要安裝其他版本的話,請修改上面命令中的版本號。

b.賦予二進位制檔案可執行許可權
# chmod +x /usr/local/bin/docker-compose
c.根據自己的情況決定是否安裝命令補全功能
# yum install bash-completion 
# curl -L https://raw.githubusercontent.com/docker/compose/1.16.1/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

重新登陸後就生效了

# docker-compose 
build    config   down     exec     images   logs     port     pull     restart  run      start    top      up       
bundle   create   events   help     kill     pause    ps       push     rm       scale    stop     unpause  version  
d.測試是否安裝成功
# docker-compose --version
docker-compose version 1.16.1, build 6d1ac21

方法二:pip

# yum install python-pip
# pip install  docker-compose

測試:

# docker-compose --version

解除安裝docker-compose
對應上面兩種安裝方法:
二進位制:

# rm  /usr/local/bin/docker-compose

pip:

# pip uninstall  docker-compose

4.安裝Harbor

安裝方式分為線上安裝和離線安裝兩種方式,我這裡選擇的是線上安裝。

wget -P /usr/loca/src/     https://github.com/vmware/harbor/releases/download/v1.2.0/harbor-online-installer-v1.2.0.tgz

也可以下載下面的檔案進行離線安裝:

https://github.com/vmware/harbor/releases/download/v1.2.0/harbor-offline-installer-v1.2.0.tgz

不過這個檔案,我沒有下載成功。因為在亞馬遜S3上總是連線超時
安裝:

# cd /usr/local/src/
# tar zxf harbor-online-installer-v1.2.0.tgz  -C /usr/local/
# cd /usr/local/harbor/

5.修改配置檔案

配置檔案為:/usr/local/harbor/harbor.cfg
配置的內容為:


# vim /usr/local/harbor/harbor.cfg
hostname = rgs.unixfbi.com
#郵箱配置
email_server = smtp.qq.com
email_server_port = 25
email_username = [email protected]
email_password =12345678
email_from = UnixFBI <[email protected]>
email_ssl = false
#禁止使用者註冊
self_registration = off
#設定只有管理員可以建立專案
project_creation_restriction = adminonly

6.執行安裝指令碼

 # /usr/local/harbor/install.sh

執行過程


[Step 0]: checking installation environment ...

Note: docker version: 17.03.1

Note: docker-compose version: 1.16.1


[Step 1]: preparing environment ...
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.


[Step 2]: checking existing instance of Harbor ...


[Step 3]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Pulling log (vmware/harbor-log:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-log
93b3dcee11d6: Pull complete
07d028a1dbdd: Pull complete
208723cd598a: Pull complete
b876b05989fc: Pull complete
12f0e0ef448a: Pull complete
Digest: sha256:608e10b7aaac07e10a4cd639d8848aef96daa7dd5c7ebaaf6a7ecd47f903e1f8
Status: Downloaded newer image for vmware/harbor-log:v1.2.0
Pulling adminserver (vmware/harbor-adminserver:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-adminserver
93b3dcee11d6: Already exists
6ab21236e58b: Pull complete
f70b0efff900: Pull complete
c5d206b5e184: Pull complete
Digest: sha256:aba66ec90fc12fe0814cecc9f647f5d17b41395199821cc7af69db9c0fbe446c
Status: Downloaded newer image for vmware/harbor-adminserver:v1.2.0
Pulling registry (vmware/registry:2.6.2-photon)...
2.6.2-photon: Pulling from vmware/registry
93b3dcee11d6: Already exists
007fe7635995: Pull complete
20d63c99b572: Pull complete
2026103b2811: Pull complete
ab7a5be79b33: Pull complete
11757dc5c642: Pull complete
69ab3e15edde: Pull complete
Digest: sha256:3cf06cdff00f48e15a5254b6d0370bcb4423dcd158e7f970dc8246893125b032
Status: Downloaded newer image for vmware/registry:2.6.2-photon
Pulling ui (vmware/harbor-ui:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-ui
93b3dcee11d6: Already exists
6ab21236e58b: Already exists
7753f4b55df6: Pull complete
a647b33bdf74: Pull complete
eb30db926101: Pull complete
204d77847826: Pull complete
4910a0b56c59: Pull complete
e880a4b0031f: Pull complete
Digest: sha256:b198d8f2f59515d286bdcf06c7f99c370eb4475e2547495c3e1db8761940646b
Status: Downloaded newer image for vmware/harbor-ui:v1.2.0
Pulling mysql (vmware/harbor-db:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-db
df559435c037: Pull complete
a6310a57af5d: Pull complete
d13d90890144: Pull complete
b694d8967a6c: Pull complete
a34f6cef56a6: Pull complete
3519eec83af5: Pull complete
34bae610e56c: Pull complete
86a867bebd89: Pull complete
3db2d0ac366e: Pull complete
c0d307ee295f: Pull complete
c5b1b404c5ee: Pull complete
14f4a4328366: Pull complete
c7fa77b46ec4: Pull complete
0cbe2b5669b8: Pull complete
Digest: sha256:c10b3555beb6d1c851ae49a4e90ef4296a1ad42bcd1a58ae97e316b034515b6e
Status: Downloaded newer image for vmware/harbor-db:v1.2.0
Pulling jobservice (vmware/harbor-jobservice:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-jobservice
93b3dcee11d6: Already exists
6ab21236e58b: Already exists
64bd0172d071: Pull complete
b4f5382f226f: Pull complete
Digest: sha256:0692648176c1c87379025b0519036b9f3f1a0eceb2646f17dd40eb143c898d5c
Status: Downloaded newer image for vmware/harbor-jobservice:v1.2.0
Pulling proxy (vmware/nginx-photon:1.11.13)...
1.11.13: Pulling from vmware/nginx-photon
93b3dcee11d6: Already exists
d2a110a9296e: Pull complete
Digest: sha256:9ec5644c667e87bf051e581ce74b2933d3ed469b27862534ba60ccf17b4ff57a
Status: Downloaded newer image for vmware/nginx-photon:1.11.13
Creating harbor-log ... 
Creating harbor-log ... done
Creating harbor-db ... 
Creating harbor-adminserver ... 
Creating registry ... 
Creating harbor-db
Creating registry
Creating registry ... done
Creating harbor-ui ... 
Creating harbor-ui ... done
Creating nginx ... 
Creating harbor-jobservice ... 
Creating harbor-jobservice
Creating nginx ... done

✔ ----Harbor has been installed and started successfully.----

Now you should be able to visit the admin portal at http://rgs.unixfbi.com. 
For more details, please visit https://github.com/vmware/harbor .

7.Harbor啟動和停止

Harbor 的日常運維管理是通過docker-compose來完成的,Harbor本身有多個服務程序,都放在docker容器之中執行,我們可以通過docker ps命令檢視。

# docker ps            
CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS              PORTS                                                              NAMES
e6da3450bebe        vmware/harbor-jobservice:v1.2.0    "/harbor/harbor_jo..."   4 days ago          Up 4 days                                                                              harbor-jobservice
6134227d7ed4        vmware/nginx-photon:1.11.13        "nginx -g 'daemon ..."   4 days ago          Up 4 days           0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp   nginx
ef5fe0b856a7        vmware/harbor-ui:v1.2.0            "/harbor/harbor_ui"      4 days ago          Up 4 days                                                                              harbor-ui
a752861c7e92        vmware/registry:2.6.2-photon       "/entrypoint.sh se..."   4 days ago          Up 4 days           5000/tcp                                                           registry
78ea91838582        vmware/harbor-db:v1.2.0            "docker-entrypoint..."   4 days ago          Up 4 days           3306/tcp                                                           harbor-db
024e226ff135        vmware/harbor-adminserver:v1.2.0   "/harbor/harbor_ad..."   4 days ago          Up 4 days                                                                              harbor-adminserver
3fab80444a1d        vmware/harbor-log:v1.2.0           "/bin/sh -c 'crond..."   4 days ago          Up 4 days           127.0.0.1:1514->514/tcp                                            harbor-log

或者docker-compose 來檢視

# cd /usr/local/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            

Harbor的啟動和停止

啟動Harbor
# docker-compose start
停止Harbor
# docker-comose stop
重啟Harbor
# docker-compose restart

8.訪問測試

在瀏覽器輸入rgs.unixfbi.com,因為我配置的域名為rgs.unixfbi.com。請大家根據自己的配置情況輸入訪問的域名;
預設賬號密碼: admin / Harbor12345 登入後修改密碼

四、測試上傳和下載映象

1.修改各docker client配置

# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --insecure-registry rgs.unixfbi.com

增加 --insecure-registry rgs.unixfbi.com 即可。
重啟docker:

# systemctl daemon-reload
# systemctl  restart docker

或者

建立/etc/docker/daemon.json檔案,在檔案中指定倉庫地址
# cat > /etc/docker/daemon.json << EOF
{ "insecure-registries":["rgs.unixfbi.com"] }
EOF
然後重啟docker就可以。

# systemctl  restart docker

這樣設定完成後,就不會提示我們使用https的錯誤了。

2.建立Dockerfile

# vim Dockerfile 
FROM centos:centos7.1.1503
ENV TZ "Asia/Shanghai"

3.建立映象

# docker build -t rgs.unixfbi.com/library/centos7.1:0.1 .

4.把映象push到Harbor

# docker login rgs.unixfbi.com
# docker push rgs.unixfbi.com/library/centos7.1:0.1

如果不是自己建立的映象,記得先執行 docker tags 給映象做tag
例如:

# docker pull busybox
# docker tag busybox:latest rgs.unixfbi.com/library/busybox:latest
# docker push rgs.unixfbi.com/library/busybox:latest

5.登入web頁面檢視映象

6.pull映象

從別的機器上拉一下映象

# docker rmi -f $(docker images -q -a )
# docker pull rgs.unixfbi.com/library/centos7.1:0.1
0.1: Pulling from library/centos7.1
07618ba636d9: Pull complete 
Digest: sha256:7f398052ae0e93ddf96ba476185c7f436b15abd27acd848a24b88ede4bb3c322
Status: Downloaded newer image for rgs.unixfbi.com/library/centos7.1:0.1

# docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
rgs.unixfbi.com/library/centos7.1   0.1                 6c849613a995        5 hours ago         212MB

五、Harbor配置TLS證書

因為我們上面對Harbor的配置都是使用的http協議訪問,但是我們工作中一般都是配置https訪問。所以我給大家演示一下怎麼配置Harbor可以使用https訪問,以及配置TLS證書都需要做哪些工作。

1.修改Harbor配置檔案

因為Harbor預設使用http協議訪問,所以我們這裡在配置檔案中,開啟https配置;
配置harbor.cfg

hostname = rgs.unixfbi.com
ui_url_protocol = https
ssl_cert = /etc/certs/ca.crt
ssl_cert_key = /etc/certs/ca.key

2.建立自簽名證書key檔案(可以申請阿里免費證書)

# mkdir /etc/certs
# openssl genrsa -out /etc/certs/ca.key 2048 
Generating RSA private key, 2048 bit long modulus
....+++
..................................................+++
e is 65537 (0x10001)

3.建立自簽名證書crt檔案

注意命令中/CN=rgs.unixfbi.com欄位中rgs.unixfbi.com修改為你自己的倉庫域名。

# openssl req -x509 -new -nodes -key /etc/certs/ca.key -subj "/CN=rgs.unixfbi.com" -days 5000 -out /etc/certs/ca.crt

4.開始安裝Harbor

# ./install.sh

✔ ----Harbor has been installed and started successfully.----

Now you should be able to visit the admin portal at https://rgs.unixfbi.com. 
For more details, please visit https://github.com/vmware/harbor .

顯示是https了。

5.客戶端配置

客戶端需要建立證書檔案存放的位置,並且把服務端建立的證書拷貝到該目錄下,然後重啟客戶端docker。我們這裡建立目錄為:/etc/docker/certs.d/rgs.unixfbi.com

# mkdir -p /etc/docker/certs.d/rgs.unixfbi.com

把服務端crt證書檔案拷貝到客戶端,例如我這的客戶端為:192.168.199.183

# scp /etc/certs/ca.crt [email protected]:/etc/docker/certs.d/rgs.unixfbi.com/

重啟客戶端docker

# systemctl restart docker

6.測試是否支援https訪問

# docker login rgs.unixfbi.com
Username (admin): 
Password: 
Login Succeeded