【Harbor】CentOS7.x上用harbor安装docker镜像仓库

一、实验背景

Docker中要使用镜像,一般会从本地、docker Hup公共仓库和其它第三方公共仓库中下载镜像,出于安全和外网(墙)资源下载速率的原因考虑企业级上不会轻易使用。

那么有没有一种办法可以存储自己的镜像又有安全认证的仓库呢?? Harbor不失为一个好的选择!

Harbor是VMware公司最近开源的企业级Docker Registry项目, 其目标是帮助用户迅速搭建一个企业级的Docker registry服务。

*提供了管理UI

*基于角色的访问控制(Role Based Access Control)

*AD/LDAP集成

*审计日志(Audit logging)

*原生支持中文

 

二、Harbor相关介绍

Harbor在架构上主要由五个组件构成:

*Proxy:Harbor的registry, UI, token等服务,通过一个前置的反向代理统一接收浏览器、Docker客户端的请求,并将请求转发给后端不同的服务。

*Registry: 负责储存Docker镜像,并处理docker push/pull 命令。由于我们要对用户进行访问控制,即不同用户对Docker image有不同的读写权限,Registry会指向一个token服务,强制用户的每次docker pull/push请求都要携带一个合法的token, Registry会通过公钥对token 进行解密验证。

*Core services: 这是Harbor的核心功能,主要提供以下服务:

*UI:提供图形化界面,帮助用户管理registry上的镜像(image), 并对用户进行授权。

*webhook:为了及时获取registry 上image状态变化的情况, 在Registry上配置webhook,把状态变化传递给UI模块。

*token 服务:负责根据用户权限给每个docker push/pull命令签发token. Docker 客户端向Registry服务发起的请求,如果不包含token,会被重定向到这里,获得token后再重新向Registry进行请求。

*Database:为core services提供数据库服务,负责储存用户权限、审计日志、Docker image分组信息等数据。

*Log collector:为了帮助监控Harbor运行,负责收集其他组件的log,供日后进行分析。

各个组件之间的关系如下图所示:

 

Harbor的每个组件都是以Docker容器的形式构建的,所以使用Docker Compose来对它进行部署。

用于部署Harbor的Docker Compose 模板位于 /Deployer/docker-compose.yml. 打开这个模板文件,会发现Harbor由5个容器组成:

*proxy: 由Nginx 服务器构成的反向代理。

*registry:由Docker官方的开源registry 镜像构成的容器实例。

*ui: 即架构中的core services, 构成此容器的代码是Harbor项目的主体。

*mysql: 由官方MySql镜像构成的数据库容器。

*log: 运行着rsyslogd的容器,通过log-driver的形式收集其他容器的日志。

这几个容器通过Docker link的形式连接在一起,这样,在容器之间可以通过容器名字互相访问。对终端用户而言,只需要暴露proxy (即Nginx)的服务端口。

Harbor的认证流程

 

A、首先,请求被代理容器监听拦截,并跳转到指定的认证服务器。

B、如果认证服务器配置了权限认证,则会返回401。通知dockerclient在特定的请求中需要带上一个合法的token。而认证的逻辑地址则指向架构图中的core services。

C、 当docker client接受到错误code。client就会发送认证请求(带有用户名和密码)到coreservices进行basic auth认证。

D、 当C的请求发送给ngnix以后,ngnix会根据配置的认证地址将带有用户名和密码的请求发送到core serivces。

E、 coreservices获取用户名和密码以后对用户信息进行认证(自己的数据库或者介入LDAP都可以)。成功以后,返回认证成功的信息。

 

 

三、实验环境

操作系统: CentOS7.5 Minimal

harbor服务端:192.168.1.105

harbor客户端:192.168.1.104

docker版本: 18.06

harbor安装版本:v1.1.1

 

 

四、安装docker

在harbor服务端和harbor客户端服务器上

 

# setenforce 0

# sed  -i  's/^SELINUX=.*/SELINUX=permissive/g'  /etc/selinux/config

 

# yum -y install  yum-utils device-mapper-persistent-data lvm2

# yum-config-manager  --add-repo    https://download.docker.com/linux/centos/docker-ce.repo

# yum list docker-ce  --showduplicates | sort  -r

 

#  yum -y install docker-ce-18.06.0.ce

# systemctl  start docker

# systemctl  status docker

# systemctl  enable  docker

# docker version

 

 

 

五、安装Harbor

在harbor服务端服务器

安装docker-compose

 

方式1:从github上下载docker-compose二进制文件安装

https://github.com/docker/compose/releases

# wget https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m`

#  mv  docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

# chmo +750  /usr/local/bin/docker-compose

# docker-compose -v

 

方式2:用pip安装

#  yum  -y insyall python-setuptools

# easy_install pip

# pip isntall docker-compose

 

下载harbor离线安装包

https://github.com/goharbor/harbor/releases/tag/v1.1.1

 

 

#  mv  harbor-offline-installer-v1.1.1.tgz  /opt

#  cd /opt 

# tar -zxf   harbor-offline-installer-v1.1.1.tgz  

#  ll  harbor  

 

 

生成自签名证书

创建一个任意目录,我们用脚本生成需要用导的证书文件

#  mkdir  /tmp/ssl

# cd   /tmp/ssl

# vim gencrt.sh

##################################################################

#!/bin/bash

openssl req -x509 \

  -nodes \

  -newkey rsa:4096 \

  -days 3650 \

  -sha256 \

  -subj "/C=CN/ST=Gunagdong/L=Shenzhen/O=Harbor/OU=Harbor/CN=www.harbor.com" \

  -keyout ca.key \

  -out ca.crt

openssl req \

  -nodes \

  -newkey rsa:4096 \

  -sha256 \

  -keyout harbor.key \

  -subj "/C=CN/ST=Gunagdong/L=Shenzhen/O=Test/OU=Test/CN=www.example.com" \

  -out harbor.csr

echo "

authorityKeyIdentifier=keyid,issuer

basicConstraints=CA:FALSE

keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment

subjectAltName=IP:192.168.1.105,DNS:www.example.com

" > extfile.cnf

openssl x509 -req \

  -days 3650 \

  -in harbor.csr \

  -CA ca.crt \

  -CAkey ca.key \

  -CAcreateserial \

  -extfile extfile.cnf \

  -out harbor.crt

########################################################################

 

# sh  gencrt.sh

 

 

创建harbor证书和私钥文件目录

# mkdir /etc/harbor/ssl

# cp  /tmp/ssl/harbor.crt    /etc/harbor/ssl

# cp  /tmp/ssl/harbor.key   /etc/harbor/ssl

#  ll   /etc/harbor/ssl

 

# openssl x509 -noout -text -in /tmp/ssl/ca.crt 

 

 

# openssl x509 -noout -text -in /etc/harbor/ssl/harbor.crt

 

将CA证书拷贝到harbor服务端和客户端

# cp  /tmp/ssl/ca.crt /etc/pki/ca-trust/source/anchors

# update-ca-trust 

# docker-compose -f /root/harbor/docker-compose.yml down

# systemctl restart docker

# docker-compose -f /root/harbor/docker-compose.yml up -d

 

#  scp  /tmp/ssl/ca.crt  root@192.168.1.104:/etc/pki/ca-trust/source/anchors

# ssh  root@192.168.1.104  "update-ca-trust "

# ssh  root@192.168.1.104  " systemctl restart docker "

 

编辑harbor配置文件

# vim  /opt/harbor/harbor.cfg

################################################

hostname = 192.168.1.105

ui_url_protocol = https

ssl_cert = /etc/harbor/ssl/harbor.crt

ssl_cert_key = /etc/harbor/ssl/harbor.key

#################################################

# cd  /opt/harbor

# ./install.sh

 

 

 

# docker ps -a 

# docker images

 

 

#  ss  -tan 

 

设置防火墙策略,方向相关端口

#  firewall-cmd --zone=public --add-port=80/tcp --permanent

#  firewall-cmd --zone=public --add-port=443/tcp --permanent

# firewall-cmd  --reload

 

六、创建仓库和用户

 

浏览器访问:http://192.168.1.105

浏览器访问:https://192.168.1.105

默认管理员账号密码:admin/Harbor12345

 

 

Harbor默认有一个名为 libary的公共仓库(项目)

 

 

 

 

七、Harbor客户端测试

 

方式一:通过获取harbor的CA证书

我们在已经将 ca.crt 拷贝到了客户端的 /etc/pki/ca-trust/source/anchors目录下

# cat /etc/pki/ca-trust/source/anchors/ca.crt

# update-ca-trust

# systemctl  restart docker

 

 

 

 

方式二:不通过CA证书,设置客户端docker信任仓库

# systemctl  status docker

 

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

#####################################################

ExecStart=/usr/bin/dockerd  --insecure-rehistry 192.168.1.105:443

######################################################

# systemctl  restart  nexus

 

登录创建的docker仓库

 

# docker login 192.168.1.105:443  -u test  -p  "Test@123"

# docker login 192.168.1.105:443  -u admin -p  "Harbor12345"

# cat /root/.docker/config.json

 

admin用户可以pull/push镜像,test用户,根据用户权限设置,决定是否有pull/push镜像权限。

 

 

测试镜像的pull/push

 

# docker pull busybox:latest

# docker tag  busybox:latest   192.168.1.105:443/test/busybox:1.0.1-RC1

# docker push 192.168.1.105:443/test/busybox:1.0.1-RC1

 

浏览器访问: https://192.168.1.105:443

admin/Harbor12345

 

 

 

 

 

关于登出docker仓库

# docker logout  192.168.1.105

# cat /root/.docker/config.json

 

 

 

八、参考

 

CentOS7.x上docker的安装方式

https://www.jianshu.com/p/d1a07a13a76f

 

用Harbor搭建docker 私有镜像仓库

https://www.jianshu.com/p/b2bfaad6e97a

 

Harbor User Guide

https://github.com/goharbor/harbor/blob/master/docs/user_guide.md

 

Harbor企业级私有镜像实战

https://www.dgstack.cn/archives/2185.html

 

Harbor1.5.0仓库使用教程

https://blog.51cto.com/11093860/2119494

 

VMware Harbor:基于 Docker Distribution 的企业级 Registry 服务

https://segmentfault.com/a/1190000007705296

 

Installation and Configuration Guide

https://github.com/goharbor/harbor/blob/v1.4.0/docs/installation_guide.md#configuring-harbor-listening-on-a-customized-port

 

Harbor 企业级 Docker Registry HTTPS配置

https://mritd.me/2016/07/03/Harbor-%E4%BC%81%E4%B8%9A%E7%BA%A7-Docker-Registry-HTTPS%E9%85%8D%E7%BD%AE

 

Protect the Docker daemon socket

https://docs.docker.com/engine/security/https

 

基于Harbor和CephFS搭建高可用Private Registry

https://tonybai.com/2017/06/09/setup-a-high-availability-private-registry-based-on-harbor-and-cephfs

 

互联网加密及OpenSSL介绍和简单使用

https://mritd.me/2016/07/02/%E4%BA%92%E8%81%94%E7%BD%91%E5%8A%A0%E5%AF%86%E5%8F%8AOpenSSL%E4%BB%8B%E7%BB%8D%E5%92%8C%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值