keepalived+lvs配置sorryserver

当配置在lvs上的rs都无法访问到时,就需要有一台备用的服务器来提供报错页面,此服务器称为sorry_server。

配置sorry_server

准备主机4台

server hostname ip
keepalived s1 172.20.27.10
sorryserver web3 172.20.27.13
nginx web1 172.20.27.20
nginx web2 172.20.27.21

1.修改keepalived配置文件

virtual_server 172.20.27.100 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    protocol TCP
    sorry_server 172.20.27.13 80        #配置sorry_server的地址和端口
    real_server 172.20.27.20 80 {
        weight 1
        HTTP_GET {
            url {
                path /monitor-page/index.html
                status_code 200
        }
            nb_get_retry 3
            delay_before_retry 3
            connect_timeout 5
    }
}
    real_server 172.20.27.21 80 {
        weight 1
        HTTP_GET {
            url {
                path /monitor-page/index.html
                status_code 200
            }
            nb_get_retry 3
            delay_before_retry 3
            connect_timeout 5
        }
    }
}

2.重启服务器查看lvs规则

#由于后端web1和web2服务没有启动,经过一段时间检测后,无法探测到主机就下线了,27.12的sorryserver上线提供服务
[root@s1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.20.27.100:80 wrr
  -> 172.20.27.20:80              Route   1      0          0         
  -> 172.20.27.21:80              Route   1      0          0         
[root@s1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.20.27.100:80 wrr
  -> 172.20.27.12:80              Route   1      0          0         

3.sorryserver上创建页面

[root@s3 ~]# echo 'sorry!' > /var/www/html/index.html

4.在sorryserver上绑定vip地址

[root@web3 ~]# bash lvs_dr_rs.sh start
[root@web3 ~]# cat lvs_dr_rs.sh 
#!/bin/bash
vip=172.20.27.100
mask='255.255.255.255'
dev=lo:1
case $1 in
start)
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    ifconfig $dev $vip netmask $mask #broadcast $vip up
    #route add -host $vip dev $dev
    echo "The RS Server is Ready!"
    ;;
stop)
    ifconfig $dev down
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo "The RS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

5.在客户端上测试访问

[root@client ~]# curl www.mylinuxops.com
sorry\!
#sorryserver开始提供服务