首页 > 代码库 > 实现keepalived + tomcat 双机热备

实现keepalived + tomcat 双机热备

1.安装keepalived

我这里是给予keepalived+mysql 主主双机热备基础上做的

global_defs {
router_id HA_MySQL
}

vrrp_instance VI_1 {
state BACKUP
interface bond0
virtual_router_id 51
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.69.37.119
}
track_script {
chk_tomcat
}
}

virtual_server 10.69.37.119 3306 {
delay_loop 2
# lb_algo rr
# lb_kind NAT
persistence_timeout 20
protocol TCP
real_server 10.69.37.75 3306{
weight 3
notify_down /usr/local/keepalived_sh/keepalived_shoudown.sh
TCP_CHECK {
connect_timeout 4
nb_get_retry 3
delay_before_retry 3
}
}
}

vrrp_script chk_tomcat {
#script "killall -0 nginx"
script "/usr/local/keepalived_sh/tomcat_check.sh"
interval 2
weight -5
fall 3
rise 2
}

下面是tomcat_check.sh

#!/bin/bash
JAVA_PROCESS=`ps -C java --no-heading| wc -l`
if [ $JAVA_PROCESS -eq 0 ];then
echo "tomcat is stop"
sleep 2
if [ `ps -C java --no-heading| wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi

结束

ps -C java --no-heading| wc -l 

这个命令是查询tomcat是否存在  结果为0不存在为1存在

 

实现keepalived + tomcat 双机热备