LVS持久连接 Alex

如果客户端连接上我的服务器,在五分钟之内,就不往其他服务器上调度了,如果时间长了,可以调度到其他机器上去,即照顾了负载均衡,也保证了会话的

持久连接
  session 绑定:对共享同一组RS的多个集群服务,需要统一进行绑定,lvs sh算法无法实现
  持久连接( lvs persistence )模板:实现无论使用任何调度算法,在一段时间内(默认360s ),能够实现将来自同一个地址的请求始终发往同一个RS
ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]]
  持久连接实现方式:
  每端口持久(PPC):每个端口定义为一个集群服务,每集群服务单独调度
  每防火墙标记持久(PFWMC):基于防火墙标记定义集群服务;可实现将多个端口上的应用统一调度,即所谓的port Affinity
  每客户端持久(PCC):基于0端口(表示所有服务)定义集群服务,即将客户端对所有应用的请求都调度至后端主机,必须定义为持久模式

 通过man ipvsadm 查看如下

-p, --persistent [timeout]
Specify that a virtual service is persistent. If this option is specified, multiple requests from a client are
redirected to the same real server selected for the first request. Optionally, the timeout of persistent sessions
may be specified given in seconds, otherwise the default of 300 seconds will be used. This option may be used in
conjunction with protocols such as SSL or FTP where it is important that clients consistently connect with the
same real server.

LVS只能针对ip

一、将调度算法修改为持久连接

ipvsadm -E -f 10 -s rr -p

(我这里是接着这个博客后面继续写的笔记,也可直接ipvsadm -A -t 10.0.0.100:80-s rr -p

二、查看ipvsadm规则

[06:50:08 root@lvs ~]#ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
FWM  10 rr persistent 360
  -> 192.168.1.5:0                Route   1      0          0         
  -> 192.168.1.6:0                Route   1      0          0  

注意:这里的默认时间写的是360,但man帮助里写的是300

三、测试访问,同一IP地址,360秒内都往同一台机器上发送

[06:38:41 root@client_ip ~]#while : ; do curl 10.0.0.100 ; curl -k https://10.0.0.100/ ; sleep 0.5; done
192.168.1.5 _______RS1
192.168.1.5 _______RS1
192.168.1.5 _______RS1
192.168.1.5 _______RS1
192.168.1.5 _______RS1
192.168.1.5 _______RS1
192.168.1.5 _______RS1
192.168.1.5 _______RS1
192.168.1.5 _______RS1
原文地址:https://www.cnblogs.com/alexlv/p/14805738.html