keepalived实现Tomcat服务双机热备
?
歡迎跳轉到本文的原文鏈接:https://honeypps.com/backend/keepalived-tomacat-standby/
?
項目中需要采用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
?
[~]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 5weight 2 } vrrp_instance VI_1 {state MASTERinterface eth0virtual_router_id 52priority 150advert_int 1authentication {auth_type PASSauth_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 5weight 2 } vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 52priority 100advert_int 1authentication {auth_type PASSauth_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-14CATALINA_HOME=/users/shr/apache-tomcat-7.0.64 JAVA_HOME=/users/shr/util/JavaDir/jdk export CATALINA_HOME export JAVA_HOMEps 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.txtstart_tomcat=$CATALINA_HOME/bin/startup.shif [ ${line} -lt 1 ] thenecho -n "===Starting tomcat===:"${start_tomcat}# :sudo service tomcat startecho "===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.txtread line2 < tomcat_process_count.txtif [ ${line2} -lt 1 ]thensudo service keepalived stopfi 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: <LOOPBACK,UP,LOWER_UP> mtu16436 qdisc noqueuelink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lo 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000link/ether 00:0c:29:6b:f2:a8 brd ff:ff:ff:ff:ff:ffinet 10.10.195.53/24 brd 10.10.195.255 scope global eth0inet 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...<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
腳本具體內容如下:
?
#!/bin/bash #chkconfig: 2345 10 90 #description: Starts and Stops the tomcatdaemon #edited by ZZH: 2015-10-14CATALINA_HOME=/users/shr/apache-tomcat-7.0.64 JAVA_HOME=/users/shr/util/JavaDir/jdk export CATALINA_HOME export JAVA_HOMEstart_tomcat=$CATALINA_HOME/bin/startup.sh stop_tomcat=$CATALINA_HOME/bin/shutdown.shif [ ! -f $CATALINA_HOME/bin/catalina.sh ] thenecho"===Tomcat is not available.==="exit fistart() {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.txtreadline < tomcat_process_count.txtrmtomcat_process_count.txtif[ $line -gt 0 ]thenecho-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.txtreadpid < tomcat_process_pid.txtrmtomcat_process_pid.txtecho-n $pidecho-n ") is running..."echoelseecho"tomcat is stopped"fi }case "$1" instart)start;;stop)stop;;restart)stopsleep1start;;status)status;;*)echo"Usage:$0 {start|stop|restart}"exit1 esacexit 0 # shell end.?
在終端輸入:
?
sudo chmod 755 tomcat sudo chkconfig --add tomcat?
?
?
tomcat 啟動 service tomcatstart
tomcat 關閉 service tomcatstop
tomcat 重啟 service tomcatrestart
tomcat狀態查看 service tomcatstatus
?
歡迎跳轉到本文的原文鏈接:https://honeypps.com/backend/keepalived-tomacat-standby/
?
歡迎支持筆者新作:《深入理解Kafka:核心設計與實踐原理》和《RabbitMQ實戰指南》,同時歡迎關注筆者的微信公眾號:朱小廝的博客。總結
以上是生活随笔為你收集整理的keepalived实现Tomcat服务双机热备的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringMVC + Apache P
- 下一篇: 修改Keepalived配置文件位置以及