lvs+keepalived配置负载均衡

为了解决单点故障以及单台服务器的压力问题,在统一访问接口的同时,尽量将访问分发到多台web服务器,于是选择了LVS+Keepalived来实现负载均衡。

1、安装keepalived

apt-get install keepalived

2、修改 /etc/keepalived/keepalived.conf

! Configuration file for keepalived
global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.60.131
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

#virtual_server_group BOSS_WEB_BALANCE {
#        192.168.60.200
        #192.168.50.118
        #192.168.60.119
        #192.168.50.119
#}

vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.60.201
        #192.168.50.118
        #192.168.60.119
        #192.168.50.119
    }
}

virtual_server 192.168.60.201 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 300
    protocol TCP

    real_server 192.168.60.119 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
             nb_get_retry 3
             delay_before_retry 3
             connect_port 80
           }
    }
}

 3、启动/终止 keepalived

/etc/init.d/keepalived start|stop

配置虚拟IP

创建realserver.sh,放在 /usr/local/test_sh/lvs/, 具备如下内容

#!/bin/bash
# Written by NetSeek
# description: Config realserver lo and apply noarp
WEB_VIP=192.168.60.118

case "$1" in
start)
       ifconfig lo:0 $WEB_VIP netmask 255.255.255.255 broadcast $WEB_VIP
       /sbin/route add -host $WEB_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 $WEB_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"
       ;;
status)
        # Status of LVS-DR real server.
        islothere=`/sbin/ifconfig lo:0 | grep $WEB_VIP`
        isrothere=`netstat -rn | grep "lo:0" | grep $web_VIP`
        if [ ! "$islothere" -o ! "isrothere" ];then
            # Either the route or the lo:0 device
            # not found.
            echo "LVS-DR real server Stopped."
        else
            echo "LVS-DR Running."
        fi
;;
*)
        # Invalid entry.
        echo "$0: Usage: $0 {start|status|stop}"
        exit 1
;;
esac
exit 0

 启动/停止虚拟IP命令

cd /usr/local/boss_sh/lvs
./realserver.sh start|stop

讲解:使用LVS+Keepalived来实现负载均衡的配置的要点

1、最好配置热备,且使用两个虚拟IP,两台服务器相互为对方的备份,甚至可以使用3台Server来配置(为了更安全)

2、安装LVS尽量使用apt-get来安装,下载源码编译,不一定能成功

3、可以使用ipvsadm来查看LVS的状态

4、可以查看/var/log/message中的日志文件来了解LVS的状态

          http://linux.chinaunix.net/techdoc/net/2009/07/21/1125256.shtml

相关推荐