tar -zxvf  keepalived-1.1.15.tar.gz

cd keepalived-1.1.15 

./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-238.9.1.el5-x86_64/

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/

mkdir /etc/keepalived

 

vi keepalived.conf

global_defs {

    notification_email {
          xxx@126.com
                       }
    notification_email_from keepalvied@gmail.com
    smtp_server 127.0.0.1
   # smtp_connect_timeout 30
    router_id LVS_DEVEL
            }
vrrp_script check_run {
     script "/usr/local/sbin/80.sh"
     interval 5
                      }
 
vrrp_instance VI_1 {
     state MASTER
     interface eth0
     virtual_router_id 52
     mcast_src_ip 192.168.1.239
     priority 100
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
                    }
     virtual_ipaddress {
         210.14.XX.XX/27
                       }
       track_script {
       check_run 
                    }
}
 
附上80.sh的脚本
#!/bin/bash
ngixpid=`ps -C nginx --no-header |wc -l`
#
if [ $ngixpid -eq 0 ];
then
/usr/local/nginx/sbin/nginx
     sleep 5
#
newnginxpid=`ps -C nginx --no-header |wc -l`
if [ $newnginxpid -eq 0 ];
then
/etc/init.d/keepalived stop
fi
#
fi
sleep 5
 
 
注意:state MASTER/BACKUP的主从位置不能搞反了; interface后面接的网卡必须是现实存在的,我这里是eth0,设置的mcast_src_ip 192.168.1.239地址必须和前面的网卡,我这儿是eth0,里面的地址一致;virtual_router_id 52,里面的id设置成52,网上很多写51,我也尝试过,但是没成功,查资料获知一个局域网里面只能有唯一的一个id,所以我估计在我在那个网络里面有这个51存在了,所以就改设置为52.
 
因为是虚拟的IP,所以可以用ip add list去查看网卡IP绑定的情况。