阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

201次阅读
没有评论

共计 4423 个字符,预计需要花费 12 分钟才能阅读完成。

思路

首先看下整体的思路
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

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

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计4423字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中