docker-镜像注册中心-harbor实践

docker-镜像注册中心-harbor实践

文章目录

  • docker-镜像注册中心-harbor实践
    • 前言
      • 安装前注意
      • 安装
      • 数据持久化
      • 更新
      • 端口修改
      • 管理harbor
      • 推送镜像
      • 拉取pull
    • 最后
    • 参考

前言

  • 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-镜像注册中心-harbor实践_第1张图片
  • 需要提前安装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

你可能感兴趣的:(docker,后台)