Keepalived+Nginx+Tomcat高可用(主从切换)以及灾难恢复 [ 原贴地址: http://www.linuxidc.com/Linux/2016-12/138482.htm ]
 

思路

首先看下整体的思路
这里写图片描述
解释说明:由于本示例不是为了做业务的集群部署,所以每台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 ?Cprefix=/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

Linux下HAProxy+Keepalived双机高可用方案  http://www.linuxidc.com/Linux/2016-02/128566.htm

Haproxy+Keepalived搭建Weblogic高可用负载均衡集群 http://www.linuxidc.com/Linux/2013-09/89732.htm

Keepalived+HAProxy配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm

CentOS 6.3下Haproxy+Keepalived+Apache配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm

Haproxy + KeepAlived 实现WEB群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm

Haproxy+Keepalived构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm

《Keepalived权威指南》下载见 http://www.linuxidc.com/Linux/2012-05/60951.htm

Nginx+Keepalived实现站点高可用  http://www.linuxidc.com/Linux/2016-12/137883.htm

Nginx+Keepalived实现站点高可用(负载均衡)  http://www.linuxidc.com/Linux/2016-12/138221.htm

CentOS6.5下 Keepalived高可用服务单实例配置 http://www.linuxidc.com/Linux/2016-12/138110.htm

配置文件keepalived.conf详解 http://www.linuxidc.com/Linux/2016-08/134792.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-12/138482.htm

---------------------------------------------------------------------------------------------
不忘初心 方得始终!

唯有志存高远,方能风行天下。

道之所存,虽千万人吾往矣! 情之所钟,虽千万里吾念矣~

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。