keepalived配置tomcat双机

使用keepalived解决tomcat单点故障问题
服务器1:192.168.10.26
服务器2:192.168.10.27
场景描述:每台服务器除了一个tomcat程序外还有一个使用脚本启动的后台jar程序
实现目标:每台服务器宕机或服务器中的一个应用挂掉后,关闭keepalived,vip漂至备机,通过脚本监测备机是否已经启动tomcat或jar应用来,若未启动则将程序启动。

安装配置keepalived

#安装相关包

yum -y install gcc make openssl openssl-devel wget kernel-devel

#下载keepalived包

#解压:

#安装keepalived-1.2.16.tar.gz

./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.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/

chmod +x /etc/init.d/keepalived

chkconfig keepalived on

3 创建编辑配置文件

mkdir -p /etc/keepalived/

主机:keepalived.conf配置文件:

! Configuration File for keepalived
vrrp_script chk_http_port {   
       script "/etc/keepalived/shell/check.sh"         ###监控脚本   
       interval 10                        ###监控时间   
       weight 2                                ###目前搞不清楚   
}  
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 11111
    }
    virtual_ipaddress {
        192.168.5.18
    }
   notify_master /etc/keepalived/shell/to_master.sh 
    track_script {   
      chk_http_port                     ### 执行监控的服务   
    } 
}

备机:keepalived.conf配置文件:

! Configuration File for keepalived
vrrp_script chk_http_port {   
       script "/etc/keepalived/shell/check.sh"         ###监控脚本   
       interval 10                        ###监控时间   
       weight 2                                  
}  
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 11111
    }
    virtual_ipaddress {
        192.168.5.18
    }
   notify_master /etc/keepalived/shell/to_master.sh 
    track_script {   
      chk_http_port                     ### 执行监控的服务   
    } 
}

脚本:to_master.sh

#!/bin/bash
source /etc/profile
netstat -lntup|grep 8080
if [ $? -ne 0 ]
then
   cd /mvtech/tomcat
   bash +x /mvtech/tomcat/bin/startup.sh
fi
ps -ef|grep /mvtech/work/isms.jar|grep StartServer
if [ $? -ne 0 ]
then
   cd /mvtech/work   
   bash +x /mvtech/work/startUp.sh 
   exit;
fi

脚本:check.sh

#!/bin/bash
ip add |grep 192.168.5.25
if [ $? -eq 0 ]
then
	netstat -lntup|grep 8080
	if [ $? -ne 0 ]
	then
	   bash +x /etc/keepalived/shell/keepalive-kill.sh;
	   exit;
	fi
	ps -ef|grep /mvtech/work/isms.jar|grep StartServer
	if [ $? -ne 0 ]
	then
	   bash +x /etc/keepalived/shell/keepalive-kill.sh;
	   exit;
	fi
fi

脚本:keepalive-kill.sh

#!/usr/bin/env bash
ps -ef|grep /mvtech/work/isms.jar|grep StartServer|awk '{print $2}'|xargs kill -9
ps -ef |grep /mvtech/tomcat |grep start|awk '{print $2}'|xargs kill -9
service keepalived stop

切换测试:
1、主机故障:确保vip漂在主机上并且备机已经启动keepalived,重启主服务器,监测vip状态
2、kill掉主机里的一个tomcat或jar进程,监测vip状态

原文地址:https://www.cnblogs.com/sdhzdtwhm/p/9644591.html