nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > nginx+tomcat+keepalived高可用

使用nginx+tomcat+keepalived实现高可用的详细步骤

作者:wqww_1

这篇文章主要介绍了nginx+tomcat+keepalived实现高可用,包括安装nginx服务的步骤,详细介绍了安装keepalived的方法,对nginx+tomcat+keepalived高可用相关知识感兴趣的朋友一起看看吧

架构设计

IP服务应用
192.168.166.128nginx/tomcat/keepalived
192.168.166.129nginx/tomcat/keepalived
192.168.166.130虚拟IP

我这是测试环境,防火墙全部关闭了。

# 查看防火墙状态
systemctl status firewalld
# 关闭防火墙
systemctl stop firewalld

安装tomcat服务

192.168.166.128

在tomcat服务webapps下创建一个目录:zkc
当前目录:

mdkir -p webapps/zkc
echo "192.168.166.128" >> webapps/zkc/a.html
#启动tomcat
./bin/startup.sh

访问地址:http://192.168.166.128:8080/zkc/a.html

192.168.166.129

在tomcat服务webapps下创建一个目录:zkc
当前目录:

mdkir -p webapps/zkc
echo "192.168.166.129" >> webapps/zkc/a.html
#启动tomcat
./bin/startup.sh

访问地址:http://192.168.166.129:8080/zkc/a.html

安装nginx服务

可以直接到官网下载或者通过yum下载
两台机器安装步骤一样

# 安装nginx
yum -y install nginx
# 进入配置文件目录
cd /etc/nginx/
# 修改配置文件
vim nginx.conf

upstream app_server{
        server  192.168.166.128:8080 weight=1 max_fails=1 fail_timeout=100;
        server  192.168.166.129:8080 weight=1 max_fails=1 fail_timeout=100;
        }
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location /zkc {
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_connect_timeout 10;
                proxy_read_timeout 600;
                proxy_send_timeout 600;
                proxy_pass http://app_server;
                access_log /var/log/nginx/access-app.log main;
    }
# 启动
nginx

访问: curl http://192.168.166.128/zkc/a.html

访问:curl http://192.168.166.129/zkc/a.html

安装keepalived

安装keepalived两台机器一样
可以上网下载keepalived或者通过yum安装

# 安装
yum -y install keepalived
cd /etc/keepalived/
# 创建检测文件
vim chk_nginx.sh
# 赋予可执行权限
chmod +x chk_nginx.sh

chk_nginx.sh 内容

#!/bin/bash
status=$(ps aux|grep nginx | grep -v grep | grep -v bash | wc -l)
#echo $status >> check.log
if [ "${status}" = "0" ]; then
        #echo "nginx已关闭,正在重启" >> check.log
        # 启动nginx
        /usr/sbin/nginx

        status=$(ps -C nginx --no-header|wc -l)
        #echo $status >> check.log
        # 再次判断
        if [ $status -eq 0 ];then
        #       echo "nginx已关闭,正在重启" >> check.log
                # 关闭keepalived
                systemctl stop keepalived;
        fi
fi

创建keepalived配置文件

# 备份原有keepalived.conf内容
mv keepalived.conf keepalived.conf_bk
vim keepalived.conf
global_defs {
   router_id LVS_Ha2
}

vrrp_script chk_mysql {
    script "/etc/keepalived/chk_nginx.sh"
    interval 2
}
vrrp_instance HA_1 {
        state BACKUP
        interface ens33
        virtual_router_id 80
        priority 80
        advert_int 2
        nopreempt
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
          192.168.166.130
        }
        track_script {
        chk_mysql
        }
}

启动keepalived

systemctl start keepalived
#查看虚拟IP
ip add

访问虚拟IP环境:curl http://192.168.166.130/zkc/a.html

说明环境已经搭建成功

验证

1、192.168.166.128 中nginx突发故障,但是keepalived使得nginx又重新启动

2、重启服务器模拟服务器故障死机
192.168.166.128重启
192.168.166.129,此时发现虚拟IP漂移到此服务器上

为什么没有128呢,是因为刚才服务重启,但是tomcat、nginx、keepalived服务没有做开机自启。

到此这篇关于nginx+tomcat+keepalived实现高可用的文章就介绍到这了,更多相关nginx+tomcat+keepalived高可用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文