Keepalived启动LVS,不能做到负载均衡

使用Keepalived启动LVS,不能做到负载均衡,所有包都只发到其中一台真实机上.此时把这台真实机的网络断掉,网络包才会转到另一台真实机.
如果先启动Keepalived,再手工清空ipvs表,再启动lvs,负载均衡就没问题.

使用Keepalived启动LVS,脚本如下:
global_defs {
router_id lvs.localdomain
}

virtual_server 192.168.21.100 13819 {
delay_loop 60
lb_algo rr
lb_kind DR
persistence_timeout 900
protocol UDP

real_server 192.168.21.153 13819 {
weight 1
TCP_CHECK {
connect_port 3031
connect_timeout 3
}
}

real_server 192.168.21.253 13819 {
weight 1
TCP_CHECK {
connect_port 3031
connect_timeout 3
}
}
}

LVS可正常启动,运行ipvsadm,显示如下信息:
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
UDP 192.168.21.100:13819 rr
-> 192.168.21.253:13819 Route 1 0 0
-> 192.168.21.153:13819 Route 1 0 0

但每次所发的包,全部转发到192.168.21.253, 断掉192.168.21.253后,包会转发到192.168.21.153

把192.168.21.253重新连上,运行
ipvsadm -C
ipvsadm -A -u 192.168.21.100:13819 -s rr
ipvsadm -a -u 192.168.21.100:13819 -r 192.168.21.153 -g -w 1
ipvsadm -a -u 192.168.21.100:13819 -r 192.168.21.253 -g -w 1

之后就能做到负载均衡,两边都可以收到包了.

原来的脚本在什么地方出了问题?
谢谢.

Forums:

persistence_timeout 900
同一IP 900秒内的请求都发到同个real server

明白,谢谢了。