HAProxy 基础介绍¶
一:Web架构介绍¶
1.1 单机房架构¶
1.2 多机房架构¶
1.3 公有云架构¶
1.4 私有云架构¶
二:负载均衡简介¶
2.1 为什么使用负载均衡¶
负载均衡(Load Balance,简称LB)是一种服务或基于硬件设备等实现的高可用反向代理技术,负载均衡将特定的业务(web服务、网络流量等)分担给指定的一个或多个后端特定的服务器或设备,从而提高公司业务的并发处理能力、保证了业务的高可用性、方便了业务后期的水平动态扩展。
https://developer.aliyun.com/article/1803 # 阿里云SLB 介绍
- 服务器的动态水平扩展 → 对用户无感知
- 增加业务并发访问及处理能力 → 解决单服务器瓶颈问题
- 节约公网IP地址 → 降低IT支出成本
- 隐藏内部服务器IP → 提高内部服务器安全性
- 配置简单 → 固定格式的配置文件
- 功能丰富 → 支持四层和七层,支持动态下线主机
- 性能较强 → 并发数万甚至数十万
2.2 负载均衡类型¶
四层(传输层层):¶
- LVS(Linux Virtual Server)
- HAProxy(High Availability Proxy)
- Nginx(1.9)
七层:¶
- HAProxy
- Nginx
硬件:¶
- F5
- Netscaler
- Array
- 深信服
- 北京灵州
2.3 应用场景¶
- 四层:Redis、Mysql、RabbitMQ、Memcache等
- 七层:Nginx、Tomcat、Apache、PHP、图片、动静分离、API等
2.4 HAProxy介绍¶
HAProxy是法国开发者威利塔罗(Willy Tarreau)在2000年使用C语言开发的一个开源软件,是一款具备高并发(一万以上)、高性能的TCP和HTTP负载均衡器,支持基于cookie的持久性,自动故障切换,支持正则表达式及web状态统计,目前最新TLS版本为2.2。
2.4.1 企业版¶
企业版将提供更多的特性和功能以及全天24小时的技术支持等服务。
2.4.2 社区版¶
社区版提供基础功能支持。
2.4.3 版本对比¶
功能 | 社区版 | 企业版 |
---|---|---|
高级HTTP/TCP负载平衡和持久性 | 支持 | 支持 |
高级健康检查 | 支持 | 支持 |
应用程序加速 | 支持 | 支持 |
高级安全特性 | 支持 | 支持 |
高级管理 | 支持 | 支持 |
HAProxy Dev Branch新功能 | 支持 | 支持 |
24*7 支持服务 | 不支持 | 支持 |
实时仪表盘 | 不支持 | 支持 |
2.4.4 HAProxy功能¶
- TCP和HTTP反向代理
- SSL/TSL服务器
- 可以针对HTTP请求添加cookie,进行路由后端服务器
- 可平衡负载至后端服务器,并支持持久连接
- 支持所有主服务器故障切换至备用服务器
- 支持专用端口实现监控服务
- 支持不影响现有连接情况下停止接受新连接请求
- 可以在双向添加,修改或删除HTTP报文首部
- 响应报文压缩
- 支持基于pattern实现连接请求的访问控制
- 通过特定的URI为授权用户提供详细的状态信息
不具备的功能: - 正向代理--squid,nginx - 缓存代理--varnish - web服务--nginx、tengine、apache、php、tomcat - UDP--目前不支持UDP协议,2.1版本会支持UDP协议代理 - 单机性能--LVS(DR)
三:HAProxy安装及基础配置¶
3.1 Ubuntu安装¶
# apt-get install software-properties-common
# add-apt-repository ppa:vbernat/haproxy-2.0
# apt update
# apt-cache madison haproxy
# apt install haproxy=2.0.4-1ppa1~bionic
# 验证haproxy版本
# haproxy -v
HA-Proxy version 2.0.4-1ppa1~bionic 2019/08/09
- https://haproxy.org/
3.2 Centos安装¶
3.2.1 默认yum源¶
# yum install haproxy -y
# 验证haproxy版本
# haproxy -v
HA-Proxy version 1.5.18 2016/05/10
Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>
3.2.2 第三方安装包¶
https://pkgs.org/download/haproxy # 下载 rpm 包
# 基于互联网在线安装
# wget http://www.nosuchhost.net/~cheese/fedora/packages/epel7/x86_64/cheese-release-7-1.noarch.rpm
# rpm -ivh cheese-release-7-1.noarch.rpm
# yum install haproxy-1.8.14-1.el7.x86_64.rpm -y
# 验证haproxy版本
# haproxy -v
HA-Proxy version 1.8.14-52e4d43 2018/09/20
Copyright 2000-2018 Willy Tarreau <willy@haproxy.org>
3.3 编译安装HAProxy¶
编译安装 Haproxy 2.0 LTS 版本,更多源码包下载地址: https://www.haproxy.org/download/
3.3.1 解决lua环境¶
HAProxy支持基于Lua实现功能扩展。Lua是一种小巧的脚本语言,于1993年由巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组开发,其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。
- Lua 应用场景
- 游戏开发
- 独立应用脚本
- Web 应用脚本
- 扩展和数据库插件,如 MYSQL Proxy
- 安全系统(如入侵检测系统)
3.3.1.1 Ubuntu基础环境¶
sudo apt install iproute2 ntpdate tcpdump telnet traceroute \
nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev \
libpcre3 libpcre3-dev zlib1g-dev gcc openssh-server iotop \
unzip zip libreadline-dev libsystemd-dev
# 安装基础命令及编译依赖环境
# pwd
/usr/local/src
# wget http://www.lua.org/ftp/lua-5.3.5.tar.gz
# tar xvf lua-5.3.5.tar.gz
# cd lua-5.3.5
# make linux test
# pwd
/usr/local/src/lua-5.3.5
# ./src/lua -v
Lua 5.3.5
Copyright (C) 1994-2018 Lua.org, PUC-Rio
或安装系统自带的 lua
# apt install lua5.3=5.3.3-1ubuntu0.18.04.1
# lua5.3 -v
3.3.1.2 Centos基础环境¶
由于CentOS自带的Lua版本比较低,并不符合HAProxy要求的Lua最低版本(5.3),因此在安装HAProxy时需要手动编译安装新版本的Lua环境。具体过程如下:
# yum install libtermcap-devel ncurses-devel libevent-devel readline-devel gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate iotop bc
# wget http://www.lua.org/ftp/lua-5.3.5.tar.gz
# tar xvf lua-5.3.5.tar.gz
# cd lua-5.3.5
# make linux test
# pwd
/usr/local/src/lua-5.3.5
# ./src/lua -v
Lua 5.3.5
Copyright (C) 1994-2018 Lua.org, PUC-Rio
# ./src/lua -v # 编译安装的版本
3.3.2 编译安装HAProxy¶
# pwd
/usr/local/src
# tar xvf haproxy-2.0.4.tar.gz
# HAPROY 1.8 及 1.9 版本编译参数:
# make ARCH=x86_64 TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 USE_LUA=1 LUA_INC=/usr/local/src/lua5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/ PREFIX=/apps/haproxy
# make install PREFIX=/apps/haproxy
# cp haproxy /usr/sbin/
3.3.3 验证HAProxy版本¶
# /usr/local/haproxy/sbin/haproxy -v
HA-Proxy version 2.0.4 2019/08/06 - https://haproxy.org/
3.3.4 HAProxy启动脚本¶
# cat /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
3.3.5 配置文件¶
# mkdir /etc/haproxy
# cat /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /usr/local/haproxy
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 99
gid 99
daemon
#nbproc 4
#cpu-map 1 0
#cpu-map 2 1
#cpu-map 3 2
#cpu-map 4 3
pidfile /var/lib/haproxy/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen stats
mode http
bind 0.0.0.0:9999
stats enable
log global
stats uri /haproxy-status
stats auth haadmin:q1w2e3r4ys
listen web_port
bind 192.168.7.101:80
mode http
log global
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
3.3.6 启动haproxy¶
# mkdir /var/lib/haproxy
# chown 99.99 /var/lib/haproxy/ -R
# systemctl start haproxy
# systemctl enable haproxy
# systemctl status haproxy
3.3.7 验证haproxy状态¶
haproxy.cfg 文件中定义了chroot、pidfile、user、group等参数,如果系统没有相应的资源会导致haproxy无法启动,具体参考日志文件/var/log/messages。
3.4 基础配置详解¶
HAProxy的配置文件haproxy.cfg由两大部分组成,分别是global和proxies部分。
进程及安全配置相关的参数
性能调整相关参数
Debug参数
proxies:代理配置段
defaults
为frontend、backend、listen提供默认配置
frontend 前端,相当于nginx中的server配置块
backend: 后端,相当于nginx中的upstream配置块
listen: 同时拥有前端和后端配置
3.4.1 global配置参数¶
- chroot:锁定运行目录
- daemon:以守护进程运行
- stats socket:socket文件
- user, group, uid, gid:运行haproxy的用户身份
- nbproc:开启的haproxy进程数,与CPU保持一致
- nbthread:指定每个haproxy进程开启的线程数,默认为每个进程一个线程
- cpu-map:绑定haproxy进程至指定CPU
- maxconn:每个haproxy进程的最大并发连接数
- maxsslconn:每个haproxy进程ssl最大连接数
- maxconnrate:每个进程每秒创建的最大连接数量
- spread-checks:后端server状态check随机提前或延迟百分比时间
- pidfile:指定pid文件路径
- log:定义全局的syslog服务器
3.4.2 Proxies配置¶
官方文档: https://cbonte.github.io/haproxy-dconv/2.0/intro.html
# 默认配置项(可继承)
- 作用范围:对后续`frontend`/`backend`/`listen`生效
- 命名规则:可指定多个name或匿名配置
- 典型用途:定义超时时间、日志格式等通用参数
https://cbonte.github.io/haproxy-dconv/2.0/configuration.html#4
注意: name 字段智能使用"-",并且严格区分大小写,例如:web 和 Web 是完全不同的两组服务器.
3.4.2.1 Proxies配置-defaults¶
default 配置参数:
- option redispatch:当对应的服务器挂掉后,强制定向到其他健康的服务器
- option abortonclose:当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
- option http-keep-alive:开启与客户端的会话保持
- option forwardfor:透传客户端真实IP至后端web服务器
- mode http:设置默认工作类型
- timeout http-keep-alive:session会话保持超时时间
- timeout connect:客户端请求从haproxy到后端server的最长连接等待时间
- timeout server:客户端请求从haproxy到后端服务端的请求处理超时时长
- timeout client:设置haproxy与客户端的最长非活动时间
- timeout check:对后端服务器的默认检测超时时间
3.4.2.2 Proxies配置-frontend¶
frontend WEB_PORT_80
bind 192.168.7.248:80
mode http
use_backend web_prot_http_nodes
3.4.2.3 Proxies配置-backend¶
backend web_prot_http_nodes
mode http
option forwardfor
server 192.168.7.101 192.168.7.101:8080 check inter 3000 fall 3 rise 5
server 192.168.7.102 192.168.7.102:8080 check inter 3000 fall 3 rise 5
3.4.2.4 frontend+backend配置实例¶
#官网业务访问入口
frontend WEB_PORT_80
bind 192.168.7.248:80
mode http
use_backend web_prot_http_nodes
backend web_prot_http_nodes
mode http
option forwardfor
server 192.168.7.101 192.168.7.101:8080 check inter 3000 fall 3 rise 5
server 192.168.7.102 192.168.7.102:8080 check inter 3000 fall 3 rise 5
3.4.2.5 Proxies配置-listen替代frontend+backend¶
#官网业务访问入口
listen WEB_PORT_80
bind 192.168.7.102:80
mode http
option forwardfor
server web1 192.168.7.101:80 check inter 3000 fall 3 rise 5
server web2 192.168.7.101:80 check inter 3000 fall 3 rise 5
四:HAProxy调度算法¶
4.1 静态算法¶
4.1.1 static-rr¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance static-rr
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 2 check inter 3000 fall 2 rise 5
4.1.2 first¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance first
server web1 192.168.7.103:80 maxconn 2 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
4.2 动态算法¶
4.2.1 roundrobin¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance roundrobin
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 2 check inter 3000 fall 2 rise 5
4.2.2 leastconn¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance leastconn
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
4.3 其他算法¶
4.3.1 source¶
4.3.1.1 map-base取模法¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode tcp
log global
balance source
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
4.3.1.2 一致性hash¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode tcp
log global
balance source
hash-type consistent
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
4.3.2 uri¶
4.3.2.1 uri取模法配置示例¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance uri
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
4.3.2.2 uri一致性hash配置示例¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance uri
hash-type consistent
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
4.3.3 url_param¶
4.3.3.1 url_param取模法配置示例¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance url_param name,age
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
4.3.3.2 url_param一致性hash配置示例¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance url_param name,age
hash-type consistent
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
4.3.4 hdr¶
4.3.4.1 hdr取模法配置示例¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance hdr(User-Agent)
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
4.3.4.2 一致性hash配置示例¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance hdr(User-Agent)
hash-type consistent
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
4.3.5 rdp-cookie¶
4.3.5.1 rdp-cookie取模法配置示例¶
listen RDP
bind 192.168.7.101:3389
balance rdp-cookie
mode tcp
server rdp0 172.18.132.20:3389 check fall 3 rise 5 inter 2000 weight 1
4.3.5.2 rdp-cookie一致性hash配置示例¶
listen RDP
bind 172.31.7.201:3389
mode tcp
balance rdp-cookie
tcp-request inspect-delay 5s
tcp-request content accept if RDP_COOKIE
persist rdp-cookie
server win-server1 172.31.4.0:3389 check fall 3 rise 5 inter 2000 weight 1
4.3.6 random¶
4.3.6.1 random配置实例¶
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance random
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
4.4 IP透传¶
4.4.1 四层IP透传¶
listen web_prot_http_nodes
bind 192.168.7.101:80
mode tcp
balance roundrobin
server web1 blogs.studylinux.net:80 send-proxy check inter 3000 fall 3 rise 5
4.4.2 七层IP透传¶
4.4.2.1 HAProxy配置¶
defaults
option forwardfor
或者:
option forwardfor header X-Forwarded-xxx
五:高级功能及配置¶
介绍 Haproxy 高级配置及实用案例
5.1 基于cookie的会话保持¶
5.1.1 配置选项¶
- cookie name:用于实现持久连接的key名称
- insert:如果客户端请求报文没有cookie就插入新的cookie到响应报文
- indirect:不会向客户端发送服务器已经处理过请求的cookie信息
- nocache:当client和hapoxy之间有缓存时,haproxy不缓存客户端cookie
5.1.2 配置示例¶
listen web_host
bind 192.168.7.101:80
mode http
log global
balance roundrobin
cookie SERVER-COOKIE insert
server web1 192.168.7.103:80 indirect nocache cookie web1 check inter 3000 fall 3 rise 5
server web2 192.168.7.104:80 cookie web2 check inter 3000 fall 3 rise 5
5.2 HAProxy状态页¶
5.2.1 状态页配置项¶
- stats hide-version:隐藏版本
- stats refresh:设定自动刷新时间间隔
- stats uri:自定义stats page uri
- stats enable:基于默认的参数启用stats page
- stats realm:账户认证时的提示信息
- stats auth:认证时的账号和密码
- stats admin:启用stats page中的管理功能
5.2.2 启用状态页¶
listen stats
bind :9009
stats enable
stats uri /haproxy-status
stats realm HAPorxy\ Stats\ Page
stats auth haadmin:123456
stats auth admin:123456
5.3 报文修改¶
在http模式下,基于实际需求修改客户端的请求报文与响应报文:
# 在请求报文尾部添加指定首部
reqadd <string> [{if | unless} <cond>]
# 从请求报文中删除匹配正则表达式的首部
reqdel <search> [{if | unless} <cond>]
reqidel <search> [{if | unless} <cond>]
# 在响应报文尾部添加指定首部
rspadd <string> [{if | unless} <cond>]
# 从响应报文中删除匹配正则表达式的首部
rspidel <search> [{if | unless} <cond>]
5.4 HAProxy日志配置¶
5.4.1 HAProxy配置¶
global
log 127.0.0.1 local{1-7} info
5.4.2 Rsyslog配置¶
vim /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log
5.5 自定义日志格式¶
5.5.1 配置选项¶
capture cookie <name> len <length>
capture request header <name> len <length>
capture response header <name> len <length>
5.6 压缩功能¶
5.6.1 配置选项¶
compression algo gzip deflate
compression type text/plain text/html text/css text/xml text/javascript application/javascript
5.7 web服务器状态监测¶
5.7.1 三种状态监测方式¶
- 基于四层的传输端口做状态监测
- 基于指定URI做状态监测
- 基于指定URI的request请求头部内容做状态监测
5.8 ACL¶
5.8.1 ACL配置选项¶
acl <aclname> <criterion> [<value>] [flags] [operator]
5.9 自定义HAProxy错误界面¶
5.9.1 基于错误页面文件¶
errorfile 500 /usr/local/haproxy/html/500.html
errorfile 502 /usr/local/haproxy/html/502.html
errorfile 503 /usr/local/haproxy/html/503.html
5.9.2 基于http重定向¶
errorloc 503 http://192.168.7.102/error_page/503.html
5.10 HAProxy四层负载¶
5.10.1 四层负载示例¶
listen redis-port
bind 192.168.7.102:6379
mode tcp
balance leastconn
server server1 192.168.7.104:6379 check
server server1 192.168.7.103:6379 check backup
5.11 HAProxy https实现¶
5.11.1 证书制作¶
# mkdir /usr/local/haproxy/certs
# cd /usr/local/haproxy/certs
# openssl genrsa -out haproxy.key 2048
# openssl req -new -x509 -key haproxy.key -out haproxy.crt -subj "/CN=www.magedu.net"
# cat haproxy.key haproxy.crt > haproxy.pem
5.11.2 https配置示例¶
#web server http
frontend web_server-http
bind 192.168.7.101:80
redirect scheme https if !{ ssl_fc }
mode http
use_backend web_host
#web server https
frontend web_server-https
bind 192.168.7.101:443 ssl crt /usr/local/haproxy/certs/haproxy.pem
mode http
use_backend web_host
backend default_host
mode http
server web1 192.168.7.102:80 check inter 2000 fall 3 rise 5
backend web_host
mode http
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server web1 192.168.7.103:80 check inter 2000 fall 3 rise 5
server web2 192.168.7.104:80 check inter 2000 fall 3 rise 5
六:重点部分¶
6.1 HAProxy调度算法¶
HAProxy的调度算法分为静态和动态调度算法,但是有些算法可以根据参数在静态和动态算法中相互转换。
6.1.1 静态算法¶
静态算法:按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、链接数和响应速度等,且无法实时修改权重,只能靠重启HAProxy生效。
6.1.1.1 static-rr¶
基于权重的轮询调度,不支持权重的运行时调整及后端服务器慢启动,其后端主机数量没有限制。
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance static-rr
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 2 check inter 3000 fall 2 rise 5
6.1.1.2 first¶
根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务,因此会忽略服务器的权重设置。
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance first
server web1 192.168.7.103:80 maxconn 2 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
6.1.2 动态算法¶
动态算法:基于后端服务器状态进行调度适当调整,比如优先调度至当前负载较低的服务器,且权重可以在haproxy运行时动态调整无需重启。
6.1.2.1 roundrobin¶
基于权重的轮询动态调度算法,支持权重的运行时调整,不完全等于lvs中的rr轮训模式,HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),其每个后端backend中最多支持4095个real server,roundrobin为默认调度算法,且支持对real server权重动态调整。
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance roundrobin
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 2 check inter 3000 fall 2 rise 5
6.1.2.2 leastconn¶
加权的最少连接的动态,支持权重的运行时调整和慢启动,即当前后端服务器连接最少的优先调度(新客户端连接),比较适合长连接的场景使用,比如MySQL等场景。
listen web_host
bind 192.168.7.101:80,:8801-8810,192.168.7.101:9001-9010
mode http
log global
balance leastconn
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
6.2 动静分离与客户端源IP透传¶
6.2.1 动静分离¶
基于文件后缀名实现动静分离:
listen web_host
bind 192.168.7.101:80
mode http
balance roundrobin
log global
option httplog
acl php_server path_end -i .php
use_backend php_server_host if php_server
acl image_server path_end -i .jpg .png .jpeg .gif
use_backend image_server_host if image_server
default_backend default_web
backend php_server_host
mode http
server web1 192.168.7.103:80 check inter 2000 fall 3 rise 5
backend image_server_host
mode http
server web1 192.168.7.104:80 check inter 2000 fall 3 rise 5
backend default_web
mode http
server web1 192.168.7.102:80 check inter 2000 fall 3 rise 5
6.2.2 客户端源IP透传¶
6.2.2.1 四层IP透传¶
listen web_prot_http_nodes
bind 192.168.7.101:80
mode tcp
balance roundrobin
server web1 blogs.studylinux.net:80 send-proxy check inter 3000 fall 3 rise 5
6.2.2.2 七层IP透传¶
defaults
option forwardfor
listen web_host
bind 192.168.7.101:80
mode http
log global
balance random
server web1 192.168.7.103:80 weight 1 check inter 3000 fall 2 rise 5
server web2 192.168.7.104:80 weight 1 check inter 3000 fall 2 rise 5
6.3 ACL使用与报文修改¶
6.3.1 ACL基本配置¶
acl <aclname> <criterion> [flags] [operator] [<value>]
6.3.2 报文修改¶
# 在请求报文尾部添加指定首部
reqadd <string> [{if | unless} <cond>]
# 从请求报文中删除匹配正则表达式的首部
reqdel <search> [{if | unless} <cond>]
# 在响应报文尾部添加指定首部
rspadd <string> [{if | unless} <cond>]
# 从响应报文中删除匹配正则表达式的首部
rspidel <search> [{if | unless} <cond>]
6.4 服务器动态下线¶
编写shell脚本,实现能够基于参数传递real server服务器IP,并实现将其从多个HAProxy进程下线与上线:
#!/bin/bash
# 定义变量
HAPROXY_SOCKET1="/var/lib/haproxy/haproxy1.sock"
HAPROXY_SOCKET2="/var/lib/haproxy/haproxy2.sock"
SERVER_IP=$1
ACTION=$2
# 检查参数
if [ $# -ne 2 ]; then
echo "Usage: $0 <server_ip> <action>"
echo "Action: down/up"
exit 1
fi
# 执行操作
case $ACTION in
down)
echo "set server web_host/$SERVER_IP state maint" | socat stdio $HAPROXY_SOCKET1
echo "set server web_host/$SERVER_IP state maint" | socat stdio $HAPROXY_SOCKET2
;;
up)
echo "set server web_host/$SERVER_IP state ready" | socat stdio $HAPROXY_SOCKET1
echo "set server web_host/$SERVER_IP state ready" | socat stdio $HAPROXY_SOCKET2
;;
*)
echo "Invalid action. Use 'down' or 'up'"
exit 1
;;
esac