1 如何实现 Tomcat 服务双机热备?-德赢Vwin官网 网
0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

如何实现 Tomcat 服务双机热备?

马哥Linux运维 来源:未知 作者:工程师李察 2018-07-28 08:36 次阅读

1. 规划

1.1服务器环境规划

负载服务器master及WEB服务器1真实IP:10.10.195.53

负载服务器backup及WEB服务器2真实IP:10.10.195.190

负载服务器虚拟ip:10.10.195.212

1.2 软件环境规划

操作系统:Red Hat Enterprise Linux Server release 5.6 (Tikanga)

keepalived:keepalived-1.2.19

Java:jdk-1.7.0_79

Tomcat:apache-tomcat-7.0.64

2. 负载服务器配置

这里只叙述如何安装配置keepalived,至于java以及tomcat的安装及配置,这里不赘述。

2.1 安装keepalived

[~]tar -zxvf keepalived-1.2.19.tar.gz

[~] cd keepalived-1.2.19

[keepalived-1.2.19] ./configure --prefix=/usr/local/keepalived --disable-fwmark

#(如果直接输入./configure有可能报错:configure:error: No SO_MARK declaration in headers)

[keepalived-1.2.19] make

[keepalived-1.2.19] make install

2.2 配置keepalived服务

[keepalived-1.2.19] cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

[keepalived-1.2.19] cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

[keepalived-1.2.19] cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

[keepalived-1.2.19] mkdir /etc/keepalived

[keepalived-1.2.19] cp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/

[keepalived-1.2.19] chkconfig --add keepalived

[keepalived-1.2.19] chkconfig keepalived on

重启\启动\关闭\查看状态keepalived

service keepalived restart

service keepalived start

service keepalived stop

service keepalived status

2.3 配置keepalived.conf文件

[~]mv /etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf.bak

[~]vi /etc/keepalived//keepalived.conf

主备机的keepalived.conf文件大致相同,只是注意红色标注的地方。

主机配置:

global_defs {

router_id NodeA

}

vrrp_script chk_http_port {

script "/opt/tomcat.pid"

interval 5

weight 2

}

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 52

priority 150

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_http_port

}

virtual_ipaddress {

10.10.195.212

}

}

备机配置:

global_defs {

router_id NodeB

}

vrrp_script chk_http_port {

script "/opt/tomcat.pid"

interval 5

weight 2

}

vrrp_instance VI_1 {

state BACKUP

interface eth0

virtual_router_id 52

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_http_port

}

virtual_ipaddress {

10.10.195.212

}

}

2.4配置/opt/tomcat.pid

#!/bin/bash

#description: check tomcat service anddecide whether stop the keepalived or not

#edited by zzh: 2015-10-14

CATALINA_HOME=/users/shr/apache-tomcat-7.0.64

JAVA_HOME=/users/shr/util/JavaDir/jdk

export CATALINA_HOME

export JAVA_HOME

ps ax --width=1000 | grep"org.apache.catalina.startup.Bootstrap start" | grep -v"grep" | awk '{printf $1 " "}' | wc | awk '{print $2}' >tomcat_process_count.txt

read line < tomcat_process_count.txt  

start_tomcat=$CATALINA_HOME/bin/startup.sh

if [ ${line} -lt 1 ]

then

echo -n "===Starting tomcat===:"

${start_tomcat}

# :sudo service tomcat start

echo "===tomcat start ok.==="

sleep 3

# check the tomcat status.

ps ax --width=1000 | grep "org.apache.catalina.startup.Bootstrapstart" | grep -v "grep" | awk '{printf $1 " "}' | wc |awk '{print $2}' > tomcat_process_count.txt

read line2 < tomcat_process_count.txt  

if [ ${line2} -lt 1 ]

then

sudo service keepalived stop

fi

fi

rm tomcat_process_count.txt

#shell end.

3. 日志查看

3.1 正常启动

输入tail -f /var/log/messages查看启动keepalived日志:

(MASTER:10.10.195.53)

Sep 29 15:49:16 shr Keepalived[5536]:Starting Keepalived v1.2.19 (09/21,2015)

Sep 29 15:49:16 shrKeepalived_healthcheckers[5538]: Netlink reflector reports IP 10.10.195.53added

Sep 29 15:49:16 shrKeepalived_healthcheckers[5538]: Netlink reflector reports IP 10.10.195.53added

Sep 29 15:49:16 shrKeepalived_healthcheckers[5538]: Registering Kernel netlink reflector

Sep 29 15:49:16 shrKeepalived_healthcheckers[5538]: Registering Kernel netlink command channel

Sep 29 15:49:16 shrKeepalived_healthcheckers[5538]: Opening file'/etc/keepalived/keepalived.conf'.

Sep 29 15:49:16 shrKeepalived_healthcheckers[5538]: Configuration is using : 6572 Bytes

Sep 29 15:49:16 shr Keepalived[5537]:Starting Healthcheck child process, pid=5538

Sep 29 15:49:16 shr Keepalived_healthcheckers[5538]:Using LinkWatch kernel netlink reflector...

Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Netlink reflector reports IP 10.10.195.53 added

Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Netlink reflector reports IP 10.10.195.53 added

Sep 29 15:49:16 shr Keepalived[5537]:Starting VRRP child process, pid=5539

Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Registering Kernel netlink reflector

Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Registering Kernel netlink command channel

Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Registering gratuitous ARP shared channel

Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Opening file '/etc/keepalived/keepalived.conf'.

Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Configuration is using : 36541 Bytes

Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Using LinkWatch kernel netlink reflector...

Sep 29 15:49:16 shr Keepalived_vrrp[5539]:VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]

Sep 29 15:49:17 shr Keepalived_vrrp[5539]:VRRP_Instance(VI_1) Transition to MASTER STATE

Sep 29 15:49:17 shr Keepalived_vrrp[5539]:VRRP_Instance(VI_1) Received lower prio advert, forcing new election

Sep 29 15:49:18 shr Keepalived_vrrp[5539]:VRRP_Instance(VI_1) Entering MASTER STATE

Sep 29 15:49:18 shr Keepalived_vrrp[5539]:VRRP_Instance(VI_1) setting protocol VIPs.

Sep 29 15:49:18 shr Keepalived_vrrp[5539]:VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.195.212

Sep 29 15:49:18 shr Keepalived_vrrp[5539]:Netlink reflector reports IP 10.10.195.212 added

Sep 29 15:49:18 shrKeepalived_healthcheckers[5538]: Netlink reflector reports IP 10.10.195.212added

Sep 29 15:49:23 shr Keepalived_vrrp[5539]:VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.195.212

(BACKUP:10.10.195.190)

Sep 29 15:46:25 server1 Keepalived[18218]:Starting Keepalived v1.2.19 (09/25,2015)

Sep 29 15:46:25 server1Keepalived_healthcheckers[18220]: Netlink reflector reports IP 10.10.195.190added

Sep 29 15:46:25 server1Keepalived_healthcheckers[18220]: Netlink reflector reports IP 10.10.195.190added

Sep 29 15:46:25 server1Keepalived_healthcheckers[18220]: Registering Kernel netlink reflector

Sep 29 15:46:25 server1Keepalived_healthcheckers[18220]: Registering Kernel netlink command channel

Sep 29 15:46:25 server1 Keepalived_healthcheckers[18220]:Opening file '/etc/keepalived/keepalived.conf'.

Sep 29 15:46:25 server1Keepalived_healthcheckers[18220]: Configuration is using : 6682 Bytes

Sep 29 15:46:25 server1 Keepalived[18219]:Starting Healthcheck child process, pid=18220

Sep 29 15:46:25 server1 Keepalived[18219]:Starting VRRP child process, pid=18221

Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Netlink reflector reports IP 10.10.195.190 added

Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Netlink reflector reports IP 10.10.195.190 added

Sep 29 15:46:25 server1Keepalived_healthcheckers[18220]: Using LinkWatch kernel netlink reflector...

Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Registering Kernel netlink reflector

Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Registering Kernel netlink command channel

Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Registering gratuitous ARP shared channel

Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Opening file '/etc/keepalived/keepalived.conf'.

Sep 29 15:46:25 server1 Keepalived_vrrp[18221]:Configuration is using : 36651 Bytes

Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Using LinkWatch kernel netlink reflector...

Sep 29 15:46:25 server1Keepalived_vrrp[18221]: VRRP_Instance(VI_1) Entering BACKUP STATE

Sep 29 15:46:25 server1 Keepalived_vrrp[18221]:VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]

3.2 主备切换

l 当在主机(MASTER:10.10.195.53)中输入 sudoservice keepalived stop,此时就会进行主备切换,主机切换成备机。

主机(MASTER:10.10.195.53)输出如下

Oct 14 13:25:09 shr Keepalived_vrrp[26683]:VRRP_Instance(VI_1) sending 0 priority

Oct 14 13:25:09 shr Keepalived_vrrp[26683]:VRRP_Instance(VI_1) removing protocol VIPs.

Oct 14 13:25:09 shrKeepalived_healthcheckers[26682]: Netlink reflector reports IP 10.10.195.212removed

Oct 14 13:25:09 shr Keepalived[26681]:Stopping Keepalived v1.2.19 (09/21,2015)

备机(BACKUP:10.10.195.190)输出如下

Oct 14 13:19:58 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Transition to MASTER STATE

Oct 14 13:19:59 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Entering MASTER STATE

Oct 14 13:19:59 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) setting protocol VIPs.

Oct 14 13:19:59 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for10.10.195.212

Oct 14 13:19:59 server1 Keepalived_healthcheckers[30889]:Netlink reflector reports IP 10.10.195.212 added

Oct 14 13:19:59 server1Keepalived_vrrp[30890]: Netlink reflector reports IP 10.10.195.212 added

Oct 14 13:20:04 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for10.10.195.212

l 当在主机(MASTER:10.10.195.53)中输入sudoservice keepalived start,此时就会切换成主机。备机(BACKUP:10.10.195.190)输出如下信息

Oct 14 13:25:11 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Received higher prio advert

Oct 14 13:25:11 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Entering BACKUP STATE

Oct 14 13:25:11 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) removing protocol VIPs.

Oct 14 13:25:11 server1Keepalived_healthcheckers[30889]: Netlink reflector reports IP 10.10.195.212removed

Oct 14 13:25:11 server1Keepalived_vrrp[30890]: Netlink reflector reports IP 10.10.195.212 removed

4. 查看虚拟ip

可以通过ip add show命令查看添加的虚拟ip:

[shr@shr bin]$ip add show

1: lo: mtu16436 qdisc noqueue

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000

link/ether 00:0c:29:6b:f2:a8 brd ff:ff:ff:ff:ff:ff

inet 10.10.195.53/24 brd 10.10.195.255 scope global eth0

inet 10.10.195.212/32scope global eth0

5. 常见错误

5. 1常见错误一:

/var/log/messages has thousands of errorslike this:

Jun 28 09:18:32 rust Keepalived_vrrp:receive an invalid ip number count

associated with VRID!

Jun 28 09:18:32 rust Keepalived_vrrp: bogusVRRP packet received on eth0 !!!

Jun 28 09:18:32 rust Keepalived_vrrp:VRRP_Instance(VI_1) Dropping received

VRRP packet...

The backup director starts up, but doesn'tlisten on the virtual addresses

at all. Its /var/log/messages has thousands of errors like this:

Jun 28 06:25:05 stye Keepalived_vrrp:receive an invalid ip number count

associated with VRID!

Jun 28 06:25:05 stye Keepalived_vrrp: bogusVRRP packet received on eth0 !!!

Jun 28 06:25:05 stye Keepalived_vrrp:VRRP_Instance(VI_1) ignoring received

advertisment...

解决方法:

改变配置文件/etc/keepalived/keepalived.conf中virtual_router_id为另一个值即可。

(changed the vrid to another number and it worked fine).

5.2 常见错误二:

主机执行到VRRP_Instance(VI_1) Entering BACKUP STATE之后没有执行VRRP_Instance(VI_1)setting protocol VIPs。

解决方法:

可能是配置文件keepalived.conf中{左边没有空格。

linux添加tomcat服务

在/etc/init.d中输入 sudo vim tomcat

脚本具体内容如下:

#!/bin/bash

#chkconfig: 2345 10 90

#description: Starts and Stops the tomcatdaemon

#edited by ZZH: 2015-10-14

CATALINA_HOME=/users/shr/apache-tomcat-7.0.64

JAVA_HOME=/users/shr/util/JavaDir/jdk

export CATALINA_HOME

export 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 ax --width=1000 | grep"org.apache.catalina.startup.Bootstrap start" | grep -v"grep" | awk '{printf $1 " "}' | wc | awk '{print $2}'> tomcat_process_count.txt

readline < tomcat_process_count.txt  

rmtomcat_process_count.txt

if[ $line -gt 0 ]

then

echo-n "tomcat ( pid = "

ps ax --width=1000 | grep"org.apache.catalina.startup.Bootstrap start" | grep -v"grep" | awk '{printf $1 " "}' | awk '{print $1}'> tomcat_process_pid.txt

readpid < tomcat_process_pid.txt  

rmtomcat_process_pid.txt

echo-n $pid

echo-n ") is running..."

echo

else

echo"tomcat is stopped"

fi

}

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

stop

sleep1

start

;;

status)

status

;;

*)

echo"Usage:$0 {start|stop|restart}"

exit1

esac

exit 0

# shell end.

在终端输入:

sudo chmod 755 tomcat

sudo chkconfig --add tomcat

tomcat 启动 service tomcatstart

tomcat 关闭 service tomcatstop

tomcat 重启 service tomcatrestart

tomcat状态查看 service tomcatstatus

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表德赢Vwin官网 网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 负载
    +关注

    关注

    2

    文章

    564

    浏览量

    34325
  • 服务器
    +关注

    关注

    12

    文章

    9123

    浏览量

    85319
  • JAVA
    +关注

    关注

    19

    文章

    2966

    浏览量

    104700

原文标题:keepalived 实现 Tomcat 服务双机热备

文章出处:【微信号:magedu-Linux,微信公众号:马哥Linux运维】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    防火墙双机命令行配置方案

    部署防火墙双机,避免防火墙出现单点故障而导致的网络瘫痪
    的头像 发表于 01-02 09:45 988次阅读
    防火墙<b class='flag-5'>双机</b><b class='flag-5'>热</b><b class='flag-5'>备</b>命令行配置方案

    浅析Tomcat服务

    Tomcat服务器是一个免费的开放源代码的Web应用服务器。因为Tomcat技术先进、性能稳定且免费,所以深受Java爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web
    发表于 07-16 06:23

    Tomcat服务器简介

    简介简介Tomcat服务器是一个开放源码的轻量级Web应用服务器,非常适合搭建微服务应用。Embedded Tomcat嵌入式
    发表于 12-16 08:24

    双机单片机系统内部通信接口的简化设计

    双机单片机系统内部通信接口的简化设计 分析了工业测控系统中双机单片机系统内部通信的特点
    发表于 10-01 22:02 13次下载

    双机与负载均衡的设计与实现

    针对OpenFlow SDN系统中的策略管理模块提出了一种新型的双机和负载均衡方案,采用了服务器集群的方式,通过Keepalived和Heartbeat
    发表于 12-24 17:57 10次下载

    基于EHW和双机技术的故障自修复电路系统设计

    基于EHW和双机技术的故障自修复电路系统设计_张峻宾
    发表于 01-07 18:39 0次下载

    双机和冷的区别

    双机特指基于高可用系统中的两台服务器的(或高可用),因两机高可用在国内使用较多,故得名
    发表于 11-21 15:15 4.3w次阅读
    <b class='flag-5'>双机</b><b class='flag-5'>热</b><b class='flag-5'>备</b>和冷<b class='flag-5'>备</b>的区别

    双机和集群的区别

    所谓双机,就是将中心服务器安装成互为备份的两台服务器,并且在同一时间内只有一台服务器运行。当
    发表于 11-21 15:37 1.3w次阅读

    VMWare7.0虚拟机双机图列教程资料免费下载

    本文档的主要内容详细介绍的是VMWare7.0虚拟机双机图列教程资料免费下载。
    发表于 06-12 15:59 8次下载

    推荐几款服务器的Windows与Linux双机软件

    服务商。今天小编就给大家推荐几款服务器的Windows与Linux双机软件! 众所周知,双机
    发表于 11-12 14:16 3074次阅读

    你知道国产双机双机的意思吗

    机的应用系统切换到另一台主机上继续运行,及时地进行错误隔绝、恢复,以最低成本实现用户几乎不停顿的业务应用。 但高可用软件也分为双机和冷
    的头像 发表于 06-29 10:46 2601次阅读
    你知道国产<b class='flag-5'>双机</b><b class='flag-5'>热</b><b class='flag-5'>备</b>与<b class='flag-5'>双机</b>冷<b class='flag-5'>备</b>的意思吗

    双机原理很神秘?看完这文,让你秒懂

    络,一旦该设备或该设备所在链路出现故障,就会极大地影响了用户的网络体验。网络可靠性技术中的双机功能就能很好的解决这个问题。新支点双机
    的头像 发表于 07-07 15:55 2375次阅读
    <b class='flag-5'>双机</b><b class='flag-5'>热</b><b class='flag-5'>备</b>原理很神秘?看完这文,让你秒懂

    企业实施服务双机方案存在重要意义

    随着网络信息不断的发展,现在很多企业对于服务器的要求比较苛刻,需要保证服务器无休止的运转服务,这对于服务器来说无疑会出现故障,给企业带来影响,那么怎么保证
    的头像 发表于 07-12 15:20 1773次阅读

    防火墙双机组网环境中的IPSecVPN实验步骤及配置

    FW1/FW2部署双机,采用主方式,FW1为主设备,FW2为设备;
    的头像 发表于 11-08 09:41 3314次阅读

    路由器功能介绍-双机备份机制

    我们以东用科技的ORB305系列工业级路由器为例,来为大家简单介绍一下。所谓的双机无非就是以7X24小时不中断的为企业提供服务为目的,各种双机
    的头像 发表于 10-08 09:16 1338次阅读
    路由器功能介绍-<b class='flag-5'>双机</b><b class='flag-5'>热</b>备份机制