一、负载均衡核心概念
1.1 负载均衡定义
负载均衡(Load Balance,简称LB)是一种基于硬件设备或软件服务的高可用反向代理技术。它将特定业务(如Web服务、网络流量)分发到后端的一个或多个服务器/设备,从而提升业务并发处理能力、保障高可用性,并支持业务水平动态扩展。
阿里云SLB介绍 :SLB技术原理浅析-阿里云开发者社区
1.2 负载均衡的核心价值
-
动态水平扩展:Web服务器扩容对用户无感知
-
突破单点瓶颈:提升业务并发处理能力
-
节约公网IP:降低IT成本支出
-
隐藏服务器IP:增强内部服务器安全性
-
配置简单:固定格式的配置文件
-
功能丰富:支持四层/七层代理、动态下线主机
-
高性能:支撑数万至数十万并发
1.3 负载均衡类型
1.3.1 硬件负载均衡
-
、F5(美国F5网络公司)
-
Netscaler(美国思杰公司)
-
Array(华耀科技)
-
AD-1000(深信服)
1.3.2 四层负载均衡(L4)
-
基于
IP+Port
转发流量 -
流量请求进行NAT处理后转发
-
记录TCP/UDP连接与服务器的映射关系
常用软件: -
LVS(重量级)
-
Nginx(轻量级,通过
upstream
模块实现) -
HAProxy(模拟四层转发)
-
1.3.3 七层负载均衡(L7)
-
通过虚拟URL或主机IP识别流量,解析应用层信息
-
代理客户端与后端服务器建立连接(如Nginx双向TCP连接)
-
常用软件:
-
Nginx(基于HTTP协议,通过
proxy_pass
实现) -
HAProxy(支持会话保持、路径转移等高级功能)
-
1.3.4 四层与七层的核心区别
特性 | 四层负载均衡 | 七层负载均衡 |
---|---|---|
工作层级 | 传输层及以下 | 应用层及以下 |
性能 | 更高(无需解析报文内容) | 较低(需解析应用层数据) |
原理 | 基于IP+Port | 基于虚拟URL/主机IP |
功能类比 | 类似路由器 | 类似代理服务器 |
安全性 | 无法防御DDoS攻击 | 可防御SYN Cookie/Flood攻击 |
二、HAProxy深度解析
HAProxy由Willy Tarreau于2000年使用C语言开发,具备以下特性:
-
高性能TCP/HTTP负载均衡(支持万级并发)
-
支持Cookie持久化、自动故障切换
-
兼容正则表达式及Web状态统计
-
官方资源:
-
企业版:https://www.haproxy.com
-
社区版:http://www.haproxy.org
-
GitHub:https://github.com/haprox
-
企业版 vs 社区版功能对比
三、HAProxy安装与配置详解
3.1.实验环境
3.2.软件安装
软件包下载地址
https://github.com/haproxy/wiki/wiki/Packages
安装软件包:
haproxy ~]# dnf install haproxy -y
查看版本
haproxy软件基本信息
软件安装包: haproxy-2.4.22-3.el9_3.x86_64.rpm
启动文件: /lib/systemd/system/haproxy.service
主配置目录: /etc/haproxy/
主配置文件: /etc/haproxy/haproxy.cfg
子配置目录: /etc/haproxy/conf.d
3.3.haproxy的基本配置信息
官方文档:http://cbonte.github.io/haproxy-dconv/
HAProxy 的配置文件haproxy.cfg由两大部分组成,分别是:
global:全局配置段
进程及安全配置相关的参数
性能调整相关参数
Debug参数
proxies:代理配置段
defaults:为frontend, backend, listen提供默认配置
frontend:前端,相当于nginx中的server {}
backend:后端,相当于nginx中的upstream {}
listen:同时拥有前端和后端配置,配置简单,生产推荐使用
3.3.1.global配置
3.3.1.1 global 配置参数说明
global log 127.0.0.1 local2 #定义全局的syslog服务器;日志服务器需要开启UDP协 议,最多可以定义两个
chroot /var/lib/haproxy #锁定运行目录
pidfile /var/run/haproxy.pid #指定pid文件
maxconn 100000 #指定最大连接数
user haproxy #指定haproxy的运行用户
group haproxy #指定haproxy的运行组
daemon #指定haproxy以守护进程方式运行
# turn on stats unix socket
stats socket /var/lib/haproxy/stats #指定haproxy的套接字文件
nbproc 2 #指定haproxy的work进程数量,默认是1个
cpu-map 1 0 #指定第一个work绑定第一个cpu核心
cpu-map 2 1 #指定第二个work绑定第二个cpu核心
nbthread 2 #指定haproxy的线程数量,默认每个进程一个线 程,此参数与nbproc互斥
maxsslconn 100000 #每个haproxy进程ssl最大连接数,用于haproxy 配置了证书的场景下
maxconnrate 100 #指定每个客户端每秒建立连接的最大数量
3.3.1 global全局配置
globallog 127.0.0.1 local2 # 定义syslog服务器chroot /var/lib/haproxy # 锁定运行目录pidfile /var/run/haproxy.pid # 指定PID文件maxconn 100000 # 最大连接数user haproxy # 运行用户group haproxy # 运行组daemon # 守护进程模式stats socket /var/lib/haproxy/stats # 状态套接字文件nbproc 2 # 工作进程数cpu-map 1 0 # 进程1绑定CPU0cpu-map 2 1 # 进程2绑定CPU1nbthread 2 # 每个进程的线程数maxsslconn 100000 # 每进程SSL最大连接数maxconnrate 100 # 每秒最大新建连接数
3.3.1.2 多进程和线程
多进程和socket文件配置如下:
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...globallog 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 100000user haproxygroup haproxydaemon# turn on stats unix socketstats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1 # 启用多个sock文件stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2nbproc 2 #启用多进程cpu-map 1 0 #进程和cpu核心绑定防止cpu抖动从而减少系统资源消耗cpu-map 2 1 #2 表示第二个进程,1表示第二个cpu核心...下面内容省略 ..
查看多进程信息
haproxy haproxy]# pstree -p | grep haproxy
|-haproxy(4816)-+-haproxy(4820)
| `-haproxy(4821)
启用多线程
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...globallog 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 100000user haproxygroup haproxydaemon# turn on stats unix socketstats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1 # 启用多个sock文件stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2nbproc 2 #启用多进程cpu-map 1 0 #进程和cpu核心绑定防止cpu抖动从而减少系统资源消耗cpu-map 2 1 #2 表示第二个进程,1表示第二个cpu核心nbthread 2 #启用多线程...下面内容省略 ..
多线程对比
未开启多线程
haproxy ~]# cat /proc/xxxx(haproxy子进程id)/status
...上面内容省略...
Threads: 1
...下面内容省略...
开启后
haproxy ~]# cat /proc/xxxx(haproxy子进程id)/status
...上面内容省略...
Threads: 2
...下面内容省略...
3.3.2 proxies配置
3.3.2.1.proxies参数说明proxies
name字段只能使用大小写字母,数字,‘-’(dash),'_‘(underscore),'.' (dot)和 ':'(colon),并且严格 区分大小写
3.3.2.2 Proxies配置-defaults
3.3.2.3 Proxies配置-frontend
frontend 配置参数:
bind:指定HAProxy的监听地址,可以是IPV4或IPV6,可以同时监听多个IP或端口,可同时用于listen字 段中
#格式:
bind [<address>]:<port_range> [, ...] [param*]
#注意:如果需要绑定在非本机的IP,需要开启内核参数:net.ipv4.ip_nonlocal_bind=1
backlog #针对所有server配置,当前端服务器的连接数达到上限后的后援队列长度,注意:不 支持backend
frontend 配置示例:
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...frontend lee-webserver-80bind 172.25.254.100:80mode httpuse_backend lee-webserver-80-RS #调用backend的名称...下面内容省略...
3.3.2.4 Proxies配置-backend
定义一组后端服务器,backend服务器将被frontend进行调用。
注意: backend 的名称必须唯一,并且必须在listen或frontend中事先定义才可以使用,否则服务无法 启动
mode http|tcp #指定负载协议类型,和对应的frontend必须一致
option #配置选项
server #定义后端real server,必须指定IP和端口
注意:option后面加 httpchk,smtpchk,mysql-check,pgsql-check,ssl-hello-chk方法,可用于实现更 多应用层检测功能。
server 配置
代码示例:
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...backend lee-webserver-80-RSmode httpserver web1 192.168.0.101:80 check inter 3s fall 3 rise 5server web2 192.168.0.102:80 check inter 3s fall 3 rise 5...上面内容省略...
测试效果:
3.3.2.5 Proxies配置-listen 简化配置
使用listen替换 frontend和backend的配置方式,可以简化设置,通常只用于TCP协议的应用 listen配置示例:
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode httpoption forwardforserver webserver1 192.168.0.101:80 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 check inter 3s fall 3 rise 5...上面内容省略...
3.4.socat 工具
对服务器动态权重和其它状态可以利用 socat工具进行调整,Socat 是 Linux 下的一个多功能的网络工 具,名字来由是Socket CAT,相当于netCAT的增强版.Socat 的主要特点就是在两个数据流之间建立双向 通道,且支持众多协议和链接方式。如 IP、TCP、 UDP、IPv6、Socket文件等
范例:利用工具socat 对服务器动态权重调整
常用示例:
#查看haproxy状态
[root@haproxy ~]# echo "show info" | socat stdio /var/lib/haproxy/statsName: HAProxyVersion: 2.4.22-f8e3218Release_date: 2023/02/14Nbthread: 1Nbproc: 1Process_num: 1Pid: 33542Uptime: 0d 0h03m43sUptime_sec: 223Memmax_MB: 0PoolAlloc_MB: 0 #查看集群状态
[root@haproxy ~]# echo "show servers state" | socat stdio /var/lib/haproxy/stats 1# be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweightsrv_iweight srv_time_since_last_change srv_check_status srv_check_resultsrv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_idsrv_fqdn srv_port srvrecord srv_use_ssl srv_check_port srv_check_addrsrv_agent_addr srv_agent_port2 webcluster 1 web1 172.25.254.20 2 0 2 2 188 6 3 7 6 0 0 0 - 80 - 0 0 - - 02 webcluster 2 web2 172.25.254.30 2 0 1 1 188 6 3 7 6 0 0 0 - 80 - 0 0 - - 04 static 1 static 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 4331 - 0 0 - - 05 app 1 app1 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 5001 - 0 0 - - 05 app 2 app2 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 5002 - 0 0 - - 05 app 3 app3 127.0.0.1 0 0 1 1 186 8 2 0 6 0 0 0 - 5003 - 0 0 - - 05 app 4 app4 127.0.0.1 0 0 1 1 186 8 2 0 6 0 0 0 - 5004 - 0 0 - - 0
[root@haproxy ~]# echo "show servers state" | socat stdio /var/lib/haproxy/stats 1# be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweightsrv_iweight srv_time_since_last_change srv_check_status srv_check_resultsrv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_idsrv_fqdn srv_port srvrecord srv_use_ssl srv_check_port srv_check_addrsrv_agent_addr srv_agent_port2 webcluster 1 web1 172.25.254.20 2 0 2 2 188 6 3 7 6 0 0 0 - 80 - 0 0 - - 02 webcluster 2 web2 172.25.254.30 2 0 1 1 188 6 3 7 6 0 0 0 - 80 - 0 0 - - 04 static 1 static 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 4331 - 0 0 - - 05 app 1 app1 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 5001 - 0 0 - - 05 app 2 app2 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 5002 - 0 0 - - 05 app 3 app3 127.0.0.1 0 0 1 1 186 8 2 0 6 0 0 0 - 5003 - 0 0 - - 05 app 4 app4 127.0.0.1 0 0 1 1 186 8 2 0 6 0 0 0 - 5004 - 0 0 - - 0
#查看集群权重
[root@haproxy ~]# echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats2 (initial 2)[root@haproxy ~]# echo get weight webcluster/web2 | socat stdio /var/lib/haproxy/stats1 (initial 1)
#设置权重
[root@haproxy ~]# echo "set weight webcluster/web1 1 " | socat stdio /var/lib/haproxy/stats[root@haproxy ~]# echo "set weight webcluster/web1 2 " | socat stdio /var/lib/haproxy/stats
下线后端服务器
[root@haproxy ~]# echo "disable server webserver_80/webserver1 " | socat stdio /var/lib/haproxy/stats
#上线后端服务器
[root@haproxy ~]# echo "enable server webserver_80/webserver1 " | socat stdio /var/lib/haproxy/stats
针对多进程处理方法
如果开启多进程那么我们在对进程的sock文件进行操作时其对进程的操作时随机的
如果需要指定操作进程那么需要用多soct文件方式来完成
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2nbproc 2cpu-map 1 0cpu-map 2 1...上面内容省略..
这样每个进程就会有单独的sock文件来进行单独管理
[root@haproxy ~]# ll /var/lib/haproxy/总用量 0srw------- 1 root root 0 8月 8 13:43 statssrw------- 1 root root 0 8月 8 13:46 stats1srw------- 1 root root 0 8月 8 13:46 stats2
3.5 haproxy的状态界面
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen statsmode httpbind 0.0.0.0:8888stats enablelog globalstats uri /haproxy-statusstats auth lee:lee...下面内容省略...
四.haproxy的算法
HAProxy通过固定参数 balance 指明对后端服务器的调度算法
balance参数可以配置在listen或backend选项中。
HAProxy的调度算法分为静态和动态调度算法
有些算法可以根据参数在静态和动态算法中相互转换。
4.1 静态算法
静态算法:按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、连接数和响应速度 等,且无法实时修改权重(只能为0和1,不支持其它值),只能靠重启HAProxy生效。
4.1.1 static-rr:基于权重的轮询调度
不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)
不支持端服务器慢启动
其后端主机数量没有限制,相当于LVS中的 wrr
慢启动是指在服务器刚刚启动上不会把他所应该承担的访问压力全部给它,而是先给一部分,当没 问题后在给一部分
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode httpbalance static-rrserver webserver1 192.168.0.101:80 weight 2 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
4.1.2 first
根据服务器在列表中的位置,自上而下进行调度
其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务
其会忽略服务器的权重设置
不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效
示例:
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rrbalance firstserver webserver1 192.168.0.101:80 weight 2 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
测试效果:
4.2 动态算法
动态算法
基于后端服务器状态进行调度适当调整,
新请求将优先调度至当前负载较低的服务器
权重可以在haproxy运行时动态调整无需重启
4.2.1 roundrobin
1. 基于权重的轮询动态调度算法,
2. 支持权重的运行时调整,不同于lvs中的rr轮训模式,
3. HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),
4. 其每个后端backend中最多支持4095个real server,
5. 支持对real server权重动态调整,
6. roundrobin为默认调度算法,此算法使用广泛
示例:
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance firstbalance roundrobinserver webserver1 192.168.0.101:80 weight 2 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
动态调整权重
[root@haproxy ~]# echo "set weight webserver_80/webserver1 2" | socat stdio /var/lib/haproxy/haproxy.sock
测试效果:
4.2.2 leastconn
leastconn加权的最少连接的动态
支持权重的运行时调整和慢启动,即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户 端连接)
比较适合长连接的场景使用,比如:MySQL等场景。
示例:
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobinbalance leastconnserver webserver1 192.168.0.101:80 weight 2 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
4.3 其他算法
其它算法即可作为静态算法,又可以通过选项成为动态算法
4.3.1 source
源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一 个后端web服务器。此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服 务器,默认为静态方式,但是可以通过hash-type支持的选项更改这个算法一般是在不插入Cookie的TCP 模式下使用,也可给拒绝会话cookie的客户提供最好的会话粘性,适用于session会话保持但不支持 cookie和缓存的场景源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法 和一致性hash
示例:
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconnbalance sourceserver webserver1 192.168.0.101:80 weight 2 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
测试:
[root@node10 ~]# for N in {1..6}; do curl 172.25.254.100; doneRS1 server - 192.168.0.102RS1 server - 192.168.0.102RS1 server - 192.168.0.102RS1 server - 192.168.0.102RS1 server - 192.168.0.102RS1 server - 192.168.0.102
如果访问客户端时一个家庭,那么所有的家庭的访问流量都会被定向到一台服务器,这时source算 法的缺陷
4.3.1.1 map-base 取模法
map-based:取模法,对source地址进行hash计算,再基于服务器总权重的取模,最终结果决定将此请 求转发至对应的后端服务器。
此方法是静态的,即不支持在线调整权重,不支持慢启动,可实现对后端服务器均衡调度
缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因总权重发生变化而导致调度结果 整体改变
hash-type 指定的默值为此算法
所谓取模运算,就是计算两个数相除之后的余数,10%7=3, 7%4=3
map-based算法:基于权重取模,hash(source_ip)%所有后端服务器相加的总权重
比如当源hash值时1111,1112,1113,三台服务器a b c的权重均为1,
即abc的调度标签分别会被设定为 0 1 2(1111%3=1,1112%3=2,1113%3=0)
1111 ----- > nodeb
1112 ------> nodec
1113 ------> nodea
如果a下线后,权重数量发生变化
1111%2=1,
1112%2=0,
1113%2=1
1112和1113被调度到的主机都发生变化,这样会导致会话丢失
取模法配置示例:
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconn#balance sourcebalance sourceserver webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
#不支持动态调整权重值
[root@haproxy ~]# echo "set weight webserver_80/webserver1 2" | socat stdio /var/lib/haproxy/haproxy.sockBackend is using a static LB algorithm and only accepts weights '0%' and '100%'.
#只能动态上线和下线
[root@haproxy ~]# echo "set weight webserver_80/webserver1 0" | socat stdio /var/lib/haproxy/haproxy.sock[root@haproxy ~]# echo "get weight webserver_80/webserver1" | socat stdio /var/lib/haproxy/haproxy.sock0 (initial 1)
4.3.1.2
一致性hash 一致性哈希,当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动hash(o) mod n
该hash算法是动态的,支持使用 socat等工具进行在线权重调整,支持慢启动
算法:
1、后端服务器哈希环点keyA=hash(后端服务器虚拟ip)%(2^32)
2、客户机哈希环点key1=hash(client_ip)%(2^32) 得到的值在[0---4294967295]之间,
3、将keyA和key1都放在hash环上,将用户请求调度到离key1最近的keyA对应的后端服务器
hash环偏斜问题
增加虚拟服务器IP数量,比如:一个后端服务器根据权重为1生成1000个虚拟IP,再hash。而后端服务器权 重为2则生成2000的虚拟IP,再bash,最终在hash环上生成3000个节点,从而解决hash环偏斜问题
hash对象
Hash对象到后端服务器的映射关系:
一致性hash示意图
后端服务器在线与离线的调度方式
一致性hash配置示例
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconn#balance sourcebalance sourcehash-type consistentserver webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
4.3.2 uri
基于对用户请求的URI的左半部分或整个uri做hash,再将hash结果对总权重进行取模后 根据最终结果将请求转发到后端指定服务器
适用于后端是缓存服务器场景
默认是静态算法,也可以通过hash-type指定map-based和consistent,来定义使用取模法还是一致性 hash
注意:此算法基于应用层,所以只支持 mode http ,不支持 mode tcp
<scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>
左半部分:/<path>;<params>
整个uri:/<path>;<params>?<query>#<frag>
4.3.2.1 uri 取模法配置示例
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconn#balance source#balance sourcebalance uriserver webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
4.3.2.2 uri 一致性hash配置示例
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconn#balance source#balance sourcebalance urihash-type consistentserver webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
访问测试:
访问不同的uri,确认可以将用户同样的请求转发至相同的服务器
[root@rs1 ~]# echo RS1 192.168.0.101 index1 > /var/www/html/index1.html[root@rs1 ~]# echo RS1 192.168.0.101 index2 > /var/www/html/index2.html[root@rs1 ~]# echo RS1 192.168.0.101 index3 > /var/www/html/index3.html[root@rs2 ~]# echo RS1 192.168.0.102 index1 > /var/www/html/index1.html[root@rs2 ~]# echo RS1 192.168.0.102 index2 > /var/www/html/index2.html[root@rs2 ~]# echo RS1 192.168.0.102 index3 > /var/www/html/index3.html
[root@node10 ~]# curl 172.25.254.100/index.htmlRS2 server - 192.168.0.102[root@node10 ~]# curl 172.25.254.100/index1.htmlRS1 192.168.0.101 index1[root@node10 ~]# curl 172.25.254.100/index2.htmlRS1 192.168.0.102 index2[root@node10 ~]# curl 172.25.254.100/index3.htmlRS1 192.168.0.101 index3
4.3.3 url_param
url_param对用户请求的url中的 params 部分中的一个参数key对应的value值作hash计算,并由服务器 总权重相除以后派发至某挑出的服务器,后端搜索同一个数据会被调度到同一个服务器,多用与电商
通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server
如果无没key,将按roundrobin算法
#假设: url = http://www.timinglee.com/foo/bar/index.php?key=value
#则: host = "www.timinglee.com"
url_param = "key=value"
4.3.3.1 url_param取模法配置示例
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconn#balance source#balance source#balance uribalance url_param name,userid #支持对多个url_param hashtserver webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
4.3.3.2 url_param一致性hash配置示例
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconn#balance source#balance source#balance uribalance url_param name,userid #支持对多个url_param hashthash-type consistentserver webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
测试访问
[root@node10 ~]# curl 172.25.254.100/index.html?name=timingleeRS1 192.168.0.101 index3[root@node10 ~]# curl 172.25.254.100/index.html?name=timingleeRS1 192.168.0.101 index3[root@node10 ~]# curl 172.25.254.100/index.html?userid=leeRS1 192.168.0.102 index3[root@node10 ~]# curl 172.25.254.100/index.html?userid=leeRS1 192.168.0.102 index3
4.3.6 算法总结
#静态
static-rr--------->tcp/http
first------------->tcp/http
#动态
roundrobin-------->tcp/http
leastconn--------->tcp/http
random------------>tcp/http
#以下静态和动态取决于hash_type是否consistent
source------------>tcp/http
Uri--------------->http
url_param--------->http
hdr--------------->http
4.3.7 各算法使用场景
first #使用较少
static-rr #做了session共享的web集群
roundrobin random leastconn #数据库
source
#基于客户端公网IP的会话保持
Uri--------------->http #缓存服务器,CDN服务商,蓝汛、百度、阿里云、腾讯
url_param--------->http #可以实现session保持
hdr #基于客户端请求报文头部做下一步处理