docker-鏡像註冊中心-harbor實踐

docker-鏡像註冊中心-harbor實踐

前言

  • docker官方的registry雖然提供了私有倉庫的能力,但易用性來說還是不太友好,沒有倉庫的管理中心,操作起來也不方便
  • Harbor是一個用於存儲Docker鏡像的企業級Registry服務
  • 除了提供可視化管理界面外,還有其他更多功能
  • github地址爲 https://github.com/goharbor/harbor ,github上有詳細的安裝說明
  • 本文將以harbor的安裝爲例進行實踐

安裝前注意

  • harbor默認使用80端口(想要修改也是可以的,通過修改docker-compose.yml和harbor.cfg),因此請保證80端口不會被佔用(如果不想使用80端口那麼請先查看本文中的端口修改此節的內容,否則修改端口意味着又要重新部署)
  • harbor安裝過程會安裝如下鏡像,因此注意原有的鏡像不要與harbor的同名,否則會導致安裝失敗
    • goharbor/nginx-photon
    • goharbor/harbor-portal
    • goharbor/harbor-core
    • goharbor/harbor-db
    • goharbor/harbor-registryctl
    • goharbor/registry-photon
    • goharbor/harbor-log
    • goharbor/harbor-jobservice
    • goharbor/redis-photon
      在這裏插入圖片描述
  • 需要提前安裝docker-compose :https://docs.docker.com/compose/install/
  • 但是docker-compose如果不科學上網那基本是下不下來的
    sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
    docker-compose --version
    
    

安裝

  • 對於harbor的安裝github上已有比較詳細的說明 :https://github.com/goharbor/harbor/blob/master/docs/installation_guide.md ,如果不看官方說明,也可以按照我下面的流程安裝

  • 下載在線版到本地,版本號請根據實際情況修改

    wget -c https://storage.googleapis.com/harbor-releases/release-1.7.0/harbor-online-installer-v1.7.5.tgz
    
  • 解壓

    tar xvf harbor-online-installer-<version>.tgz
    
  • 打開./harbor/harbor.cfg,主要修改hostname爲本機ip,其餘基本保持默認

  • 以下是harbor.cfg文件的內容

    ## Configuration file of Harbor
    
    #This attribute is for migrator to detect the version of the .cfg file, DO NOT MODIFY!
    _version = 1.7.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.
    #DO NOT comment out this line, modify the value of "hostname" directly, or the installation will fail.
    hostname = 192.168.25.215
    
    #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
    
    #Maximum number of job workers in job service
    max_job_workers = 10
    
    #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 internal components via http proxy.
    http_proxy =
    https_proxy =
    no_proxy = 127.0.0.1,localhost,core,registry
    
    #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 = 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
    
    #The url for an ldap endpoint.
    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
    
    #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
    
    #************************END INITIAL PROPERTIES************************
    
    #######Harbor DB configuration section#######
    
    #The address of the Harbor database. Only need to change when using external db.
    db_host = postgresql
    
    #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 = 5432
    
    #The user name of Harbor database
    db_user = postgres
    
    ##### End of Harbor DB configuration#######
    
    ##########Redis server configuration.############
    
    #Redis connection address
    redis_host = redis
    
    #Redis connection port
    redis_port = 6379
    
    #Redis connection password
    redis_password =
    
    #Redis connection db index
    #db_index 1,2,3 is for registry, jobservice and chartmuseum.
    #db_index 0 is for UI, it's unchangeable
    redis_db_index = 1,2,3
    
    ########## End of Redis server configuration ############
    
    ##########Clair DB configuration############
    
    #Clair DB host address. Only change it when using an exteral DB.
    clair_db_host = postgresql
    #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 = root123
    #Clair DB connect port
    clair_db_port = 5432
    #Clair DB username
    clair_db_username = postgres
    #Clair default database
    clair_db = postgres
    
    #The interval of clair updaters, the unit is hour, set to 0 to disable the updaters.
    clair_updaters_interval = 12
    
    ##########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
    
    
    ### Harbor Storage settings ###
    #Please be aware that the following storage settings will be applied to both docker registry and helm chart repository.
    #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".
    #To avoid duplicated configurations, both docker registry and chart repository follow the same storage configuration specifications of docker registry.
    #Refer to https://docs.docker.com/registry/configuration/#storage for all available configuration.
    registry_storage_provider_config =
    #registry_custom_ca_bundle is the path to the custom root ca certificate, which will be injected into the truststore
    #of registry's and chart repository's containers.  This is usually needed when the user hosts a internal storage with self signed certificate.
    registry_custom_ca_bundle =
    
    #If reload_config=true, all settings which present in harbor.cfg take effect after prepare and restart harbor, it overwrites exsiting settings.
    #reload_config=true
    #Regular expression to match skipped environment variables
    #skip_reload_env_pattern=(^EMAIL.*)|(^LDAP.*)
    
  • 保存後執行install.sh即會進入安裝過程,此過程會創建文件等,因此需要有權限,加上sudo

  • 可以看到安裝過程中會從dockerhub拉取相關鏡像,如果某些鏡像網絡原因下載失敗,則再次執行install.sh嘗試即可

  • 當看到如下提示即表示安裝成功

    ✔ ----Harbor has been installed and started successfully.----
    
    Now you should be able to visit the admin portal at http://192.168.25.215.
    For more details, please visit https://github.com/goharbor/harbor .
    
  • 此時打開(ip地址根據實際情況修改) http://192.168.25.215,默認用戶/密碼爲 admin/Harbor12345,登錄成功,表示大功告成,鼓掌鼓掌

數據持久化

  • 你一定會有疑惑,如果harbor移除後數據會不會丟失,答案是不會,因爲默認數據就保存在物理機的/data目錄下,當然你可以在harbor.cfg中修改

更新

  • 如果配置文件有更新,則需要執行以下命令

    $ sudo docker-compose down -v
    $ vim harbor.cfg
    $ sudo prepare
    $ sudo docker-compose up -d
    

端口修改

  • 默認端口非常容易被佔用

  • 因此有必要修改默認端口,主要分爲三步

  • 第一步是修改docker-compose.yml,用8888代替80,如下

    proxy:
        image: goharbor/nginx-photon:v1.6.0
        container_name: nginx
        restart: always
        volumes:
          - ./common/config/nginx:/etc/nginx:z
        ports:
          - 8888:80
          - 443:443
        depends_on:
          - postgresql
          - registry
          - core
          - portal
          - log
        logging:
          driver: "syslog"
          options:  
            syslog-address: "tcp://127.0.0.1:1514"
            tag: "proxy"
    
  • 第二步是修改harbor.cfg

    hostname = 192.168.25.215:8888
    
  • 第三步則是重新部署harbor

管理harbor

  • harbor有一大堆鏡像,因此可以使用docker-compose來管理它們的啓停

  • 在docker-compose.yml的同級目錄下

        sudo docker-compose stop 	#停止
        sudo docker-compose start  #啓動
    

推送鏡像

  • 登錄到剛剛部署完畢的harbor

    docker login 192.168.25.215
    
  • 登錄時會產生如下錯誤,這是因爲我們在安裝時用的是http,因此需要在daemon.json中增加–insecure-registry

    Error response from daemon: Get https://192.168.25.215/v2/: dial tcp 192.168.25.215:443: connect: connection refused

  • 在/etc/docker/daemon.json中增加

    {
      "insecure-registries":["192.168.25.215"]
    }
    
    
  • 保存後重啓docker

  • 此時爲hello-world打一個tag

    sudo docker tag hello-world 192.168.25.215/hwj/helloworld:1
    
    
  • 然後推送上去

    # docker push reg.yourdomain.com/myproject/myrepo:mytag
    sudo docker push 192.168.25.215/hwj/helloworld:1
    
  • 會發現提示:denied: requested access to the resource is denied

  • 這是因爲我們還沒有創建項目,因此需要在 http://192.168.25.215 這個網頁中創建hwj這個項目,創建後再push就會發現可以了

拉取pull

  • 如果想拉取私有倉庫的鏡像,那麼則需要加上私有倉庫服務的ip和端口,如

    sudo docker pull 192.168.25.215/hwj/helloworld
    
  • 或者在/etc/docker/daemon.json中的registry-mirrors添加 http://192.168.25.215
    {
    “registry-mirrors”: [“http://192.168.25.215:80”],
    “insecure-registries”:[“192.168.25.215:80”]
    }

  • 然後重啓docker

  • 之後就可以直接使用名稱下載,不用每次都加上ip和端口了

    sudo docker pull hwj/helloworld
    

最後

  • 總的來說harbor安裝相比nexus還是複雜了些,如果就單純地想存鏡像的話那麼建議使用nexus就好了

參考

Harbor倉庫介紹與搭建過程-運維生涯-51CTO博客
https://blog.51cto.com/11093860/2117805

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