Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,经过添加一些企业必需的功能特性,例如安全、标识和管理等,扩展了开源Docker Distribution。做为一个企业级私有Registry服务器,Harbor提供了更好的性能和安全。提高用户使用Registry构建和运行环境传输镜像的效率。Harbor支持安装在多个Registry节点的镜像资源复制,镜像所有保存在私有Registry中, 确保数据和知识产权在公司内部网络中管控。另外,Harbor也提供了高级的安全特性,诸如用户管理,访问控制和活动审计等。前端
基于角色的访问控制 :用户与Docker镜像仓库经过“项目”进行组织管理,一个用户能够对多个镜像仓库在同一命名空间(project)里有不一样的权限。java
镜像复制 : 镜像能够在多个Registry实例中复制(同步)。尤为适合于负载均衡,高可用,混合云和多云的场景。node
图形化用户界面 : 用户能够经过浏览器来浏览,检索当前Docker镜像仓库,管理项目和命名空间。python
AD/LDAP 支持 : Harbor能够集成企业内部已有的AD/LDAP,用于鉴权认证管理。mysql
审计管理 : 全部针对镜像仓库的操做均可以被记录追溯,用于审计管理。linux
国际化 : 已拥有英文、中文、德文、日文和俄文的本地化版本。更多的语言将会添加进来。nginx
RESTful API : RESTful API 提供给管理员对于Harbor更多的操控, 使得与其它管理软件集成变得更容易。git
部署简单 : 提供在线和离线两种安装工具, 也能够安装到vSphere平台(OVA方式)虚拟设备。github
Harbor在架构上主要由6个组件构成:web
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,供往后进行分析。
各个组件之间的关系以下图所示:
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)的服务端口。
环境名称 | 版本 |
---|---|
系统版本 | 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 |
安装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
必须先安装docker
# curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
若是须要安装其余版本的话,请修改上面命令中的版本号。
# chmod +x /usr/local/bin/docker-compose
# 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
# docker-compose --version docker-compose version 1.16.1, build 6d1ac21
# yum install python-pip # pip install docker-compose
测试:
# docker-compose --version
卸载docker-compose
对应上面两种安装方法:
二进制:
# rm /usr/local/bin/docker-compose
pip:
# pip uninstall docker-compose
安装方式分为在线安装和离线安装两种方式,我这里选择的是在线安装。
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/
配置文件为:/usr/local/harbor/harbor.cfg
配置的内容为:
# vim /usr/local/harbor/harbor.cfg hostname = rgs.unixfbi.com #邮箱配置 email_server = smtp.qq.com email_server_port = 25email_username = unixfbi@unixfbi.com email_password =12345678email_from = UnixFBI <unixfbi@unixfbi.com> email_ssl = false#禁止用户注册 self_registration = off #设置只有管理员能够建立项目 project_creation_restriction = adminonly
# /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 .
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-jobservice6134227d7ed4 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 registry78ea91838582 vmware/harbor-db:v1.2.0 "docker-entrypoint..." 4 days ago Up 4 days 3306/tcp harbor-db024e226ff135 vmware/harbor-adminserver:v1.2.0 "/harbor/harbor_ad..." 4 days ago Up 4 days harbor-adminserver3fab80444a1d 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
在浏览器输入rgs.unixfbi.com,由于我配置的域名为rgs.unixfbi.com。请你们根据本身的配置状况输入访问的域名;
默认帐号密码: admin / Harbor12345 登陆后修改密码
# 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的错误了。
# vim Dockerfile FROM centos:centos7.1.1503 ENV TZ "Asia/Shanghai"
# docker build -t rgs.unixfbi.com/library/centos7.1:0.1 .
# 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
从别的机器上拉一下镜像
# docker rmi -f $(docker images -q -a ) # docker pull rgs.unixfbi.com/library/centos7.1:0.10.1: Pulling from library/centos7.107618ba636d9: 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的配置都是使用的http协议访问,可是咱们工做中通常都是配置https访问。因此我给你们演示一下怎么配置Harbor可使用https访问,以及配置TLS证书都须要作哪些工做。
由于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
# mkdir /etc/certs # openssl genrsa -out /etc/certs/ca.key 2048 Generating RSA private key, 2048 bit long modulus ....+++ ..................................................+++ e is 65537 (0x10001)
注意命令中/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
# ./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了。
客户端须要建立证书文件存放的位置,而且把服务端建立的证书拷贝到该目录下,而后重启客户端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
# docker login rgs.unixfbi.comUsername (admin): Password: Login Succeeded
遇到的问题就是Harbor我配置的是http访问,可是docker客户端默认都是https访问Harbor,因此就会产生错误。下面看看我是怎么解决这个问题的吧。下面咱们来访问如下Harbor
# docker pull rgs.unixfbi.com/library/centos7.1:0.1Error 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 ,设置证书。好吧,这种方法其实我不说,你也知道。
本文出自 “运维特工” 博客,转载请务必保留此出处 http://www.unixfbi.com