上一篇对Harbor做了简单介绍,这篇分享具体如何搭建。
环境准备
- Centos :7.2
- Docker :1.13.1
- Docker-compose :1.21.0, build 5920eb0
- Python :2.7.5
- IP :192.168.1.169
软硬件配置建议
主机软件/硬件环境
Docker 1.9.1+
Linux Ubuntu 14.04+(x86_64),Centos 7+(x86_64)
CPU 4核
内存 4G+
硬盘 500G(根据镜像仓库大小配置)
docker客户端环境
Docker 1.9.1+
浏览器环境
浏览器 要求
Chrome 最新版本(建议)
Firefox 最新版本(建议)
IE 9 +
安装Harbor
制作自签证书
Docker官方是推荐采用Secure Registry的工作模式的,即transport采用tls。这样我们就需要为Registry配置tls所需的key和crt文件。
由于没有知名权威CA签署的证书文件,这里我们自己制作一个。
使用openssl工具可以很容易地生成私人证书文件
[root@iz51hzu4zdjgpnz abiao]# mkdir -p certs
[root@iz51hzu4zdjgpnz abiao]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout certs/abiao.key -x509 -days 365 -out certs/abiao.crt
Generating a 4096 bit RSA private key
.......................................................................................++
.........................................................................................................................................................................................................................................++
writing new private key to 'certs/abiao.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:GUANGDONG
Locality Name (eg, city) [Default City]:SHEN ZHEN
Organization Name (eg, company) [Default Company Ltd]:FOO
Organizational Unit Name (eg, section) []:BAR
Common Name (eg, your name or your server's hostname) []:abiao.com
生成过程会提示填入各种信息,注意CN一栏要填入跟访问的地址相同的域名,我的是:abiao.com。
生成文件
- 秘钥文件:abiao.key
- 证书文件:abiao.crt
证书文件需要发送给用户,并且配置到用户Docker Host上,注意路径需要跟域名一致,例如:
/etc/docker/certs.d/abiao.com/abiao.crt
复制证书
- 创建一个certs目录。
[root@iz51hzu4zdjgpnz abiao]# cd /opt/
[root@iz51hzu4zdjgpnz opt]# mkdir -p certs
- 移动证书到certs目录。
[root@iz51hzu4zdjgpnz opt]# cp -rf /home/abiao/certs/* ./certs/
下载Harbour版本的二进制文件
https://github.com/vmware/harbor/releases
[root@iz51hzu4zdjgpnz opt] wget https://storage.googleapis.com/harbor-releases/release-1.5.0/harbor-offline-installer-v1.5.0-rc5.tgz
[root@iz51hzu4zdjgpnz opt] tar -zxvf harbor-offline-installer-v1.5.0-rc5.tgz
Harbor 配置
[root@iz51hzu4zdjgpnz opt]# cd harbor
[root@iz51hzu4zdjgpnz harbor]# vim harbor.cfg
只需修改如下内容
hostname = abiao.com
ui_url_protocol = https
customize_crt = off
ssl_cert = /opt/certs/abiao.crt
ssl_cert_key = /opt/certs/abiao.key
安装
[root@iz51hzu4zdjgpnz harbor]# ./install.sh
注:Harbor是通过docker-compose安装的,所以确保服务器已安装docker-compose。
Harbor 登录
如果一切正常,执行上述步骤,即可安装成功。现在通过浏览器来访问Harbor,访问地址https://192.168.1.169/harbor
默认的管理员用户名/密码是admin / Harbor12345
测试服务
这里拿安装的主机来测试服务是否正常。客户端主机访问步骤也可参考一下步骤。
修改hosts配置
[root@iz51hzu4zdjgpnz harbor]# vim /etc/hosts
192.168.1.169 abiao.com
证书文件配置到用户Docker Host上,注意路径需要跟域名一致
[root@iz51hzu4zdjgpnz opt]# cd /etc/docker/certs.d/
[root@iz51hzu4zdjgpnz certs.d]# mkdir -p abiao.com
[root@iz51hzu4zdjgpnz certs.d]# cp /opt/certs/abiao.crt ./abiao.com/
从 Docker Hub拉取 docker.io/hello-world 镜像
[root@iz51hzu4zdjgpnz opt]# docker pull hello-world
- 登录仓库
[root@iz51hzu4zdjgpnz abiao.com]# docker login abiao.com
Username: admin
Password:
Login Succeeded
标记镜像
将镜像标记为 abiao.com/abiao,在推送时,Docker会将其解释为仓库的位置
[root@iz51hzu4zdjgpnz abiao.com]# docker tag docker.io/hello-world:latest abiao.com/abiao/hello-world:1.0.0
- 推送镜像
[root@iz51hzu4zdjgpnz abiao.com]# docker push abiao.com/abiao/hello-world:1.0.0
The push refers to a repository [abiao.com/abiao/hello-world]
428c97da766c: Pushed
1.0.0: digest: sha256:1a6fd470b9ce10849be79e99529a88371dff60c60aab424c077007f6979b4812 size: 524
浏览器端查看推送结果
错误处理
像私有仓库推送镜像时,出现如下报错
[root@iz51hzu4zdjgpnz abiao.com]# docker push abiao.com/abiao/hello-world:1.0.0
The push refers to a repository [abiao.com/abiao/hello-world]
428c97da766c: Preparing
denied: requested access to the resource is denied
原因:
报错是因为找不到abiao的这个命名空间
解决:
在浏览器前端新建名为abiao的命名空间,再次推送就正常了。