#######keepdalived+lvs################

########keepalived安装################

######内核结合否则不能vrrp####
ln -s /usr/src/kernels/2.6.18-194.el5-i686/ /usr/src/linux

yum -y install yum-fastestmirror

yum -y install gcc
###在编译时,keepalived+lvs依赖openssl和内核源代码 ######
yum -y install openssl-devel kernel-devel

ln -s /usr/src/kernels/2.6.18-308.11.1.el5-i686/ /usr/src/linux

wget

tar zxvf keepalived-1.1.19.tar.gz

cd keepalived-1.1.19

./configure --prefix=/usr/local/keepalived/

###前三项 Use IPVS Framework ,IPVS sync daemon support,Use VRRP Framework为yes 编译成功##########

make

make install

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/  ###命令

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/  ###配置文件
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/   ###启动项
chkconfig --level 35 keepalived on
mkdir /etc/keepalived
vi /etc/keepalived/keepalived.conf

global_defs {

        notification_email {

              

        }

        notification_email_from

        smtp_server 127.0.0.1

        smtp_connect_timeout 30

        router_id test1

 

}

 

vrrp_instance VI_1 {

        state BACKUP

        interface eth0

        virtual_router_id 51

        priority 100   #权重

        advert_int 1   #检测间隔

 
 nopreempt #不抢占 

        smtp_alert

        authentication {

                auth_type PASS

                auth_pass 123

        }

        virtual_ipaddress {

                192.168.1.100    #虚拟IP

        }

}

########## WEB负载均衡#############
virtual_server 192.168.1.100 80 {
    delay_loop 6                  #(每隔10秒查询realserver状态)
    lb_algo wlc                  #(lvs 算法)
    lb_kind DR                  #(Direct Route)
    persistence_timeout 60        #(同一IP的连接60秒内被分配到同一台realserver)
    protocol TCP                #(用TCP协议检查realserver状态)
 
    real_server 192.168.1.80 80 {
        weight 100               #(权重)
        TCP_CHECK {
        connect_timeout 10       #(10秒无响应超时)
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.1.81 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

############# LVS 安装##############

wget

tar -zxvf ipvsadm-1.24.tar.gz

cd ipvsadm-1.24

make
make install

###若ipvsadm 能查看则证明安装成功

###ip a 查看虚拟IP

###########在备用服务器上########

如复制虚拟机网卡不能启动,则删除ifcfg-eth0 中的HWADDR(网卡MAC)
priority 90   #权重

###若要双方不抢占,则双方都要关闭防火墙 iptales -F

或者,iptables命令双方添加组播地址:iptables -I RH-Firewall-1-INPUT -d 224.0.0.18 -j ACCEPT (若重启iptables 会失效)

#####在每一台real上运行增加lookup#####

vi /root/lvs.sh

#!/bin/bash

SNS_VIP=192.168.1.100  ###虚拟IP

 
/etc/rc.d/init.d/functions
 
case "$1" in
start)
       /sbin/ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
       /sbin/route add -host $SNS_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 $SNS_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