Docker-----私有仓库 Harbor 的搭建与操作使用方法

一、Harbor简介

虽然Docker官方提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署我们私有环境内的Registry也是非常必要的。

Harbor是由VMware公司开源的企业级的Docker Registry管理项目,相比docker官方拥有更丰富的权限权利和完善的架构设计,适用大规模docker集群部署提供仓库服务。

它主要提供 Dcoker Registry 管理界面UI,可基于角色访问控制,镜像复制, AD/LDAP 集成,日志审核等功能,完全的支持中文。
在这里插入图片描述

二、Harbor 的主要功能

基于角色的访问控制
用户与Docker镜像仓库通过“项目”进行组织管理,一个用户可以对多个镜像仓库在同一命名空间(project)里有不同的权限。

基于镜像的复制策略
镜像可以在多个Registry实例中复制(可以将仓库中的镜像同步到远程的Harbor,类似于MySQL主从同步功能),尤其适合于负载均衡,高可用,混合云和多云的场景。

图形化用户界面
用户可以通过浏览器来浏览,检索当前Docker镜像仓库,管理项目和命名空间。

支持 AD/LDAP
Harbor可以集成企业内部已有的AD/LDAP,用于鉴权认证管理。

镜像删除和垃圾回收
Harbor支持在Web删除镜像,回收无用的镜像,释放磁盘空间。image可以被删除并且回收image占用的空间。

审计管理
所有针对镜像仓库的操作都可以被记录追溯,用于审计管理。

RESTful API
RESTful API 提供给管理员对于Harbor更多的操控, 使得与其它管理软件集成变得更容易。

部署简单
提供在线和离线两种安装工具, 也可以安装到vSphere平台(OVA方式)虚拟设备。

Harbor 的所有组件都在 Docker 中部署,所以 Harbor 可使用 Docker Compose 快速部署。
注意: 由于 Harbor 是基于 Docker Registry V2 版本,所以 docker 版本必须 > = 1.10.0 docker-compose >= 1.6.0

三、Harbor 架构组件

架构组件图:
在这里插入图片描述

1、Proxy:反向代理工具

2、Registry:负责存储docker镜像,处理上传/下载命令。对用户进行访问控制,它指向一个token服务,强制用户的每次docker pull/push请求都要携带一个合法的token,registry会通过公钥对token进行解密验证。

3、Core service:Harbor的核心功能:

UI:图形界面
Webhook:及时获取registry上image状态变化情况,在registry上配置 webhook,把状态变化传递给UI模块。
Token服务:复杂根据用户权限给每个docker push/p/ull命令签发token。Docker客户端向registry服务发起的请求,如果不包含token,会被重定向到这里,获得token后再重新向registry进行请求。

4、Database:提供数据库服务,存储用户权限,审计日志,docker image分组信息等数据

5、Log collector:为了帮助监控harbor运行,复责收集其他组件的log,供日后进行分析

四、Harbor 部署

4.1、环境准备

两台虚拟机:

harbor (harbor服务端,用于搭建私有仓库)
192.168.100.3 docker-ce、docker-compose(必须安装)、Harbor

client(客户端,用于远程访问私有仓库) 192.168.100.4 docker-ce

4.2、安装compose 和 harbor

1.下载软件

cd /opt/dockersoft/

tar zxvf harbor-offline-installer-v1.2.2.tgz -C /usr/local

2. 配置Harbor参数文件并启动

vim /usr/local/harbor/harbor.cfg
[root@pc-3 dockersoft]# tar zxvf harbor-offline-installer-v1.2.2.tgz -C /usr/local
在这里插入图片描述

[root@pc-3 harbor]# systemctl start docker

Cd    /usr/local/harbor

改完参数要修改此命令 ,修改可选参数不需要运行此脚本

[root@pc-3 harbor]# sh install.sh

[Step 0]: checking installation environment ...

Note: docker version: 19.03.13

Note: docker-compose version: 1.21.1

[Step 1]: loading Harbor images ...
dd60b611baaa: Loading layer  133.2MB/133.2MB
........................................................
Creating nginx              ... done

✔ ----Harbor has been installed and started successfully.----

Now you should be able to visit the admin portal at http://192.168.100.3.
For more details, please visit https://github.com/vmware/harbor .

[root@pc-3 harbor]#

访问192.168.100.3

在这里插入图片描述

登录用户,用户名和密码在配置文件中都有定义,可查看

在这里插入图片描述
在这里插入图片描述

创建用户zhangsan

在这里插入图片描述
在这里插入图片描述

在项目 sha 中 添加成员zhangsan

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

测试张三登录

在这里插入图片描述

4.3 本地服务登陆docker login -u admin -p Harbor12345 http://127.0.0.1

[root@pc-3 harbor]# docker login -u admin -p Harbor12345 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@pc-3 harbor]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
127.0.0.1/sha/nginx         v1                  7e4d58f0e5f3        13 days ago         133MB

4.4 本地镜像上传

[root@docker01 harbor]# docker tag nginx:latest 127.0.0.1/sha/nginx:v1
[root@docker01 harbor]# docker push 127.0.0.1/sha/nginx

[root@pc-3 harbor]# docker push 127.0.0.1/sha/nginx
The push refers to repository [127.0.0.1/sha/nginx]
908cf8238301: Pushed
eabfa4cd2d12: Pushed
60c688e8765e: Pushed
f431d0917d41: Pushed
07cab4339852: Pushed
v1: digest: sha256:794275d96b4ab96eeb954728a7bf11156570e8372ecd5ed0cbc7280313a27d19 size: 1362

在这里插入图片描述

4.5 远程客户端登陆,尝试上传镜像

[root@pc-4 dockersoft]# docker login -u admin -p Harbor12345 http://192.168.100.3
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: Get https://192.168.100.3/v2/: dial tcp 192.168.100.3:443: connect: connection refused
[root@pc-4 dockersoft]#

会报443错误,这是因为harbor是基于http 80而不是httpds
解决方法:

在这里插入图片描述

14 ExecStart=/usr/bin/dockerd -H fd://  --insecure-registry 192.168.100.3 -containerd=/run/container    d/containerd.sock
[root@pc-4 dockersoft]# docker login -u admin -p Harbor12345 http://192.168.100.3
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@pc-4 dockersoft]#

4.6 远程上传镜像centos:7

[root@pc-4 dockersoft]# docker pull centos:7
7: Pulling from library/centos
75f829a71a1c: Pull complete
Digest: sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@pc-4 dockersoft]#  docker tag centos:7 192.168.100.3/sha/centos7:v1
[root@pc-4 dockersoft]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
192.168.100.3/sha/centos7   v1                  7e6257c9f8d8        6 weeks ago         203MB
centos                      7                   7e6257c9f8d8        6 weeks ago         203MB
[root@pc-4 dockersoft]# docker push 192.168.100.200/accp/centos7
The push refers to repository [192.168.100.200/accp/centos7]
An image does not exist locally with the tag: 192.168.100.200/accp/centos7
[root@pc-4 dockersoft]# docker push 192.168.100.3/sha/centos7
The push refers to repository [192.168.100.3/sha/centos7]
613be09ab3c0: Pushed
v1: digest: sha256:fe2347002c630d5d61bf2f28f21246ad1c21cc6fd343e70b4cf1e5102f8711a9 size: 529
[root@pc-4 dockersoft]#

在这里插入图片描述

4.7 下载镜像操作

在这里插入图片描述

[root@pc-4 dockersoft]# docker pull 192.168.100.3/sha/nginx:v1
v1: Pulling from sha/nginx
d121f8d1c412: Pull complete
ebd81fc8c071: Pull complete
655316c160af: Pull complete
d15953c0e0f8: Pull complete
2ee525c5c3cc: Pull complete
Digest: sha256:794275d96b4ab96eeb954728a7bf11156570e8372ecd5ed0cbc7280313a27d19
Status: Downloaded newer image for 192.168.100.3/sha/nginx:v1
192.168.100.3/sha/nginx:v1

4.8 .报错总结

//报错:
docker-compose up -d
Creating network "harbor_harbor" with the default driver
ERROR:Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed:jptables
--wait-t nat-I DOCKER -i br-25094fc09b3c -j RETURN:jptables: No chain/target/match by that
name.
(exit status 1))
//解决:关闭防火墙后,docker需要重启
systemctl restart docker 
docker-compose up -daemon

8.Harbor 容器的关闭与启动
docker-compose up -d	"所有容器启动"
docker-compose down -v	"所有Harbor容器停止,但保留数据镜像"

[root@docker01 ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
127.0.0.1/accp/nginx        v1                  7e4d58f0e5f3        13 days ago         133MB
nginx                       latest              7e4d58f0e5f3        13 days ago         133MB
127.0.0.1/accp/centos7      v1                  7e6257c9f8d8        6 weeks ago         203MB
vmware/harbor-log           v1.2.2              36ef78ae27df        2 years ago         200MB
vmware/harbor-jobservice    v1.2.2              e2af366cba44        2 years ago         164MB
vmware/harbor-ui            v1.2.2              39efb472c253        2 years ago         178MB
vmware/harbor-adminserver   v1.2.2              c75963ec543f        2 years ago         142MB
vmware/harbor-db            v1.2.2              ee7b9fa37c5d        2 years ago         329MB
vmware/nginx-photon         1.11.13             6cc5c831fc7f        2 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        3 years ago         156MB
vmware/notary-photon        server-0.5.0        6e2646682e3c        3 years ago         157MB
photon                      1.0                 e6e4e4a2ba1b        4 years ago         128MB

猜你喜欢

转载自blog.csdn.net/BIGmustang/article/details/108769446