当前位置: > Linux服务器 > nginx >

Keepalived+Nginx+Tomcat高可用(主从切换)以及灾难恢复

时间:2016-12-22 15:04来源:linux.it.net.cn 作者:IT
Keepalived+Nginx+Tomcat高可用(主从切换)以及灾难恢复

思路

首先看下整体的思路

解释说明:由于本示例不是为了做业务的集群部署,所以每台ngix都是直接到一台具体的tomcat上,所以这两个tomcat都是独立的并不是集群,而在实际上tomcat应该是借助第三方的rpc框架的业务集群。本文主要是介绍keepalived和nginx的搭建以及灾难恢复。

nginx的安装

这个参考这篇文章 http://www.linuxidc.com/Linux/2016-12/138481.htm ,在本文中只是用到了nginx的代理功能:

server {
        listen       80;
        server_name  mini03;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

       #location / {
       #     root   html;
       #     index  index.html index.htm;
       # }
       location / {
            root   html;
           #index  index.html index.htm;
            proxy_pass http://mini03:8080;#拦截所有请求转向tomcat
        }
  }

keepalived的安装和使用

下载keepalived官网: http://keepalived.org

将keepalived解压到/usr/local/src目录下

tar -zxvf keepalived-1.2.19.tar.gz -C /usr/local/src

进入到/usr/local/src/keepalived-1.2.19目录

cd /usr/local/src/keepalived-1.2.19

开始configure

./configure –prefix=/usr/local/keepalived

编译并安装

make && make install

将keepalived添加到系统服务中并开机启动

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 
mkdir -p /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived  
chkconfig keepalived on

配置keepalived虚拟IP、心跳检查、以及灾难恢复

解释:当然这里的恢复值得是在主机不宕机的情况下进行nginx和tomcat的恢复,就向上面,keepalived加到开机自启动后,nginx和tomcat也就能自动恢复了。

修改配置文件:/etc/keepalived/keepalived.conf

Master的:

global_defs {
 #   notification_email {
 #       zhouxiao@example.com
 #       itsection@example.com
 #   }
 #   notification_email_from itsection@example.com
 #   smtp_server mail.example.com
 #   smtp_connect_timeout 30
    router_id LVS_DEVEL
}
vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 2 
    weight -5
    fall 3  
    rise 2
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.64.100/24
    }
    track_script {
       chk_nginx
    }
    notify_master "/usr/local/keepalived/sbin/notify.sh master"
    notify_backup "/usr/local/keepalived/sbin/notify.sh backup"
    notify_fault "/usr/local/keepalived/sbin/notify.sh fault"
}

backup:

global_defs {
 #   notification_email {
 #       zhouxiao@example.com
 #       itsection@example.com
 #   }
 #   notification_email_from itsection@example.com
 #   smtp_server mail.example.com
 #   smtp_connect_timeout 30
    router_id LVS_DEVEL
}
vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 2 
    weight -5
    fall 3  
    rise 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 98
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.64.100/24
    }
    track_script {
       chk_nginx
    }
    notify_master "/usr/local/keepalived/sbin/notify.sh master"
    notify_backup "/usr/local/keepalived/sbin/notify.sh backup"
    notify_fault "/usr/local/keepalived/sbin/notify.sh fault"
}

至于keepalvied中每个字段的含义自行找相应的文档看看,很简单的。

看下心跳检测脚本,也就是主从切换的:

/etc/keepalived/check_nginx.sh:

#!/bin/bash
# curl -IL http://localhost/member/login.htm
# curl --data "memberName=fengkan&password=22" http://localhost/member/login.htm
count=0
for (( k=0; k<2; k++ ))
do
    check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\\n" http://localhost -o /dev/null )
    if [ "$check_code" -ne "200" ]; then
        count=$(expr $count + 1)
        sleep 3
        continue
    else
        count=0
        break
    fi
done
if [ "$count" != "0" ]; then
#   /etc/init.d/keepalived stop
    exit 1
else
    exit 0
fi

可以看到这里我很取巧的用了访问tomcat的默认的路径,能访问证明,都是好的,不能访问,我重启tomcat和nginx。

在来看下,灾难恢复的脚本:

/usr/local/keepalived/sbin/notify.sh

#!/bin/bash

case "$1" in
        master)
                . /etc/profile;/usr/local/tomcat/apache-tomcat-7.0.68/bin/catalina.sh start
                /usr/local/nginx/sbin/nginx
                exit 0
        ;;
        backup)
                . /etc/profile;/usr/local/tomcat/apache-tomcat-7.0.68/bin/catalina.sh start
                /usr/local/nginx/sbin/nginx -s stop
                /usr/local/nginx/sbin/nginx
                exit 0
        ;;
        fault)
                /usr/local/nginx/sbin/nginx -s stop
                exit 0
        ;;
        *)
                echo 'Usage:notify.sh{master|backup|fault}'
                exit 1
        ;;
esac


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容