LVS+Keepalived(DR模式)实现WEB服务器的高可用负载均衡

DR模式

Direct Routing直连路由
通过为请求报文的重新封装一个的MAC首部进行转发;源MAC是DIP所在接口的MAC,目标MAC是挑选出某RS的RIP所在接口的MAC地址;IP首部不会发生编发(CIP<-->VIP)

环境:Centos 7.0
内核:kernel-3.10.0-327.el7.x86_64
keepalived版本:Keepalived v1.2.13
LVS-Master:192.168.1.1
LVS-Backup:192.168.1.2
VIP :192.168.1.100
web1 :192.169.1.3
web2 :192.168.1.4

在LVS-Master上的keepalvied配置

vim /etc/keepalived/keepalived.conf
global_defs {
#   notification_email {
#     [email protected]
#     [email protected]
#     [email protected]         #故障时发送邮件的邮箱
#   }
#   notification_email_from [email protected]       #是有哪个邮箱发
#   smtp_server 192.168.1.1         #邮箱的smtp server地址
#   smtp_connect_timeout 30                  #链接smtp server的超时时间
   router_id LVS1       #表示运行Keepalived服务器标识(BAKUP:LVS2)
}

vrrp_instance VI_1 {
    state MASTER                #(MASTER|BACKUP)
    interface eno16777728       #网卡名称
    virtual_router_id 51            #虚拟路由ID,主备保持一致
    priority 100                    #优先级
    advert_int 1                    #MASTER与BACKUP之间同步检查的间隔
    authentication {
        auth_type PASS          #认证方式
        auth_pass 1111          #认证密码
    }
    virtual_ipaddress {
        192.168.1.100               #VIP
    }
}
virtual_server 192.168.1.100 80 {
   delay_loop 6       # 健康检查的时间间隔
    lb_algo rr           # 负载均衡算法(rr wrr lc wlc lblcsh dh)
    lb_kind DR         #LVS实现负载均衡的的机制(NAT TUN DR)
   nat_mask 255.255.255.0
    persistence_timeout 50  # 同一IP 的连接50秒内被分配到同一台realserver(测试时建议改为0)
    protocol TCP            # 用TCP监测realserver的状态

   real_server 192.168.1.3 80 {       # 定义realserver
        weight 1                       # 定义权重
       TCP_CHECK {  
            connect_timeout 3          # 三秒无响应超时
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
   }
    real_server 192.168.1.4 80 {
       weight 1
        TCP_CHECK {
           connect_timeout 3
            nb_get_retry 3
           delay_before_retry 3
           connect_port 80
      }
   }
}

在LVS-BACKUP需要更改的位置

vim /etc/keepalived/keepalived.conf
global_defs {
   router_id LVS2       
}

vrrp_instance VI_1 {
    state BACKUP                
    interface eno16777728       
    virtual_router_id 51           
    priority 90                 
    advert_int 1                    

在web服务器上创建脚本/etc/init.d/lvsrs,所有RS上都需要执行这个脚本。

#!/bin/bash

VIP=192.168.1.100

/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
 sysctl -p >/dev/null 2>&1
 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 "RealServer Stoped"
 ;;
*)
 echo "Usage: $0 {start|stop}"
 exit 1
esac

exit 0

你可能感兴趣的:(LVS+Keepalived(DR模式)实现WEB服务器的高可用负载均衡)