文章目錄
- 一、概述
- 二、socat工具
- 三、static-rr
- 四、first
HAProxy通過固定參數 balance 指明對后端服務器的調度算法,該參數可以配置在listen或backend選項中。
HAProxy的調度算法分為靜態和動態調度算法,但是有些算法可以根據參數在靜態和動態算法中相互轉換。
官方文檔: http://cbonte.github.io/haproxy-dconv/2.4/configuration.html#4-balance
一、概述
按照事先定義好的規則輪詢進行調度,不關心后端服務器的當前負載、連接數和響應速度等,且無法實時動態修改權重(只能為0和1,不支持其它值)或者修改后不生效,如果需要修改只能靠重啟HAProxy生效
二、socat工具
對服務器動態權重和其它狀態可以利用 socat工具進行調整,Socat 是 Linux 下的一個多功能的網絡工具,名字來由是Socket CAT,相當于netCAT的增強版.Socat 的主要特點就是在兩個數據流之間建立雙向通道,且支持眾多協議和鏈接方式。如 IP、TCP、 UDP、IPv6、Socket文件等。
例:利用工具socat對服務器動態權重調整
[root@centos7 ~]# yum install -y socat#查看幫助
[root@centos7 ~]# socat -h
[root@centos7 ~]# echo "help" | socat stdio /var/lib/haproxy/haproxy.sock
[root@centos7 ~]#echo "show info" | socat stdio /var/lib/haproxy/haproxy.sock
[root@centos7 ~]#cat /etc/haproxy/haproxy.cfg
......
listen lhl-test-80
bind :81,:82
mode http
server web1 10.0.0.17:80 check inter 3000 fall 3 rise 5
server web2 10.0.0.27:80 check weight 3
......[root@centos7 ~]#echo "show servers state" | socat stdio /var/lib/haproxy/haproxy.sock[root@centos7 ~]#echo "get weight lhl-test-80/web2" | socat stdio /var/lib/haproxy/haproxy.sock
3 (initial 3)#修改weight,注意只針對單進程有效
[root@centos7 ~]#echo "set weight lhl-test-80/web2 2" | socat stdio /var/lib/haproxy/haproxy.sock
[root@centos7 ~]#echo "get weight lhl-test-80/web2" | socat stdio /var/lib/haproxy/haproxy.sock
2 (initial 3)#將后端服務器禁用,注意只針對單進程有效
[root@centos7 ~]#echo "disable server lhl-test-80/web2" | socat stdio /var/lib/haproxy/haproxy.sock#啟用后端服務器
[root@centos7 ~]#echo "enable server lhl-test-80/web2" | socat stdio /var/lib/haproxy/haproxy.sock#將后端服務器軟下線,即weight設為0
[root@centos7 ~]#echo "set weight lhl-test-80/web1 0" | socat stdio /var/lib/haproxy/haproxy.sock#針對haproxy的多進程,將后端服務器禁用
[root@centos7 ~]#vim /etc/haproxy/haproxy.cfg
......
stats socket /var/lib/haproxy/haproxy1.sock mode 600 level admin process 1 #綁定第
1個進程和socket文件
stats socket /var/lib/haproxy/haproxy2.sock mode 600 level admin process 2 #綁定第
2個進程和socket文件
nbproc 2
.....[root@centos7 ~]#echo "disable server lhl-test-80/web2" | socat stdio
/var/lib/haproxy/haproxy1.sock
[root@centos7 ~]#echo "disable server lhl-test-80/web2" | socat stdio
/var/lib/haproxy/haproxy2.sock[root@haproxy ~]#for i in {1..2};do echo "set weight lhl-test-80/web$i 10" | socat stdio /var/lib/haproxy/haproxy$i.sock;done
#如果靜態算法,如:static-rr,可以更改weight為0或1,但不支持動態更改weight為其它值,否則會提示下面信息
[root@centos7 ~]#echo "set weight lhl-test-80/web1 0" | socat stdio /var/lib/haproxy/haproxy.sock[root@centos7 ~]#echo "set weight lhl-test-80/web1 1" | socat stdio /var/lib/haproxy/haproxy.sock
[root@centos7 ~]#echo "set weight lhl-test-80/web1 2" | socat stdio /var/lib/haproxy/haproxy.sock
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.
例:上線和下線后端服務器腳本
[root@centos7 ~ ]#cat haproxy_host_up_down.sh
. /etc/init.d/functions
case $1 in
up)echo "set weight lhl-m42-web-80/$2 1" | socat stdio /var/lib/haproxy/haproxy.sock[ $? -eq 0 ] && action "$2 is up";;
down)echo "set weight lhl-m42-web-80/$2 0" | socat stdio /var/lib/haproxy/haproxy.sock[ $? -eq 0 ] && action "$2 is down";;
*)echo "Usage: `basename $0` up|down IP";;
esac
三、static-rr
static-rr:基于權重的輪詢調度,不支持運行時利用socat進行權重的動態調整(只支持0和1,不支持其它值)及后端服務器慢啟動,其后端主機數量沒有限制,相當于LVS中的 wrr
listen web_hostbind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010mode httplog globalbalance static-rrserver web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5server web2 10.0.0.27:80 weight 2 check inter 3000 fall 2 rise 5
四、first
first:根據服務器在列表中的位置,自上而下進行調度,但是其只會當第一臺服務器的連接數達到上限,新請求才會分配給下一臺服務,因此會忽略服務器的權重設置,此方式使用較少
不支持用socat進行動態修改權重,可以設置0和1,可以設置其它值但無效
listen web_hostbind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010mode httplog globalbalance firstserver web1 10.0.0.17:80 maxconn 2 weight 1 check inter 3000 fall 2 rise 5server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 5