lvs+keepalived 高可用服务

lvs+keepalived 高可用服务

环境是ubuntu 14.04
1. 安装keepalived ,
sudo apt-get install keepalived
2. 安装lvs服务
sudo apt-get install ipvsadm

环境配置:
vip:192.168.199.200

lvs-master: 192.168.199.154
lvs-slave: 192.168.199.245

web-server1:192.168.199.234
web-server2:192.168.199.132
web-server3:192.168.199.194

master 的keepalived配置:

! Configuration File for keepalived
global_defs {
    notification_email {
        xxx@126.com
    }
    notification_email_from xxx@126.com
    smtp_server smtp.126.com
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}


vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.199.200
    }
}

virtual_server 192.168.199.200 80 {  
    delay_loop 6   
    lb_algo rr   
    lb_kind DR   
    nat_mask 255.255.255.0   
    #persistence_timeout 50   
    protocol TCP
    real_server 192.168.199.194 80 { #配置realaserver  
        weight 1   
        HTTP_GET { #监控配置   
            url {   
              path /ww/tests   
          status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
    real_server 192.168.199.132 80 {   
        weight 1   
        HTTP_GET {   
            url {   
              path /ww/tests   
              status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
}

slave 的keepalived配置:

! Configuration File for keepalived
global_defs {
    notification_email {
        qqqil@126.com
    }
    notification_email_from qqqil@126.com
    smtp_server smtp.126.com
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state SLAVE 
    interface eth0
    mcast_src_ip 192.168.199.245
    virtual_router_id 51
    priority 80
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.199.200
    }
}

virtual_server 192.168.199.200 80 {  
    delay_loop 6   
    lb_algo rr   
    lb_kind DR   
    nat_mask 255.255.255.0   
    #persistence_timeout 50   
    protocol TCP
    real_server 192.168.199.194 80 { #配置realaserver  
        weight 1   
        HTTP_GET { #监控配置   
            url {   
              path /ww/tests   
          status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
    real_server 192.168.199.132 80 {   
        weight 1   
        HTTP_GET {   
            url {   
              path /ww/tests   
              status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
}

lvs-dr 模式下虚拟服务的端口和真实的服务的端口要一致

web服务器的设置脚本realserver.sh:

#!/bin/bash
echo "begin to set realserver"
VIP=192.168.199.200
host=`/bin/hostname`
case "$1" in
    start)
        /sbin/ifconfig lo down
        /sbin/ifconfig lo up
        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
        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up  
        /sbin/route add -host $VIP dev lo:0
        ;;
    stop)
        /sbin/ifconfig lo:0 down
        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
        ;;
    status)
        islothere=`/sbin/ifconfig lo:0 |grep $VIP`
        isrothere=`netstat -rn |grep "lo:0" |grep $VIP`
        if [ ! "$islothere" -o ! "isrothere" ];then
            echo "LVS_DR real serve stopped"
        else
            echo "LVS-DR real server running"
        fi
        ;;
    *)
        echo "$0 :usage $0 {start|status|stop}"
        exit 1
        ;;
esac

设置真实服务器lvs:
./realserver.sh start

启动master,slave的keepalived服务:
sudo service keepalived start

检测lvs服务:

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.199.200:80 rr
  -> 192.168.199.132:80           Route   1      0          0         
  -> 192.168.199.194:80           Route   1      0          0

测试:
http://192.168.199.200

可以获得正确的返回,即配置正确

故障测试:
1. 停掉master上的keepalived服务
sudo service keepalived stop
http://192.168.199.200
仍能正常返回

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值