准备
apt install python3-pip
ln -s /usr/bin/python3 /usr/bin/python
安装docker-composepip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U docker-compose
查看docker-compose版本**docker-compose -v**
安装Harbor
下载harbor
wget https://github.com/goharbor/harbor/releases/download/v2.5.0/harbor-offline-installer-v2.5.0.tgz
解压离线安装包
tar -zxf harbor-offline-installer-v2.5.0.tgz
修改配置文件
cd harbor
cp harbor.yml.tmpl harbor.yml
# mkdir -p /opt/application/harbor //用于存放harbor的持久化数据
harbor.yml配置文件主要修改参数如下:
vim harbor.yml
hostname: 192.168.0.8 //设置访问地址,可以使用ip、域名,不可以设置为127.0.0.1或localhost。默认情况下,harbor使用的端口是80,若使用自定义的端口,除了要改docker-compose.yml文件中的配置外,这里的hostname也要加上自定义的端口,否则在docker login、push时会报错
#http配置
http:
# port for http, default is 80. If https enabled, this port will redirect to https port
port: 9999
#https配置(如不需要可不配置,注释掉)
# https related config
#https:
# https port for harbor, default is 443
#port: 443
# The path of cert and key files for nginx
#certificate: /your/certificate/path
#private_key: /your/private/key/path
#external_url: https://reg.mydomain.com:8433 //如果要启用外部代理,比如外层的NGINX、LB等,请取消注释external_url,当它启用时,hostname将不再使用。
harbor_admin_password: Harbor12345 //admin密码
#数据库配置
database:
# The password for the root user of Harbor DB. Change this before any production use.
password: root123
# The maximum number of connections in the idle connection pool. If it <=0, no idle connections are retained.
max_idle_conns: 50
# The maximum number of open connections to the database. If it <= 0, then there is no limit on the number of open connections.
# Note: the default number of connections is 100 for postgres.
max_open_conns: 100
#持久化数据目录
data_volume: /opt/application/harbor
默认是使用 HTTP 协议,我们可以配置证书并使用 HTTPS 来访问 Harbor。
2.3 配置HTTPS
说实话我开始是抵触的,我不想去使用它,但是当我考虑到,我不可能暂停我所有已经运行的容器来修改配置,来解决 docker login的问题,所有我只能老老实实的来配置 HTTPS ,这个烦人的 HTTPS。
官方配置文档: https://github.com/goharbor/harbor/blob/master/docs/configure_https.md
我们这里演示的是创建自己的 证书,实际生产环境中我们可以去阿里云或者其他云服务器厂商申请免费的 证书。
创建证书
# 创建存放证书的目录
mkdir -p /data/cert/
cd /data/cert/
# 创建自签名证书key文件
openssl genrsa -out ca.key 4096
openssl req -x509 -new -nodes -sha512 -days 3650 \
-subj "/C=TW/ST=Taipei/L=Taipei/O=example/OU=Personal/CN=192.168.15.170" \
-key ca.key \
-out ca.crt #CN 替换为你的仓库域名
修改配置
#配置 HTTPS 配置
https:
# # https port for harbor, default is 443
port: 443
# # The path of cert and key files for nginx
certificate: /data/cert/ca.crt
private_key: /data/cert/ca.key
重新初始化 Harbor
# 暂停
docker-compose down -v
prepare # 生成配置文件,根据 harbor.yml 配置生成docker-compose文件。
docker-compose up -d # 后台启动
客户端配置
开始安装
./install.sh
用浏览器访问前面配置的hostname测试是否安装成功
Harbor如何停止与启动
# cd /root/harbor //切换到harbor安装包目录
# docker-compose stop //停止Harbor
# docker-compose start //启动Harbor
# docker-compose restart //重启Harbor
上传和下载
客户机添加安全仓库
docker1.4版本以后docker registry使用的是https,但是Harbor默认使用的是http方式,上传下载时会报错,可以通过修改本机/etc/docker/daemon.json文件解决
vim /etc/docker/daemon.json
# 将安全仓库设为服务器ip
{
"insecure-registries": ["192.168.68.20:8520"]
}
# 重启docker使配置生效
systemctl restart docker
进入网页创建用户和一个私有项目,并把用户加入私有项目中
将本地镜像重写tag
docker tag hello-world:latest 192.168.68.20:8520/cs/hello-world:v1
新tag格式为:服务器IP/项目名/镜像名:版本
登陆harbor仓库,并上传下载
docker login 192.168.68.20:8520
# 然后根据提示输入用户名和密码
docker pull hello-world:latest
docker tag hello-world:latest 192.168.68.20:8520/cs/hello-world:v1
# 下载和上传
docker push 192.168.68.20:8520/cs/hello-world:v1
docker pull 192.168.68.20:8520/cs/hello-world:v1
harbor开机自启
sudo vim /etc/rc.local
添加
cd $HOME/harbor && docker-compose up -d
Harbor使用外部代理
harbor默认只能使用harbor.yml中hostname指定的ip或主机名作为web访问地址,但在实际使用过程中,一般不允许ip地址或者主机名直接暴露在外访问,故需要配置nginx代理,通过代理后指定的地址进行访问。
配置方式:
修改harbor.yml,把https相关的注释(如果没有注释,http会自动重定向到https,导致多次重定向),然后添加external_url配置:
# Configuration file of Harbor
# The IP address or hostname to access admin UI and registry service.
# DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
hostname: 192.168.0.8:9999
# http related config
http:
# port for http, default is 80. If https enabled, this port will redirect to https port
port: 9999
# https related config
#https:
# https port for harbor, default is 443
#port: 443
# The path of cert and key files for nginx
#certificate: /your/certificate/path
#private_key: /your/private/key/path
# # Uncomment following will enable tls communication between all harbor components
# internal_tls:
# # set enabled to true means internal tls is enabled
# enabled: true
# # put your cert and key files on dir
# dir: /etc/harbor/tls/internal
# Uncomment external_url if you want to enable external proxy
# And when it enabled the hostname will no longer used
external_url: https://harbor.xxx.cn #如果这里是https,nginx代理就需要配置ssl……
修改配置后docker-compose down停止所有服务,删除当前配置目录:rm -rf ./common/config下配置清单,重新执行install.sh生成配置
NGINX外部代理配置文件:
server {
listen 80;
server_name harbor.xxx.cn;
#client_max_body_size 1000M;
access_log /data/wwwlogs/harbor.xxx.cn_access.log combined;
rewrite ^(.*) https://$server_name$1 permanent;
location / {
#proxy_redirect off;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:9999;
}
}
##########################ssl#######################
server {
listen 443 ssl;
server_name harbor.xxx.cn;
ssl_certificate sslkey/harbor.xxx.cn_chain.crt;
ssl_certificate_key sslkey/harbor.xxx.cn_key.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
access_log /data/wwwlogs/harbor.xxx.cn_access.log combined;
location / {
#proxy_redirect off;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:9999;
}
}
注意:这几项配置都不要,注释掉,否则在pull和push镜像的时候会报错。
#proxy_redirect off;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
具体报错如下:
# docker push harbor.xxx.cn/xxx/ubuntu
The push refers to repository [harbor.xxx.cn/xxx/ubuntu]
7555a8182c42: Pushing [==================================================>] 72.78MB/72.78MB
unknown blob
docker push harbor.xxx.cn/xxx/ubuntu
The push refers to repository [harbor.xxx.cn/xxx/ubuntu]
7555a8182c42: Pushing [==================================================>] 72.78MB/72.78MB
dial tcp 127.0.0.1:9999: connect: connection refused
另外一个报错:push的镜像文件比较大的时候
error parsing HTTP 413 response body: invalid character ‘<’ looking for beginning of value: “\r\n\r\n\r\n
413 Request Entity Too Large
解决方式:
修改外部代理nginx配置文件:nginx.conf
client_max_body_size默认为0, 修改0为特定的大小即可。如 client_max_body_size 102400M