测试服务器
主:10.0.0.10
备:10.0.0.8
主服务器keepalived配置文件
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script checkhaproxy
{
script "/etc/keepalived/1.3.5/check_self.sh"
interval 5
}
vrrp_script check_tomcat
{
script "/etc/keepalived/1.3.5/check_tomcat.sh"
interval 5
}
vrrp_instance VI_1 {
#注意主备参数选择
state MASTER #主 #修改点, 主机为 MASTER,备机为 BACKUP
# state BACKUP #备
interface eth0 #改成本机网卡名 例如 eth0
virtual_router_id 51
nopreempt #非抢占模式
# preempt_delay 10
priority 50 #主高于备, 例如 主 50,备 30
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 10.0.0.10 #本机内网 IP
unicast_peer {
10.0.0.8 #对端设备的 IP 地址,例如:10.0.0.1
}
virtual_ipaddress {
10.0.0.17 #内网 VIP
}
notify_master "/etc/keepalived/1.3.5/notify_action.sh MASTER"
notify_backup "/etc/keepalived/1.3.5/notify_action.sh BACKUP"
notify_fault "/etc/keepalived/1.3.5/notify_action.sh FAULT"
notify_stop "/etc/keepalived/1.3.5/notify_action.sh STOP"
garp_master_delay 1
garp_master_refresh 5
track_interface {
eth0 #改成本机网卡名 例如 eth0
}
track_script {
checkhaproxy
check_tomcat
}
}
check_self.sh
#!/bin/bash
#/etc/keepalived/1.3.5/check_self.sh
vip=10.0.0.17 #请您改成内网 VIP
interface=eth0 #您的网络接口名
state_file=/var/keepalived/state
vip_last_check_result_file=/var/keepalived/vip_last_check_result
vip_operater=/etc/keepalived/1.3.5/vip.py
state=`cat $state_file`
log_file=/var/log/keepalived.log
log_write()
{
echo "[`date '+%Y-%m-%d %T'`] $1" >> $log_file
}
[ ! -d /var/keepalived/ ] && mkdir -p /var/keepalived/
[ ! -f $vip_last_check_result_file ] && touch $vip_last_check_result_file
[ ! -f $state_file ] && echo -n "FAULT" > $state_file
CMD=`ip addr show dev $interface | grep $vip | awk '{print $2}' | awk -F/ '{print $1}'| wc -l`
case $state in
"MASTER")
if [ ${CMD} -ne 1 ]; then
log_write "it is detected no vip on nic in cvm in MASTER state, add vip on this nic"
ip addr add $vip dev $interface
echo -n "false" > $vip_last_check_result_file
else
is_vip_in_master=`timeout 3 python $vip_operater query`
if [ "x$is_vip_in_master" == "xfalse" ]; then
echo -n "false" > $vip_last_check_result_file
python $vip_operater migrate &
elif [ "x$is_vip_in_master" == "xtrue" ]; then
vip_last_check_result=`cat $vip_last_check_result_file`
[ "x$vip_last_check_result" == "xfalse" ] && log_write " vip_check pass"
echo -n "true" > $vip_last_check_result_file
else
log_write "$vip_operater check vip time out"
fi
fi
exit 0
;;
*)
if [ ${CMD} -ne 0 ]; then
sleep 2
CMD=`ip addr show dev eth0 | grep $vip | awk '{print $2}' | awk -F/ '{print $1}'| wc -l`
if [ ${CMD} -ne 0 ]; then
log_write "detect vip in non-MASTER status, so ystemctl restart keepalived"
ip addr del $vip dev $interface
systemctl restart keepalived &
exit 1
fi
fi
exit 0
;;
esac
check_tomcat.sh
#!/bin/bash
CATALINA_HOME=/usr/local/tomcat/8.5.30-8080
TOMCAT_PROCESS=`ps -ef | grep $CATALINA_HOME/ | grep -v grep | grep -v tail | wc -l`
log_file=/etc/keepalived/1.3.5/check_tomcat-8080.log
if [ "$TOMCAT_PROCESS" -eq 0 ]; then
echo "[`date '+%Y-%m-%d %T'`] tomcat is stop" >> $log_file
sleep 2
exit 1
fi
~
备服务器设置与主一直,只有ip不同
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script checkhaproxy
{
script "/etc/keepalived/1.3.5/check_self.sh"
interval 5
}
vrrp_script check_tomcat
{
script "/etc/keepalived/1.3.5/check_tomcat.sh"
interval 5
}
vrrp_instance VI_1 {
#注意主备参数选择
# state MASTER #主 #修改点, 主机为 MASTER,备机为 BACKUP
state BACKUP #备
interface eth0 #改成本机网卡名 例如 eth0
virtual_router_id 51
nopreempt #非抢占模式
# preempt_delay 10
priority 30 #主高于备, 例如 主 50,备 30
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 10.0.0.8 #本机内网 IP
unicast_peer {
10.0.0.10 #对端设备的 IP 地址,例如:10.0.0.1
}
virtual_ipaddress {
10.0.0.17 #内网 VIP
}
notify_master "/etc/keepalived/1.3.5/notify_action.sh MASTER"
notify_backup "/etc/keepalived/1.3.5/notify_action.sh BACKUP"
notify_fault "/etc/keepalived/1.3.5/notify_action.sh FAULT"
notify_stop "/etc/keepalived/1.3.5/notify_action.sh STOP"
garp_master_delay 1
garp_master_refresh 5
track_interface {
eth0 #改成本机网卡名 例如 eth0
}
track_script {
checkhaproxy
check_tomcat
}
}