1.首先安装lvs

yum -y install  popt-static   libnl libnl-devel  popt-devel  kernel-devel

wget
ln -s /usr/src/kernels/2.6.32-358.2.1.el6.x86_64/  /usr/src/linux
tar zxvf ipvsadm-1.26.tar.gz
cd ipvsadm-1.26
make
make install
cd ..

2.安装keepalived. 

参见本博客

3.配置keepalived配置文件

vi /etc/keepalived/keepalived.conf

global_defs {

 
notification_email {
               aaaaa
        }
        notification_email_from
        smtp_server mail.test.com
        smtp_connect_timeout 30
        router_id LVS1
}
 
vrrp_sync_group test {
group {
        loadbalance
}
}
 
vrrp_instance loadbalance {
        state MASTER                   #备机修改为BACKUP
        interface eth0
        lvs_sync_daemon_inteface eth0
        virtual_router_id 51
        priority 180                        #备机修改优先级150
        advert_int 1
 
authentication {
        auth_type PASS
        auth_pass 1111
}
 
virtual_ipaddress {
        8.8.8.8 dev eth0 label eth0:2

}

}
 
 
virtual_server 8.8.8.8 3306 {
        delay_loop 6
        lb_algo wrr                #加权轮循
        lb_kind DR                #DR模式
        #persistence_timeout 20
        protocol TCP
        real_server 8.8.8.6 3306 {
        weight 3
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306
}
}
        real_server 8.8.8.7 3306 {
        weight 3
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306
}
}
}

4配置lvs 的real server

#!/bin/bash

#description : Start Real Server
VIP=8.8.8.8
. /etc/rc.d/init.d/functions
case "$1" in
    start)
        echo " Start LVS  of  Real Server"
 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 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
        ;;
    stop)
        /sbin/ifconfig lo:0 down
        echo "close LVS Director server"
        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 "Usage: $0 {start|stop}"
        exit 1
esac