Harbor简介
1.Harbor介绍
2.Harbor特性
-
基于角色的访问控制 :用户与Docker镜像仓库通过“项目”进行组织管理,一个用户可以对多个镜像仓库在同一命名空间(project)里有不同的权限。 -
镜像复制 :镜像可以在多个Registry实例中复制(同步)。尤其适合于负载均衡,高可用,混合云和多云的场景。 -
图形化用户界面 :用户可以通过浏览器来浏览,检索当前Docker镜像仓库,管理项目和命名空间。 -
AD/LDAP 支持 :Harbor可以集成企业内部已有的AD/LDAP,用于鉴权认证管理。 -
审计管理 :所有针对镜像仓库的操作都可以被记录追溯,用于审计管理。 -
国际化 :已拥有英文、中文、德文、日文和俄文的本地化版本。更多的语言将会添加进来。 -
RESTful API :RESTful API 提供给管理员对于Harbor更多的操控, 使得与其它管理软件集成变得更容易。 -
部署简单 :提供在线和离线两种安装工具, 也可以安装到vSphere平台(OVA方式)虚拟设备。
3.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 客户端向Regiøstry服务发起的请求,如果不包含token,会被重定向到这里,获得token后再重新向Registry进行请求。 -
Database:为core services提供数据库服务,负责储存用户权限、审计日志、Docker image分组信息等数据。 -
Job Services:提供镜像远程复制功能,可以把本地镜像同步到其他Harbor实例中。 -
Log collector:为了帮助监控Harbor运行,负责收集其他组件的log,供日后进行分析。
4.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-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。
二、安装配置Harbor简介
1.环境说明
|
|
|
|
|
|
|
|
|
|
|
|
2.首先安装docker
# 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
# systemctl start docker
yum remove docker-ce
卸载后images,containers,volumes,configuration files 是不能自动删除的,为了删除all images,containers,and volumes,请执行如下命令:
rm -rf /var/lib/docker
3.安装docker-compose
方法一:二进制安装
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
对应上面两种安装方法:
二进制:
# rm /usr/local/bin/docker-compose
# 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
安装:
# cd /usr/local/src/
# tar zxf harbor-online-installer-v1.2.0.tgz -C /usr/local/
# cd /usr/local/harbor/
5.修改配置文件
配置的内容为:
# 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启动和停止
# 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
# 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
# docker-compose start
停止Harbor
# docker-comose stop
重启Harbor
# docker-compose restart
8.访问测试
默认账号密码:admin / Harbor12345 登录后修改密码
三、测试上传和下载镜像
1.修改各docker client配置
# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --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
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 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证书
1.修改Harbor配置文件
配置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 .
5.客户端配置
/etc/docker/certs.d/rgs.unixfbi.com
# mkdir -p /etc/docker/certs.d/rgs.unixfbi.com
# scp /etc/certs/ca.crt root@192.168.199.183:/etc/docker/certs.d/rgs.unixfbi.com/
# systemctl restart docker
6.测试是否支持https访问
# docker login rgs.unixfbi.com
Username (admin):
Password:
Login Succeeded
五、遇到问题
# 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访问了。
本文分享自微信公众号 - DevOps持续交付(devopscd)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
来源:oschina
链接:https://my.oschina.net/u/2483000/blog/4362652