目录
前言
Harbor是VMware公司开源的企业级Docker Registry项目
1.Harbor的优势
1.基于角色控制:有管理员与普通用户,可赋权普通用户,比如只能上传和下载,可根据项目来进行操作和管理
2.基于镜像的复制策略:也与权限相关,比如有只一-些用户与组才能对此项目进行相对应的操作
3.支持LDAP/AD: 域控制,比如南京去下载北京harbor
私有仓库的镜像,两端打上,局域网的地址,连接在一一块,数据信息的传输通过一条隧道,会通过两层加密,第一层为隧道
加密,第二层为数据加密,安全可靠
4.图像删除和垃圾收集:即回收站机制
5.图形UI:具有统计功能,比如访问量与镜像^下载热度
6.审计:日志,这里意义不大,主要还是借助于ELK
7.RESTful API:定义Web 语言规范的格式,方便调用Harbor 的接口,也便于二次开发
1.2Harbor的构成
组件 | 作用 |
---|---|
Proxy | 通过一个前置的反向代理统一接收浏览器、Docker客户端的请求,并将请求转发给后端不同的服务 |
Registry | (核心组件)负责储存Docker镜像,并处理docker push/pull命令 |
Core services | Harbor的核心功能, 包括UI、webhook、 token服务 |
Database | 为core services提供数据库服务 |
Log collector | 负责收集其他组件的log,供日后进行分析 |
这些组件都是以容器封装的方式,使用docker-compose编排管理完成的
registry是代码仓库
2.3关于 Harbor.cfg 配置文件中有两类参数
2.3.1、所需参数
如果用户更新它们并运行 install.sh脚本重新安装 Harbor,参数将生效。具体参数如下:
**hostname:**用于访问用户界面和 register 服务。它应该是目标机器的 IP 地址或完全限 定的域名(FQDN)
例如 192.168.35.40 或 hub.klj.cn。不要使用 localhost 或 127.0.0.1 为主机名。
ui_url_protocol:(http 或 https,默认为 http)用于访问 UI 和令牌/通知服务的协议。如果公证处于启用状态,则此参数必须为 https。(身份验证时会向Mysql数据库进行比对,然后授予令牌)
**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.3.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 分钟。
project_creation_restriction:用于控制哪些用户有权创建项目的标志。默认情况下, 每个人都可以创建一个项目。如果将其值设置为“adminonly”,那么只有 admin 可以创建项目。
verify_remote_cert:打开或关闭,默认打开。此标志决定了当Harbor与远程 register 实例通信时是否验证 SSL/TLS 证书。将此属性设置为 off 将绕过 SSL/TLS 验证,这在远程实例具有自签名或不可信证书时经常使用。
2.部署Harbor服务
2.1下载 Harbor 安装程序
wget http:// harbor.orientsoft.cn/harbor-1.2.2/harbor-offline-installer-v1.2.2.tgz
或者上传压缩包harbor-offline-installer-v1.2.2.tgz
tar zxvf harbor-offline-installer-v1.2.2.tgz -C /usr/local/
2.2配置 Harbor参数文件
vim /usr/local/harbor/harbor.cfg
5 hostname = 192.168.226.128 ##修改成本机地址
2.3启动 Harbor
启动之前先确认Docker-Compose 版本安装是否成功
[root@harbor ~]# docker-compose -v
docker-compose version 1.21.1, build 5a3f1a3
[root@harbor ~]# sh /usr/local/harbor/install.sh ##启动启动 Harbor;docker-compose中已定义相关脚本
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating harbor-db ... done
Creating registry ... done
Creating harbor-adminserver ... done
Creating harbor-ui ... done
Creating harbor-jobservice ... done
Creating nginx ... done
✔ ----Harbor has been installed and started successfully.----
2.4查看 Harbor 启动镜像
[root@harbor harbor]# cd /usr/local/harbor/
[root@harbor 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,:::443->443/tcp, 0.0.0.0:4443->4443/tcp,:::4443->4443/tcp,
0.0.0.0:80->80/tcp,:::80->80/tcp
registry /entrypoint.sh serve /etc/ ... Up 5000/tcp
[root@harbor harbor]# cat /usr/local/harbor/docker-compose.yml ##查看文件内容;7个组件的详细信息
version: '2'
services:
log:
image: vmware/harbor-log:v1.2.2
container_name: harbor-log
restart: always
volumes:
- /var/log/harbor/:/var/log/docker/:z
ports:
- 127.0.0.1:1514:514
networks:
- harbor
registry:
image: vmware/registry:2.6.2-photon
container_name: registry
restart: always
volumes:
- /data/registry:/storage:z
- ./common/config/registry/:/etc/registry/:z
networks:
- harbor
environment:
- GODEBUG=netdns=cgo
command:
["serve", "/etc/registry/config.yml"]
depends_on:
- log
logging:
driver: "syslog"
options:
syslog-address: "tcp://127.0.0.1:1514"
tag: "registry"
mysql:
image: vmware/harbor-db:v1.2.2
container_name: harbor-db
restart: always
volumes:
- /data/database:/var/lib/mysql:z
networks:
- harbor
env_file:
- ./common/config/db/env
depends_on:
- log
logging:
driver: "syslog"
options:
syslog-address: "tcp://127.0.0.1:1514"
tag: "mysql"
adminserver:
image: vmware/harbor-adminserver:v1.2.2
container_name: harbor-adminserver
env_file:
- ./common/config/adminserver/env
restart: always
volumes:
- /data/config/:/etc/adminserver/config/:z
- /data/secretkey:/etc/adminserver/key:z
- /data/:/data/:z
networks:
- harbor
depends_on:
- log
logging:
driver: "syslog"
options:
syslog-address: "tcp://127.0.0.1:1514"
tag: "adminserver"
ui:
image: vmware/harbor-ui:v1.2.2
container_name: harbor-ui
env_file:
- ./common/config/ui/env
restart: always
volumes:
- ./common/config/ui/app.conf:/etc/ui/app.conf:z
- ./common/config/ui/private_key.pem:/etc/ui/private_key.pem:z
- /data/secretkey:/etc/ui/key:z
- /data/ca_download/:/etc/ui/ca/:z
- /data/psc/:/etc/ui/token/:z
networks:
- harbor
depends_on:
- log
- adminserver
- registry
logging:
driver: "syslog"
options:
syslog-address: "tcp://127.0.0.1:1514"
tag: "ui"
jobservice:
image: vmware/harbor-jobservice:v1.2.2
container_name: harbor-jobservice
env_file:
- ./common/config/jobservice/env
restart: always
volumes:
- /data/job_logs:/var/log/jobs:z
- ./common/config/jobservice/app.conf:/etc/jobservice/app.conf:z
- /data/secretkey:/etc/jobservice/key:z
networks:
- harbor
depends_on:
- ui
- adminserver
logging:
driver: "syslog"
options:
syslog-address: "tcp://127.0.0.1:1514"
tag: "jobservice"
proxy:
image: vmware/nginx-photon:1.11.13
container_name: nginx
restart: always
volumes:
- ./common/config/nginx:/etc/nginx:z
networks:
- harbor
ports:
- 80:80
- 443:443
- 4443:4443
depends_on:
- mysql
- registry
- ui
- log
logging:
driver: "syslog"
options:
syslog-address: "tcp://127.0.0.1:1514"
tag: "proxy"
networks:
harbor:
external: false
2.5访问harbor的UI界面
打开浏览器访问 http://192.168.226.128 的管理页面,默认 的管理员用户名和密码是 admin/Harbor12345。
添加项目并且填写项目名称mytest
2.6命令行登录harbor
1.Harbor 服务器基于本地操作
使用 Docker 命令在本地通过 127.0.0.1 来登录和推送镜像。默认情况下,Register 服务器在端口 80 上侦听。
[root@harbor harbor]# docker login -u admin -p Harbor12345 http://127.0.0.1 ##//登录
....
Login Succeeded
[root@harbor harbor]# docker pull cirros ##下载镜像进行测试
Using default tag: latest
latest: Pulling from library/cirros
d0b405be7a32: Pull complete
bd054094a037: Pull complete
c6a00de1ec8a: Pull complete
Digest: sha256:1e695eb2772a2b511ccab70091962d1efb9501fdca804eb1d52d21c0933e7f47
Status: Downloaded newer image for cirros:latest
docker.io/library/cirros:latest
[root@harbor harbor]# docker tag cirros:latest 127.0.0.1/mytest/cirros:v1 ##镜像打标签
[root@harbor harbor]# docker push 127.0.0.1/mytest/cirros:v1
The push refers to repository [127.0.0.1/mytest-test/cirros]
984ad441ec3d: Preparing
f0a496d92efa: Preparing
e52d19c3bee2: Preparing
denied: requested access to the resource is denied
[root@harbor harbor]# docker tag cirros:latest 127.0.0.1/mytest/cirros ##镜像打标签
[root@harbor harbor]# docker push 127.0.0.1/mytest/cirros ##上传镜像到Harbor
Using default tag: latest
The push refers to repository [127.0.0.1/mytest/cirros]
984ad441ec3d: Pushed
f0a496d92efa: Pushed
e52d19c3bee2: Pushed
latest: digest: sha256:483f15ac97d03dc3d4dcf79cf71ded2e099cf76c340f3fdd0b3670a40a198a22 size: 943
2.Harbor 服务器非本地操作
以上操作都是在 Harbor 服务器本地操作。如果其他客户端上传镜像到 Harbor,就会报
如下错误。出现这问题的原因 Docker Registry 交互默认使用的是 HTTPS,但是搭建私有镜
像默认使用的是 HTTP 服务,所以与私有镜像交互时出现以下错误。
[root@client ~]# docker login -u admin -p Harbor12345 http://192.168.226.128 ##非本地登录会报错
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: Get https://192.168.226.128/v2/: EOF
[root@client ~]# vim /usr/lib/systemd/system/docker.service ##指向私有仓库地址
13 ExecStart=/usr/bin/dockerd -H fd:// --insecure-registry 192.168.35.40 --containerd=/run/containerd/containerd.sock
[root@client ~]# systemctl daemon-reload
[root@client ~]# systemctl restart docker
[root@harbor harbor]# docker login -u admin -p Harbor12345 http://192.168.226.128
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-store
Login Succeeded
[root@client ~]# docker pull cirros
Using default tag: latest
latest: Pulling from library/cirros
Digest: sha256:1e695eb2772a2b511ccab70091962d1efb9501fdca804eb1d52d21c0933e7f47
Status: Image is up to date for cirros:latest
docker.io/library/cirros:latest
[root@client ~]# docker tag cirros 192.168.226.128/myproject-kgc/cirros:v2
[root@harbor harbor]# docker tag cirros:latest 192.168.226.128/mytest/cirros:1
[root@harbor harbor]#
[root@harbor harbor]# docker push 192.168.226.128/mytest/cirros:1
The push refers to repository [192.168.226.128/mytest/cirros]
984ad441ec3d: Layer already exists
f0a496d92efa: Layer already exists
e52d19c3bee2: Layer already exists
1: digest: sha256:483f15ac97d03dc3d4dcf79cf71ded2e099cf76c340f3fdd0b3670a40a198a22 size: 943
2.7harbor用户管理
1.创建 Harbor 用户
系统管理----用户管理----+用户----输入用户名、邮箱、全名、密码
2.项目添加成员
3.验证是否可以使用zhangsan用户开发人员的身份下载
//在客户端上操作
[root@harbor harbor]# docker rmi 192.168.226.128mytest/cirros ##删除之前下载的镜像
Untagged: 192.168.226.128/mytest/cirros:latest
[root@client ~]# docker logout 192.168.226.128 ##注销登录
Removing login credentials for 192.168.226.128
[root@harbor harbor]# docker login -u zhangsan -p Harbor12345 http://192.168.226.128 ##登录
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-store
Login Succeeded
[root@harbor harbor]# docker pull 192.168.226.128/mytest/cirros:latest
latest: Pulling from mytest/cirros
Digest: sha256:483f15ac97d03dc3d4dcf79cf71ded2e099cf76c340f3fdd0b3670a40a198a22
Status: Downloaded newer image for 192.168.226.128/mytest/cirros:latest
192.168.226.128/mytest/cirros:latest
2.8维护管理Harbor
可以使用 docker-compose 来管理 Harbor。一些有用的命令如下所示,必须在与docker-compose.yml 相同的目录中运行。
修改 Harbor.cfg 配置文件
要更改 Harbour 的配置文件时,请先停止现有的 Harbour 实例并更新 Harbor.cfg;然后运行 prepare 脚本来填充配置;最后重新创建并启动 Harbour 的实例。
docker-compose down -v
Stopping nginx ... done
Stopping harbor-jobservice ... done
Stopping harbor-ui ... done
Stopping harbor-adminserver ... done
Stopping harbor-db ... done
Stopping registry ... done
Stopping harbor-log ... done
Removing nginx ... done
Removing harbor-jobservice ... done
Removing harbor-ui ... done
Removing harbor-adminserver ... done
Removing harbor-db ... done
Removing registry ... done
Removing harbor-log ... done
Removing network harbor_harbor
[root@harbor harbor]# cd /usr/local/harbor/
[root@harbor harbor]# ./prepare
Clearing the configuration file: ./common/config/adminserver/env
Clearing the configuration file: ./common/config/ui/env
Cleari:ng 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.
[root@harbor harbor]# 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 nginx ... done
Creating harbor-jobservice ... done