LVS只是做一个负载均衡,通过访问VIP来访问后端的网站程序,一旦LVS宕机,整个网站就访问不了,这就出现了单点。所以要结合keepalive这种高可用软件来保证整个网站的高可用。本文将介绍如何利用keepalive来实现LVS的高可用(LVSDR模式为例,生产环境后台的real server 网站内容是一致的,为了看到实验效果,这里是两个不同的页面)。 

一、环境介绍

VIP192.168.1.188

RIP1(Web1): 192.168.1.2

RIP2(Web2): 192.168.1.5

LVS1 :192.168.1.6

LVS2: 192.168.1.7

web1页面

wKioL1Vlx4HRVzwsAAB4QQhSn2A022.jpg

web2页面

wKiom1VlxfGT8hXeAAB26U4-POA544.jpg

二、配置realserver

分别在web1web2上执行lvs_rserver.sh

[root@web1 ~]# ./lvs_rserver.sh  start
RealServer Start OK
[root@web1 ~]#
[root@web2 ~]# ./lvs_rserver.sh  start
RealServer Start OK
[root@web2 ~]#

lvs_rserver.sh脚本内容如下:

[root@web1 ~]# cat lvs_rserver.sh
#!/bin/sh
#LVS Client Server
VIP=192.168.1.188
case $1  in                                                                                                                                                                                                                                                   
start)                                                                                                                                                                                                                                           
   ifconfig lo:0  $VIP  netmask  255.255.255.255  broadcast  $VIP
   /sbin/route  add –host  $VIP  dev lo:0
   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
   sysctl -p >/dev/null 2>&1
   echo "RealServer Start OK"
   exit 0
;;                                                                                                                                                                                                                                        
stop)
   ifconfig lo:0 down
   route del $VIP >/dev/null 2>&1
   echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
    echo "0">/proc/sys/net/ipv4/conf/lo/arp_announce
   echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
   echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
   echo "RealServer Stoped OK"
   exit 1
;;
*)
   echo "Usage: $0 {start|stop}"
;;
esac
[root@web2 ~]#

 

三、安装配置lvskeepalive

分别在lvs1lvs2上安装lvskeepalive

lvs及keppalive的安装见《构建负载均衡服务器之二 LVS详解及应用》 《构建高可用服务器之一 Keepalive介绍及安装》

lvs1lvs2上配置VIP并加载相关模块

[root@lvs1 ~]# ifconfig eth0:0192.168.1.188 netmask 255.255.255.0
[root@lvs1 ~]# modprobe ip_vs
[root@lvs1 ~]# modprobe ip_vs_rr
[root@lvs1 ~]# modprobe ip_vs_wrr
[root@lvs1 ~]#

 

[root@lvs2 ~]# ifconfig eth0:0192.168.1.188 netmask 255.255.255.0
[root@lvs2 ~]# modprobe ip_vs
[root@lvs2 ~]# modprobe ip_vs_rr
[root@lvs2 ~]# modprobe ip_vs_wrr
[root@lvs2 ~]#

lvs1上修改keepalive.conf文件,修改后内容如下

[root@lvs1 ~]# vim/etc/keepalived/keepalived.conf
[root@lvs1 ~]# cat/etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
   notification_email {
     654001593@qq.com
   }
  notification_email_from 654001593@qq.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
 
# VIP1
vrrp_instance VI_1 {
   state MASTER
   interface eth0
   lvs_sync_daemon_inteface eth0
   virtual_router_id 51
   priority 100
   advert_int 5
   nopreempt
   authentication {
       auth_type PASS
       auth_pass 1111
    }
   virtual_ipaddress {
       192.168.1.188  
    }
}
 
virtual_server 192.168.1.188 80 {
   delay_loop 6   
   lb_algo wrr   
   lb_kind DR  
#   persistence_timeout 60   
   protocol TCP        
 
   real_server 192.168.1.2 80 {
       weight 100       
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
    }
   real_server 192.168.1.5 80 {
       weight 100
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       
       }
    }
}
 
[root@lvs1 ~]#

lvs2上的keepalive.conf文件基本和lvs1上的一致,但是state这一项lvs1上是MASTER,而lvs2上的BACKUP;优先级prioritylvs1上为100,在lvs2上为90

lvs2上修改keepalive.conf文件,修改后内容如下

[root@lvs2 ~]# cat/etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
  notification_email {
     654001593@qq.com
   }
  notification_email_from 654001593@qq.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
 
# VIP1
vrrp_instance VI_1 {
   state BACKUP  
   interface eth0
   lvs_sync_daemon_inteface eth0
   virtual_router_id 51
     priority 90
   advert_int 5
   nopreempt
   authentication {
       auth_type PASS
       auth_pass 1111
    }
   virtual_ipaddress {
       192.168.1.188  
    }
}
 
virtual_server 192.168.1.188 80 {
   delay_loop 6   
   lb_algo wrr   
   lb_kind DR  
#   persistence_timeout 60   
   protocol TCP        
 
   real_server 192.168.1.2 80 {
       weight 100       
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
    }
   real_server 192.168.1.5 80 {
       weight 100
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       
       }
    }
}
 
[root@lvs2 ~]#

四、测试

停掉lvs1的网卡

ping VIP一样一直在通信(配置有nopreempt)

wKiom1VlxjbCuoE_AAKXVkxtx28353.jpg

通过VIP访问网站

wKioL1Vlx8jAZwxQAAC-GF1rLm0878.jpg

刷新

wKioL1Vlx8nzo7YPAACYbDYOU9o621.jpg

即使lvs1宕机后又恢复,此时依旧是通过lvs2访问(由是否配置nopreempt决定)。