目录
前言
一、本地私有仓库
1、下载registry镜像
2、在daemon.json文件中添加私有镜像仓库地址
3、运行registry容器
参数说明
4、Docker容器的重启策略
5、为镜像打标签
6、上传镜像到私有仓库
7、列出私有仓库的所有镜像
8、 列出私有仓库的 centos 镜像有哪些 tag
9、删除原有 centos 镜像,测试私有仓库下载
二、Harbor仓库
1. Harbor 简介
2. Harbor 的特性
3. Harbor 的构成
4. Harbor 部署
4.1 服务器设置
4.2 部署 Docker-Compose 服务
4.3 部署 Harbor
4.4 harbor.cfg 配置文件中的两类参数
4.5 启动 harbor
4.6 查看 harbor 启动镜像
4.7 创建一个新项目
4.8 在其他客户端上传镜像
5. 维护管理 Harbor
5.1通过 Harbor Web 创建项目
5.2 创建 Harbor 用户
5.3 修改 harbor.cfg 配置文件
5.4 移除 Harbor 服务容器同时保留镜像数据/数据库,并进行迁移
5.5 如需重新部署,需要移除 Harbor 服务器全部数据
对于一些有能力的企业来说,有时希望能够创建一个私有化的Docker仓库,这样一来可以将自己定制的系统发布到自己的仓库里,在保证安全的前提下使用;二来可以节省网络带宽,可以使得Docker仓库的下载在公司内网内完成。接下来,我们开始介绍以下Docker私有化仓库的搭建。
[root@docker ~]#docker pull registry
......
[root@docker ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 2 weeks ago 141MB
registry latest b8604a3fe854 2 weeks ago 26.2MB
centos 7 eeb6ee3f44bd 2 months ago 204MB
[root@docker ~]#cat /etc/docker/daemon.json
{
"insecure-registries": ["192.168.223.53:5000"],
"registry-mirrors": ["https://4iv7219l.mirror.aliyuncs.com"]
}
[root@docker~]#systemctl restart docker.service
[root@docker ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 2 weeks ago 141MB
registry latest b8604a3fe854 2 weeks ago 26.2MB
centos 7 eeb6ee3f44bd 2 months ago 204MB
[root@docker ~]#docker run -itd -v /data/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:latest
465355484f317cf31c4df4d2d90edf078bc6063cca7bd175b80c3abdb83a03ca
[root@docker ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
465355484f31 registry:latest "/entrypoint.sh /etc…" 9 seconds ago Up 8 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp registry
[root@docker ~]#docker exec -it 465355484f31 /bin/sh
/ # ls
bin etc media proc sbin tmp
dev home mnt root srv usr
entrypoint.sh lib opt run sys var
[root@docker ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 2 weeks ago 141MB
registry latest b8604a3fe854 2 weeks ago 26.2MB
centos 7 eeb6ee3f44bd 2 months ago 204MB
[root@docker ~]#docker tag centos:7 192.168.223.53:5000/centos:test1
[root@docker ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 2 weeks ago 141MB
registry latest b8604a3fe854 2 weeks ago 26.2MB
centos 7 eeb6ee3f44bd 2 months ago 204MB
192.168.223.53:5000/centos test1 eeb6ee3f44bd 2 months ago 204MB
[root@docker ~]#docker push 192.168.223.53:5000/centos:test1
The push refers to repository [192.168.223.53:5000/centos]
174f56854903: Pushed
test1: digest: sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f size: 529
[root@docker ~]#curl -XGET http://192.168.223.53:5000/v2/_catalog
{"repositories":["centos"]}
root@docker ~]#curl -XGET http://192.168.223.53:5000/v2/centos/tags/list
{"name":"centos","tags":["test1"]}
[root@docker ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 2 weeks ago 141MB
registry latest b8604a3fe854 2 weeks ago 26.2MB
192.168.223.53:5000/centos test1 eeb6ee3f44bd 2 months ago 204MB
centos 7 eeb6ee3f44bd 2 months ago 204MB
[root@docker ~]#docker rmi -f 192.168.223.53:5000/centos:test1 centos:7 &> /dev/null
[root@docker ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 2 weeks ago 141MB
registry latest b8604a3fe854 2 weeks ago 26.2MB
[root@docker ~]#docker pull 192.168.223.53:5000/centos:test1 &> /dev/null
[root@docker ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 2 weeks ago 141MB
registry latest b8604a3fe854 2 weeks ago 26.2MB
192.168.223.53:5000/centos test1 eeb6ee3f44bd 2 months ago 204MB
Harbor 在架构上主要有 Proxy、Registry、Core services、Database(Harbor-db)、Log collector(Harbor-log)、Job services
六个组件。
组件 | 说明 |
Proxy | Harbor 的 Registry、UI、Token 服务等组件,都处在 nginx 反向代理后边。该代理将来自浏览器、docker clients 的请求转发到后端不同的服务上 |
Registry | 负责储存 Docker 镜像,并处理 Docker push/pull 命令。由于要对用户进行访问控制,即不同用户对 Docker 镜像有不同的读写权限,Registry 会指向一个 Token 服务,强制用户的每次 Docker pull/push 请求都要携带一个合法的 Token, Registry 会通过公钥对 Token 进行解密验证 |
Core services | Harbor的核心功能,主要提供以下3个服务: ① UI(harbor-ui): 提供图形化界面,帮助用户管理 Registry 上的镜像(image), 并对用户进行授权。 ② WebHook:为了及时获取 Registry 上 image 状态变化的情况,在 Registry 上配置 Webhook,把状态变化传递给 UI 模块。 ③ Token 服务:负责根据用户权限给每个 Docker push/pull 命令签发 Token。Docker 客户端向 Registry 服务发起的请求, 如果不包含 Token,会被重定向到 Token 服务,获得 Token 后再重新向 Registry 进行请求 |
Database(harbor-db) | 为 core services 提供数据库服务,负责储存用户权限、审计日志、Docker 镜像分组信息等数据 |
Job services | 主要用于镜像复制,本地镜像可以被同步到远程 Harbor 实例上 |
Log collector(harbor-log) | 负责收集其他组件的日志到一个地方 |
服务器 | IP地址 | 主要软件 |
---|---|---|
Harbor 服务器 | 192.168.223.53 | docker-ce、docker-compose、harbor-offline-v1.2.2 |
client 服务器 | 192.168.223.37 | docker-ce |
下载或者上传 Docker-Compose
#wget http://101.34.22.188/docker-compose/docker-compose -P /usr/local/bin
[root@docker ~]#curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
[root@docker ~]#chmod +x /usr/local/bin/docker-compose
[root@docker ~]#docker-compose -v
docker-compose version 1.21.1, build 5a3f1a3
下载或上传 Harbor 安装程序
#wget http://101.34.22.188/harbor/harbor-offline-installer-v1.2.2.tgz -P /opt
#wget http://harbor.orientsoft.cn/harbor-1.2.2/harbor-offline-installer-v1.2.2.tgz
[root@docker ~]#cd /opt
[root@docker /opt]#rz -E
#传入 harbor 安装包 harbor-offline-installer-v1.2.2.tgz
[root@docker /opt]#tar zxvf harbor-offline-installer-v1.2.2.tgz -C /usr/local/
修改 harbor 安装的配置文件
[root@docker /opt]#vim /usr/local/harbor/harbor.cfg
#5行,修改设置为 Harbor 服务器的 IP 地址或者域名
hostname = 192.168.223.53
#59行,指定管理员的初始密码,默认的用户名/密码是 admin/Harbor12345
harbor_admin_password = Harbor12345
关于 harbor.cfg 配置文件中有两类参数:所需参数和可选参数,这些参数需要在配置文件 harbor.cfg 中设置。如果用户更新它们并运行 install.sh 脚本重新安装 harbor,参数将生效。
所需参数如下:
这些参数对于更新是可选的,即用户可以将其保留为默认值,并在启动 Harbor 后在 Web UI 上进行更新。如果进入 Harbor.cfg,只会在第一次启动 Harbor 时生效,随后对这些参数的更新,Harbor.cfg 将被忽略。
如果选择通过 UI 设置这些参数,请确保在启动 Harbor 后立即执行此操作。具体来说,必须在注册或在 Harbor 中创建任何新用户之前设置所需的 auth_mode。当系统中有用户时(除了默认的 admin 用户),auth_mode 不能被修改。
可选参数如下:
另外,默认情况下,Harbor 将镜像存储在本地文件系统上。在生产环境中,可以考虑使用其他存储后端而不是本地文件系统,如 S3、Openstack Swif、Ceph
等对象存储。但需要更新 common/templates/registry/config.yml
文件。
[root@docker ~]#cd /usr/local/harbor/
[root@docker /usr/local/harbor]#ls
common docker-compose.yml harbor.v1.2.2.tar.gz NOTICE
docker-compose.clair.yml harbor_1_1_0_template install.sh prepare
docker-compose.notary.yml harbor.cfg LICENSE upgrade
[root@docker /usr/local/harbor]#./install.sh
......
✔ ----Harbor has been installed and started successfully.----
Now you should be able to visit the admin portal at http://192.168.10.20.
For more details, please visit https://github.com/vmware/harbor .
[root@docker /usr/local/harbor]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 2 weeks ago 141MB
registry latest b8604a3fe854 2 weeks ago 26.2MB
192.168.223.53:5000/centos test1 eeb6ee3f44bd 2 months ago 204MB
vmware/harbor-log v1.2.2 36ef78ae27df 4 years ago 200MB
vmware/harbor-jobservice v1.2.2 e2af366cba44 4 years ago 164MB
vmware/harbor-ui v1.2.2 39efb472c253 4 years ago 178MB
vmware/harbor-adminserver v1.2.2 c75963ec543f 4 years ago 142MB
vmware/harbor-db v1.2.2 ee7b9fa37c5d 4 years ago 329MB
vmware/nginx-photon 1.11.13 6cc5c831fc7f 4 years ago 144MB
vmware/registry 2.6.2-photon 5d9100e4350e 4 years ago 173MB
vmware/postgresql 9.6.4-photon c562762cbd12 4 years ago 225MB
vmware/clair v2.0.1-photon f04966b4af6c 4 years ago 297MB
vmware/harbor-notary-db mariadb-10.1.10 64ed814665c6 4 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 5 years ago 128MB
[root@docker /usr/local/harbor]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
00d3e2e74c23 vmware/nginx-photon:1.11.13 "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp, 0.0.0.0:4443->4443/tcp, :::4443->4443/tcp nginx
7778c13a8990 vmware/harbor-jobservice:v1.2.2 "/harbor/harbor_jobs…" About a minute ago Up About a minute harbor-jobservice
479134517ebb vmware/harbor-ui:v1.2.2 "/harbor/harbor_ui" About a minute ago Up About a minute harbor-ui
0c99fc134e9f vmware/registry:2.6.2-photon "/entrypoint.sh serv…" About a minute ago Up About a minute 5000/tcp registry
9b3442b48fce vmware/harbor-db:v1.2.2 "docker-entrypoint.s…" About a minute ago Up About a minute 3306/tcp harbor-db
cdd81206d44f vmware/harbor-adminserver:v1.2.2 "/harbor/harbor_admi…" About a minute ago Up About a minute harbor-adminserver
1a2a543cf2b2 vmware/harbor-log:v1.2.2 "/bin/sh -c 'crond &…" About a minute ago Up About a minute 127.0.0.1:1514->514/tcp harbor-log
启动 harbor 前需删除 registry 镜像/容器,否则无法生成新的 registry 镜像。
[root@docker /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,:::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
浏览器访问:http://192.168.223.53/
登录 harbor web ui 界面,默认的管理员用户名和密码是 admin/Harbor12345
输入用户名和密码登录界面后可以创建一个新项目。点击+项目按钮
填写项目名称为test_project,点击确定按钮来创建新项目,首页即可查看
此时可使用 Docker 命令在本地通过 127.0.0.1 来登录和推送镜像。默认情况下,Registry 服务器在端口 80 上侦听
登录 Harbor
[root@docker ~]#docker login -uadmin -pHarbor12345 http://127.0.0.1
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@docker ~]#docker pull nginx:latest
......
[root@docker ~]#docker images | grep nginx
nginx latest f652ca386ed1 19 hours ago 141MB
vmware/nginx-photon 1.11.13 6cc5c831fc7f 4 years ago 144MB
为镜像打标签
[root@docker ~]#docker tag nginx:latest 127.0.0.1/test_project/nginx:v1
[root@docker ~]#docker images | grep nginx
127.0.0.1/test_project/nginx v1 f652ca386ed1 19 hours ago 141MB
nginx latest f652ca386ed1 19 hours ago 141MB
vmware/nginx-photon 1.11.13 6cc5c831fc7f 4 years ago 144MB
上传镜像到Harbor
[root@docker ~]#docker push 127.0.0.1/test_project/nginx:v1
The push refers to repository [127.0.0.1/test_project/nginx]
2bed47a66c07: Pushed
82caad489ad7: Pushed
d3e1dca44e82: Pushed
c9fcd9c6ced8: Pushed
0664b7821b60: Pushed
9321ff862abb: Pushed
v1: digest: sha256:4424e31f2c366108433ecca7890ad527b243361577180dfd9a5bb36e828abf47 size: 1570
web 查看 project 状态
以上操作都是在 Harbor 服务器本地操作。如果其他客户端登录到 Harbor,就会报如下错误。出现这问题的原因为 Docker Registry 交互默认使用的是 HTTPS,但是搭建私有镜像默认使用的是 HTTP 服务,所以与私有镜像交互时出现以下错误:
[root@client ~]#docker login -uadmin -pHarbor12345 http://192.168.223.53
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: Get "https://192.168.10.20/v2/": dial tcp 192.168.10.20:443: connect: connection refused
在 Docker 客户端配置操作
解决办法是在docker server启动的时候,增加启动参数,默认使用HTTP来访问
[root@client ~]#vim /usr/lib/systemd/system/docker.service
#13行修改
ExecStart=/usr/bin/dockerd -H fd:// --insecure-registry 192.168.223.53 --containerd=/run/containerd/containerd.sock
#或修改为 ExecStart=/usr/bin/dockerd --insecure-registry 192.168.223.53
[root@client ~]#cat /etc/docker/daemon.json
{
"insecure-registries": ["192.168.223.53"],
"registry-mirrors": ["https://e2jmea9e.mirror.aliyuncs.com"]
}
重启 Docker 再次登录
[root@client ~]#systemctl daemon-reload
[root@client ~]#systemctl restart docker
再次登录Harbor
[root@client ~]#docker login -uadmin -pHarbor12345 http://192.168.223.53
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 images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@client ~]# docker pull 192.168.223.53/test_project/nginx:v1
v1: Pulling from test_project/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:ee89b00528ff4f02f2405e4ee221743ebc3f8e8dd0bfd5c4c20a2fa2aaa7ede3
Status: Downloaded newer image for 192.168.223.53/test_project/nginx:v1
192.168.223.53/test_project/nginx:v1
[root@client ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.223.53/test_project/nginx v1 605c77e624dd 6 months ago 141MB
上传镜像进行测试
[root@client ~]#docker pull cirros
......
[root@client ~]#docker tag cirros:latest 192.168.223.53/test_project/cirros:v1
[root@client ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.223.53/test_project/nginx v1 f652ca386ed1 20 hours ago 141MB
192.168.223.53/test_project/cirros v1 f9cae1daf5f6 8 months ago 12.6MB
cirros
[root@client ~]#docker push 192.168.223.53/test_project/cirros:v1
......
在 Harbor 仓库中,任何镜像在被 push 到 regsitry 之前都必须有一个自己所属的项目。
单击 +项目,填写项目名称,项目级别若设置为 私有,则不勾选。如果设置为 公共 仓库,则所有人对此项目下的镜像拥有读权限,命令行中不需要执行 Docker login 即可下载镜像,镜像操作与 Docker Hub 一致。
创建用户并分配权限
添加项目成员
单击项目 --> test_project --> 成员 --> + 成员,填写上述创建的用户 test 并分配角色为 "开发人员"
在客户端上使用普通账户操作镜像
删除全部本地镜像
[root@client ~]#docker rmi -f `docker images -q`
......
[root@client ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
退出当前用户,然后使用上述创建的账户 test 登录
[root@client ~]#docker logout 192.168.223.53
Removing login credentials for 192.168.223.53
[root@client ~]#docker login -utest -pTest12345 http://192.168.223.53
......
Login Succeeded
下载 harbor 仓库镜像
root@client ~]#docker pull 192.168.223.53/test_project/cirros:v1
......
[root@client ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.223.53/test_project/cirros v1 f9cae1daf5f6 8 months ago 12.6MB
上传镜像至 harbor 仓库
root@client ~]#docker tag 192.168.223.53/test_project/cirros:v1 192.168.223.53/test_project/cirros:v2
[root@client ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.223.53/test_project/cirros v1 f9cae1daf5f6 8 months ago 12.6MB
192.168.223.53/test_project/cirros v2 f9cae1daf5f6 8 months ago 12.6MB
[root@client ~]#docker push 192.168.223.53/test_project/cirros:v2
......
要更改 Harbor 的配置文件中的可选参数时,需先停止现有的 Harbor 实例并更新 Harbor.cfg;然后运行 prepare 脚本来填充配置;最后重新创建并启动 Harbour 的实例。
使用 docker-compose 管理 Harbor 时,必须在与 docker-compose.yml 相同的目录中运行。
[root@docker ~]#cd /usr/local/harbor/
[root@docker /usr/local/harbor]#docker-compose down -v
Stopping harbor-jobservice ... done
Stopping nginx ... done
Stopping harbor-ui ... done
......
Removing network harbor_harbor
[root@docker /usr/local/harbor]#vim harbor.cfg
[root@docker /usr/local/harbor]#./prepare
Clearing the configuration file: ./common/config/adminserver/env
......
The configuration files are ready, please use docker-compose to start the service.
[root@docker /usr/local/harbor]#docker-compose up -d
如果有以下报错,需要开启防火墙 firewalld 服务解决
Creating 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-b53c314f45e8 -j RETURN: iptables: No chain/target/match by that name.
(exit status 1))
> systemctl restart firewalld.service
> docker-compose up -d
在 Harbor 服务器上操作
移除 Harbor 服务容器
[root@docker ~]#cd /usr/local/harbor/
[root@docker /usr/local/harbor]#docker-compose down -v
......
把项目中的镜像数据进行打包,持久数据,如镜像,数据库等在宿主机的 /data/ 目录下,日志在宿主机的 /var/log/Harbor/ 目录下
[root@docker /usr/local/harbor]#ls /data/registry/docker/registry/v2/repositories/test_project/
cirros nginx
[root@docker /usr/local/harbor]#cd /data/registry/docker/registry/v2/repositories/test_project/
[root@docker /data/registry/docker/registry/v2/repositories/test_project]#tar zcvf test-registry.tar.gz ./*
......
[root@docker ~]#cd /usr/local/harbor
[root@docker /usr/local/harbor]#docker-compose down -v
Stopping nginx ... done
......
Removing network harbor_harbor
[root@docker /usr/local/harbor]#rm -rf /data/database
[root@docker /usr/local/harbor]#rm -rf /data/registry