1、简述keepalived工作原理
(1)keepalived的功能:
- 基于vrrp协议完成地址流动
- 为vip地址所在的节点生成ipvs规则(在配置文件中预先定义)
- 为ipvs集群的各Real Server 做健康状态检测
- 基于脚本调用接口完成脚本中定义的功能,进而影响集群事务,以此支持nginx、haproxy等服务
2、编译安装haproxy
在CentOS7上编译安装haproxy
由于编译安装haproxy,需要lua环境且最低版本为5.3。CentOS7自带的lua版本并不符合该要求,所以应该先编译安装lua.
(1)编译安装lua:
35 2022-08-11 17:06:15 yum install gcc readline-devel -y
44 2022-08-11 17:08:12 cd /usr/local/src/
51 2022-08-11 17:11:21 wget http://www.lua.org/ftp/lua-5.3.5.tar.gz
54 2022-08-11 17:11:50 tar xf lua-5.3.5.tar.gz
55 2022-08-11 17:11:53 ll
56 2022-08-11 17:12:00 cd lua-5.3.5/
57 2022-08-11 17:12:16 make linux test
[17:40:34 root@CS7-VM8-10 lua-5.3.5]#src/lua -v
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio #编译安装成功
(2)编译安装haproxy:
40 2022-08-11 17:00:03 yum -y install gcc openssl-devel pcre-devel systemd-devel
41 2022-08-11 17:07:34 rz -E
42 2022-08-11 17:07:36 ll
43 2022-08-11 17:07:59 tar xf haproxy-2.4.18.tar.gz -C /usr/local/src
44 2022-08-11 17:08:12 cd /usr/local/src/haproxy-2.4.18/
74 2022-08-11 17:42:48 make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/
75 2022-08-11 17:45:16 make install PREFIX=/apps/haproxy
76 2022-08-11 17:45:46 ln -s /apps/haproxy/sbin/haproxy /usr/sbin/
77 2022-08-11 17:45:55 tree /apps/haproxy/
78 2022-08-11 17:46:10 which haproxy
79 2022-08-11 17:46:19 haproxy -v
80 2022-08-11 17:46:46 systemctl daemon-reload
81 2022-08-11 17:47:07 systemctl start haproxy #此时还不能正常启动服务(缺少service文件)
82 2022-08-11 17:47:24 rpm -ql haproxy #参照CentOS8中yum安装时的启动文件。
83 2022-08-11 17:47:50 vim /usr/lib/systemd/system/haproxy.service #创建service文件
84 2022-08-11 17:49:01 mkdir /etc/haproxy #创建自定义配置文件
85 2022-08-11 17:49:18 vim /etc/haproxy/haproxy.cfg
86 2022-08-11 17:50:54 ss -ntl
87 2022-08-11 17:51:09 mkdir /var/lib/haproxy #准备socket文件目录
88 2022-08-11 17:51:18 useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy #设置用户和目录权限
89 2022-08-11 17:51:24 systemctl enable --now haproxy
90 2022-08-11 17:51:31 systemctl status haproxy
91 2022-08-11 17:51:51 ss -ntl #开启9999端口(根据文件:/etc/haproxy/haproxy.cfg )
/usr/lib/systemd/system/haproxy.service 文件格式如下:
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
/etc/haproxy/haproxy.cfg 文件格式如下:
global
maxconn 100000
chroot /apps/haproxy
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
#uid 99
#gid 99
user haproxy
group haproxy
daemon
#nbproc 4
#cpu-map 1 0
#cpu-map 2 1
#cpu-map 3 2
#cpu-map 4 3
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 haadmin:123456
listen web_port
bind 10.0.8.7:80
mode http
log global
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
(3)验证:
3、总结haproxy各调度算法的实现方式及其应用场景
haproxy 配置文件默认的几大块:/etc/haproxy/haproxy.cfg
(1)global
(2)defaults
(3)listen
(4)frontend
(5)backend
其中前、后端是多对多的关系,因此,分开存放更好。(/etc/haproxy/conf.d/*.cfg)
3.1静态算法
3.1.1定义:
按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、连接数和响应速度等,
且无法实时修改权重(只能为0和1,不支持其它值),只能靠重启HAProxy生效。
3.1.2 static-rr
3.1.2.1 定义:
基于权重的轮询调度,不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)及后端服务器慢启动,其后端主机数量没有限制,相当于LVS中的 wrr
3.1.2.2 用法:
...
frontend test2_web_80
bind 10.0.8.7:80
use_backend test2_web80
backend test2_web80
balance static-rr
server 10.0.8.8 10.0.8.8:80 check
server 10.0.8.18 10.0.8.18:80 check
#用socat用具查看权重,默认为1
[10:31:30 root@CS7-VM8-10 ~]#echo "get weight test2_web80/10.0.8.18" | socat stdio /var/lib/haproxy/haproxy.sock
1 (initial 1)
#把权重设为2,不支持(仅支持0和1)
[10:31:31 root@CS7-VM8-10 ~]#echo "set weight test2_web80/10.0.8.18 2" | socat stdio /var/lib/haproxy/haproxy.sock
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.
#把权重设为0,即下线后端服务器(10.0.8.18)
[10:31:46 root@CS7-VM8-10 ~]#echo "set weight test2_web80/10.0.8.18 0" | socat stdio /var/lib/haproxy/haproxy.sock
#查看设置的权重为0是否成功
[10:32:01 root@CS7-VM8-10 ~]#echo "get weight test2_web80/10.0.8.18" | socat stdio /var/lib/haproxy/haproxy.sock
0 (initial 1)
验证:
[10:30:25 root@CS7-VM8-10 ~]#curl 10.0.8.7
10.0.8.8
[10:30:25 root@CS7-VM8-10 ~]#curl 10.0.8.7
10.0.8.8
[10:32:20 root@CS7-VM8-10 ~]#curl 10.0.8.7
10.0.8.8
[10:32:21 root@CS7-VM8-10 ~]#curl 10.0.8.7
10.0.8.8
[10:32:21 root@CS7-VM8-10 ~]#
3.1.3 first(略)
根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限,
新请求才会分配给下一台服务,因此会忽略服务器的权重设置,此方式使用较少
3.2动态算法
3.2.1定义:
基于后端服务器状态进行调度适当调整,新请求将优先调度至当前负载较低的服务器,
且权重可以在haproxy运行时动态调整无需重启。
3.2.2工具:socat
对服务器动态权重和其他的状态可以利用socat 工具进行动态调整。
Socat 的主要特点就是在两个数据流之间建立双向通道,
且支持众多协议和链接方式。如 IP、TCP、 UDP、IPv6、Socket文件等。
该工具的帮助用法:
socat -h
例如:
echo "help" | socat stdio /var/lib/haproxy/haproxy.sock
echo "show servers state" | socat stdio /var/lib/haproxy/haproxy.sock
3.2.3 roundrobin(默认算法)
3.2.3.1 定义
roundrobin:基于权重的轮询动态调度算法,支持权重的运行时调整,不同于lvs中的rr轮训模式,
HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),其每个后端backend中最多支持4095个real server,
支持对real server权重动态调整,roundrobin为默认调度算法,此算法使用广泛
3.2.3.2 用法
......
frontend test2_web_80
bind 10.0.8.7:80
use_backend test2_web80
backend test2_web80
balance roundrobin
server 10.0.8.8 10.0.8.8:80 check
server 10.0.8.18 10.0.8.18:80 check
[10:57:38 root@CS7-VM8-10 ~]#echo "get weight test2_web80/10.0.8.18" | socat stdio /var/lib/haproxy/haproxy.sock
1 (initial 1)
#动态修改权重为2
[10:58:00 root@CS7-VM8-10 ~]#echo "set weight test2_web80/10.0.8.18 2" | socat stdio /var/lib/haproxy/haproxy.sock
[10:58:09 root@CS7-VM8-10 ~]#echo "get weight test2_web80/10.0.8.18" | socat stdio /var/lib/haproxy/haproxy.sock
2 (initial 1)
[10:58:11 root@CS7-VM8-10 ~]#
验证:
[10:59:05 root@CS7-VM8-10 ~]#curl 10.0.8.7
10.0.8.18
[10:59:05 root@CS7-VM8-10 ~]#curl 10.0.8.7
10.0.8.18
[10:59:05 root@CS7-VM8-10 ~]#curl 10.0.8.7
10.0.8.8
[10:59:06 root@CS7-VM8-10 ~]#curl 10.0.8.7
10.0.8.18
[10:59:06 root@CS7-VM8-10 ~]#curl 10.0.8.7
10.0.8.18
[10:59:07 root@CS7-VM8-10 ~]#curl 10.0.8.7
10.0.8.8
[10:59:07 root@CS7-VM8-10 ~]#
3.2.4 leastconn (用法都类似,略)
eastconn加权的最少连接的动态,支持权重的运行时调整和慢启动,即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户端连接),
比较适合长连接的场景使用,比如:MySQL等场景。
3.2.5 random(用法都类似,略)
在1.9版本开始增加 random的负载平衡算法,其基于随机数作为一致性hash的key,随机负载平衡对于大型服务器场或经常添加或删除服务器非常有用,
支持weight的动态调整,weight较大的主机有更大概率获取新请求
3.3其他算法(即可作为静态算法,又可以通过选项成为动态算法)
3.3.1 source
源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一个后端web服务器。
此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服务器,
默认为静态方式,但是可以通过hash-type支持的选项更改.
3.3.1.1 map-base 取模法(hash-type指定的默认算法)
配置实例:
......
frontend test2_web_80
bind 10.0.8.7:80
use_backend test2_web80
backend test2_web80
balance source
hash-type map-based
server 10.0.8.8 10.0.8.8:80 check
server 10.0.8.18 10.0.8.18:80 check
3.3.1.2 一致性hash
配置实例:
......
frontend test2_web_80
bind 10.0.8.7:80
use_backend test2_web80
backend test2_web80
balance source
hash-type consistent
server 10.0.8.8 10.0.8.8:80 check
server 10.0.8.18 10.0.8.18:80 check
3.3.2 uri
配置实例:
......
frontend test2_web_80
bind 10.0.8.7:80
use_backend test2_web80
backend test2_web80
balance uri
#hash-type consistent #uri一致性hash配置
server 10.0.8.8 10.0.8.8:80 check
server 10.0.8.18 10.0.8.18:80 check
curl http://10.0.8.8/test1.html
curl http://10.0.8.8/test2.html
3.3.3 url_param
配置实例:
......
frontend test2_web_80
bind 10.0.8.7:80
use_backend test2_web80
backend test2_web80
balance url_param userid
# hash-type consistent #url_param一致性hash配置
server 10.0.8.8 10.0.8.8:80 check
server 10.0.8.18 10.0.8.18:80 check
#假设:
url = http://www.apc.org/foo/bar/index.php?key=value
#则:
host = "www.apc.org"
url_param = "key=value"
# curl http://10.0.8.8/index.html?userid=<NAME_ID>
# curl "http://10.0.8.8/index.html?userid=<NAME_ID>&typeid=<TYPE_ID>"
3.3.4 hdr
配置实例:
......
frontend test2_web_80
bind 10.0.8.7:80
use_backend test2_web80
backend test2_web80
balance hdr(User-Agent)
# hash-type consistent #一致性hash配置
server 10.0.8.8 10.0.8.8:80 check
server 10.0.8.18 10.0.8.18:80 check
curl -v http://10.0.8.8/index.html
curl -vA 'firefox' http://10.0.8.8/index.html
curl -vA 'firefox' http://10.0.8./index.html
3.3.5 rdp-cookie
配置实例:
......
frontend test2_web_80
bind 10.0.8.7:80
use_backend test2_web80
backend test2_web80
mode tcp
balance rdp-cookie
# hash-type consistent #一致性hash配置
server 10.0.8.8 10.0.8.8:80 check
server 10.0.8.18 10.0.8.18:80 check
3.4各个算法的使用场景
算法 | 使用场景 |
---|---|
first | 使用较少,略 |
static-rr | 做了session共享的web集群 |
roundrobin | 默认的算法 |
random | |
leastconn | 数据库 |
source | 基于客户端公网ip的会话保持 |
uri | 缓存服务器,CDN服务商 |
url_param | 可以实现session |
hdr | 基于客户端请求报文头部做下一步处理 |
rdp-cookie | 基于windows 主机,很少使用 |
4、使用haproxy的ACL实现基于文件后缀名的动静分离
(1)haproxy服务器的配置
haproxy的子配置文件:
...
frontend test2_web_80
bind 10.0.8.7:80
mode http
balance roundrobin
log global
option httplog
##################### acl setting ###############################
#acl acl_apc_domain hdr_dom(host) -i www.apc.org
#acl acl_mobile_domain hdr_dom(host) -i www.mobile.org
acl acl_static path_end -i .jpg .jpeg .png .css .gif .js .html
acl acl_dynamic path_ebd -i .php .py
###################### acl hosts #################################
#use_backend apc_hosts if acl_apc_domain
#use_backend mobile_hosts if acl_mobile_domain
use_backend mobile_hosts if acl_static
use_backend apc_hosts if acl_dynamic
default_backend apc_hosts
#default_backend apc_hosts
#################################################################
# use_backend test2_web80
# #http-request add-header X-Haproxy-Current-Date %T
#acl_static
backend mobile_hosts
mode http
server web2 10.0.8.18:80 check
#acl_dynamic
backend apc_hosts
mode http
server web1 10.0.8.8:80 check
(2)检测
- 访问静态资源检测:
- 访问动态资源检测: