1、搭建haproxy的监控页面

环境是基于上篇文章的环境,环境参考实现Haproxy来代理后端的服务这篇文章。

root@haproxy:~# vim /etc/haproxy/haproxy.cfg	#主配置文件
global                                                                                                                                                                                                          
    maxconn 100000
    chroot /apps/haproxy
    stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
    user haproxy
    group haproxy
    daemon
    pidfile /var/lib/haproxy/haproxy.pid
    log 127.0.0.1 local2 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  admin:wm521314

#后端服务的配置我放到子配置了
root@haproxy:~# vim /etc/haproxy/conf.d/zg_web_80.cfg 
listen zg_web_80                                                                                                                                                                                                
    bind 10.0.0.100:80
    mode http
    server web1 10.0.0.101:80 check inter 3000 fall 2 rise 5
    server web2 10.0.0.102:80 check inter 3000 fall 2 rise 5

web服务配置:
#这里我就简单配置一下了,就是测试一下。
root@web1:~# apt -y install nginx ;systemctl enable --now nginx;echo "Welcome to `hostname -I` page" > /var/www/html/index.nginx-debian.html
root@web2:~# apt -y install nginx ;systemctl enable --now nginx;echo "Welcome to `hostname -I` page" > /var/www/html/index.nginx-debian.html

image.png image.png 在浏览器中访问http://10.0.0.100:9999/haproxy-status,在按照配置文件配置的授权登陆的账号信息进行登陆就可以进入到haproxy的监控页面。 image.png image.png

2、Haproxy的调度算法

HAProxy的调度算法分为静态和动态调度算法,但是有些算法可以根据参数在静态和动态算法中相互转换;HAProxy通过固定参数balance指明对后端服务器的调度算法,该参数可以配置在listen或backend选项中。 官方文档: https://cbonte.github.io/haproxy-dconv/2.4/configuration.html#4-balance

2.1、静态算法

静态算法:按照事先定义好的规则轮询进行调度,不关心后端服务器的当前负载、连接数和响应速度等,且无法实时动态修改权重(只能为0和1,不支持其它值)或者修改后不生效,如果需要修改只能靠重启HAProxy生效。

2.1.1、socat工具

这个工具是对服务器的权重和其他状态进行调整的,socat是linux下的一个多功能的网络工具。

root@haproxy:~# apt -y install socat
#查看帮助
root@haproxy:~# socat -h
root@haproxy:~# echo "help" | socat stdio /var/lib/haproxy/haproxy.sock
root@haproxy:~# echo "show info" | socat stdio /var/lib/haproxy/haproxy.sock
#下线服务
root@haproxy:~# echo "set weight zg_web_80/web1 0" | socat stdio /var/lib/haproxy/haproxy.sock
#上线服务
root@haproxy:~# echo "set weight zg_web_80/web1 1" | socat stdio /var/lib/haproxy/haproxy.sock

2.1.2、static-rr

static-rr:基于权重的轮询调度,不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)及后端服务器慢启动,其后端主机数量没有限制,相当于LVS中的wrr。 示例:

root@haproxy:~# vim /etc/haproxy/conf.d/zg_web_80.cfg
listen zg_web_80
    bind 10.0.0.100:80
    mode http
    balance static-rr
    server web1 10.0.0.101:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 10.0.0.102:80 weight 2 check inter 3000 fall 2 rise 5
root@haproxy:~# systemctl reload haproxy

#验证访问
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page

image.png

2.1.3、first

first:根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务,因此会忽略服务器的权重设置,此方式使用较少;不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效。 示例:

maxconn <maxconn>	#当前后端server的最大并发连接数
root@haproxy:~# vim /etc/haproxy/conf.d/zg_web_80.cfg
listen zg_web_80
    bind 10.0.0.100:80
    mode http
    balance first
    server web1 10.0.0.101:80 maxconn 1 weight 1 check inter 3000 fall 2 rise 5                                                                                                                                 
    server web2 10.0.0.102:80 weight 1 check inter 3000 fall 2 rise 5
root@haproxy:~# systemctl reload haproxy

#测试访问
使用这个调度算法的时候在加上maxconn的时候是要达到设置的最大连接数才会往其他的服务上调度。
root@web2:~# while true;do curl http://10.0.0.100/;sleep 1;done
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page

2.2、动态算法

动态算法是基于后端服务器状态进行适当的调度,新的请求将优先调度到当前负载较低的服务器,且权重可以在haproxy运行时动态调整无需重启服务。

2.2.1、roundrobin

roundrobin是基于权重的动态轮询调度算法,支持权重的运行时调整,不同于lvs中的rr轮询默认,haproxy中roundrobin支持慢启动(新加的服务器会逐渐增加转发数),其每个后端backend中最多支持4095个real server,支持对real server权重动态调整,roundrobin为默认调度算法,此算法使用广泛。 示例:

root@haproxy:~# vim /etc/haproxy/conf.d/zg_web_80.cfg 
listen zg_web_80
    bind 10.0.0.100:80
    mode http
    balance roundrobin                                                                                                                                                                                          
    server web1 10.0.0.101:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 10.0.0.102:80 weight 1 check inter 3000 fall 2 rise 5
root@haproxy:~# systemctl reload haproxy

#验证访问
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page

#这个支持动态调整权重
root@haproxy:~# echo "get weight zg_web_80/web1" | socat stdio /var/lib/haproxy/haproxy.sock
1 (initial 1)

root@haproxy:~# echo "set weight zg_web_80/web1 3" | socat stdio /var/lib/haproxy/haproxy.sock

root@haproxy:~# echo "get weight zg_web_80/web1" | socat stdio /var/lib/haproxy/haproxy.sock
3 (initial 1)

image.png

2.2.2、leastconn

leastconn加权的最少连接的动态,支持权重的运行时调整和慢启动,即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户端连接),比较适合长连接的场景使用,比如:MySQL等场景。 示例:

root@haproxy:~# vim /etc/haproxy/conf.d/zg_web_80.cfg
listen zg_web_80
    bind 10.0.0.100:80
    mode http
    balance leastconn
    server web1 10.0.0.101:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 10.0.0.102:80 weight 1 check inter 3000 fall 2 rise 5
root@haproxy:~# systemctl reload haproxy

#验证访问
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page、

#这个调度算法也支持动态修改权重
root@haproxy:~# echo "get weight zg_web_80/web2" | socat stdio /var/lib/haproxy/haproxy.sock
1 (initial 1)

root@haproxy:~# echo "set weight zg_web_80/web2 2" | socat stdio /var/lib/haproxy/haproxy.sock

root@haproxy:~# echo "get weight zg_web_80/web2 2" | socat stdio /var/lib/haproxy/haproxy.sock
2 (initial 1)

image.png

2.2.3、random

在1.9版本开始增加random的负载平衡算法,其基于随机数作为一致性hash的key,随机负载平衡对于大型服务器场或经常添加或删除服务器非常有用,支持weight的动态调整,weight较大的主机有更大概率获取新请求。 示例:

root@haproxy:~# vim /etc/haproxy/conf.d/zg_web_80.cfg
listen zg_web_80
    bind 10.0.0.100:80
    mode http
    balance random                                                                                                                                                                                              
    server web1 10.0.0.101:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 10.0.0.102:80 weight 1 check inter 3000 fall 2 rise 5
root@haproxy:~# systemctl reload haproxy

#验证访问
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page

2.3、其他算法

其它算法即可作为静态算法,又可以通过选项成为动态算法。

2.3.1、source

  • 源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一个后端web服务器。此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服务器,默认为静态方式,但是可以通过hash-type支持的选项更改。
  • 这个算法一般是在不插入Cookie的TCP模式下使用,也可给拒绝会话cookie的客户提供最好的会话粘性,适用于session会话保持但不支持cookie和缓存的场景。
  • 源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法和一致性hash

map-base取模法 map-based:取模法,对source地址进行hash计算,再基于服务器总权重的取模,最终结果决定将此请求转发至对应的后端服务器。此方法是静态的,即不支持在线调整权重,不支持慢启动,可实现对后端服务器均衡调度。缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因总权重发生变化而导致调度结果整体改变,hash-type指定的默认值为此算法。

所谓取模运算,就是计算两个数相除之后的余数,10%7=3, 7%4=3 map-based算法:基于权重取模,hash(source_ip)%所有后端服务器相加的总权重

示例:

root@haproxy:~# vim /etc/haproxy/conf.d/zg_web_80.cfg
listen zg_web_80
    bind 10.0.0.100:80
    mode http
    balance source
    hash-type map-based                                                                                                                                                                                             
    server web1 10.0.0.101:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 10.0.0.102:80 weight 1 check inter 3000 fall 2 rise 5
root@haproxy:~# systemctl reload haproxy.service

#验证访问
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.102  page

一致性hash算法 一致性哈希,当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动,hash(o)mod n ,该hash算法是动态的,支持使用 socat等工具进行在线权重调整,支持慢启动。 算法:

1、key1=hash(source_ip)%(2^32) [0---4294967295] 2、keyA=hash(后端服务器虚拟ip)%(2^32) 3、将key1和keyA都放在hash环上,将用户请求调度到离key1最近的keyA对应的后端服务器

hash环偏斜的问题

增加虚拟服务器IP数量,比如:一个后端服务器根据权重为1生成1000个虚拟IP,再hash。而后端服务器权重为2则生成2000的虚拟IP,再bash,最终在hash环上生成3000个节点,从而解决hash环偏斜问题。

一致性hash示意图:后端服务器在线与离线的调度方式 image.png 示例:

root@haproxy:~# vim /etc/haproxy/conf.d/zg_web_80.cfg
listen zg_web_80
    bind 10.0.0.100:80
    mode http
    balance source
    hash-type consistent                                                                                                                                                                                        
    server web1 10.0.0.101:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 10.0.0.102:80 weight 1 check inter 3000 fall 2 rise 5
root@haproxy:~# systemctl reload haproxy.service

#验证访问
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/
Welcome to 10.0.0.101  page

2.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>

image.png 示例:

#在web服务器上准备一些测试文件
root@web1:~# for i in {1..6};do echo "test$i on `hostname -I`" > /var/www/html/test$i.html;done
root@web1:~# ls /var/www/html/
index.html  test1.html  test2.html  test3.html  test4.html  test5.html  test6.html

root@web2:~# for i in {1..6};do echo "test$i on `hostname -I`" > /var/www/html/test$i.html;done
root@web2:~# ls /var/www/html/
index.html  test1.html  test2.html  test3.html  test4.html  test5.html  test6.html

#配置基于uri的一致性的hash
root@haproxy:~# vim /etc/haproxy/conf.d/zg_web_80.cfg
listen zg_web_80
    bind 10.0.0.100:80
    mode http
    balance uri                                                                                                                                                                                                 
    hash-type consistent
    server web1 10.0.0.101:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 10.0.0.102:80 weight 1 check inter 3000 fall 2 rise 5
root@haproxy:~# systemctl reload haproxy.service

#测试访问不同的uri,查看结果
root@node4:~# curl http://10.0.0.100/test1.html
test1 on 10.0.0.101 
root@node4:~# curl http://10.0.0.100/test1.html
test1 on 10.0.0.101 
root@node4:~# curl http://10.0.0.100/test3.html
test3 on 10.0.0.102 
root@node4:~# curl http://10.0.0.100/test3.html
test3 on 10.0.0.102 
root@node4:~# curl http://10.0.0.100/test5.html
test5 on 10.0.0.101 
root@node4:~# curl http://10.0.0.100/test5.html
test5 on 10.0.0.101

2.3.3、url_param

url_param对用户请求的url中的 params 部分中的一个参数key对应的value值作hash计算,并由服务器总权重相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server,如果无没key,将按roundrobin算法。

#假设: url = http://www.stars.com/foo/bar/index.php?key=value #则: host = "www.stars.com" url_param = "key=value"

示例:

#基于url_param一致性的hash配置
root@haproxy:~# vim /etc/haproxy/conf.d/zg_web_80.cfg
listen zg_web_80                                                                                                                                                                                                
    bind 10.0.0.100:80
    mode http
    balance url_param userid
    hash-type consistent
    server web1 10.0.0.101:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 10.0.0.102:80 weight 1 check inter 3000 fall 2 rise 5
root@haproxy:~# systemctl reload haproxy.service

#测试访问
root@node4:~# curl http://10.0.0.100/test3.html?userid=zhang
test3 on 10.0.0.102 
root@node4:~# curl http://10.0.0.100/test5.html?userid=zhang
test5 on 10.0.0.102 
root@node4:~# curl http://10.0.0.100/test5.html?userid=meng
test5 on 10.0.0.101 
root@node4:~# curl http://10.0.0.100/test2.html?userid=meng
test2 on 10.0.0.101

2.3.4、hdr

针对用户每个http头部(header)请求中的指定信息做hash,此处由 name 指定的http首部将会被取出并做hash计算,然后由服务器总权重取模以后派发至某挑出的服务器,如果无有效值,则会使用默认的轮询调度。 示例:

#配置基于hdr一致性的hash
root@haproxy:~# vim /etc/haproxy/conf.d/zg_web_80.cfg
listen zg_web_80
    bind 10.0.0.100:80
    mode http
    balance hdr(User-Agent)                                                                                                                                                                                     
    hash-type consistent
    server web1 10.0.0.101:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 10.0.0.102:80 weight 1 check inter 3000 fall 2 rise 5
root@haproxy:~# systemctl reload haproxy.service

#测试访问
root@node4:~# curl http://10.0.0.100/test2.html
test2 on 10.0.0.102 
root@node4:~# curl http://10.0.0.100/index.html
Welcome to 10.0.0.101  page
root@node4:~# curl http://10.0.0.100/test6.html
test6 on 10.0.0.101 
root@node4:~# curl -A "IE" http://10.0.0.100/test6.html
test6 on 10.0.0.101 
root@node4:~# curl -A "firefox" http://10.0.0.100/test6.html
test6 on 10.0.0.102 
root@node4:~# curl -A "chrome" http://10.0.0.100/test6.html
test6 on 10.0.0.101

2.3.5、rdp-cookie

rdp-cookie对远windows远程桌面的负载,使用cookie保持会话,默认是静态,也可以通过hash-type指定map-based和consistent,来定义使用取模法还是一致性hash。在这里我就不演示了,没有装Windows的虚拟机。

2.4、算法总结

#静态 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 rdp-cookie-------->tcp

每个算法的使用场景

first #使用较少 static-rr #做了session共享的web集群 roundrobin #默认的调度算法 random leastconn #用于数据库 source #基于客户端公网IP的会话保持 Uri--------------->http #缓存服务器,CDN服务商,蓝汛、百度、阿里云、腾讯 url_param--------->http #可以实现session保持 hdr #基于客户端请求报文头部做下一步处理 rdp-cookie #基于Windows主机,很少使用