LVS负载均衡(5)-- LVS持久连接
-
持久连接:
持久连接用于实现无论使用任何调度算法,在一段时间内(默认300s ),能够实现将来自同一个地址的请求始终发往同一个RS。
-
语法格式:
ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]]
选项
在生成lvs规则时加-p选项,-p选项指明保持连接的时间,默认300s。
-
持久连接三种实现方式
-
1.每端口持久(PPC)
每个端口定义为一个集群服务,每集群服务单独调度;
-
2.每防火墙标记持久(PFWMC)
基于防火墙标记定义集群服务;可实现将多个端口上的应用统一调度,即所谓的port Affinity;
-
3.每客户端持久(PCC)
基于0端口(表示所有服务)定义集群服务,即将客户端对所有应用的请求都调度至后端主机,必须定义为持久模式;
-
-
配置示例一:使用防火墙标记做持久连接,仍然使用上述dr模型:
#1.修改上节定义的防火墙标记为持久连接 [root@lvs-01 ~]# ipvsadm -E -f 10 -s rr -p 300 [root@lvs-01 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn FWM 10 rr persistent 300 <==持久连接 -> 192.168.50.22:0 Route 1 0 0 -> 192.168.50.23:0 Route 1 0 0 #2.客户端测试,同一个客户端的所有的请求都调度到一台RS上: [root@xuzhichao ~]# for i in {1..10} ;do curl -k -L -Hhost:www,xuzhichao.com http://192.168.20.50; done node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page [root@xuzhichao ~]# for i in {1..10} ;do curl -k -Hhost:www,xuzhichao.com https://192.168.20.50; done node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page
-
配置示例二:基于客户端做调度,访问192.168.50.100的任意tcp服务均调度至同一台RS上。
#配置LVS规则,端口号写0,表示所有的服务 [root@lvs-01 ~]# ipvsadm -C [root@lvs-01 ~]# ipvsadm -A -t 192.168.50.100:0 -s rr -p [root@lvs-01 ~]# ipvsadm -a -t 192.168.50.100:0 -r 192.168.50.22 -g [root@lvs-01 ~]# ipvsadm -a -t 192.168.50.100:0 -r 192.168.50.23 -g [root@lvs-01 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.50.100:0 rr persistent 360 -> 192.168.50.22:0 Route 1 0 0 -> 192.168.50.23:0 Route 1 0 0 #2.router设备把VIP的22端口映射出口用于测试: [root@router ~]# iptables -t nat -A PREROUTING -d 192.168.20.50 -p tcp --dport 22 -j DNAT --to 192.168.50.100:22 #3.客户端测试: #测试443和80端口访问和22端口访问都调度了同一台RS设备上: [root@xuzhichao ~]# for i in {1..10} ;do curl -k -Hhost:www,xuzhichao.com https://192.168.20.50; done node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page node2.xuzhichao.com page [root@xuzhichao ~]# ssh 192.168.20.50 root@192.168.20.50's password: Last login: Tue Jul 13 11:07:43 2021 from 192.168.20.17 [root@nginx03 ~]# exit logout Connection to 192.168.20.50 closed. [root@xuzhichao ~]# ssh 192.168.20.50 root@192.168.20.50's password: Last login: Tue Jul 13 11:07:50 2021 from 192.168.20.17 [root@nginx03 ~]# exit logout Connection to 192.168.20.50 closed.