RHEL 5.4下部署LVS(DR)+Keepalived实现高性能高可用负载均衡

一、简介

LVS+Keepalived能实现的功能:利用LVS控制器主备模式避免单点故障以及自动删除故障WEB服务器结点并当它恢复后再自动添加到群集中。

拓扑图:

RHEL 5.4下部署LVS(DR)+Keepalived实现高性能高可用负载均衡

二、系统环境

系统平台:RHEL 5.4

LVS版本:ipvsadm-1.24

keepalived版本:keepalived-1.1.15

三、安装

1、在两台LVS Server上安装lvs、keepalived软件(LVS_MASTER & LVS_BACKUP)
lvs install -------------

[root@LVS-MASTER ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

[root@LVS-MASTER ~]# ln -s/usr/src/kernels/2.6.18-194.el5-i686//usr/src/linux/

[root@LVS-MASTER ~]# tar zxvf ipvsadm-1.24.tar.gz

[root@LVS-MASTER ~]# cd ipvsadm-1.24

[root@LVS-MASTER ipvsadm-1.24]# make && make install

Keepalived install -------------

[root@LVS-MASTER ~]# wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

[root@LVS-MASTER ~]# tar zxvf keepalived-1.1.15.tar.gz

[root@LVS-MASTER ~]# cd keepalived-1.1.15

[root@LVS-MASTER keepalived-1.1.15]# ./configure && make && make install

######### 将keepalived做成启动服务,方便管理##########

[root@LVS-MASTER ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/

[root@LVS-MASTER ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

[root@LVS-MASTER ~]# mkdir /etc/keepalived/

[root@LVS-MASTER ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

[root@LVS-MASTER ~]# cp /usr/local/sbin/keepalived /usr/sbin/

[root@LVS-MASTER ~]# service keepalived start | stop

2、keepalived的配置

###################  MASTER ###################

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.148
    }
}

virtual_server 10.0.0.148 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 10.0.0.137 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 10.0.0.139 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

 

#################### BACKUP ###################

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.148
    }
}

virtual_server 10.0.0.148 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 10.0.0.137 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 10.0.0.139 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

3、在两台Web Server上执行realserver.sh脚本,为lo:0绑定VIP地址10.0.0.148、抑制ARP广播。

[root@web1 ~]# cat realserver.sh
#!/bin/bash
#description: Config realserver

VIP=10.0.0.148
 
/etc/rc.d/init.d/functions
 
case "$1" in
start)
       /sbin/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)
       /sbin/ifconfig lo:0 down
       /sbin/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

[root@web1 ~]# sh realserver.sh start

相关推荐