目录
搭建
提示:harbor的安装文件最好不要丢弃,放置好,以后harbor的修改会用到。
主要流程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# 下载harbor离线安装包 https://github.com/goharbor/harbor/releases/download/v2.2.2/harbor-offline-installer-v2.2.2.tgz # 解压 tar -xzvf harbor-offline-installer-v2.2.2.tgz # 进入文件夹 cd harbor # 修改harbor配置 cp harbor.yml.tmpl harbor.yml vi harbor.yml #########配置文件 START############ # 域名或IP(不能是localhost 或 127.0.0.1) hostname: reg.xxx.com # http配置 http: # 端口 port: 80 # https 配置(暂时关闭,先以http为主) #https: # https port for harbor, default is 443 #port: 443 # The path of cert and key files for nginx #certificate: /data/cert/reg.aligo.ml.crt #private_key: /data/cert/reg.aligo.ml.key # harbor管理员密码 harbor_admin_password: 123456 # Harbor 数据库密码 database: password: 123456 其它配置看着修改 #########配置文件 END############ # 部署 ./install.sh # 通过ip和harbor.yml配置的http端口访问,看见登录即为成功。 |
主要文件介绍
common
: 生成的各个容器的配置文件都在这里。
docker-compose.yml
: harbor编排文件。
harbor.yml
: harbor配置文件,原来没有,cp harbor.yml.tmpl而来
。
harbor.yml.tmpl
: harbor配置模板。
install.sh
: harbor安装脚本。
prepare
: 根据harbor.yml生成各个容器配置文件,数据于common/config
。
harbor是由docker-compose编排而成,harbor.yml作为harbor配置文件,(prepare根据harbor.yml生成)各个容器的配置和数据都存放在common/config文件夹。
而install.sh就是:prepare(根据harbor.yml生成各个容器的配置和数据) -> docker-compose(harbor编排启动)。
修改配置文件harbor.yml后使生效
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# 进入harbor安装文件夹 cd harbor # 如果harbor已在运行,先删除(只删除容器,不会删除数据) docker-compose down -v # 清空原来的配置(可选) rm -rf ./common/config/ # 生成新的配置 ./prepare # 重新启动harbor docker-compose up -d |
或者简单一点(效果一样)
1 |
./install.sh |
安装后的一些配置
默认就有一个lib项目和admin用户。项目是存放镜像的,admin用户可以对任意项目操作。
首先需要创建一个用户和项目
点击创建用户
按照提示,输入用户名密码邮箱这些不用多说,注意这些信息在推送镜像的时候会用到,不要随便设一个!
然后创建一个镜像仓库
为仓库起名后点击确定。-1
表示无限制。
点进去给这个镜像仓库分配管理用户
输入之前创建的用户名,确定即可。
推送本地docker镜像到harbor私服
接下就是样演示推送镜像到harbor私服了,点击镜像仓库这一栏。有一个推送镜像的按钮。点一下会出现推送命令。注意必须使用这个推送命令所示的前缀,不然是推送不了的。
示例:
1 2 3 4 5 6 7 8 9 10 11 |
# 登录,使用刚创建的账号密码登录 docker login hardor域名或IP地址 # 下载官方镜像 docker pull k8s.gcr.io/coredns:1.7.0 # tag改为hardor仓库的 docker tag k8s.gcr.io/coredns:1.7.0 docker.aligo.ml/k8s/coredns:1.7.0 # push到hardor仓库 docker push docker.aligo.ml/k8s/coredns:1.7.0 |
以上这个示例就可以备份官方的镜像。
报错
Error response from daemon: Get https://xxx/v2/: dial tcp xxxx:443: connect: connection refused
出现这个错误是因为hardor没有配置https,但是docker为了安全,默认使用https连接,所以要告诉docker,这个http地址是安全的,不必要使用https。假设hardor地址为:192.168.10.11:81
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# 查找docker服务文件 find / -name docker.service -type f /usr/lib/systemd/system/docker.service # 编辑 vi /usr/lib/systemd/system/docker.service #添加--insecure-registry=hardor域名或ip ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry=192.168.10.11:81 # 编辑 vi /etc/docker/daemon.json { "insecure-registries":["192.168.10.11:81"] } # 重启生效 systemctl daemon-reload systemctl restart docker |
拉取harbor上的镜像
1 |
docker pull hardor镜像地址 |
报错
Error response from daemon: Get https://xxxx/v2/: dial tcp xxxxx:443: connect: no route to host
和上面的原因一样的,按照上面的操作即可。
配置https
harbor配置https访问
总的来说,步骤如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#! /bin/bash # 域名还是IP,true-IP false-域名 hostnameIsIP=false readonly hostnameIsIP # 填写域名或者IP hostname=reg.aligo.ml readonly hostname # 创建工作目录 mkdir -p ~/harbor_ssl cd ~/harbor_ssl # 安装openssl yum install -y openssl openssl-devel # 生成CA证书私钥 openssl genrsa -out ca.key 4096 # 生成CA证书 openssl req -x509 -new -nodes -sha512 -days 3650 \ -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=${hostname}" \ -key ca.key \ -out ca.crt # 生成服务器私钥 openssl genrsa -out ${hostname}.key 4096 # 生成服务器证书签名请求(CSR) openssl req -sha512 -new \ -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=${hostname}" \ -key ${hostname}.key \ -out ${hostname}.csr # 生成一个x509 v3扩展文件 cat > v3.ext <<-EOF authorityKeyIdentifier=keyid,issuer basicConstraints=CA:FALSE keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment extendedKeyUsage = serverAuth if hostnameIsIP then subjectAltName = IP:${hostname} else subjectAltName = @alt_names [alt_names] DNS.1=${hostname} DNS.2=${hostname} DNS.3=${hostname} fi EOF # 使用该v3.ext文件为您的Harbor主机生成证书 openssl x509 -req -sha512 -days 3650 \ -extfile v3.ext \ -CA ca.crt -CAkey ca.key -CAcreateserial \ -in ${hostname}.csr \ -out ${hostname}.crt # 将服务器证书和密钥复制到Harbor主机上的/data/cert/文件夹中 mkdir -p /data/cert/ cp ${hostname}.crt /data/cert/ cp ${hostname}.key /data/cert/ # 转换harbor.od.com.crt为harbor.od.com.cert,供Docker使用 openssl x509 -inform PEM -in ${hostname}.crt -out ${hostname}.cert # 将服务器证书,密钥和CA文件复制到Harbor主机上的Docker证书文件夹中。您必须首先创建适当的文件夹 mkdir -p /etc/docker/certs.d/${hostname}/ cp ${hostname}.cert /etc/docker/certs.d/${hostname}/ cp ${hostname}.key /etc/docker/certs.d/${hostname}/ cp ca.crt /etc/docker/certs.d/${hostname}/ # 重新启动Docker Engine systemctl restart docker |
后面的步骤按教程执行。
nginx反代harbor
环境:同一台主机,有一个docker的nginx和harbor,现在使用docker的nginx代理harbor。
harbor会创建自己的网络:harbor_harbor
1 2 3 4 5 6 |
$ docker network ls NETWORK ID NAME DRIVER SCOPE 49ac102d44d5 bridge bridge local d62178d9350b harbor_harbor bridge local f9aa23827a19 host host local |
而nginx使用另外的network,所以要把harbor的network改成和nginx一样的,使他们可以内网通信。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 打开harbor安装文件夹 cd harbor # 卸载 docker-compose down -v # 清空配置 rm -rf common/config/ # 生成配置 (docker-compose.yml也会被重置) ./prepare # 修改docker-compose.yml vi docker-compose.yml networks: harbor: external: false 修改为: networks: harbor: external: name: my_net(nginx使用的network名称) # 启动harbor docker-compose up -d |
harbor使用nginx对外提供访问,容器名是nginx,可以docker ps -a
查看
1 2 3 4 |
$ docker ps -a IMAGE PORTS NAMES goharbor/nginx-photon:v2.2.2 0.0.0.0:80->8080/tcp, 0.0.0.0:443->8443/tcp nginx |
检查harbor是否使用了nginx的network
1 2 3 4 5 6 7 8 9 10 |
$ docker inspect nginx "Networks": { "my_net": { # 注意这里 "IPAMConfig": null, "Links": null, "Aliases": [ "proxy", "c989fdac10af" ], |
nginx的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
upstream harbor { # 内网反代 # harbor内网的端口是8080,nginx是harbor的nginx容器名 server nginx:8080; # harbor的https内网端口是8443, # 如果harbor已配置好https,把8080的注释,8443去掉注释 #server nginx:8443; } server { listen 80; listen 443 ssl; # 这里的ssl文件可以和harbor的不同 ssl_certificate /etc/nginx/conf.d/ssl_files/aligo.ml/fullchain.pem; ssl_certificate_key /etc/nginx/conf.d/ssl_files/aligo.ml/privkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5; ssl_prefer_server_ciphers on; ssl_session_timeout 10m; ssl_session_cache builtin:1000 shared:SSL:10m; ssl_buffer_size 1400; add_header Strict-Transport-Security max-age=15768000; #ssl_stapling on; #ssl_stapling_verify on; # 改成你的域名,且与harbor.yml的hostname、external_url一致 server_name reg.harbor.com; add_header X-Frame-Options "SAMEORIGIN"; add_header X-XSS-Protection "1; mode=block"; add_header X-Content-Type-Options "nosniff"; location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } location / { proxy_pass http://harbor; 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; client_max_body_size 0; # 0-不限制上传大小,避免413。 client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 6 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } } |
如果harbor配置了https,只需反代到harbor的https端口,因为harbor开启https,访问http会强制转跳到https。
报错
x509: certificate signed by unknown authority
1 2 3 |
$ docker pull reg.aligo.ml/k8s/pause:3.3 Error response from daemon: Get https://reg.aligo.ml/v2/: x509: certificate signed by unknown authority |
这是应为自制的CA证书不受信任。(可以自己出钱买一个正规的)
解决如下:
1 2 3 4 5 |
vi /etc/docker/daemon.json { "insecure-registries":["reg.harbor.com"], } |
使用insecure-registries
字段,添加域名为受信任域名即可。
error parsing HTTP 413 response body: invalid character ‘<‘ looking for beginning of value
先查看反代的设置有没有设置上传大小的限制:
1 2 3 4 5 6 |
location / { proxy_pass http://harbor; 。。。 client_max_body_size 0; # 0-不限制上传大小,避免413。 。。。 } |
如果再报413,那么修改nginx/openresty的全局配置文件nginx.conf:
1 2 3 4 5 |
http { 。。。 client_max_body_size 0; # 0-不限制上传大小,避免413。 。。。 } |