tomcat + keepalived 实现主备高可用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tomcat + keepalived 实现主备高可用相关的知识,希望对你有一定的参考价值。
tomacat7+ keepalived 高可用集群配置
服务器环境规划
node1: 10.10.3.16 app01
node2: 10.10.3.17 app02
vip:
软件环境规划
操作系统:oracle linux6.8 4.1.12-37.4.1.el6uek.x86_64
keepalived:keepalived-1.3.5
Java:jdk-1.7.0_80
Tomcat:apache-tomcat-7.0.81
配置要求:
tomcat主从,只有一台机器提供服务,另一台standby
安装keepalived(两台机器都需要安装)
1、准备:
yum install openssl-devel libnl3-devel ipset-devel iptables-devel libnfnetlink-devel
wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz
2、安装
tar zxf keepalived-1.3.5.tar.gz
cd keepalived-1.3.5
./configure --prefix=/usr/local/keepalived --with-init=SYSV --不指定的话,默认不生成RECL6的启动脚本
make && make install
3、准备脚本配置文件:
[[email protected] keepalived-1.3.5]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[[email protected] keepalived-1.3.5]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[[email protected] keepalived-1.3.5]# cp -p /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[[email protected] keepalived-1.3.5]# mkdir /etc/keepalived
[[email protected] keepalived-1.3.5]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[[email protected] local]# chkconfig --add keepalived
[[email protected] local]# chkconfig --list keepalived
[[email protected] local]# chkconfig keepalived on
[[email protected] local]# chmod +x /etc/init.d/keepalived
****以上部分,在两台机器上的操作完全相同
4、调整keepalived配置文件
node1
[[email protected] local]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/opt/tomcat.pid"
interval 1
weight -2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_interface {
eth0
}
track_script {
chk_httpp_port
}
virtual_ipaddress {
10.10.3.200
}
}
node2
[[email protected] local]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/opt/tomcat.pid"
interval 1
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99 #此处不同,要低于master
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_interface {
eth0
}
track_script {
chk_httpp_port
}
virtual_ipaddress {
10.10.3.200
}
}
5、在两台机器上编辑监控脚本
[[email protected] local]# vim /opt/tomcat.pid
#!/bin/bash
# keepalived for tomcat
# edited by sky: 2017-09-12
PS_NUM=`ps -C java --no-headers |wc -l`
if [ $PS_NUM -eq 0 ]; then
/etc/init.d/keepalived stop
fi
6、在两台机器上编写tomcat启动脚本(相同)
vim /etc/init.d/tomcat
#!/bin/bash
#chkconfig: 2345 10 90
#description: Starts and Stops the tomcatdaemon
#edited by sky: 2017-09-12
CATALINA_HOME=/app/tomcat
JAVA_HOME=/usr/local/jdk1.7.0_80
export CATALINA_HOME JAVA_HOME
start_tomcat=$CATALINA_HOME/bin/startup.sh
stop_tomcat=$CATALINA_HOME/bin/shutdown.sh
if [ ! -f $CATALINA_HOME/bin/catalina.sh ]
then
echo "===Tomcat is not available.==="
exit
fi
start()
{
echo -n "===Starting tomcat===:"
${start_tomcat}
echo "===tomcat start ok.==="
}
stop()
{
echo -n "===Shutting down tomcat===:"
${stop_tomcat}
echo "===tomcat stop ok.==="
}
status()
{
PS_NUM=`ps -C java --no-headers | wc -l`
if [ $PS_NUM -gt 0 ]
then
echo -e "tomcat ( pid = `ps -C java --no-headers |awk ‘{print $1}‘`) is running...\n"
else
echo "tomcat is stopped"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 1
start
;;
status)
status
;;
*)
echo "Usage:$0 {start|stop|restart|status}"
exit 1
esac
exit 0
# shell end.
7、在两台机器上分别启动tomcat和keepalived
service tomcat start
service keepalived start
测试:
可关闭master上的tomcat, 则可以看到vip会漂移到backup服务器, 启动master的tomcat服务器,然后再重启keepalived,则vip会自动漂移到master
8、检查日志:
tail -f /var/log/messages
9、检查vip是否在主服务器上
ifconfig
10、vip切换及恢复原则:
1、切换:master服务器的tomcat服务停止,则master上的keepalived会自动停止, vip漂移到backup服务器
2、恢复: master服务器修复完毕后, 请分别启动tomcat和keepalived,则vip会自动漂移到master服务器
本文出自 “sky” 博客,转载请与作者联系!
以上是关于tomcat + keepalived 实现主备高可用的主要内容,如果未能解决你的问题,请参考以下文章
MySQL进阶Keepalived1.4.0结合MySQL 5.7.19实现主备高可用
Mysql + Keepalived 实现双主互备高可用详细配置