来源:http://www.rainsts.net/article.asp?id=1025
LVS 虽然实现了负载均衡,但无法解决一个严重的问题,就是当某台 RealServer 当机时,无法将分配到该服务器的后续请求指向其他存活的服务器。
LVS Server : 192.168.1.10 Virtual IP : 192.168.1.100 Real Server1 : 192.168.1.20 Real Server2 : 192.168.1.30
$ sudo apt-get install ipvsadm keepalived
$ sudo vim /etc/keepalived/keepalived.conf global_defs { router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } } virtual_server 192.168.1.100 80 { delay_loop 1 # 每隔 1 秒查询 RealServer 状态 lb_algo wrr # LVS 算法 lb_kind DR # Direct Route #persistence_timeout 60 # 同一 IP 在 60 秒内分配到同一台 RealServer protocol TCP # 使用 TCP 协议检查 RealServer 状态 real_server 192.168.1.20 80 { weight 3 # 权重 TCP_CHECK { connect_timeout 10 # 10 秒无响应超时 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.1.30 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }}
$ sudo service keepalived start
$ sudo 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.1.100:80 wrr -> 192.168.1.20:80 Route 3 0 0 -> 192.168.1.30:80 Route 3 0 0
$ sudo vim /etc/rc.local #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. ifconfig lo:0 192.168.1.100 netmask 255.255.255.255 broadcast 192.168.1.100 up route add -host 192.168.1.100 dev lo:0 echo "0" > /proc/sys/net/ipv4/ip_forward echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce exit 0