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

  

架构设计

IiBzcmM9

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
当前目录:

IiBzcmM9

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

IiBzcmM9

192.168.166.129

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

IiBzcmM9

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

IiBzcmM9

安装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

IiBzcmM9

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

IiBzcmM9

安装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

IiBzcmM9

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

IiBzcmM9

说明环境已经搭建成功

验证

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

IiBzcmM9

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

IiBzcmM9

IiBzcmM9

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

到此这篇关于nginx+tomcat+keepalived实现高可用的文章就介绍到这了,更多相关nginx+tomcat+keepalived高可用内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!

相关文章