Keepalived+Nginx+Tomcat 高可用Web服务

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Doudou_Mylove/article/details/82840686

 

前面我们搭建了一个keepalived+lvs,今天把httpd换成nginx+tomcat.

 

Nginx可以用来作为反向代理服务器,来提供负载均衡的能力,使我们的web服务器,能够水平扩容,从而处理更多的用户请求,但是反向代理服务器又变成了一个单点,当反向代理服务器挂了,整合Web服务器就不能被外界访问到,所以我们必须要保证反向代理服务器的高可用。

Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前端服务的高可用。

 

准备环境

Centos 6.9(因为VIP的问题只能用虚拟机测试)
nginx+keepalived(Master)          192.168.20.130
nginx+keepalived(Backup)          192.168.20.131
tomcat1                                     192.168.20.135
tomcat2                                     192.168.20.136
tomcat3                                     192.168.20.138
tomcat4                                     192.168.20.139

以上6台全部关闭防火墙service iptables stop.

 

四台tomcat安装 ,修改主页面便于测试,浏览器分别输入http://192.168.20.135(136/138/139)能够访问,这里省略,详情可访问

https://blog.csdn.net/Doudou_Mylove/article/details/82815557

两台nginx+keepalived安装用yum(Nginx需要先专用的nginx源)
rpm -ivh
http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum -y install nginx keepalived
启动nginx:
/etc/init.d/nginx start
浏览器访问nginx是否OK.

配置nginx反向代理
两台nginx配置一样
Vim /etc/nginx/nginx.conf(找到下图位置插入下列内容)

upstream tomcat {
         server 192.168.20.135:8080 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.20.136:8080 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.20.139:8080 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.20.138:8080 weight=1 max_fails=2 fail_timeout=30s;
    }   

    server {
        listen       80;
        server_name  localhost;

        #地址匹配
        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_set_header X-real-ip $remote_addr;
            proxy_pass http://tomcat;
        } 

配置完成后重启nginx

/etc/init.d/nginx restart

浏览器访问http://192.168.20.130http://192.168.20.131

依次显示四个tomcat主页面即完成了nginx+tomcat负载均衡

 

最后配置keepalived

192.168.20.130(Master )

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
   router_id localhost
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    mcast_src_ip 192.168.20.130
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    track_script {
        chk_nginx  #检测脚本,上面配置的
    }

    virtual_ipaddress {
        192.168.20.134
        192.168.20.137
    }
}

192.168.20.131(BACKUP )

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
   router_id localhost
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    mcast_src_ip 192.168.20.131
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    track_script {
        chk_nginx
    }

    virtual_ipaddress {
        192.168.20.134
        192.168.20.137
    }
}

 

在/etc/keepalived目录下,添加check_nginx.sh,主备两台都要写(检查nginx存活的脚本)

vim /etc/keepalived/check_nginx.sh

#!/bin/bash
COUNT=$(ps -C nginx --no-header |wc -l)
echo $COUNT
#判断Nginx 是否都挂掉了
if [ $COUNT -eq 0 ]
then
        #如果挂掉了,就启动nginx
        /etc/init.d/nginx start
        echo "重启nginx"
        #等5秒钟后,再次查看是否 启动成功
        sleep 5
        #如果nginx没有启动起来,就直接干掉keepalived
        COUNT=$(ps -C nginx --no-header |wc -l)
        if [ $COUNT -eq 0 ]
        then
                echo "干掉keepalived"
                #如果killall命令不能使用,就需要安装psmisc工具了
                #yum install -y psmisc
                killall keepalived
        fi
fi

关于keepalived配置的几点说明

- state - 主服务器需配成MASTER,从服务器需配成BACKUP
- interface - 这个是网卡名
- mcast_src_ip - 配置各自的实际IP地址
- priority - 主服务器的优先级必须比从服务器的高,优先级越高数字越大
- virtual_ipaddress - 配置虚拟IP
- authentication - auth_pass主从服务器必须一致,keepalived靠这个来通信
- virtual_router_id - 主从服务器必须保持一致

配置完成后主备都启动keepalived
/etc/init.d/keepalived start

使用ip addr查看VIP是否被绑定到Master上,

 

浏览器分别输入http://192.168.20.134/http://192.168.20.137依次显示四台tomcat主页面表示nginx负载均衡之keepalived高可用配置完成.

 

 

测试把Master上的keepalived关闭,查看VIP是否漂移到backup上,如果漂移成功浏览器依然可以正常访问四个tomcat主页面大功告成.

 

 

 

 

 

 

 

 

 

展开阅读全文

没有更多推荐了,返回首页