HAProxy基础配置-haproxy常见的调度算法
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
我们知道nginx有四种(rr,ip_hash,uri hash,lastconn)常用的调度算法,LVS有10种(常用的也就3-4种,而且生产环境种使用lvs的场景相对较少,毕竟大多数公司没有淘宝,天猫那个访问量)调度算法,而haproxy的调度算法相对nginx还是比较多的。
除此之外,haproxy还支持通过socket的方式实现服务器动态的上下限,而nginx和lvs却不支持该功能,接下来我们一起学习一下haproxy吧~
一.HAProxy调度算法需要使用balance指令来声明
balance:
指明对后端服务器的调度算法,配置在listen或backend
1>.编辑haproxy的配置文件(开启socket功能,便于下面测试动态调度算法与通过该socket和haproxy服务在不重启的情况下动态变更配置)
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /yinzhengjie/softwares/haproxy
#如果需要使用动态调度算法需要将socket功能打开
stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
nbthread 2
pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
log 127.0.0.1 local5 info
defaults
option http-keep-alive
option forwardfor
option redispatch
option abortonclose
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_80
bind 172.30.1.102:80
mode http
server web01 172.30.1.106:80 check inter 3000 fall 3 rise 5
server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/haproxy/haproxy.sock
srw------- 1 root root 0 Jan 3 14:36 /yinzhengjie/softwares/haproxy/haproxy.sock
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/haproxy/haproxy.pid
-rw-r--r-- 1 root root 6 Jan 3 14:36 /yinzhengjie/softwares/haproxy/haproxy.pid
[root@node102.yinzhengjie.org.cn ~]#
2>.安装socat命令(下面在测试动态修改权重案例时会用到该工具)
[root@node102.yinzhengjie.org.cn ~]# yum -y install socat
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 9.1 kB 00:00:00
* base: mirrors.aliyun.com
* epel: mirrors.yun-idc.com
* extras: mirror.bit.edu.cn
* updates: mirrors.aliyun.com
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/repodata/repomd.xml: [Errno 12] Timeout on http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86
_64/repodata/repomd.xml: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')Trying other mirror.
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package socat.x86_64 0:1.7.3.2-2.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=================================================================================================================================================
Package Arch Version Repository Size
=================================================================================================================================================
Installing:
socat x86_64 1.7.3.2-2.el7 base 290 k
Transaction Summary
=================================================================================================================================================
Install 1 Package
Total download size: 290 k
Installed size: 1.1 M
Downloading packages:
socat-1.7.3.2-2.el7.x86_64.rpm | 290 kB 00:00:07
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : socat-1.7.3.2-2.el7.x86_64 1/1
Verifying : socat-1.7.3.2-2.el7.x86_64 1/1
Installed:
socat.x86_64 0:1.7.3.2-2.el7
Complete!
[root@node102.yinzhengjie.org.cn ~]#