欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

使用Harbor构建docker私有仓库

发布时间:2024/2/28 编程问答 69 豆豆
生活随笔 收集整理的这篇文章主要介绍了 使用Harbor构建docker私有仓库 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

目录

  • 一、概述
    • 1、什么是Harbor
    • 2、Harbor的优势
    • 3、Docker私有仓库架构
    • 4、Harbor 配置文件以及相关参数
      • (1)所需参数
      • (2)可选参数
  • 二、使用Harbor构建docker私有仓库
    • 1、部署 Harbor 服务
      • (1)下载 Harbor 安装程序
      • (2)配置 Harbor 参数文件
      • (3)启动 Harbor
      • (4)查看 Harbor 启动镜像和容器
      • (5)在UI界面创建项目
      • (6)在本地测试仓库功能
      • (7)在客户端上传镜像
    • 2、维护管理Harbor
      • (1)停止现有的 Harbor 实例
      • (2)修改配置文件 Harbor.cfg
      • (3)运行 prepare 脚本来填充配置
      • (4)重启服务
    • 3、创建 Harbor 用户
    • 3、移除 Harbor 服务容器以及全部数据

一、概述

1、什么是Harbor

Harbor 是VMware公司开源的云本地 registry 仓库,有可视化的Web管理界面,可以方便的管理和储存 Docker 镜像。Harbor 支持在多个仓库直接进行复制镜像,提供用户管理和访问控制和活动审计。

2、Harbor的优势

  • 基于角色控制
  • 基于镜像的复制策略
  • 支持LDAP/AD域,通过VPN连接域使用
  • 图像删除和垃圾收集
  • 图像UI,方便
  • 审计,此功能使用较少,一般企业中用ELK收集、分析日志
  • RESTful API

3、Docker私有仓库架构

图中组件解释
Proxy:通过一个前置的反向代理统一接收浏览器、Docker客户端的请求,并将请求转发给后端不同的服务
Registry:负责存储Docker镜像,并处理docker push/pull命令
Core services:Harbor的核心功能,包括UI、webhook、token服务
Database:为core services提供数据库服务
Log collector:·负责收集其他组件的log,供日后进行分析

图解
所有的请求都经过proxy代理,proxy代理转发给Core services和Registry,其中Core services包括UI界面、token令牌和webhook网页服务功能,Registry主要提供镜像存储功能。如果要进行下载上传镜像,要经过token令牌验证然后从Registry获取或上传镜像,每一次下载或上传都会生成日志记录,会记入Log collector,而用户身份权限及一些镜像语言信息会被存储在Database中。

4、Harbor 配置文件以及相关参数

Harbor的配置文件是:/usr/local/harbor/harbor.cfg
关于 Harbor.cfg 配置文件中有两类参数:所需参数和可选参数

(1)所需参数

修改此参数后,需要运行 install.sh脚本重新安装 Harbour,参数才会生效。

  • hostname
    • 用于访问用户界面和 register 服务。
    • 它应该是目标机器的 IP 地址或完全限定的域名(FQDN)
    • 例如 192.168.163.100 或test.com。
    • 不要使用 localhost 或 127.0.0.1 为主机名。
  • ui_url_protocol(参数选项:http 或 https,默认为 http)
    • 用于访问 UI 和令牌/通知服务的协议。
    • 如果公证处于启用状态,则此参数必须为 https。
  • max_job_workers
    • 镜像复制作业线程。
  • db_password
    • 用于db_auth 的MySQL数据库root 用户的密码。
  • customize_crt
    • 该属性可设置为打开或关闭,默认打开。
    • 打开此属性时,准备脚本创建私钥和根证书,用于生成/验证注册表令牌。
    • 当由外部来源提供密钥和根证书时,将此属性设置为 off。
  • ssl_cert
    • SSL 证书的路径,仅当协议设置为 https 时才应用。
  • ssl_cert_key
    • SSL 密钥的路径,仅当协议设置为 https 时才应用。
  • secretkey_path
    • 用于在复制策略中加密或解密远程 register 密码的密钥路径。
    • 不建议配置,有很大的安全隐患

(2)可选参数

这些参数对于更新是可选的,即用户可以将其保留为默认值,并在启动 Harbor 后在 Web UI 上进行更新。
如果进入 Harbor.cfg,只会在第一次启动 Harbor 时生效,随后对这些参数 的更新,Harbor.cfg 将被忽略。
注意:如果选择通过UI设置这些参数,请确保在启动Harbour后立即执行此操作。具体来说,必须在注册或在 Harbor 中创建任何新用户之前设置所需的

  • auth_mode
    • 当系统中有用户时(除了默认的 admin 用户),auth_mode 不能被修改。
  • Email
    • Harbor需要该参数才能向用户发送“密码重置”电子邮件,并且只有在需要该功能时才需要。
    • 请注意,在默认情况下SSL连接时没有启用。如果SMTP服务器需要SSL,但不支持STARTTLS,那么应该通过设置启用SSL email_ssl = TRUE。
  • harbour_admin_password
    • 管理员的初始密码,只在Harbour第一次启动时生效。
    • 之后,此设置将被忽略,并且应 UI中设置管理员的密码。
    • 默认的用户名/密码是 admin/Harbor12345。
  • auth_mode
    • 使用的认证类型
    • 默认情况下,它是 db_auth,即凭据存储在数据库中。
    • 对于LDAP身份验证,请将其设置为 ldap_auth。
  • self_registration
    • 启用/禁用用户注册功能。
    • 禁用时,新用户只能由 Admin 用户创建,只有管理员用户可以在 Harbour中创建新用户。
    • 注意:当 auth_mode 设置为 ldap_auth 时,自注册功能将始终处于禁用状态,并且该标志被忽略。
  • Token_expiration
    • 由令牌服务创建的令牌的到期时间(分钟),默认为 30 分钟。
    • 即登录–退出后,30分钟内可以不输入用户名和密码登录,30分钟后需要再次验证。
  • project_creation_restriction
    • 用于控制哪些用户有权创建项目的标志,表示哪些用户可以创建项目。
    • 默认情况下, 每个人都可以创建一个项目。
    • 如果将其值设置为“adminonly”,那么只有 admin 可以创建项目。
  • verify_remote_cert
    • 打开或关闭,默认打开。
    • 此标志决定了当Harbor与远程 register 实例通信时是否验证 SSL/TLS 证书。
    • 将此属性设置为 off 将绕过 SSL/TLS 验证,这在远程实例具有自签名或不可信证书时经常使用。

另外,默认情况下,Harbour 将镜像存储在本地文件系统上。在生产环境中,可以考虑 使用其他存储后端而不是本地文件系统,如 S3、Openstack Swif、Ceph 等。但需要更新common/templates/registry/config.yml 文件。

二、使用Harbor构建docker私有仓库

案例环境

主机操作系统主机名/IP地址主要软件及版本
Harbor(服务端)Centos 7192.168.163.100docker
docker-compose
harbor-offline-v1.1.2
client(客户端)Centos 7192.168.163.150docker

案例需求
通过Harbor创建Docker私有仓库
使用图形化管理Docker私有仓库镜像

实验准备
在服务端安装docker和docker-compose
客户端上安装docker
关闭防火墙
修改主机名

1、部署 Harbor 服务

Harbor 被部署为多个 Docker 容器,因此可以部署在任何支持 Docker 的 Linux 发行版上。
服务端主机需要安装Docker 和 Docker Compose。
安装Docker:Docker的第一次接触
安装Docker Compose如下

放入命令压缩包 cp -p docker-compose /usr/local/bin/ chmod +x /usr/local/bin/docker-compose

    (1)下载 Harbor 安装程序

    Harbor:192.168.163.100
    安装包下载

    //在线下载 wget http:// harbor.orientsoft.cn/harbor-1.2.2/harbor-offline-installer-v1.2.2.tgz//如果在先下载慢,可以使用上面的链接下载,在拖入 tar zxvf harbor-offline-installer-v1.2.2.tgz -C /usr/local/

    (2)配置 Harbor 参数文件

    Harbor:192.168.163.100

    vim /usr/local/harbor/harbor.cfg

    //第5行
    hostname = 192.168.163.100

      (3)启动 Harbor

      Harbor:192.168.163.100

      sh /usr/local/harbor/install.sh
      • 1

      (4)查看 Harbor 启动镜像和容器

      Harbor:192.168.163.100
      查看镜像

      docker images #----------------------------------输出内容--------------------------------- REPOSITORY TAG IMAGE ID CREATED SIZE vmware/harbor-log v1.2.2 36ef78ae27df 3 years ago 200MB vmware/harbor-jobservice v1.2.2 e2af366cba44 3 years ago 164MB vmware/harbor-ui v1.2.2 39efb472c253 3 years ago 178MB vmware/harbor-adminserver v1.2.2 c75963ec543f 3 years ago 142MB vmware/harbor-db v1.2.2 ee7b9fa37c5d 3 years ago 329MB vmware/nginx-photon 1.11.13 6cc5c831fc7f 3 years ago 144MB vmware/registry 2.6.2-photon 5d9100e4350e 3 years ago 173MB vmware/postgresql 9.6.4-photon c562762cbd12 3 years ago 225MB vmware/clair v2.0.1-photon f04966b4af6c 3 years ago 297MB vmware/harbor-notary-db mariadb-10.1.10 64ed814665c6 3 years ago 324MB vmware/notary-photon signer-0.5.0 b1eda7d10640 4 years ago 156MB vmware/notary-photon server-0.5.0 6e2646682e3c 4 years ago 157MB photon 1.0 e6e4e4a2ba1b 4 years ago 128MB
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16


      查看容器

      docker ps -a #-----------------------------输出内容--------------------------------------- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4e2940a0d0c7 vmware/nginx-photon:1.11.13 "nginx -g 'daemon of…" 13 minutes ago Up 13 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp nginx 2aa1257ff3c2 vmware/harbor-jobservice:v1.2.2 "/harbor/harbor_jobs…" 13 minutes ago Up 13 minutes harbor-jobservice 19c8fc2c6e83 vmware/harbor-ui:v1.2.2 "/harbor/harbor_ui" 13 minutes ago Up 13 minutes harbor-ui 8fe0eed3aa85 vmware/registry:2.6.2-photon "/entrypoint.sh serv…" 13 minutes ago Up 13 minutes 5000/tcp registry ac68e32fac3f vmware/harbor-db:v1.2.2 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 3306/tcp harbor-db d8a1d21defc4 vmware/harbor-adminserver:v1.2.2 "/harbor/harbor_admi…" 13 minutes ago Up 13 minutes harbor-adminserver 496ccc523f5d vmware/harbor-log:v1.2.2 "/bin/sh -c 'crond &…" 13 minutes ago Up 13 minutes 127.0.0.1:1514->514/tcp harbor-log
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10


      检查服务容器是否开启

      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
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

      (5)在UI界面创建项目

      真机
      上图检查一切正常,此时可以打开浏览器访问 http://192.168.163.100 的管理页面,默认的管理员用户名和密码是 admin/Harbor12345。

      登录后的页面

      添加项目并且填写项目名称


      (6)在本地测试仓库功能

      Harbor:192.168.163.100

      此时可使用 Docker 命令在本地通过 127.0.0.1 来登录和推送镜像。默认情况下,
      Register 服务器在端口 80 上侦听。

      登录

      docker login -u admin -p Harbor12345 http://127.0.0.1
      • 1



      下载镜像进行测试

      docker pull nginx
      • 1


      镜像打标签

      docker tag nginx 127.0.0.1/testproject/nginx:v1
      • 1

      上传镜像到Harbor

      docker push 127.0.0.1/testproject/nginx:v1
      • 1


      验证是否上传成功

      (7)在客户端上传镜像

      client:192.168.163.150
      上述操作都是在 Harbor 服务器本地操作。如果其他客户端上传镜像到 Harbor,就会报
      如下错误。出现这问题的原因 Docker Registry 交互默认使用的是 HTTPS,但是搭建私有镜
      像默认使用的是 HTTP 服务,所以与私有镜像交互时出现以下错误。

      docker login -u admin -p Harbor12345 http://192.168.163.100 #-------------------输出内容------------------------------------------ WARNING! Using --password via the CLI is insecure. Use --password-stdin. Error response from daemon: Get https://192.168.163.100/v2/: dial tcp 192.168.163.100:443: connect: connection refused
      • 1
      • 2
      • 3
      • 4

      解决方法

      vim /usr/lib/systemd/system/docker.service #--------------------修改内容----------------------------------- ExecStart=/usr/bin/dockerd -H fd:// --insecure-registry 192.168.163.100 --containerd=/run/containerd/containerd.sock #---------------------------------------------------------------//重启服务 systemctl daemon-reload systemctl restart docker//再次登录 docker login -u admin -p Harbor12345 http://192.168.163.100 #----------------------输出内容-------------------------------------------- WARNING! Using --password via the CLI is insecure. Use --password-stdin. WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded #-------------------------------------------------------------------- docker pull tomcatdocker imagesdocker tag tomcat 192.168.163.100/testproject/tomcat:v2docker push 192.168.163.100/testproject/tomcat:v2
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7


      2、维护管理Harbor

      可以使用 docker-compose 来管理 Harbor。一些有用的命令如下所示,必须在与
      docker-compose.yml 相同的目录中运行。

      修改 Harbor.cfg 配置文件所需选项的步骤
      要更改 Harbour 的配置文件时,请先停止现有的 Harbor 实例并更新 Harbor.cfg;然
      后运行 prepare 脚本来填充配置;最后重新创建并启动 Harbour 的实例。

      (1)停止现有的 Harbor 实例

      docker-compose down -v #---------------输出内容-------------------- Stopping nginx ... done Stopping harbor-jobservice ... done Stopping harbor-ui ... done Stopping registry ... done Stopping harbor-db ... done Stopping harbor-adminserver ... done Stopping harbor-log ... done Removing nginx ... done Removing harbor-jobservice ... done Removing harbor-ui ... done Removing registry ... done Removing harbor-db ... done Removing harbor-adminserver ... done Removing harbor-log ... done Removing network harbor_harbor
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17

      (2)修改配置文件 Harbor.cfg

      vim harbor.cfg
      • 1

      (3)运行 prepare 脚本来填充配置

      ./prepare #---------------------------输出内容---------------------------- 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.
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23

      (4)重启服务

      docker-compose up -d #-----------输出内容----------------------------------- Creating network "harbor_harbor" with the default driver Creating harbor-log ... done Creating registry ... done Creating harbor-adminserver ... done Creating harbor-db ... done Creating harbor-ui ... done Creating harbor-jobservice ... done Creating nginx ... done
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10

      如果启动时报错(此报错比较少见,毕竟防火墙已开始就关闭了)

      docker-compose up -dCreating network "harbor_harbor" with the default driver ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed: iptables --wait -t nat -I DOCKER -i br-25094fc09b3c -j RETURN: iptables: No chain/target/match by that name.(exit status 1))

      解决方法:关闭防火墙后,docker需要重启

      systemctl stop firewalld systemctl restart docker docker-compose up -d
      • 1
      • 2
      • 3

      3、创建 Harbor 用户




      创建项目开发人员





      在客户端上操作
      删除之前打标签的镜像

      docker rmi 192.168.163.100/testproject/tomcat:v2 #----------------------输出内容----------------------------- Untagged: 192.168.163.100/testproject/tomcat:v2 Untagged: 192.168.163.100/testproject/tomcat@sha256:8b42182fb220374cc8be73fbf4d9dba5946c9e6cb024c19f8470a27a6557e364
      • 1
      • 2
      • 3
      • 4


      测试创建的新用户能否使用

      //注销登录 docker logout 192.168.163.100 #------------------输出内容-------------------------------- Removing login credentials for 192.168.163.100 #--------------------------------------------------------//使用新创建的用户和密码登录私有仓库 docker login 192.168.163.100 #------------------输出内容------------------------- Username: zhangsan Password: //输入密码 WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded #--------------------------------------------------//从私有仓库下载镜像 docker pull 192.168.163.100/testproject/nginx:v1 #-----------------------输出内容----------------------------- v1: Pulling from testproject/nginx a076a628af6f: Pull complete 0732ab25fa22: Pull complete d7f36f6fe38f: Pull complete f72584a26f32: Pull complete 7125e4df9063: Pull complete Digest: sha256:0b159cd1ee1203dad901967ac55eee18c24da84ba3be384690304be93538bea8 Status: Downloaded newer image for 192.168.163.100/testproject/nginx:v1 192.168.163.100/testproject/nginx:v1//查看镜像 docker images #-------------------输出内容--------------------------------- REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 040bdb29ab37 2 months ago 649MB 192.168.163.100/testproject/nginx v1 f6d0b4767a6c 2 months ago 133MB

      3、移除 Harbor 服务容器以及全部数据

      移除 Harbor 服务容器同时保留镜像数据/数据库
      //在Harbor服务器上操作

      docker-compose down -v Stopping nginx ... done Stopping harbor-jobservice ... done Stopping harbor-ui ... done Stopping registry ... done Stopping harbor-db ... done Stopping harbor-adminserver ... done Stopping harbor-log ... done Removing nginx ... done Removing harbor-jobservice ... done Removing harbor-ui ... done Removing registry ... done Removing harbor-db ... done Removing harbor-adminserver ... done Removing harbor-log ... done Removing network harbor_harbor
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16

      如需重新部署,需要移除 Harbor 服务容器全部数据
      持久数据,如镜像,数据库等在宿主机的/data/目录下,日志在宿主机的
      /var/log/Harbor/目录下。

      rm -rf /data/database/ rm -rf /data/registry/
      • 1
      • 2

      总结

      以上是生活随笔为你收集整理的使用Harbor构建docker私有仓库的全部内容,希望文章能够帮你解决所遇到的问题。

      如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。