在說harbor以前,咱們首先說一說直接使用docker registry的一些缺陷:mysql
鑑於以上缺點,咱們一般在生產環境中,不會直接使用docker registry來實現提供鏡像服務。而Harbor正好解決了上述全部的問題。nginx
Harbor是一個用於存儲和分發Docker鏡像的企業級Registry服務器,經過添加一些企業必需的功能特性,例如安全、標識和管理等,擴展了開源Docker Distribution。做爲一個企業級私有Registry服務器,Harbor提供了更好的性能和安全。提高用戶使用Registry構建和運行環境傳輸鏡像的效率。Harbor支持安裝在多個Registry節點的鏡像資源複製,鏡像所有保存在私有Registry中,確保數據和知識產權在公司內部網絡中管控。另外,Harbor也提供了高級的安全特性,諸如用戶管理,訪問控制和活動審計等。git
Harbor官方網站:http://vmware.github.io/harbor/github
Harbor源碼地址:https://github.com/vmware/harborweb
harbor的二進制包同時提供online和offline版本,咱們這裏直接使用online版本。redis
如上圖所示,harbor由6大模塊級成:sql
須要說明的是,harbor的每一個組件都是以Docker容器的形式構建的,可使用Docker Compose來進行部署,固然,若是你的環境中使用了kubernetes,harbor也提供了kubernetes的配置文件。docker
harbor共有8個容器組成:數據庫
下面咱們全部的配置都以harbor 1.5.2版本做配置說明。json
harbor能夠支持http和https,建議使用https,https證書最好是受信任的ca頒發的證書,這樣,在配置docker的時候,就不須要添加"insecure-registries"配置項。咱們這裏就直接使用自簽名證書。
生成證書:
openssl genrsa -des3 -out server.key 1024 openssl rsa -in server.key -out server.key #去除server.key的加密口令 openssl req -new -key server.key -out server.csr openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
將證書放入/data/certs/目錄下:
cp server.crt server.key /data/certs
harbor的核心配置文件是harbor.cfg,路徑在源碼目錄下的make目錄下,示例配置以下:
_version = 1.5.0 # harbor的訪問地址 hostname = hub.dz11.com # 使用https協議 ui_url_protocol = https # harbor的數據庫密碼 db_password = xxxxx max_job_workers = 50 customize_crt = on # 證書相關路徑 ssl_cert = /data/certs/dz11.com.crt ssl_cert_key = /data/certs/dz11.com.key secretkey_path = /data admiral_url = NA # 定義日誌滾動 log_rotate_count = 50 log_rotate_size = 200M http_proxy = https_proxy = no_proxy = 127.0.0.1,localhost,ui # email相關配置 email_identity = email_server = smtp.163.com email_server_port = 25 email_username = yan_ruo_gu@163.com email_password = yan_ruo_gu0711 email_from = admin <yan_ruo_gu@163.com> email_ssl = false # 登陸harbor的管理員密碼 harbor_admin_password = xxxxxx # harbor的驗證方式,支持db_auth和ldap_auth,這裏使用的是ldap_auth,若是使用db_auth的話,直接將auth_mode修改成db_auth便可,ldap的配置再也不須要。 auth_mode = ldap_auth ldap_url = ldap://10.1.1.1:389 ldap_searchdn = CN=jenkins,OU=LDAP,OU=Dev-wh.xxx.com,DC=dev-wh,DC=xxx,DC=com ldap_search_pwd = xxxx ldap_basedn = OU=Dev-wh.xxx.com,DC=dev-wh,DC=xxx,DC=com ldap_filter = (objectClass=person) ldap_uid = 0 ldap_scope = 2 ldap_timeout = 5 self_registration = on token_expiration = 30 project_creation_restriction = everyone verify_remote_cert = on # 數據庫相關配置,默認若是不須要使用自建的數據庫,這些配置就都不須要 db_host = mysql db_password = root123 db_port = 3306 db_user = root redis_url = redis:6379 clair_db_host = postgres clair_db_password = password clair_db_port = 5432 clair_db_username = postgres clair_db = postgres uaa_endpoint = uaa.mydomain.org uaa_clientid = id uaa_clientsecret = secret uaa_verify_cert = true uaa_ca_cert = /path/to/ca.pem registry_storage_provider_name = filesystem registry_storage_provider_config =
harbor支持docker-compose和kubernetes的部署方式,默認採用docker-compose做單機部署。
先執行./prepare,而後執行./install.sh進行啓動。執行./install.sh的時候,即調用了docker-compose運行了當前目錄下的docker-compose.yml文件。
在執行.prepare的時候拋出以下異常:
root@ubuntu:~/harbor# ./prepare Generated and saved secret to 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/config.yml Generated configuration file: ./common/config/log/logrotate.conf Generated configuration file: ./common/config/jobservice/config.yml Generated configuration file: ./common/config/ui/app.conf Fail to generate key file: ./common/config/ui/private_key.pem, cert file: ./common/config/registry/root.crt
須要修改prepare文件,將第498行:
empty_subj = "/C=/ST=/L=/O=/CN=/"
修改以下:
empty_subj = "/C=US/ST=California/L=Palo Alto/O=VMware, Inc./OU=Harbor/CN=notarysigner"
在實際啓動過程當中,出現過registry啓動失敗的狀況,/var/log/harbor/registry.log輸出以下:
May 30 21:06:00 172.18.0.1 registry[3218]: panic: unable to configure authorization (token): unable to open token auth root certificate bundle file "/etc/registry/root.crt": open /etc/registry/root.crt: permission denied May 30 21:06:00 172.18.0.1 registry[3218]: May 30 21:06:00 172.18.0.1 registry[3218]: goroutine 1 [running]: May 30 21:06:00 172.18.0.1 registry[3218]: panic(0xb4cd40, 0xc4203ae160) May 30 21:06:00 172.18.0.1 registry[3218]: #011/usr/local/go/src/runtime/panic.go:500 +0x1a1 May 30 21:06:00 172.18.0.1 registry[3218]: github.com/docker/distribution/registry/handlers.NewApp(0x1067820, 0xc4203a8630, 0xc4202df180, 0x1067820) May 30 21:06:00 172.18.0.1 registry[3218]: #011/go/src/github.com/docker/distribution/registry/handlers/app.go:302 +0x1b6a May 30 21:06:00 172.18.0.1 registry[3218]: github.com/docker/distribution/registry.NewRegistry(0x7fcfa30dd198, 0xc4203a8630, 0xc4202df180, 0xe, 0x0, 0x0) May 30 21:06:00 172.18.0.1 registry[3218]: #011/go/src/github.com/docker/distribution/registry/registry.go:86 +0x213 May 30 21:06:00 172.18.0.1 registry[3218]: github.com/docker/distribution/registry.glob..func1(0x108f1a0, 0xc42036d240, 0x1, 0x1) May 30 21:06:00 172.18.0.1 registry[3218]: #011/go/src/github.com/docker/distribution/registry/registry.go:55 +0x106 May 30 21:06:00 172.18.0.1 registry[3218]: github.com/docker/distribution/vendor/github.com/spf13/cobra.(*Command).execute(0x108f1a0, 0xc42036d1f0, 0x1, 0x1, 0x108f1a0, 0xc42036d1f0) May 30 21:06:00 172.18.0.1 registry[3218]: #011/go/src/github.com/docker/distribution/vendor/github.com/spf13/cobra/command.go:495 +0x190 May 30 21:06:00 172.18.0.1 registry[3218]: github.com/docker/distribution/vendor/github.com/spf13/cobra.(*Command).Execute(0x108f340, 0xc4201d7f40, 0xc4200001a0) May 30 21:06:00 172.18.0.1 registry[3218]: #011/go/src/github.com/docker/distribution/vendor/github.com/spf13/cobra/command.go:560 +0x3c3 May 30 21:06:00 172.18.0.1 registry[3218]: main.main() May 30 21:06:00 172.18.0.1 registry[3218]: #011/go/src/github.com/docker/distribution/cmd/registry/main.go:24 +0x2d
這是由於registry容器沒有/etc/registry/root.crt的訪問權限致使。這個文件默認是掛載的./common/config/registry/root.crt
這個文件,因此咱們須要對這個文件做受權。
經過觀察可知,harbor的容器啓動用戶非root身份,而是以一個uid和gid都爲10000的用戶。因此只須要爲該用戶受權便可:
chown 10000.10000 ./common/config/registry/root.crt
正常啓動以後,配置完成。
能夠經過以下方式訪問: https://hub.dz11.com
須要說明的是,harbor支持http和https,但若是使用http的話,在拉取鏡像的時候,會拋出倉庫不受信任的異常。須要在全部的docker客戶端的docker配置文件/etc/docker/daemon.json中添加以下配置:
{ "insecure-registries": ["https://hub.dz11.com"], }
若是使用自簽名的https證書,仍然會提示證書不受信任的問題。須要將自簽名的ca證書發送到全部的docker客戶端的指定目錄。
關於使用自簽名證書配置harbor的具體過程能夠參考:https://github.com/WingkaiHo/docker-calico/blob/master/harbor/README.md