harbor的安裝和使用

1、介紹

Docker容器應用的開發和運行離不開可靠的鏡像管理,雖然Docker官方也提供了公共的鏡像倉庫,但是從安全和效率等方面考慮,部署我們私有環境內的Registry也是非常必要的。Harbor是由VMware公司開源的企業級的Docker Registry管理項目,它包括權限管理(RBAC)、LDAP、日誌審覈、管理界面、自我註冊、鏡像複製和中文支持等功能。

2、前提準備

確認已安裝好docker、docker-compose

[root@MyCentos7-1 harbor]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
 Go version:      go1.9.4
 Git commit:      94f4240/1.13.1
 Built:           Fri May 18 15:44:33 2018
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
 Go version:      go1.9.4
 Git commit:      94f4240/1.13.1
 Built:           Fri May 18 15:44:33 2018
 OS/Arch:         linux/amd64
 Experimental:    false
[root@MyCentos7-1 harbor]# docker-compose version
docker-compose version 1.12.0, build b31ff33
docker-py version: 2.2.1
CPython version: 2.7.13
OpenSSL version: OpenSSL 1.0.1t  3 May 2016

3、安裝Harbor

3.1下載Harbor安裝文件並解壓(離線包)

[root@MyCentos7-1 docker]# wget https://storage.googleapis.com/harbor-releases/release-1.5.0/harbor-offline-installe-v1.5.1.tgz
[root@MyCentos7-1 docker]# tar xvf harbor-offline-installer-v1.5.1.tgz

3.2修改配置文件harbor.cfg(紅色字體爲改動部分)

解壓縮之後,目錄下回生成harbor.cfg文件,該文件就是Harbor的配置文件

[root@MyCentos7-1 harbor]# ll
總用量 856144
drwxr-xr-x. 4 root root        37 6月  11 10:54 common
-rw-r--r--. 1 root root      1185 5月  31 14:45 docker-compose.clair.yml
-rw-r--r--. 1 root root      1725 5月  31 14:45 docker-compose.notary.yml
-rw-r--r--. 1 root root      3596 5月  31 14:45 docker-compose.yml
drwxr-xr-x. 3 root root       156 5月  31 14:45 ha
-rw-r--r--. 1 root root      6683 6月  11 10:51 harbor.cfg
-rw-r--r--. 1 root root 876607879 5月  31 14:46 harbor.v1.5.1.tar.gz
-rwxr-xr-x. 1 root root      5773 5月  31 14:45 install.sh
-rw-r--r--. 1 root root     10771 5月  31 14:45 LICENSE
-rw-r--r--. 1 root root       482 5月  31 14:45 NOTICE
-rwxr-xr-x. 1 root root     27379 5月  31 14:45 prepare

 

[root@MyCentos7-1 harbor]# vim harbor.cfg

## Configuration file of Harbor

#This attribute is for migrator to detect the version of the .cfg file, DO NOT MODIFY!
_version = 1.5.0
#The IP address or hostname to access admin UI and registry service.
#DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
hostname = MyCentos7-1          ##設置訪問地址,可以使用ip、主機名,不可以設置爲127.0.0.1或localhost

#The protocol for accessing the UI and token/notification service, by default it is http.
#It can be set to https if ssl is enabled on nginx.
ui_url_protocol = http      ##設置訪問協議,默認http,若設爲https則nginx ssl需要設置on

#Maximum number of job workers in job service  
max_job_workers = 50 

#Determine whether or not to generate certificate for the registry's token.
#If the value is on, the prepare script creates new root cert and private key 
#for generating token to access the registry. If the value is off the default key/cert will be used.
#This flag also controls the creation of the notary signer's cert.
customize_crt = on

#The path of cert and key files for nginx, they are applied only the protocol is set to https
ssl_cert = /data/cert/server.crt           ##若沒有此目錄則需要手動建立
ssl_cert_key = /data/cert/server.key

#The path of secretkey storage
secretkey_path = /data

#Admiral's url, comment this attribute, or set its value to NA when Harbor is standalone
admiral_url = NA

#Log files are rotated log_rotate_count times before being removed. If count is 0, old versions are removed rather than rotated.
log_rotate_count = 50
#Log files are rotated only if they grow bigger than log_rotate_size bytes. If size is followed by k, the size is assumed to be in kilobytes. 
#If the M is used, the size is in megabytes, and if G is used, the size is in gigabytes. So size 100, size 100k, size 100M and size 100G 
#are all valid.
log_rotate_size = 200M

#Config http proxy for Clair, e.g. http://my.proxy.com:3128
#Clair doesn't need to connect to harbor ui container via http proxy.
http_proxy =
https_proxy =
no_proxy = 127.0.0.1,localhost,ui

#NOTES: The properties between BEGIN INITIAL PROPERTIES and END INITIAL PROPERTIES
#only take effect in the first boot, the subsequent changes of these properties 
#should be performed on web ui

#************************BEGIN INITIAL PROPERTIES************************

#Email account settings for sending out password resetting emails.

#Email server uses the given username and password to authenticate on TLS connections to host and act as identity.
#Identity left blank to act as username.
email_identity =             ##郵件設置,發送重置密碼郵件時使用

email_server = smtp.mydomain.com
email_server_port = 25
email_username = [email protected]
email_password = abc
email_from = admin <[email protected]>
email_ssl = false
email_insecure = false

##The initial password of Harbor admin, only works for the first time when Harbor starts. 
#It has no effect after the first launch of Harbor.
#Change the admin password from UI after launching Harbor.
harbor_admin_password = 12345    ##啓動Harbor後,管理員UI登錄的密碼,默認是Harbor12345

##By default the auth mode is db_auth, i.e. the credentials are stored in a local database.
#Set it to ldap_auth if you want to verify a user's credentials against an LDAP server.
auth_mode = db_auth   ##認證方式,這裏支持多種認證方式,如LADP、本次存儲、數據庫認證。默認是db_auth,mysql數據庫認證

#The url for an ldap endpoint.  ##LDAP認證時配置項
ldap_url = ldaps://ldap.mydomain.com

#A user's DN who has the permission to search the LDAP/AD server. 
#If your LDAP/AD server does not support anonymous search, you should configure this DN and ldap_search_pwd.
#ldap_searchdn = uid=searchuser,ou=people,dc=mydomain,dc=com

#the password of the ldap_searchdn
#ldap_search_pwd = password

#The base DN from which to look up a user in LDAP/AD
ldap_basedn = ou=people,dc=mydomain,dc=com

#Search filter for LDAP/AD, make sure the syntax of the filter is correct.
#ldap_filter = (objectClass=person)

# The attribute used in a search to match a user, it could be uid, cn, email, sAMAccountName or other attributes depending on your LDAP/AD  
ldap_uid = uid 

#the scope to search for users, 0-LDAP_SCOPE_BASE, 1-LDAP_SCOPE_ONELEVEL, 2-LDAP_SCOPE_SUBTREE
ldap_scope = 2 

#Timeout (in seconds)  when connecting to an LDAP Server. The default value (and most reasonable) is 5 seconds.
ldap_timeout = 5

#Verify certificate from LDAP server
ldap_verify_cert = true

#The base dn from which to lookup a group in LDAP/AD
ldap_group_basedn = ou=group,dc=mydomain,dc=com

#filter to search LDAP/AD group
ldap_group_filter = objectclass=group
#The attribute used to name a LDAP/AD group, it could be cn, name
ldap_group_gid = cn

#The scope to search for ldap groups. 0-LDAP_SCOPE_BASE, 1-LDAP_SCOPE_ONELEVEL, 2-LDAP_SCOPE_SUBTREE
ldap_group_scope = 2

#Turn on or off the self-registration feature
self_registration = on     ##是否開啓自注冊

#The expiration time (in minute) of token created by token service, default is 30 minutes
token_expiration = 30      ##Token有效時間,默認30分鐘

#The flag to control what users have permission to create projects
#The default value "everyone" allows everyone to creates a project. 
#Set to "adminonly" so that only admin user can create project.
project_creation_restriction = everyone     ##用戶創建項目權限控制,默認是everyone(所有人),也可以設置爲adminonly(只能管理員)

#************************END INITIAL PROPERTIES************************

#######Harbor DB configuration section#######

#The address of the Harbor database. Only need to change when using external db.
db_host = mysql

#The password for the root user of Harbor DB. Change this before any production use.
db_password = root123

#The port of Harbor database host
db_port = 3306

#The user name of Harbor database
db_user = root

##### End of Harbor DB configuration#######

#The redis server address. Only needed in HA installation.
#address:port[,weight,password,db_index]
#redis_url = redis:6379
redis_url = 


##########Clair DB configuration############

#Clair DB host address. Only change it when using an exteral DB.
clair_db_host = postgres

#The password of the Clair's postgres database. Only effective when Harbor is deployed with Clair.
#Please update it before deployment. Subsequent update will cause Clair's API server and Harbor unable to access Clair's database.
clair_db_password = password

#Clair DB connect port
clair_db_port = 5432

#Clair DB username
clair_db_username = postgres

#Clair default database
clair_db = postgres

##########End of Clair DB configuration############

#The following attributes only need to be set when auth mode is uaa_auth
uaa_endpoint = uaa.mydomain.org
uaa_clientid = id
uaa_clientsecret = secret
uaa_verify_cert = true
uaa_ca_cert = /path/to/ca.pem


### Docker Registry setting ###
#registry_storage_provider can be: filesystem, s3, gcs, azure, etc.
registry_storage_provider_name = filesystem
#registry_storage_provider_config is a comma separated "key: value" pairs, e.g. "key1: value, key2: value2".
#Refer to https://docs.docker.com/registry/configuration/#storage for all available configuration.
registry_storage_provider_config =

 

3.3安裝並啓動

修改完配置文件後,在的當前目錄執行./install.sh,Harbor服務就會根據當期目錄下的docker-compose.yml開始下載依賴的鏡像,檢測並按照順序依次啓動各服務

 

[root@MyCentos7-1 harbor]# ./install.sh 

[Step 0]: checking installation environment ...

Note: docker version: 1.13.1

Note: docker-compose version: 1.12.0

[Step 1]: loading Harbor images ...
52ef9064d2e4: Loading layer [==================================================>] 135.9 MB/135.9 MB
4a6862dbadda: Loading layer [==================================================>] 23.25 MB/23.25 MB
58b7d0c522b2: Loading layer [==================================================>]  24.4 MB/24.4 MB
9cd4bb748634: Loading layer [==================================================>] 7.168 kB/7.168 kB
c81302a14908: Loading layer [==================================================>] 10.56 MB/10.56 MB
7848e9ba72a3: Loading layer [==================================================>] 24.39 MB/24.39 MB
Loaded image: vmware/harbor-ui:v1.5.1
f1691b5a5198: Loading layer [==================================================>] 73.15 MB/73.15 MB
a529013c99e4: Loading layer [==================================================>] 3.584 kB/3.584 kB
d9b4853cff8b: Loading layer [==================================================>] 3.072 kB/3.072 kB
3d305073979e: Loading layer [==================================================>] 4.096 kB/4.096 kB
c9e17074f54a: Loading layer [==================================================>] 3.584 kB/3.584 kB
956055840e30: Loading layer [==================================================>] 9.728 kB/9.728 kB
Loaded image: vmware/harbor-log:v1.5.1
185db06a02d0: Loading layer [==================================================>] 23.25 MB/23.25 MB
835213979c70: Loading layer [==================================================>]  20.9 MB/20.9 MB
f74eeb41c1c9: Loading layer [==================================================>]  20.9 MB/20.9 MB
Loaded image: vmware/harbor-jobservice:v1.5.1
9bd5c7468774: Loading layer [==================================================>] 23.25 MB/23.25 MB
5fa6889b9a6d: Loading layer [==================================================>]  2.56 kB/2.56 kB
bd3ac235b209: Loading layer [==================================================>]  2.56 kB/2.56 kB
cb5d493833cc: Loading layer [==================================================>] 2.048 kB/2.048 kB
557669a074de: Loading layer [==================================================>]  22.8 MB/22.8 MB
f02b4f30a9ac: Loading layer [==================================================>]  22.8 MB/22.8 MB
Loaded image: vmware/registry-photon:v2.6.2-v1.5.1
5d3b562db23e: Loading layer [==================================================>] 23.25 MB/23.25 MB
8edca1b0e3b0: Loading layer [==================================================>] 12.16 MB/12.16 MB
ce5f11ea46c0: Loading layer [==================================================>]  17.3 MB/17.3 MB
93750d7ec363: Loading layer [==================================================>] 15.87 kB/15.87 kB
36f81937e80d: Loading layer [==================================================>] 3.072 kB/3.072 kB
37e5df92b624: Loading layer [==================================================>] 29.46 MB/29.46 MB
Loaded image: vmware/notary-server-photon:v0.5.1-v1.5.1
0a2f8f90bd3a: Loading layer [==================================================>] 401.3 MB/401.3 MB
41fca4deb6bf: Loading layer [==================================================>] 9.216 kB/9.216 kB
f2e28262e760: Loading layer [==================================================>] 9.216 kB/9.216 kB
68677196e356: Loading layer [==================================================>]  7.68 kB/7.68 kB
2b006714574e: Loading layer [==================================================>] 1.536 kB/1.536 kB
Loaded image: vmware/mariadb-photon:v1.5.1
a8c4992c632e: Loading layer [==================================================>] 156.3 MB/156.3 MB
0f37bf842677: Loading layer [==================================================>] 10.75 MB/10.75 MB
9f34c0cd38bf: Loading layer [==================================================>] 2.048 kB/2.048 kB
91ca17ca7e16: Loading layer [==================================================>] 48.13 kB/48.13 kB
5a7e0da65127: Loading layer [==================================================>]  10.8 MB/10.8 MB
Loaded image: vmware/clair-photon:v2.0.1-v1.5.1
0e782fe069e7: Loading layer [==================================================>] 23.25 MB/23.25 MB
67fc1e2f7009: Loading layer [==================================================>] 15.36 MB/15.36 MB
8db2141aa82c: Loading layer [==================================================>] 15.36 MB/15.36 MB
Loaded image: vmware/harbor-adminserver:v1.5.1
3f87a34f553c: Loading layer [==================================================>] 4.772 MB/4.772 MB
Loaded image: vmware/nginx-photon:v1.5.1
Loaded image: vmware/photon:1.0
ad58f3ddcb1b: Loading layer [==================================================>] 10.95 MB/10.95 MB
9b50f12509bf: Loading layer [==================================================>]  17.3 MB/17.3 MB
2c21090fd212: Loading layer [==================================================>] 15.87 kB/15.87 kB
38bec864f23e: Loading layer [==================================================>] 3.072 kB/3.072 kB
6e81ea7b0fa6: Loading layer [==================================================>] 28.24 MB/28.24 MB
Loaded image: vmware/notary-signer-photon:v0.5.1-v1.5.1
897a26fa09cb: Loading layer [==================================================>] 95.02 MB/95.02 MB
16e3a10a21ba: Loading layer [==================================================>] 6.656 kB/6.656 kB
85ecac164331: Loading layer [==================================================>] 2.048 kB/2.048 kB
37a2fb188706: Loading layer [==================================================>]  7.68 kB/7.68 kB
Loaded image: vmware/postgresql-photon:v1.5.1
bed9f52be1d1: Loading layer [==================================================>] 11.78 kB/11.78 kB
d731f2986f6e: Loading layer [==================================================>]  2.56 kB/2.56 kB
c3fde9a69f96: Loading layer [==================================================>] 3.072 kB/3.072 kB
Loaded image: vmware/harbor-db:v1.5.1
7844feb13ef3: Loading layer [==================================================>] 78.68 MB/78.68 MB
de0fd8aae388: Loading layer [==================================================>] 3.072 kB/3.072 kB
3f79efb720fd: Loading layer [==================================================>]  59.9 kB/59.9 kB
1c02f801c2e8: Loading layer [==================================================>] 61.95 kB/61.95 kB
Loaded image: vmware/redis-photon:v1.5.1
454c81edbd3b: Loading layer [==================================================>] 135.2 MB/135.2 MB
e99db1275091: Loading layer [==================================================>] 395.4 MB/395.4 MB
051e4ee23882: Loading layer [==================================================>] 9.216 kB/9.216 kB
6cca4437b6f6: Loading layer [==================================================>] 9.216 kB/9.216 kB
1d48fc08c8bc: Loading layer [==================================================>]  7.68 kB/7.68 kB
0419724fd942: Loading layer [==================================================>] 1.536 kB/1.536 kB
543c0c1ee18d: Loading layer [==================================================>] 655.2 MB/655.2 MB
4190aa7e89b8: Loading layer [==================================================>] 103.9 kB/103.9 kB
Loaded image: vmware/harbor-migrator:v1.5.0


[Step 2]: preparing environment ...
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
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 3]: checking existing instance of Harbor ...
[Step 4]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Creating harbor-log
Creating harbor-adminserver
Creating redis
Creating harbor-db
Creating registry
Creating harbor-ui
Creating nginx
Creating harbor-jobservice

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

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

4、訪問Harbor

啓動完成後,我們訪問剛設置的hostname即可 http://MyCentos7-1,默認是80端口,如果端口占用,我們可以去修改docker-compose.yml文件中,對應服務的端口映射。windos,hosts文件地址:C:\Windows\System32\drivers\etc,將域名與ip添加進去即可。 (或直接訪問harbor所在物理機地址:http://192.168.126.131:80)

輸入用戶名admin,默認密碼(或已修改密碼)登錄系統

 

我們可以看到系統各個模塊如下:

  • 項目:新增/刪除項目,查看鏡像倉庫,給項目添加成員、查看操作日誌、複製項目等
  • 日誌:倉庫各個鏡像create、push、pull等操作日誌
  • 系統管理 
    • 用戶管理:新增/刪除用戶、設置管理員等
    • 複製管理:新增/刪除從庫目標、新建/刪除/啓停複製規則等
    • 配置管理:認證模式、複製、郵箱設置、系統設置等
  • 其他設置 
    • 用戶設置:修改用戶名、郵箱、名稱信息
    • 修改密碼:修改用戶密碼

注意:非系統管理員用戶登錄,只能看到有權限的項目和日誌,其他模塊不可見。

 

  • 新建項目

我們新建一個名稱爲wanyang的項目,設置不公開。注意:當項目設爲公開後,任何人都有此項目下鏡像的讀權限。命令行用戶不需要“docker login”就可以拉取此項目下的鏡像。 

 

新建項目完畢後,我們就可以用admin賬戶提交本地鏡像到Harbor倉庫了。例如我們提交本地nginx鏡像

 

1、admin登錄
$ docker login mycentos7-1
Username: admin
Password:
Login Succeeded

2、給鏡像打tag【tag 修改 image 的名字. 格式爲: userip/項目名/image名字:版本號】
$ docker tag nginx 192.168.126.131/docker/nginx:latest
$ docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
nginx                              latest              958a7ae9e569        2 weeks ago         109 MB
mycentos7-1/docker/nginx         latest                958a7ae9e569        2 weeks ago         109 MB

3、push到倉庫
$ docker push mycentos7-1/docker/nginx
The push refers to a repository [docker.bksx.com/docker/nginx]
a552ca691e49: Pushed
7487bf0353a7: Pushed
8781ec54ba04: Pushed
latest: digest: sha256:41ad9967ea448d7c2b203c699b429abe1ed5af331cd92533900c6d77490e0268 size: 948

 

 上傳完畢後,登錄Web Harbor,選擇項目,項目名稱docker,就可以查看剛纔上傳的nginx image了



  • FAQ
  • 配置並啓動Harbor之後,本地執行登錄操作,報錯:
docker login mycentos7-1
Username: admin
Password:
Error response from daemon: Get https://mycentos7-1/v2/users/: dial tcp 192.168.126.131:443: getsockopt: connection refused

這是因爲docker1.3.2版本開始默認docker registry使用的是https,我們設置Harbor默認http方式,所以當執行用docker login、pull、push等命令操作非https的docker regsitry的時就會報錯。解決辦法:

    • 如果系統是MacOS,則可以點擊“Preference”裏面的“Advanced”在“Insecure 
      Registry”里加上mycentos7-1,重啓Docker客戶端就可以了。
    • 如果系統是Ubuntu,則修改配置文件/lib/systemd/system/docker.service,修改[Service]下ExecStart參數,增加– insecure-registry=mycentos7-1。
    • 如果系統是Centos,可以修改配置/etc/sysconfig/docker,將OPTIONS增加 --insecure-registry=mycentos7-1。
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false --insecure-registry=mycentos7-1'
    • 如果是新版本的docker(17.0.3及以上版本)在/etc/sysconfig/ 沒有docker這個配置文件的情況下。

 

#在/etc/docker/目錄下創建daemon.json文件
#並在daemon.json中添加以下參數
[root@MyCentos7-1 harbor]# vim /etc/docker/daemon.json 
{
  "insecure-registries": [
    "mycentos7-1"     ------注:此爲harbor的域名或地址
  ]
}

 

注意:該文件必須符合 json 規範,否則 Docker 將不能啓動。


 

如果需要修改Harbor的配置文件harbor.cfg,因爲Harbor是基於docker-compose服務編排的,我們可以使用docker-compose命令重啓Harbor。不修改配置文件,重啓Harbor命令:docker-compose start | stop | restart

注:需要在harbor安裝目錄所在文件夾下執行,否則會報錯

 

[root@MyCentos7-1 ~]# docker-compose up -d
ERROR: 
        Can't find a suitable configuration file in this directory or any
        parent. Are you in the right directory?

        Supported filenames: docker-compose.yml, docker-compose.yaml

[root@MyCentos7-1 ~]# cd /service/docker/harbor/

[root@MyCentos7-1 harbor]# ll
總用量 856144
drwxr-xr-x. 4 root root 37 6月 11 10:54 common
-rw-r--r--. 1 root root 1185 5月 31 14:45 docker-compose.clair.yml
-rw-r--r--. 1 root root 1725 5月 31 14:45 docker-compose.notary.yml
-rw-r--r--. 1 root root 3596 5月 31 14:45 docker-compose.yml
drwxr-xr-x. 3 root root 156 5月 31 14:45 ha
-rw-r--r--. 1 root root 6687 6月 11 18:47 harbor.cfg
-rw-r--r--. 1 root root 6687 6月 11 10:24 harbor.cfg.bak
-rw-r--r--. 1 root root 876607879 5月 31 14:46 harbor.v1.5.1.tar.gz
-rwxr-xr-x. 1 root root 5773 5月 31 14:45 install.sh
-rw-r--r--. 1 root root 10771 5月 31 14:45 LICENSE
-rw-r--r--. 1 root root 482 5月 31 14:45 NOTICE
-rwxr-xr-x. 1 root root 27379 5月 31 14:45 prepare

故應該在/service/docker/harbor目錄下執行。

 

 

1、停止Harbor
$ docker-compose down -v
Stopping nginx ... done
Stopping harbor-jobservice ... done
......
Removing harbor-log ... done
Removing network harbor_harbor

2、啓動Harbor
$ docker-compose up -d
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... 
......
Creating nginx
Creating harbor-jobservice ... done

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章