一、haproxy调度算法汇总
静态算法
1. static-rr
基于权重的轮询调度,不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它
值)及后端服务器慢启动,其后端主机数量没有限制,相当于LVS中的 wrr
listen web_server
bind 10.0.0.21:80
mode http
log global
balance static-rr
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
2. first
根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务,因此会忽略服务器的权重设置,此方式使用较少不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效
listen web_server
bind 10.0.0.21:80
mode http
log global
balance first
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
动态算法
1. roundrobin
基于权重的轮询动态调度算法,支持权重的运行时调整,不同于lvs中的rr轮训模式,HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),其每个后端backend中最多支持4095个real server,支持对real server权重动态调整,roundrobin为默认调度算法,此算法使用广泛
listen web_server
bind 10.0.0.21:80
mode http
log global
balance roundrobin
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
支持动态调整权重:
[root@node-01 haproxy]# echo "get weight web_host/web1" | socat stdio /var/lib/haproxy/haproxy.sock
1 (initial 1)
[root@node-01 haproxy]# echo "set weight web_host/web1 3" | socat stdio /var/lib/haproxy/haproxy.sock
[root@node-01 haproxy]# echo "get weight web_host/web1" | socat stdio /var/lib/haproxy/haproxy.sock
3 (initial 1)
2. leastconn
leastconn加权的最少连接的动态,支持权重的运行时调整和慢启动,即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户端连接),比较适合长连接的场景使用,比如:MySQL等场景。
listen web_server
bind 10.0.0.21:80
mode http
log global
balance leastconn
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
3. random
在1.9版本开始增加 random的负载平衡算法,其基于随机数作为一致性hash的key,随机负载平衡对于大型服务器场或经常添加或删除服务器非常有用,支持weight的动态调整,weight较大的主机有更大概率获取新请求
listen web_server
bind 10.0.0.21:80
mode http
log global
balance random
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
其它算法
1. source
源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一个后端web服务器。此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服务器,默认为静态方式,但是可以通过hash-type支持的选项更改.这个算法一般是在不插入Cookie的TCP模式下使用,也可给拒绝会话cookie的客户提供最好的会话粘性,适用于session会话保持但不支持cookie和缓存的场景。
源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是 取模法 和 一致性hash
1.1 map-base 取模法
对 source 地址进行 hash 计算,再基于服务器总权重的取模,最终结果决定将此请求转发至对应的后端服务器。此方法是静态的,即不支持在线调整权重,不支持慢启动,可实现对后端服务器均衡调度。缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因总权重发生变化而导致调度结果整体改变,hash-type 指定的默认值为此算法。
listen web_server
bind 10.0.0.21:80
mode http
log global
balance source
hash-type map-based
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
1.2 一致性hash
一致性哈希,当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动,该hash算法是动态的,支持使用 socat等工具进行在线权重调整,支持慢启动。
listen web_server
bind 10.0.0.21:80
mode http
log global
balance source
hash-type consistent
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
2. uri
基于对用户请求的URI的左半部分或整个uri做hash,再将hash结果对总权重进行取模后,根据最终结果将请求转发到后端指定服务器,适用于后端是缓存服务器场景,默认是静态算法,也可以通过 hashtype 指定 map-based 和 consistent,来定义使用取模法还是一致性hash。
注意:此算法基于应用层,所以只支持 mode http ,不支持 mode tcp
listen web_server
bind 10.0.0.21:80
mode http
log global
balance uri
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
uri 一致性hash配置示例
listen web_server
bind 10.0.0.21:80
mode http
log global
balance uri
hash-type consistent
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
3. url_param
url_param对用户请求的url中的 params 部分中的一个参数key对应的value值作hash计算,并由服务器总权重相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server,如果无没key,将按roundrobin算法
listen web_server
bind 10.0.0.21:80
mode http
log global
balance url_param userid
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
url_param一致性hash配置示例
listen web_server
bind 10.0.0.21:80
mode http
log global
balance url_param userid keyword
hash-type consistent
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
4. hdr
针对用户每个http头部(header)请求中的指定信息做hash,此处由 name 指定的http首部将会被取出并做hash计算,然后由服务器总权重取模以后派发至某挑出的服务器,如果无有效值,则会使用默认的轮询调度。
listen web_server
bind 10.0.0.21:80
mode http
log global
balance hdr(User-Agent)
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
5. rdp-cookie
rdp-cookie对远windows远程桌面的负载,使用cookie保持会话,默认是静态,也可以通过hash-type指定map-based和consistent,来定义使用取模法还是一致性hash。
rdp-cookie 取模法配置示例
listen RDP
bind 10.0.0.21:3389
balance rdp-cookie
mode tcp
server rdp0 10.0.0.200:3389 check fall 3 rise 5 inter 2000 weight 1
rdp-cookie 一致性hash配置
listen RDP_3389
bind 10.0.0.21:3389
balance rdp-cookie
hash-type consistent
mode tcp
server rdp0 10.0.0.200:3389 check fall 3 rise 5 inter 2000 weight 1
算法总结
静态
static-rr ---------> tcp/http #适用于做了session共享的web集群
first -------------> tcp/http
动态
roundrobin --------> tcp/http
leastconn ---------> tcp/http 多用于数据库调度
random ------------> tcp/http
以下静态和动态取决于hash_type是否consistent
source ------------> tcp/http
Uri --------------> http 缓存服务器,CDN服务商,蓝汛、百度、阿里云、腾讯
url_param ---------> http 可以实现session保持
hdr ---------------> http 基于客户端请求报文头部做下一步处理
rdp-cookie --------> tcp 基于Windows主机,很少使用
二、haproxy + nginx 实现四、七层IP透传
- 四层IP透传haproxy配置
listen web_server
bind 10.0.0.21:80
mode tcp
log global
balance roundrobin
server web1 10.0.0.101:80 send-proxy check inter 3000 fall 2 rise 5
server web2 10.0.0.102:80 send-proxy check inter 3000 fall 2 rise 5
- 四层IP透传nginx配置
server{
listen 80 proxy_protocol;
server_name www.tuntun.net;
charset utf-8;
}
- nginx日志格式配置
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
访问haprox主机 http://10.0.0.21/,可在nginx日志查看到透传IP
4、七层IP透传haproxy配置
listen web_server
bind 10.0.0.21:80
mode http
log global
balance roundrobin
option forwardfor
server web1 10.0.0.101:80 check inter 3s fall 2 rise 5
server web2 10.0.0.102:80 check inter 3s fall 2 rise 5
- 七层IP透传nginx配置
server{
listen 80;
server_name www.tuntun.net;
charset utf-8;
}