Harbor是一個用於存儲和分發Docker鏡像的企業級Registry服務器,經過添加一些企業必需的功能特性,例如安全、標識和管理等,擴展了開源Docker Distribution。做爲一個企業級私有Registry服務器,Harbor提供了更好的性能和安全。提高用戶使用Registry構建和運行環境傳輸鏡像的效率。Harbor支持安裝在多個Registry節點的鏡像資源複製,鏡像所有保存在私有Registry中, 確保數據和知識產權在公司內部網絡中管控。另外,Harbor也提供了高級的安全特性,諸如用戶管理,訪問控制和活動審計等。前端
基於角色的訪問控制 :用戶與Docker鏡像倉庫經過「項目」進行組織管理,一個用戶能夠對多個鏡像倉庫在同一命名空間(project)裏有不一樣的權限。java
鏡像複製 : 鏡像能夠在多個Registry實例中複製(同步)。尤爲適合於負載均衡,高可用,混合雲和多雲的場景。node
圖形化用戶界面 : 用戶能夠經過瀏覽器來瀏覽,檢索當前Docker鏡像倉庫,管理項目和命名空間。python
AD/LDAP 支持 : Harbor能夠集成企業內部已有的AD/LDAP,用於鑑權認證管理。mysql
審計管理 : 全部針對鏡像倉庫的操做均可以被記錄追溯,用於審計管理。linux
國際化 : 已擁有英文、中文、德文、日文和俄文的本地化版本。更多的語言將會添加進來。nginx
RESTful API : RESTful API 提供給管理員對於Harbor更多的操控, 使得與其它管理軟件集成變得更容易。git
部署簡單 : 提供在線和離線兩種安裝工具, 也能夠安裝到vSphere平臺(OVA方式)虛擬設備。github
Harbor在架構上主要由6個組件構成:web
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,供往後進行分析。
各個組件之間的關係以下圖所示:
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)的服務端口。
環境名稱 | 版本 |
---|---|
系統版本 | 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 |
安裝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
必須先安裝docker
# curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
若是須要安裝其餘版本的話,請修改上面命令中的版本號。
# chmod +x /usr/local/bin/docker-compose
# 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
# docker-compose --version docker-compose version 1.16.1, build 6d1ac21
# yum install python-pip # pip install docker-compose
測試:
# docker-compose --version
卸載docker-compose
對應上面兩種安裝方法:
二進制:
# rm /usr/local/bin/docker-compose
pip:
# pip uninstall docker-compose
安裝方式分爲在線安裝和離線安裝兩種方式,我這裏選擇的是在線安裝。
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/
配置文件爲:/usr/local/harbor/harbor.cfg
配置的內容爲:
# vim /usr/local/harbor/harbor.cfg hostname = rgs.unixfbi.com #郵箱配置 email_server = smtp.qq.com email_server_port = 25email_username = unixfbi@unixfbi.com email_password =12345678email_from = UnixFBI <unixfbi@unixfbi.com> email_ssl = false#禁止用戶註冊 self_registration = off #設置只有管理員能夠建立項目 project_creation_restriction = adminonly
# /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 .
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-jobservice6134227d7ed4 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 registry78ea91838582 vmware/harbor-db:v1.2.0 "docker-entrypoint..." 4 days ago Up 4 days 3306/tcp harbor-db024e226ff135 vmware/harbor-adminserver:v1.2.0 "/harbor/harbor_ad..." 4 days ago Up 4 days harbor-adminserver3fab80444a1d 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
在瀏覽器輸入rgs.unixfbi.com,由於我配置的域名爲rgs.unixfbi.com。請你們根據本身的配置狀況輸入訪問的域名;
默認帳號密碼: admin / Harbor12345 登陸後修改密碼
# 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的錯誤了。
# vim Dockerfile FROM centos:centos7.1.1503 ENV TZ "Asia/Shanghai"
# docker build -t rgs.unixfbi.com/library/centos7.1:0.1 .
# 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
從別的機器上拉一下鏡像
# docker rmi -f $(docker images -q -a ) # docker pull rgs.unixfbi.com/library/centos7.1:0.10.1: Pulling from library/centos7.107618ba636d9: 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的配置都是使用的http協議訪問,可是咱們工做中通常都是配置https訪問。因此我給你們演示一下怎麼配置Harbor可使用https訪問,以及配置TLS證書都須要作哪些工做。
由於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
# mkdir /etc/certs # openssl genrsa -out /etc/certs/ca.key 2048 Generating RSA private key, 2048 bit long modulus ....+++ ..................................................+++ e is 65537 (0x10001)
注意命令中/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
# ./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了。
客戶端須要建立證書文件存放的位置,而且把服務端建立的證書拷貝到該目錄下,而後重啓客戶端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 root@192.168.199.183:/etc/docker/certs.d/rgs.unixfbi.com/
重啓客戶端docker
# systemctl restart docker
# docker login rgs.unixfbi.comUsername (admin): Password: Login Succeeded
遇到的問題就是Harbor我配置的是http訪問,可是docker客戶端默認都是https訪問Harbor,因此就會產生錯誤。下面看看我是怎麼解決這個問題的吧。下面咱們來訪問如下Harbor
# docker pull rgs.unixfbi.com/library/centos7.1:0.1Error response from daemon: Get https://rgs.unixfbi.com/v1/_ping: dial tcp 192.168.199.233:443: getsockopt: connection refused
問題緣由:
由於docker默認訪問倉庫時都是使用的https協議,而咱們的倉庫配置的是http
解決方法:
方法一:
在docker啓動的配置倉庫地址添加以下內容:--insecure-registry rgs.unixfbi.com
# vim /usr/lib/systemd/system/docker.service ExecStart=/usr/bin/dockerd --insecure-registry rgs.unixfbi.com
而後
# 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 ,設置證書。好吧,這種方法其實我不說,你也知道。
本文出自 「運維特工」 博客,轉載請務必保留此出處 http://www.unixfbi.com