Docker——Harbor镜像仓库高可用搭建

方案一:共享存储

多个harbor使用同一个共享存储(保存镜像数据和元数据信息)

多个harbor使用同一个数据库

配置负载均衡,将外部请求分发到各个harbor节点

架构简单,数据实时同步,但依赖共享存储,数据库有单点故障风险

方案二:镜像复制

部署两个或多个harbor服务器,每个集群使用自己的存储和数据库

harbor各节点通过harbor自带的复制功能进行镜像和元数据同步,使个集群的仓库保持同步

配置负载均衡(https),将外部请求分发到各个harbor节点

各个harbor服务器需要独立部署,单点故障风险小,但数据同步可能存在延迟,集群间的数据同步相对较差

环境部署规划

proxy:10.0.0.5

node01:10.0.0.191

node02:10.0.0.192

准备节点(harbor安装流程看Docker——Harbor镜像仓库创建篇)
配置node1复制规则(图形界面操作)

node01节点点击仓库管理 -> 新建目标,填写harbor node02节点信息,然后测试连接并完成认证

image-20250711214519328.png

配置node01复制管理

node01节点点击复制管理 -> 新建规则

image-20250711215031647.png

勾选规则点击复制,执行一次复制任务

image-20250711215142037.png

node02节点点击仓库管理 -> 新建目标,填写harbor node02节点信息,然后测试连接并完成认证

image-20250711220044467.png

配置node02复制管理

node02节点点击复制管理 -> 新建规则

image-20250711220312890.png

勾选规则点击复制,执行一次复制任务

image-20250711220411777.png

推送镜像测试,查看仓库之间是否有同步成功

接入haproxy负载均衡并配置https

创建证书目录并准备https证书

[root@proxy01 ~]# mkdir /certs
[root@proxy01 ~]# unzip harbor.rhien.cn_nginx.zip -d /certs
Archive: harbor.rhien.cn_nginx.zip
Aliyun Certificate Download
inflating: /certs/harbor.rhien.cn.pem
inflating: /certs/harbor.rhien.cn.key

将证书合并为harbor.pem

[root@proxy01 ~]# cd /certs/
[root@proxy01 certs]# cat harbor.rhien.cn.pem > harbor.pem
[root@proxy01 certs]# cat harbor.rhien.cn.key >> harbor.pem

安装haproxy

[root@proxy01 ~]# yum install haproxy -y

修改haproxy.cfg

[root@proxy01 ~]# cat /etc/haproxy/haproxy.cfg

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------

frontend harbor
    bind *:80
    bind *:443 ssl crt /certs/harbor.pem
    mode http
    redirect scheme https if !{ ssl_fc }	# 跳转至https协议
    use_backend harbor_cluster
    
    # 添加协议头部
  	# 如果客户端使用的协议不是 SSL/TLS(即请求未被加密),则设置 X-Forwarded-Proto 的值为 http
    http-request set-header X-Forwarded-Proto http if !{ ssl_fc }
  	# 如果客户端使用的协议是 SSL/TLS(即请求被加密),则设置 X-Forwarded-Proto 的值为 https
    http-request set-header X-Forwarded-Proto https if { ssl_fc }

backend harbor_cluster
    balance source				# 确保同一节点请求转发至同一后端harbor服务
    server node1 10.0.0.191:80 check port 80
    server node2 10.0.0.192:80 check port 80

启动haproxy

[root@proxy01 ~]# systemctl start haproxy

在harbor节点添加域名解析(否则会登录失败)

[root@dockera harbor]# cat /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.5 harbor.rhien.cn
认证docker login(填写对应域名)

[root@dockera harbor]# docker login harbor.rhien.cn
Username: admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

在harbor节点测试推送到harbor.rhien.cn

[root@dockera harbor]# docker push harbor.rhien.cn/infra/centos:centos7.9.2009

在harbor节点网页上查看是否全部上传成功