1.4.2.   服务器2keepalived配置
! Configuration File for keepalived
global_defs {
notification_email {
master@extmail.org
}
notification_email_from yunlei_ma@extmail.org
smtp_server mail.extmail.org
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_mongodb {
script "/etc/keepalived/mongodb_check.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mongodb
}
virtual_ipaddress {
192.168.1.18
}
}
  1.4.3.   Mongodb服务检测脚本
/etc/keepalived/mongodb_check.sh
#!/bin/bash
daemonid=`ps -ef |grep /usr/local/mongodb/bin/mongod |grep dbpath|awk '{print $2}'`
if [ "$daemonid" != "" ];then
echo $daemonid
exit 0
else
echo $daemonid
/etc/init.d/keepalived stop
exit 1
fi
  1.5.    服务器防火墙设置
  iptables –I INPUT –p tcp –dport 27017 –j ACCEPT
  iptables –I INPUT –p udp –dport 27017 –j ACCEP
  /etc/init.d/iptables save
  /etc/init.d/iptables restart
  1.6.    双机切换测试
  1.6.1.   数据库停止
  数据库停止后,keepalived也将停止,公共IP自动转移至备用机。当宕机的数据库重启后,数据将自动从主服务器同步。
  1.6.2.   Keepalived停止
  Keepalived停止后,公共IP自动完成转移,不影响数据库。