您的位置:首页 > 运维架构 > Docker

搭建Harbor企业级docker仓库

2017-10-11 12:47 961 查看

搭建Harbor企业级docker仓库

一、Harbor简介

1.Harbor介绍

Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全、标识和管理等,扩展了开源Docker Distribution。作为一个企业级私有Registry服务器,Harbor提供了更好的性能和安全。提升用户使用Registry构建和运行环境传输镜像的效率。Harbor支持安装在多个Registry节点的镜像资源复制,镜像全部保存在私有Registry中, 确保数据和知识产权在公司内部网络中管控。另外,Harbor也提供了高级的安全特性,诸如用户管理,访问控制和活动审计等。

2.Harbor特性

  • 基于角色的访问控制 :用户与Docker镜像仓库通过“项目”进行组织管理,一个用户可以对多个镜像仓库在同一命名空间(project)里有不同的权限。
  • 镜像复制 : 镜像可以在多个Registry实例中复制(同步)。尤其适合于负载均衡,高可用,混合云和多云的场景。
  • 图形化用户界面 : 用户可以通过浏览器来浏览,检索当前Docker镜像仓库,管理项目和命名空间。
  • AD/LDAP 支持 : Harbor可以集成企业内部已有的AD/LDAP,用于鉴权认证管理。
  • 审计管理 : 所有针对镜像仓库的操作都可以被记录追溯,用于审计管理。
  • 国际化 : 已拥有英文、中文、德文、日文和俄文的本地化版本。更多的语言将会添加进来。
  • RESTful API : RESTful API 提供给管理员对于Harbor更多的操控, 使得与其它管理软件集成变得更容易。
  • 部署简单 : 提供在线和离线两种安装工具, 也可以安装到vSphere平台(OVA方式)虚拟设备。

3.Harbor组件

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

  • 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 客户端向Regiøstry服务发起的请求,如果不包含token,会被重定向到这里,获得token后再重新向Registry进行请求。

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

  • Job Services:提供镜像远程复制功能,可以把本地镜像同步到其他Harbor实例中。

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

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

4.Harbor实现

Harbor的每个组件都是以Docker容器的形式构建的,官方也是使用Docker Compose来对它进行部署。用于部署Harbor的Docker Compose模板位于 harbor/docker-compose.yml,打开这个模板文件,发现Harbor是由7个容器组成的;

# 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, 0.0.0.0:4443->4443/tcp, 0.0.0.0:80->80/tcp
registry             /entrypoint.sh serve /etc/ ...   Up      5000/tcp

nginx:nginx负责流量转发和安全验证,对外提供的流量都是从nginx中转,所以开放https的443端口,它将流量分发到后端的ui和正在docker镜像存储的docker registry。
harbor-jobservice:harbor-jobservice 是harbor的job管理模块,job在harbor里面主要是为了镜像仓库之前同步使用的;
harbor-ui:harbor-ui是web管理页面,主要是前端的页面和后端CURD的接口;
registry:registry就是docker原生的仓库,负责保存镜像。
harbor-adminserver:harbor-adminserver是harbor系统管理接口,可以修改系统配置以及获取系统信息。
这几个容器通过Docker link的形式连接在一起,在容器之间通过容器名字互相访问。对终端用户而言,只需要暴露proxy (即Nginx)的服务端口。
harbor-db:harbor-db是harbor的数据库,这里保存了系统的job以及项目、人员权限管理。由于本harbor的认证也是通过数据,在生产环节大多对接到企业的ldap中;
harbor-log:harbor-log是harbor的日志服务,统一管理harbor的日志。通过inspect可以看出容器统一将日志输出的syslog。

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

二、安装和配置Harbor

1.环境说明

环境名称 版本
系统版本 CentOS Linux release 7.2.1511 (Core)
docker-ce 17.03.1-ce
docker-compose 1.16.1
Harbor v1.2.0
安装方式 在线安装
安装位置 /usr/local/harbor

2.首先安装docker

安装Harbor需要先安装docker和docker-compose

# yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
配置repository:
# yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

安装最新版本docker-ce   Docker version 17.03.1-ce
# yum install docker-ce

启动docker:

# systemctl start docker

如果需要卸载docker-ce,操作如下:
yum remove docker-ce
卸载后images,containers,volumes,configuration files 是不能自动删除的,为了删除all images,containers,and volumes,请执行如下命令:
rm -rf /var/lib/docker

3.安装docker-compose

必须先安装docker

方法一:二进制安装

a.下载二进制文件
# curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

如果需要安装其他版本的话,请修改上面命令中的版本号。

b.赋予二进制文件可执行权限
# chmod +x /usr/local/bin/docker-compose
c.根据自己的情况决定是否安装命令补全功能
# yum install bash-completion
# curl -L https://raw.githubusercontent.com/docker/compose/1.16.1/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

重新登陆后就生效了

# docker-compose
build    config   down     exec     images   logs     port     pull     restart  run      start    top      up
bundle   create   events   help     kill     pause    ps       push     rm       scale    stop     unpause  version
d.测试是否安装成功
# docker-compose --version
docker-compose version 1.16.1, build 6d1ac21

方法二:pip

# yum install python-pip
# pip install  docker-compose

测试:

# docker-compose --version

卸载docker-compose
对应上面两种安装方法:
二进制:

# rm  /usr/local/bin/docker-compose

pip:

# pip uninstall  docker-compose

4.安装Harbor

安装方式分为在线安装和离线安装两种方式,我这里选择的是在线安装。

wget -P /usr/loca/src/     https://github.com/vmware/harbor/releases/download/v1.2.0/harbor-online-installer-v1.2.0.tgz

也可以下载下面的文件进行离线安装:

https://github.com/vmware/harbor/releases/download/v1.2.0/harbor-offline-installer-v1.2.0.tgz

不过这个文件,我没有下载成功。因为在亚马逊S3上总是连接超时
安装:

# cd /usr/local/src/
# tar zxf harbor-online-installer-v1.2.0.tgz  -C /usr/local/
# cd /usr/local/harbor/

5.修改配置文件

配置文件为:/usr/local/harbor/harbor.cfg
配置的内容为:

# vim /usr/local/harbor/harbor.cfg
hostname = rgs.unixfbi.com
#邮箱配置
email_server = smtp.qq.com
email_server_port = 25
email_username = unixfbi@unixfbi.com
email_password =12345678
email_from = UnixFBI <unixfbi@unixfbi.com>
email_ssl = false
#禁止用户注册
self_registration = off
#设置只有管理员可以创建项目
project_creation_restriction = adminonly

6.执行安装脚本

# /usr/local/harbor/install.sh

执行过程

[Step 0]: checking installation environment ...

Note: docker version: 17.03.1

Note: docker-compose version: 1.16.1

[Step 1]: preparing environment ...
Clearing the configuration file: ./common/config/adminserver/env
Clearing the configuration file: ./common/config/ui/env
Clearing 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.

[Step 2]: checking existing instance of Harbor ...

[Step 3]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Pulling log (vmware/harbor-log:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-log
93b3dcee11d6: Pull complete
07d028a1dbdd: Pull complete
208723cd598a: Pull complete
b876b05989fc: Pull complete
12f0e0ef448a: Pull complete
Digest: sha256:608e10b7aaac07e10a4cd639d8848aef96daa7dd5c7ebaaf6a7ecd47f903e1f8
Status: Downloaded newer image for vmware/harbor-log:v1.2.0
Pulling adminserver (vmware/harbor-adminserver:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-adminserver
93b3dcee11d6: Already exists
6ab21236e58b: Pull complete
f70b0efff900: Pull complete
c5d206b5e184: Pull complete
Digest: sha256:aba66ec90fc12fe0814cecc9f647f5d17b41395199821cc7af69db9c0fbe446c
Status: Downloaded newer image for vmware/harbor-adminserver:v1.2.0
Pulling registry (vmware/registry:2.6.2-photon)...
2.6.2-photon: Pulling from vmware/registry
93b3dcee11d6: Already exists
007fe7635995: Pull complete
20d63c99b572: Pull complete
2026103b2811: Pull complete
ab7a5be79b33: Pull complete
11757dc5c642: Pull complete
69ab3e15edde: Pull complete
Digest: sha256:3cf06cdff00f48e15a5254b6d0370bcb4423dcd158e7f970dc8246893125b032
Status: Downloaded newer image for vmware/registry:2.6.2-photon
Pulling ui (vmware/harbor-ui:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-ui
93b3dcee11d6: Already exists
6ab21236e58b: Already exists
7753f4b55df6: Pull complete
a647b33bdf74: Pull complete
eb30db926101: Pull complete
204d77847826: Pull complete
4910a0b56c59: Pull complete
e880a4b0031f: Pull complete
Digest: sha256:b198d8f2f59515d286bdcf06c7f99c370eb4475e2547495c3e1db8761940646b
Status: Downloaded newer image for vmware/harbor-ui:v1.2.0
Pulling mysql (vmware/harbor-db:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-db
df559435c037: Pull complete
a6310a57af5d: Pull complete
d13d90890144: Pull complete
b694d8967a6c: Pull complete
a34f6cef56a6: Pull complete
3519eec83af5: Pull complete
34bae610e56c: Pull complete
86a867bebd89: Pull complete
3db2d0ac366e: Pull complete
c0d307ee295f: Pull complete
c5b1b404c5ee: Pull complete
14f4a4328366: Pull complete
c7fa77b46ec4: Pull complete
0cbe2b5669b8: Pull complete
Digest: sha256:c10b3555beb6d1c851ae49a4e90ef4296a1ad42bcd1a58ae97e316b034515b6e
Status: Downloaded newer image for vmware/harbor-db:v1.2.0
Pulling jobservice (vmware/harbor-jobservice:v1.2.0)...
v1.2.0: Pulling from vmware/harbor-jobservice
93b3dcee11d6: Already exists
6ab21236e58b: Already exists
64bd0172d071: Pull complete
b4f5382f226f: Pull complete
Digest: sha256:0692648176c1c87379025b0519036b9f3f1a0eceb2646f17dd40eb143c898d5c
Status: Downloaded newer image for vmware/harbor-jobservice:v1.2.0
Pulling proxy (vmware/nginx-photon:1.11.13)...
1.11.13: Pulling from vmware/nginx-photon
93b3dcee11d6: Already exists
d2a110a9296e: Pull complete
Digest: sha256:9ec5644c667e87bf051e581ce74b2933d3ed469b27862534ba60ccf17b4ff57a
Status: Downloaded newer image for vmware/nginx-photon:1.11.13
Creating harbor-log ...
Creating harbor-log ... done
Creating harbor-db ...
Creating harbor-adminserver ...
Creating registry ...
Creating harbor-db
Creating registry
Creating registry ... done
Creating harbor-ui ...
Creating harbor-ui ... done
Creating nginx ...
Creating harbor-jobservice ...
Creating harbor-jobservice
Creating nginx ... done

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

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

7.Harbor启动和停止

Harbor 的日常运维管理是通过docker-compose来完成的,Harbor本身有多个服务进程,都放在docker容器之中运行,我们可以通过docker ps命令查看。

# docker ps
CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS              PORTS                                                              NAMES
e6da3450bebe        vmware/harbor-jobservice:v1.2.0    "/harbor/harbor_jo..."   4 days ago          Up 4 days                                                                              harbor-jobservice
6134227d7ed4        vmware/nginx-photon:1.11.13        "nginx -g 'daemon ..."   4 days ago          Up 4 days           0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp   nginx
ef5fe0b856a7        vmware/harbor-ui:v1.2.0            "/harbor/harbor_ui"      4 days ago          Up 4 days                                                                              harbor-ui
a752861c7e92        vmware/registry:2.6.2-photon       "/entrypoint.sh se..."   4 days ago          Up 4 days           5000/tcp                                                           registry
78ea91838582        vmware/harbor-db:v1.2.0            "docker-entrypoint..."   4 days ago          Up 4 days           3306/tcp                                                           harbor-db
024e226ff135        vmware/harbor-adminserver:v1.2.0   "/harbor/harbor_ad..."   4 days ago          Up 4 days                                                                              harbor-adminserver
3fab80444a1d        vmware/harbor-log:v1.2.0           "/bin/sh -c 'crond..."   4 days ago          Up 4 days           127.0.0.1:1514->514/tcp                                            harbor-log

或者docker-compose 来查看

# cd /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, 0.0.0.0:4443->4443/tcp, 0.0.0.0:80->80/tcp
registry             /entrypoint.sh serve /etc/ ...   Up      5000/tcp           

Harbor的启动和停止

启动Harbor
# docker-compose start
停止Harbor
# docker-comose stop
重启Harbor
# docker-compose restart

8.访问测试

在浏览器输入rgs.unixfbi.com,因为我配置的域名为rgs.unixfbi.com。请大家根据自己的配置情况输入访问的域名;
默认账号密码: admin / Harbor12345 登录后修改密码

四、测试上传和下载镜像

1.修改各docker client配置

# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --insecure-registry rgs.unixfbi.com

增加 --insecure-registry rgs.unixfbi.com 即可。
重启docker:

# systemctl daemon-reload
# systemctl  restart docker

或者

创建/etc/docker/daemon.json文件,在文件中指定仓库地址
# cat > /etc/docker/daemon.json << EOF
{ "insecure-registries":["rgs.unixfbi.com"] }
EOF
然后重启docker就可以。

# systemctl  restart docker

这样设置完成后,就不会提示我们使用https的错误了。

2.创建Dockerfile

# vim Dockerfile
FROM centos:centos7.1.1503
ENV TZ "Asia/Shanghai"

3.创建镜像

# docker build -t rgs.unixfbi.com/library/centos7.1:0.1 .

4.把镜像push到Harbor

# docker login rgs.unixfbi.com
# docker push rgs.unixfbi.com/library/centos7.1:0.1

如果不是自己创建的镜像,记得先执行 docker tags 给镜像做tag
例如:

# docker pull busybox
# docker tag busybox:latest rgs.unixfbi.com/library/busybox:latest
# docker push rgs.unixfbi.com/library/busybox:latest

5.登录web页面查看镜像

6.pull镜像

从别的机器上拉一下镜像

# docker rmi -f $(docker images -q -a )
# docker pull rgs.unixfbi.com/library/centos7.1:0.1
0.1: Pulling from library/centos7.1
07618ba636d9: Pull complete
Digest: sha256:7f398052ae0e93ddf96ba476185c7f436b15abd27acd848a24b88ede4bb3c322
Status: Downloaded newer image for rgs.unixfbi.com/library/centos7.1:0.1

# docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
rgs.unixfbi.com/library/centos7.1   0.1                 6c849613a995        5 hours ago         212MB

五、Harbor配置TLS证书

因为我们上面对Harbor的配置都是使用的http协议访问,但是我们工作中一般都是配置https访问。所以我给大家演示一下怎么配置Harbor可以使用https访问,以及配置TLS证书都需要做哪些工作。

1.修改Harbor配置文件

因为Harbor默认使用http协议访问,所以我们这里在配置文件中,开启https配置;
配置harbor.cfg

hostname = rgs.unixfbi.com
ui_url_protocol = https
ssl_cert = /etc/certs/ca.crt
ssl_cert_key = /etc/certs/ca.key

2.创建自签名证书key文件

# mkdir /etc/certs
# openssl genrsa -out /etc/certs/ca.key 2048
Generating RSA private key, 2048 bit long modulus
....+++
..................................................+++
e is 65537 (0x10001)

3.创建自签名证书crt文件

注意命令中

/CN=rgs.unixfbi.com
字段中
rgs.unixfbi.com
修改为你自己的仓库域名。

# openssl req -x509 -new -nodes -key /etc/certs/ca.key -subj "/CN=rgs.unixfbi.com" -days 5000 -out /etc/certs/ca.crt

4.开始安装Harbor

# ./install.sh

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

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

显示是https了。

5.客户端配置

客户端需要创建证书文件存放的位置,并且把服务端创建的证书拷贝到该目录下,然后重启客户端docker。我们这里创建目录为:

/etc/docker/certs.d/rgs.unixfbi.com

# mkdir -p /etc/docker/certs.d/rgs.unixfbi.com

把服务端crt证书文件拷贝到客户端,例如我这的客户端为:192.168.199.183

# scp /etc/certs/ca.crt root@192.168.199.183:/etc/docker/certs.d/rgs.unixfbi.com/

重启客户端docker

# systemctl restart docker

6.测试是否支持https访问

# docker login rgs.unixfbi.com
Username (admin):
Password:
Login Succeeded

六、遇到问题

遇到的问题就是Harbor我配置的是http访问,但是docker客户端默认都是https访问Harbor,所以就会产生错误。下面看看我是怎么解决这个问题的吧。下面我们来访问以下Harbor

# docker pull rgs.unixfbi.com/library/centos7.1:0.1
Error response from daemon: Get https://rgs.unixfbi.com/v1/_ping: dial tcp 192.168.199.233:443: getsockopt: connection refused

问题原因
因为docker默认访问仓库时都是使用的https协议,而我们的仓库配置的是http
解决方法
方法一
在docker启动的配置仓库地址添加如下内容:

--insecure-registry rgs.unixfbi.com

# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --insecure-registry rgs.unixfbi.com

然后

# systemctl daemon-reload
# systemctl  restart docker

方法二
创建/etc/docker/daemon.json文件,在文件中指定仓库地址

# cat > /etc/docker/daemon.json << EOF
{ "insecure-registries":["rgs.unixfbi.com"] }
EOF
然后重启docker就可以了

# systemctl  restart docker

方法三
就是把你的仓库也配置成https ,设置证书。好吧,这种方法其实我不说,你也知道。

参考文档

官网 https://github.com/vmware/harbor/tree/master/docs
https://vmware.github.io/harbor/cn/#gettingHarbor
https://www.geek-share.com/detail/2670418284.html
https://www.geek-share.com/detail/2687876740.html
https://www.geek-share.com/detail/2690739333.html
http://blog.csdn.net/u010278923/article/details/72468791

本文出自 “运维特工” 博客,转载请务必保留此出处 http://www.unixfbi.com

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: