昨晚写了关于lvs的文章,今天以实例介绍下LVS+keepalived实现双机热备

服务器IP地址设置表

服务器名称

网络接口

IP地址

路由器

外网IP

121.201.24.138

lvs-master

eth0

172.16.100.2


eth1(VIP)

172.16.100.100

lvs-backup

eth0

172.16.100.3


lo:0(VIP)

172.16.100.100

web1

eth0

172.16.100.4


lo:0(VIP)

172.16.100.100

web2

eth0

172.16.100.5


lo:0(VIP)

172.16.100.100


部署两台或者多台LVS调度器,仅有一台调度器作为主服务器,其他调度器作为备用,当主调度器发生故障时,keepalived会自动将备用服务器升为主服务器

1.lvsmaster端

下载keepalived服务

yum install -y keepalived

源码安装

yum install -y openssl openssl-devel
wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz

tar xvf keepalived-1.2.12.tar.gz
cd keepalived-1.2.12
./configure --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --mandir=/usr/share
make && make install

配置keepalived

[root@lvsmaster ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
  notification_email {
       [email protected]
  }
## 默认的配置文件中,使用第三方smtp服务器,但这在现实中几乎没有意义(需要验证的原因),我们将其指定为localhost, 将通知信息的发送交给本地sendmail服务处理
  notification_email_from root@localhost
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}

vrrp_instance VI_1 {
   state MASTER         ## 从使用BACKUP
   interface eth0          ## 确认对外开放的端口,这里设置错误,将导致不会建立VIP
   virtual_router_id 51
   priority 100            ## 从要比主低,我设置为80
   advert_int 5
   mcast_src_ip 172.16.100.2  ##lvs的IP,从IP为172.16.100.3
   authentication {
       auth_type PASS
       auth_pass Golo
   }

# notify_master,notify_backup和notify_fault
# 分别表示在节点变为master,节点变为backup和节点不可用时执行的脚本
   notify_master "/etc/keepalived/master.sh"
   notify_backup "/etc/keepalived/backup.sh"
   notify_fault "/etc/keepalived/fault.sh"

## 在keepalived的配置文件最前面加入以下代码,定义一个跟踪脚本:(解决脑裂问题)
vrrp_script check_local { #定义一个名称为check_local的检查脚本
   script "/usr/local/keepalived/bin/check_local.sh" #shell脚本的路径
interval 5  #运行间隔
weight -15 #优先级变化
}

## 再在vrrp_instance配置中加入以下代码使用上面定义的检测脚本:
track_script {
   check_local
}

   virtual_ipaddress {
      172.16.100.100     ## VIP
   }
}
virtual_server 172.16.100.100 80 {
   delay_loop 6
   lb_algo rr
   lb_kind DR
   persistence_timeout 60
   protocol TCP

   real_server 172.16.100.2 80 {
       weight 3
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
}
   }
   real_server 172.16.100.3 80 {
       weight 3
       TCP_CHECK {
       connect_timeout 10
       nb_get_retry 3
       delay_before_retry 3
       connect_port 80
       }
    }
}


2.lvsbackup

lvs-backup的修改参照上面的注释部分


开启keepalived服务

/etc/init.d/keepalived start

执行ipvsadm可以看到keepalived中配置已经在ipvsadm中调用

[root@lvsmastert ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
 -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.100.100:http rr persistent 50
 -> web1:http                    Route   3      0          0        
 -> web2:http                    Route   3      0          0      


3.web

web端配置于前篇lvs文章中一样,这里写成脚本形式开机自启动

在/etc/init.d/下web1 172.16.100.4和web2 172.16.100.5上的脚本lvs

内容如下:

#add for chkconfig
#chkconfig: 2345 70 30  
#description: lvs's script
#processname: lvs
#!/bin/bash
VIP=172.16.100.100
source /etc/rc.d/init.d/functions
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
      echo "RealServer Start OK"
      ;;
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 "RealServerStoped"
      ;;
      *)
      echo "Usage: $0 {start|stop}"
      exit 1
esac
exit 0

为脚本lvs添加权限,并启动

chkconfig lvs on
service lvs start


安装并启动nginx,这里不多做介绍


4.结果验证

在lvs服务器上查看系统日志,服务启动成功

[root@lvsmaster ~]# tail -f -n 100 /var/log/messages
Aug 13 16:03:25 sz-mycar-lvssla1 Keepalived_healthcheckers[20193]: IPVS: Service already exists
Aug 13 16:03:25 sz-mycar-lvssla1 Keepalived_healthcheckers[20193]: IPVS: Destination already exists
Aug 13 16:03:25 sz-mycar-lvssla1 Keepalived_healthcheckers[20193]: IPVS: Destination already exists
Aug 13 16:03:25 sz-mycar-lvssla1 Keepalived_healthcheckers[20193]: Using LinkWatch kernel netlink reflector...
Aug 13 16:03:25 sz-mycar-lvssla1 Keepalived_healthcheckers[20193]: Activating healthchecker for service [172.16.100.4]:80
Aug 13 16:03:25 sz-mycar-lvssla1 Keepalived_healthcheckers[20193]: Activating healthchecker for service [172.16.100.5]:80
Aug 13 16:03:34 sz-mycar-lvssla1 Keepalived_healthcheckers[20193]: Remote SMTP server [127.0.0.1]:25 connected.
Aug 13 16:03:34 sz-mycar-lvssla1 Keepalived_healthcheckers[20193]: SMTP alert successfully sent.


查看lvsmaster,可看到VIP在master上

[root@lvsmaster ~]# ip a li eth2
2: eth2: mtu 1500 qdisc pfifo_fast state UP qlen 1000
   link/ether 52:54:52:4f:2a:de brd ff:ff:ff:ff:ff:ff
   inet 172.16.100.2/24 brd 172.16.100.255 scope global eth2
   inet 172.16.100.100/32 scope global eth2
   inet6 fe80::5054:52ff:fe4f:2ade/64 scope link
      valid_lft forever preferred_lft forever


将lvsmaster上的keepalived服务关闭,可发现VIP已经切换到lvsbackup

[root@lvsbackup ~]# ip a li eth2
2: eth2: mtu 1500 qdisc pfifo_fast state UP qlen 1000
   link/ether 52:54:2b:55:07:d9 brd ff:ff:ff:ff:ff:ff
   inet 172.16.100.3/24 brd 172.16.100.255 scope global eth2
   inet 172.16.100.100/32 scope global eth2
   inet6 fe80::5054:2bff:fe55:7d9/64 scope link
      valid_lft forever preferred_lft forever


主从切换成功,且服务照常运行

LVS+keepalived实现双机热备_第1张图片