keepalived实现Tomcat服务双机热备

项目中需要采用tomcat双机热备机制,以确保系统性能。本人通过查阅资料以及亲自实现完成,写下这篇博文,供个人读者参考。

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

[plain] view plain copy

  1. [~]tar -zxvf keepalived-1.2.19.tar.gz  
  2. [~] cd keepalived-1.2.19  
  3. [keepalived-1.2.19] ./configure --prefix=/usr/local/keepalived --disable-fwmark  
  4. #(如果直接输入./configure有可能报错:configure:error: No SO_MARK declaration in headers)  
  5. [keepalived-1.2.19] make  
  6. [keepalived-1.2.19] make install  


2.2 配置keepalived服务

[plain] view plain copy

  1. [keepalived-1.2.19] cp /usr/local/keepalived/sbin/keepalived /usr/sbin/  
  2. [keepalived-1.2.19] cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/  
  3. [keepalived-1.2.19] cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/  
  4. [keepalived-1.2.19] mkdir /etc/keepalived  
  5. [keepalived-1.2.19] cp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/  
  6. [keepalived-1.2.19] chkconfig --add keepalived  
  7. [keepalived-1.2.19] chkconfig keepalived on  


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

service keepalived restart

service keepalived start

service keepalived stop

service keepalived status

2.3 配置keepalived.conf文件

[plain] view plain copy

  1. [~]mv /etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf.bak  
  2. [~]vi /etc/keepalived//keepalived.conf   

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

主机配置:

[plain] view plain copy

  1. global_defs {  
  2.    router_id NodeA  
  3. }  
  4. vrrp_script chk_http_port {  
  5.    script "/opt/tomcat.pid"  
  6.    interval 5  
  7.    weight 2  
  8. }  
  9. vrrp_instance VI_1 {  
  10.    state MASTER  
  11.    interface eth0  
  12.    virtual_router_id 52  
  13.    priority 150  
  14.    advert_int 1  
  15.    
  16.    authentication {  
  17.        auth_type PASS  
  18.        auth_pass 1111  
  19.     }  
  20.    
  21.    track_script {  
  22.        chk_http_port  
  23.     }  
  24.    
  25.    virtual_ipaddress {  
  26.        10.10.195.212  
  27.     }  
  28. }  

备机配置:

[plain] view plain copy

  1. global_defs {  
  2.    router_id NodeB  
  3. }  
  4. vrrp_script chk_http_port {  
  5.    script "/opt/tomcat.pid"  
  6.    interval 5  
  7.    weight 2  
  8. }  
  9. vrrp_instance VI_1 {  
  10.    state BACKUP  
  11.    interface eth0  
  12.    virtual_router_id 52  
  13.    priority 100  
  14.    advert_int 1  
  15.    
  16.    authentication {  
  17.        auth_type PASS  
  18.        auth_pass 1111  
  19.     }  
  20.    
  21.    track_script {  
  22.        chk_http_port  
  23.     }  
  24.    
  25.    virtual_ipaddress {  
  26.        10.10.195.212  
  27.     }  
  28. }  

2.4配置/opt/tomcat.pid

[plain] view plain copy

  1. #!/bin/bash  
  2. #description: check tomcat service anddecide whether stop the keepalived or not  
  3. #edited by zzh: 2015-10-14  
  4.    
  5. CATALINA_HOME=/users/shr/apache-tomcat-7.0.64  
  6. JAVA_HOME=/users/shr/util/JavaDir/jdk  
  7. export CATALINA_HOME  
  8. export JAVA_HOME  
  9.    
  10. 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  
  11. read line < tomcat_process_count.txt  
  12.    
  13. start_tomcat=$CATALINA_HOME/bin/startup.sh  
  14.    
  15. if [ ${line} -lt 1 ]  
  16. then  
  17.        echo -n "===Starting tomcat===:"  
  18.        ${start_tomcat}  
  19.        # :sudo service tomcat start  
  20.        echo "===tomcat start ok.==="  
  21.        sleep 3  
  22.    
  23.        # check the tomcat status.  
  24.        ps ax --width=1000 | grep "org.apache.catalina.startup.Bootstrapstart" | grep -v "grep" | awk '{printf $1 " "}' | wc |awk '{print $2}' > tomcat_process_count.txt  
  25.        read line2 < tomcat_process_count.txt  
  26.        if [ ${line2} -lt 1 ]  
  27.        then  
  28.                 sudo service keepalived stop  
  29.        fi  
  30. fi  
  31. rm tomcat_process_count.txt  
  32. #shell end.  

3. 日志查看

3.1 正常启动

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

MASTER10.10.195.53

[plain] view plain copy

  1. Sep 29 15:49:16 shr Keepalived[5536]:Starting Keepalived v1.2.19 (09/21,2015)  
  2. Sep 29 15:49:16 shrKeepalived_healthcheckers[5538]: Netlink reflector reports IP 10.10.195.53added  
  3. Sep 29 15:49:16 shrKeepalived_healthcheckers[5538]: Netlink reflector reports IP 10.10.195.53added  
  4. Sep 29 15:49:16 shrKeepalived_healthcheckers[5538]: Registering Kernel netlink reflector  
  5. Sep 29 15:49:16 shrKeepalived_healthcheckers[5538]: Registering Kernel netlink command channel  
  6. Sep 29 15:49:16 shrKeepalived_healthcheckers[5538]: Opening file'/etc/keepalived/keepalived.conf'.  
  7. Sep 29 15:49:16 shrKeepalived_healthcheckers[5538]: Configuration is using : 6572 Bytes  
  8. Sep 29 15:49:16 shr Keepalived[5537]:Starting Healthcheck child process, pid=5538  
  9. Sep 29 15:49:16 shr Keepalived_healthcheckers[5538]:Using LinkWatch kernel netlink reflector...  
  10. Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Netlink reflector reports IP 10.10.195.53 added  
  11. Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Netlink reflector reports IP 10.10.195.53 added  
  12. Sep 29 15:49:16 shr Keepalived[5537]:Starting VRRP child process, pid=5539  
  13. Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Registering Kernel netlink reflector  
  14. Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Registering Kernel netlink command channel  
  15. Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Registering gratuitous ARP shared channel  
  16. Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Opening file '/etc/keepalived/keepalived.conf'.  
  17. Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Configuration is using : 36541 Bytes  
  18. Sep 29 15:49:16 shr Keepalived_vrrp[5539]:Using LinkWatch kernel netlink reflector...  
  19. Sep 29 15:49:16 shr Keepalived_vrrp[5539]:VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]  
  20. Sep 29 15:49:17 shr Keepalived_vrrp[5539]:VRRP_Instance(VI_1) Transition to MASTER STATE  
  21. Sep 29 15:49:17 shr Keepalived_vrrp[5539]:VRRP_Instance(VI_1) Received lower prio advert, forcing new election  
  22. Sep 29 15:49:18 shr Keepalived_vrrp[5539]:VRRP_Instance(VI_1) Entering MASTER STATE  
  23. Sep 29 15:49:18 shr Keepalived_vrrp[5539]:VRRP_Instance(VI_1) setting protocol VIPs.  
  24. Sep 29 15:49:18 shr Keepalived_vrrp[5539]:VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.195.212  
  25. Sep 29 15:49:18 shr Keepalived_vrrp[5539]:Netlink reflector reports IP 10.10.195.212 added  
  26. Sep 29 15:49:18 shrKeepalived_healthcheckers[5538]: Netlink reflector reports IP 10.10.195.212added  
  27. 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)

[plain] view plain copy

  1. Sep 29 15:46:25 server1 Keepalived[18218]:Starting Keepalived v1.2.19 (09/25,2015)  
  2. Sep 29 15:46:25 server1Keepalived_healthcheckers[18220]: Netlink reflector reports IP 10.10.195.190added  
  3. Sep 29 15:46:25 server1Keepalived_healthcheckers[18220]: Netlink reflector reports IP 10.10.195.190added  
  4. Sep 29 15:46:25 server1Keepalived_healthcheckers[18220]: Registering Kernel netlink reflector  
  5. Sep 29 15:46:25 server1Keepalived_healthcheckers[18220]: Registering Kernel netlink command channel  
  6. Sep 29 15:46:25 server1 Keepalived_healthcheckers[18220]:Opening file '/etc/keepalived/keepalived.conf'.  
  7. Sep 29 15:46:25 server1Keepalived_healthcheckers[18220]: Configuration is using : 6682 Bytes  
  8. Sep 29 15:46:25 server1 Keepalived[18219]:Starting Healthcheck child process, pid=18220  
  9. Sep 29 15:46:25 server1 Keepalived[18219]:Starting VRRP child process, pid=18221  
  10. Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Netlink reflector reports IP 10.10.195.190 added  
  11. Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Netlink reflector reports IP 10.10.195.190 added  
  12. Sep 29 15:46:25 server1Keepalived_healthcheckers[18220]: Using LinkWatch kernel netlink reflector...  
  13. Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Registering Kernel netlink reflector  
  14. Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Registering Kernel netlink command channel  
  15. Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Registering gratuitous ARP shared channel  
  16. Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Opening file '/etc/keepalived/keepalived.conf'.  
  17. Sep 29 15:46:25 server1 Keepalived_vrrp[18221]:Configuration is using : 36651 Bytes  
  18. Sep 29 15:46:25 server1Keepalived_vrrp[18221]: Using LinkWatch kernel netlink reflector...  
  19. Sep 29 15:46:25 server1Keepalived_vrrp[18221]: VRRP_Instance(VI_1) Entering BACKUP STATE  
  20. 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)输出如下

[plain] view plain copy

  1. Oct 14 13:25:09 shr Keepalived_vrrp[26683]:VRRP_Instance(VI_1) sending 0 priority  
  2. Oct 14 13:25:09 shr Keepalived_vrrp[26683]:VRRP_Instance(VI_1) removing protocol VIPs.  
  3. Oct 14 13:25:09 shrKeepalived_healthcheckers[26682]: Netlink reflector reports IP 10.10.195.212removed  
  4. Oct 14 13:25:09 shr Keepalived[26681]:Stopping Keepalived v1.2.19 (09/21,2015)  

备机(BACKUP:10.10.195.190)输出如下

[plain] view plain copy

  1. Oct 14 13:19:58 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Transition to MASTER STATE  
  2. Oct 14 13:19:59 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Entering MASTER STATE  
  3. Oct 14 13:19:59 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) setting protocol VIPs.  
  4. Oct 14 13:19:59 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for10.10.195.212  
  5. Oct 14 13:19:59 server1 Keepalived_healthcheckers[30889]:Netlink reflector reports IP 10.10.195.212 added  
  6. Oct 14 13:19:59 server1Keepalived_vrrp[30890]: Netlink reflector reports IP 10.10.195.212 added  
  7. 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)输出如下信息:

[plain] view plain copy

  1. Oct 14 13:25:11 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Received higher prio advert  
  2. Oct 14 13:25:11 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Entering BACKUP STATE  
  3. Oct 14 13:25:11 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) removing protocol VIPs.  
  4. Oct 14 13:25:11 server1Keepalived_healthcheckers[30889]: Netlink reflector reports IP 10.10.195.212removed  
  5. Oct 14 13:25:11 server1Keepalived_vrrp[30890]: Netlink reflector reports IP 10.10.195.212 removed  

4. 查看虚拟ip

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

[plain] view plain copy

  1. [shr@shr bin]$ip add show  
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu16436 qdisc noqueue  
  3.    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00  
  4.    inet 127.0.0.1/8 scope host lo  
  5. 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000  
  6.    link/ether 00:0c:29:6b:f2:a8 brd ff:ff:ff:ff:ff:ff  
  7.    inet 10.10.195.53/24 brd 10.10.195.255 scope global eth0  
  8.    inet 10.10.195.212/32scope global eth0  

5. 常见错误

5. 1常见错误一:

[plain] view plain copy

  1. /var/log/messages has thousands of errorslike this:  
  2.    
  3. Jun 28 09:18:32 rust Keepalived_vrrp:receive an invalid ip number count  
  4. associated with VRID!  
  5. Jun 28 09:18:32 rust Keepalived_vrrp: bogusVRRP packet received on eth0 !!!  
  6. Jun 28 09:18:32 rust Keepalived_vrrp:VRRP_Instance(VI_1) Dropping received  
  7. VRRP packet...  
  8.    
  9. The backup director starts up, but doesn'tlisten on the virtual addresses  
  10. at all. Its /var/log/messages has thousands of errors like this:  
  11.    
  12. Jun 28 06:25:05 stye Keepalived_vrrp:receive an invalid ip number count  
  13. associated with VRID!  
  14. Jun 28 06:25:05 stye Keepalived_vrrp: bogusVRRP packet received on eth0 !!!  
  15. Jun 28 06:25:05 stye Keepalived_vrrp:VRRP_Instance(VI_1) ignoring received  
  16. advertisment...<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>  

解决方法:

改变配置文件/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

脚本具体内容如下:

[plain] view plain copy

  1. #!/bin/bash  
  2. #chkconfig: 2345 10 90  
  3. #description: Starts and Stops the tomcatdaemon  
  4. #edited by ZZH: 2015-10-14  
  5.    
  6. CATALINA_HOME=/users/shr/apache-tomcat-7.0.64  
  7. JAVA_HOME=/users/shr/util/JavaDir/jdk  
  8. export CATALINA_HOME  
  9. export JAVA_HOME  
  10.    
  11. start_tomcat=$CATALINA_HOME/bin/startup.sh  
  12. stop_tomcat=$CATALINA_HOME/bin/shutdown.sh  
  13.    
  14. if [ ! -f $CATALINA_HOME/bin/catalina.sh ]  
  15. then  
  16.          echo"===Tomcat is not available.==="  
  17.          exit  
  18. fi  
  19.    
  20. start()  
  21. {  
  22.          echo-n "===Starting tomcat===:"  
  23.          ${start_tomcat}  
  24.          echo"===tomcat start ok.==="  
  25. }  
  26.    
  27. stop()  
  28. {  
  29.          echo-n "===Shutting down tomcat===:"  
  30.          ${stop_tomcat}  
  31.          echo"===tomcat stop ok.==="  
  32. }  
  33.    
  34. status()  
  35. {  
  36.          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  
  37.          readline < tomcat_process_count.txt  
  38.          rmtomcat_process_count.txt  
  39.          if[ $line -gt 0 ]  
  40.                    then  
  41.                             echo-n "tomcat ( pid = "  
  42.                             ps ax --width=1000 | grep"org.apache.catalina.startup.Bootstrap start" | grep -v"grep" | awk '{printf $1 " "}' | awk '{print $1}'> tomcat_process_pid.txt  
  43.                             readpid < tomcat_process_pid.txt  
  44.                             rmtomcat_process_pid.txt  
  45.                             echo-n $pid  
  46.                             echo-n ") is running..."  
  47.                             echo  
  48.                    else  
  49.                             echo"tomcat is stopped"  
  50.          fi  
  51. }  
  52.    
  53. case "$1" in  
  54.          start)  
  55.                    start  
  56.                    ;;  
  57.          stop)  
  58.                    stop  
  59.                    ;;  
  60.          restart)  
  61.                    stop  
  62.                    sleep1  
  63.                    start  
  64.                    ;;  
  65.          status)  
  66.                    status  
  67.                    ;;  
  68.          *)  
  69.                    echo"Usage:$0 {start|stop|restart}"  
  70.                    exit1  
  71. esac  
  72.    
  73. exit 0  
  74. # shell end.  

在终端输入:

[plain] view plain copy

  1. sudo chmod 755 tomcat  
  2. sudo chkconfig --add tomcat  

tomcat 启动 service tomcatstart

tomcat 关闭 service tomcatstop

tomcat 重启 service tomcatrestart

tomcat状态查看 service tomcatstatus

猜你喜欢

转载自blog.csdn.net/bbwangj/article/details/80347914