HAProxy 提供高可用性、负载均衡以及基于 TCP 和 HTTP 应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。

该文档在CentOS7.6系统下进行编译安装haproxy 1.8.25(长期支持版本TLS)。

主机名称

ip地址

操作系统

角色

软件版本

备注

opsproxy-shqs-1

10.99.73.38

CentOS 7.6

haproxy

1.8.25

长期支持版本LTS

一、编译安装haproxy

1.1 下载haproxy源码包

wget http://www.haproxy.org/download/1.8/src/haproxy-1.8.25.tar.gz
tar zxvf haproxy-1.8.25.tar.gz

1.2 编译安装haproxy

我的编译软件统一安装在/opt/app/install/目录下面,然后软连接到/opt/app/下面。所以这里设置haproxy的安装目录为/opt/app/install/haproxy

# 准备编译环境
yum install -y gcc gcc-c++ pcre pcre-devel openssl openssl-devel systemd-devel

# 切换到haproxy源码根目录
cd haproxy-1.8.25

# 编译 linux内核版本大于2.6.28的都可以使用TARGET=linux2628
make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/opt/app/install/haproxy

# 安装
make install PREFIX=/opt/app/install/haproxy

# 可执行文件拷贝一份到系统执行文件目录,该目录在path变量里面,可以直接使用haproxy命令
cp haproxy /usr/sbin/

# 软连接(这一步谨是我自己对软件目录管理的需求)
ln -s /opt/app/install/haproxy /opt/app/haproxy

# 配置haproxy用户和组。因为yum安装haproxy生成的也是这个ID,这里组/用户ID设置为188,仅仅是为了保持一致,其实设置为多少都可以。
groupadd -g 188 haproxy
useradd -u 188 -g 188 -r -s /sbin/nologin haproxy

# haproxy附加参数文件,这个可以不用设置的,这里是为了和yum安装方式保持一致。
touch /etc/sysconfig/haproxy
vi /etc/sysconfig/haproxy
OPTIONS=""

# pid文件放置目录,目录可以自己定义在启动脚本里面使用
mkdir /opt/app/haproxy/run 

# socket方式的相关文件目录,这里没有用到
# mkdir /var/lib/haproxy
# chown -R haproxy:haproxy /var/lib/haproxy/

1.3 haproxy配置文件

这里仅仅是一个简单的配置文件,只是配置了一个统计服务stats,并没有配置实际的业务服务代理。部署验证这些就够了。
配置文件目录/opt/app/haproxy/haproxy.cfg

vi /opt/app/haproxy/haproxy.cfg

global
  user     haproxy
  group    haproxy
  daemon
  nbproc   2
  #cpu-map  1  0
  #cpu-map  2  1
  maxconn  100000
  chroot   /opt/app/haproxy
  pidfile  /opt/app/haproxy/run/haproxy.pid
  log      127.0.0.1 local0 info

defaults
  log      global
  option   httplog
  option   http-keep-alive
  option   redispatch
  option   forwardfor
  maxconn  100000
  mode     http
  retries  3
  timeout  check 5s
  timeout  connect 5s
  timeout  client 60s
  timeout  server 60s
  timeout  http-request 10s
  timeout  queue 1m

listen stats
  bind     0.0.0.0:1234
  log      global
  mode     http
  stats    enable
  stats    hide-version
  stats    realm Haproxy\ Statistics
  stats    uri     /stats
  stats    refresh 5s
  stats    auth    admin:admin

1.4 配置system自启动文件

  • 编写配置文件
vi /usr/lib/systemd/system/haproxy.service

[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
EnvironmentFile=/etc/sysconfig/haproxy
ExecStartPre=/usr/sbin/haproxy -f /opt/app/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /opt/app/haproxy/haproxy.cfg -p /opt/app/haproxy/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed

[Install]
WantedBy=multi-user.target
  • 加载文件
systemctl daemon-reload
  • 配置自启动
systemctl enable haproxy
  • 启动服务
systemctl start haproxy
systemctl status haproxy

netstat -pltn
tcp        0      0 0.0.0.0:1234            0.0.0.0:*               LISTEN      20808/haproxy
  • 访问http://10.99.73.38:1234/stats
  • 如何yum安装haproxy haproxy编译安装_如何yum安装haproxy

1.5 配置记录Haproxy访问日志

  • 根据haproxy.cfg配置文件里面配置log 127.0.0.1 local0 info进行配置
yum -y install rsyslog
touch /etc/rsyslog.d/haproxy.conf

vi /etc/rsyslog.d/haproxy.conf

$ModLoad imudp
$UDPServerRun 514
local0.* /var/log/haproxy.log
  • 重新启动rsyslog生效配置
systemctl restart rsyslog
  • 日志访问
# 需要访问过haproxy才会生成文件以及内容
tail -f /var/log/haproxy.log

Jul  2 22:50:51 localhost haproxy[18038]: 127.0.0.1:46154 [02/Jul/2020:22:50:51.310] stats stats/<STATS> -1/-1/-1/-1/0 401 262 - - PR-- 1/1/0/0/3 0/0 "GET /stats HTTP/1.1"
Jul  2 22:50:51 localhost haproxy[18038]: 127.0.0.1:46154 [02/Jul/2020:22:50:51.310] stats stats/<STATS> -1/-1/-1/-1/0 401 262 - - PR-- 1/1/0/0/3 0/0 "GET /stats HTTP/1.1"

二、配置业务代理

2.1 docker镜像仓库harbor代理实例配置

主机名称

ip地址

操作系统

角色

端口

备注

opsharbor-shqs-1

10.99.73.38

CentOS 7.6

docker hub<主节点>

80 自动跳转443

443

前面实际是一个nginx

opsharbor-shqs-2

10.99.73.39

CentOS 7.6

docker hub<从节点>

80 自动跳转443

443

前面实际是一个nginx

这里我们使用haproxy来代理harbor,并使用tcp协议代理,实际处理流程交给后端的nginx处理

client --> haproxy-1(master-vip)  --> harbor-1 
           |                      \  /
           |                       / \
           + haproxy-2(backup)     -->   harbor-2
这里的配置适合haproxy+keepalived与harbor不在同一组服务器上的方式,如果在同一组服务器上由于VIP只在一台服务器上,haproxy相同的配置导致从节点监控VIP:port无法启动。

如果确实在一组服务器上,需要配置一个健康脚本,让VIP所在节点haproxy运行,而非VIP节点haproxy停止运行。并实时监控切换。
global
  user     haproxy
  group    haproxy
  daemon
  nbproc   2
  #cpu-map  1  0
  #cpu-map  2  1
  maxconn  100000
  chroot   /opt/app/haproxy
  pidfile  /opt/app/haproxy/run/haproxy.pid
  log      127.0.0.1 local0 info

defaults
  log      global
  mode     http
  maxconn  100000
  option   httplog
  option   dontlognull
  option   http-keep-alive
  option   redispatch
  #option   forwardfor
  retries  3
  timeout  check 10s
  timeout  connect 100s
  timeout  client 3600s
  timeout  server 3600s
  timeout  http-request 100s
  timeout  queue 1m

listen stats 
  bind     0.0.0.0:1234
  log      global
  mode     http
  stats    enable
  stats    hide-version
  stats    realm Haproxy\ Statistics
  stats    uri /stats
  stats    auth    admin:admin

listen harbor_http
  bind :80
  balance first
  mode tcp
  option tcplog
  server harbor_1 10.99.73.38:80 check inter 20000 rise 2 fall 5
  server harbor_2 10.99.73.39:80 backup check inter 20000 rise 2 fall 5

listen harbor_https
  bind :443
  balance first
  mode tcp
  option tcplog
  server harbor_1 10.99.73.38:443 check inter 20000 rise 2 fall 5
  server harbor_2 10.99.73.39:443 backup check inter 20000 rise 2 fall 5

重启haproxy服务

systemctl restart haproxy.service
systemctl status haproxy.service

netstat -plnt |grep haproxy
tcp        0      0 0.0.0.0:80          0.0.0.0:*               LISTEN      7087/haproxy        
tcp        0      0 0.0.0.0:443         0.0.0.0:*               LISTEN      7087/haproxy