Harbor是什么?

Harbor是基于角色策略访问控制的开源镜像管理工具,通过扫描镜像确保镜像安全无漏洞,帮助你安全管理云原生平台。

Harbor的优势

Harbor和Registry都是Docker的镜像仓库,但是Harbor作为更多企业的选择,是因为相比较于Regisrty来说,它具有很多的优势。

  • 提供分片传输机制,优化网络传输,Docker镜像是是分片的,每次传输全量文件不经济。
  • 提供WEB界面,优化用户体验。
  • 支持水平扩展集群,分担服务器压力。
  • 支持权限控制,为不同身份的人员分配不同的权限,使操作更安全
  • 提供基于角色的访问控制机制,并通过项目来对镜像进行组织和访问权限的控制。

    Harbor安装

    1.去Harbor下载页面,下载最新的安装包与asc公钥。

    ```shell

    create directory for harbor

    mkdir -p harbor/data mkdir -p harbor/logs

download harbor installer and correspond asc file

cd harbor wget -b https://github.com/goharbor/harbor/releases/download/v2.4.1/harbor-offline-installer-v2.4.1.tgz.asc wget -b https://github.com/goharbor/harbor/releases/download/v2.4.1/harbor-offline-installer-v2.4.1.tgz

  1. <a name="yXAgL"></a>
  2. ## 2.获取 *.asc公钥文件
  3. ```shell
  4. gpg --keyserver hkps://keyserver.ubuntu.com --recv-keys 644FF454C0B4115C
  1. gpg: 下载密钥‘C0B4115C’,从 hkps 服务器 keyserver.ubuntu.com
  2. gpg: /root/.gnupg/trustdb.gpg:建立了信任度数据库
  3. gpg: 密钥 C0B4115C:公钥“Harbor-sign (The key for signing Harbor build) <jiangd@vmware.com>”已导入
  4. gpg: 合计被处理的数量:1
  5. gpg: 已导入:1 (RSA: 1)

3.验证下载的.asc文件

  1. gpg -v --keyserver hkps://keyserver.ubuntu.com --verify harbor-offline-installer-v2.4.1.tgz.asc
  1. gpg: 假定被签名的数据是‘harbor-offline-installer-v2.4.1.tgz
  2. gpg: 20211216 星期四 122454 CST 创建的签名,使用 RSA,钥匙号 C0B4115C
  3. gpg: 使用 PGP 信任模型
  4. gpg: 完好的签名,来自于“Harbor-sign (The key for signing Harbor build) <jiangd@vmware.com>”
  5. gpg: 警告:这把密钥未经受信任的签名认证!
  6. gpg: 没有证据表明这个签名属于它所声称的持有者。
  7. 主钥指纹: 7722 D168 DAEC 4578 06C9 6FF9 644F F454 C0B4 115C
  8. gpg: 二进制 签名,散列算法 SHA512

4.解压安装包

  1. tar xzvf harbor-offline-installer-v2.4.1.tgz

5.配置harbor.yml

  1. hostname: 192.168.101.181
  2. http:
  3. port: 8083
  4. harbor_admin_password: your-login-pass
  5. database:
  6. password: your-db-pass
  7. max_idle_conns: 50
  8. max_open_conns: 500
  9. data_volume: /www/pre/harbor/data
  10. trivy:
  11. ignore_unfixed: false
  12. skip_update: false
  13. insecure: false
  14. jobservice:
  15. max_job_workers: 10
  16. notification:
  17. webhook_job_max_retry: 10
  18. chart:
  19. absolute_url: disabled
  20. log:
  21. level: info
  22. local:
  23. rotate_count: 50
  24. rotate_size: 200M
  25. location: /www/pre/harbor/logs
  26. _version: 2.4.0

注:一定要确保端口号未被占用,否则执行下一步操作时会报错

6.配置Docker

6.1 开启RemoteApi

  1. vim /usr/lib/systemd/system/docker.service

注释docker.service13行,并下方加上ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock这行,暴露2375端口,如果服务器防火墙是开启状态,需要放行2375端口。

  1. [Unit]
  2. Description=Docker Application Container Engine
  3. Documentation=https://docs.docker.com
  4. After=network-online.target firewalld.service containerd.service
  5. Wants=network-online.target
  6. Requires=docker.socket containerd.service
  7. [Service]
  8. Type=notify
  9. # the default is not to use systemd for cgroups because the delegate issues still
  10. # exists and systemd currently does not support the cgroup feature set required
  11. # for containers run by docker
  12. #ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  13. ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
  14. ExecReload=/bin/kill -s HUP $MAINPID
  15. TimeoutSec=0
  16. RestartSec=2
  17. Restart=always
  18. # Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
  19. # Both the old, and new location are accepted by systemd 229 and up, so using the old location
  20. # to make them work for either version of systemd.
  21. StartLimitBurst=3
  22. # Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
  23. # Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
  24. # this option work for either version of systemd.
  25. StartLimitInterval=60s
  26. # Having non-zero Limit*s causes performance problems due to accounting overhead
  27. # in the kernel. We recommend using cgroups to do container-local accounting.
  28. LimitNOFILE=infinity
  29. LimitNPROC=infinity
  30. LimitCORE=infinity
  31. # Comment TasksMax if your systemd version does not support it.
  32. # Only systemd 226 and above support this option.
  33. TasksMax=infinity
  34. # set delegate yes so that systemd does not reset the cgroups of docker containers
  35. Delegate=yes
  36. # kill only the docker process, not all processes in the cgroup
  37. KillMode=process
  38. OOMScoreAdjust=-500
  39. [Install]
  40. WantedBy=multi-user.target

6.2 配置http访问

如果不想使用https安全访问模式,需要设置insecure-registries

  1. vim /etc/docker/daemon.json
  1. {
  2. "registry-mirrors":[
  3. "https://hhfivgbp.mirror.aliyuncs.com"
  4. ],
  5. "insecure-registries":[
  6. "192.168.101.181:8083"
  7. ],
  8. "live-restore":true
  9. }
  1. - `registry-mirrors`镜像加速地址在这里找:[https://cr.console.aliyun.com/cn-beijing/instances/mirrors](https://cr.console.aliyun.com/cn-beijing/instances/mirrors)
  2. - `live-restore`重载docker守护进程而不重启容器。

6.3 重启docker

  1. systemctl daemon-reload
  2. systemctl restart docker

7.运行安装脚本

  1. sh install.sh
  1. [Step 0]: checking if docker is installed ...
  2. Note: docker version: 20.10.12
  3. [Step 1]: checking docker-compose is installed ...
  4. Note: docker-compose version: 1.29.2
  5. [Step 2]: loading Harbor images ...
  6. Loaded image: goharbor/registry-photon:v2.4.1
  7. Loaded image: goharbor/notary-signer-photon:v2.4.1
  8. Loaded image: goharbor/harbor-core:v2.4.1
  9. Loaded image: goharbor/redis-photon:v2.4.1
  10. Loaded image: goharbor/harbor-jobservice:v2.4.1
  11. Loaded image: goharbor/harbor-registryctl:v2.4.1
  12. Loaded image: goharbor/nginx-photon:v2.4.1
  13. Loaded image: goharbor/notary-server-photon:v2.4.1
  14. Loaded image: goharbor/harbor-log:v2.4.1
  15. Loaded image: goharbor/harbor-db:v2.4.1
  16. Loaded image: goharbor/harbor-exporter:v2.4.1
  17. Loaded image: goharbor/trivy-adapter-photon:v2.4.1
  18. Loaded image: goharbor/chartmuseum-photon:v2.4.1
  19. Loaded image: goharbor/prepare:v2.4.1
  20. Loaded image: goharbor/harbor-portal:v2.4.1
  21. [Step 3]: preparing environment ...
  22. [Step 4]: preparing harbor configs ...
  23. prepare base dir is set to /mnt/1068cb5c-d183-48ef-b545-5717408aba10/www/pre/harbor/harbor
  24. WARNING:root:WARNING: HTTP protocol is insecure. Harbor will deprecate http protocol in the future. Please make sure to upgrade to https
  25. Clearing the configuration file: /config/registry/root.crt
  26. Clearing the configuration file: /config/registry/passwd
  27. Clearing the configuration file: /config/registry/config.yml
  28. Clearing the configuration file: /config/nginx/nginx.conf
  29. Clearing the configuration file: /config/jobservice/env
  30. Clearing the configuration file: /config/jobservice/config.yml
  31. Clearing the configuration file: /config/portal/nginx.conf
  32. Clearing the configuration file: /config/registryctl/env
  33. Clearing the configuration file: /config/registryctl/config.yml
  34. Clearing the configuration file: /config/log/logrotate.conf
  35. Clearing the configuration file: /config/log/rsyslog_docker.conf
  36. Clearing the configuration file: /config/core/env
  37. Clearing the configuration file: /config/core/app.conf
  38. Clearing the configuration file: /config/db/env
  39. Generated configuration file: /config/portal/nginx.conf
  40. Generated configuration file: /config/log/logrotate.conf
  41. Generated configuration file: /config/log/rsyslog_docker.conf
  42. Generated configuration file: /config/nginx/nginx.conf
  43. Generated configuration file: /config/core/env
  44. Generated configuration file: /config/core/app.conf
  45. Generated configuration file: /config/registry/config.yml
  46. Generated configuration file: /config/registryctl/env
  47. Generated configuration file: /config/registryctl/config.yml
  48. Generated configuration file: /config/db/env
  49. Generated configuration file: /config/jobservice/env
  50. Generated configuration file: /config/jobservice/config.yml
  51. loaded secret from file: /data/secret/keys/secretkey
  52. Generated configuration file: /compose_location/docker-compose.yml
  53. Clean up the input dir
  54. Note: stopping existing Harbor instance ...
  55. Stopping harbor-jobservice ... done
  56. Stopping harbor-core ... done
  57. Stopping registry ... done
  58. Stopping redis ... done
  59. Stopping registryctl ... done
  60. Stopping harbor-portal ... done
  61. Stopping harbor-db ... done
  62. Stopping harbor-log ... done
  63. Removing harbor-jobservice ... done
  64. Removing nginx ... done
  65. Removing harbor-core ... done
  66. Removing registry ... done
  67. Removing redis ... done
  68. Removing registryctl ... done
  69. Removing harbor-portal ... done
  70. Removing harbor-db ... done
  71. Removing harbor-log ... done
  72. Removing network harbor_harbor
  73. [Step 5]: starting Harbor ...
  74. Creating network "harbor_harbor" with the default driver
  75. Creating harbor-log ... done
  76. Creating harbor-portal ... done
  77. Creating registry ... done
  78. Creating harbor-db ... done
  79. Creating redis ... done
  80. Creating registryctl ... done
  81. Creating harbor-core ... done
  82. Creating nginx ... done
  83. Creating harbor-jobservice ... done
  84. ----Harbor has been installed and started successfully.----

8.Harbor Web

打开浏览器访问 http://192.168.101.181:8083/,账号默认是admin,密码是harbor.yml中配置的密码。
image.png

发布镜像

安装Dockerfile使用镜像

  1. docker pull bitnami/java:1.8.312

Dockerfile

  1. FROM bitnami/java:1.8.312
  2. MAINTAINER menglt@yeah.net
  3. ENV TZ=Asia/Shanghai
  4. ENV JAVA_OPTS="-Xms512m -Xmx1024m -Djava.security.egd=file:/dev/./urandom"
  5. RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
  6. RUN mkdir -p /your-project
  7. WORKDIR /project-module
  8. EXPOSE 4000
  9. ADD ./target/project-module-api.jar ./
  10. CMD sleep 60;java $JAVA_OPTS -jar project-module-api.jar

Maven插件

spring-boot-maven-plugin

  1. <!--spring boot 默认插件-->
  2. <plugin>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-maven-plugin</artifactId>
  5. <version>${spring-boot.version}</version>
  6. <executions>
  7. <execution>
  8. <goals>
  9. <goal>repackage</goal>
  10. </goals>
  11. </execution>
  12. </executions>
  13. </plugin>

docker-maven-plugin

  1. <properties>
  2. <docker.registry>192.168.101.181:8083</docker.registry>
  3. <docker.host>http://192.168.101.181:2375</docker.host>
  4. <!-- namespace对应harbor中的项目 -->
  5. <docker.namespace>your-namespace</docker.namespace>
  6. <docker.username>admin</docker.username>
  7. <docker.password>your-password</docker.password>
  8. </properties>
  9. <!-- maven docker 打包插件 -->
  10. <plugin>
  11. <groupId>io.fabric8</groupId>
  12. <artifactId>docker-maven-plugin</artifactId>
  13. <version>${docker.plugin.version}</version>
  14. <configuration>
  15. <dockerHost>${docker.host}</dockerHost>
  16. <registry>${docker.registry}</registry>
  17. <authConfig>
  18. <push>
  19. <username>${docker.username}</username>
  20. <password>${docker.password}</password>
  21. </push>
  22. </authConfig>
  23. <images>
  24. <image>
  25. <name>${docker.registry}/${docker.namespace}/${project.name}:${project.version}</name>
  26. <build>
  27. <dockerFile>${project.basedir}/Dockerfile</dockerFile>
  28. </build>
  29. </image>
  30. </images>
  31. </configuration>
  32. </plugin>

Maven命令

  1. mvn install docker:build
  2. mvn docker:push

附录

[

](http://192.168.101.181:8083/)