使用keepalived+nginx+tomcat搭建高性能web服务器集群,前端用nginx做反向代理实现负载均衡,同时结合keepalived对nginx实现高可用,后端使用tomcat做动态jsp解析,实现了动静分离。

(本文永久地址:http://woymk.blog.51cto.com/10000269/1928716)

keepalived+nginx+tomcat搭建高性能web服务器集群_第1张图片

搭建环境:centos 6.9

准备四台服务器 
vip: 192.168.75.130
master: 192.168.75.131 (安装nginx做反向代理实现负载匀衡,结合keepalived实现高可用)
backup: 192.168.75.132 (同上)
web1: 192.168.75.151 (安装nginx和tomcat实现动静分离)
web2: 192.168.75.152 (同上)
这里为节省服务器,nginx和tomcat装在了一台服务上,也可以分开配置。


配置步骤如下:

1. 安装nginx、tomcat软件

四台服务器分别安装好nginx
安装方法参见:LNMP环境搭建


web1,web2上分别安装好tomcat

安装方法参见:Tomcat8.5的安装配置

web1, web2上分别启动nginx和tomcat
service nginx start
service tomcat start


2. 在master和backup上配置nginx

vi /usr/local/nginx/conf/nginx.conf

加入或修改以下内容:

    upstream web_nginx {
        server 192.168.75.151;
        server 192.168.75.152;
    }

    upstream web_tomcat {

        #ip_hash;

        server 192.168.75.151:8080;
        server 192.168.75.152:8080;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass      http://web_nginx/;
            proxy_set_header Host   $host;
            proxy_set_header X-Real-IP      $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location ~ \.jsp$ {
            proxy_pass   http://web_tomcat/;
        }

    }

分别在master和backup上启动nginx服务

service nginx start


3. 配置keepalived

在master和backup上分别安装keepalived
yum install -y keepalived


1) 配置master
vi /etc/keepalived/keepalived.conf
清空原文,加入如下内容:
! Configuration File for keepalived

global_defs {
   notification_email {
     #[email protected]
     #[email protected]
     #[email protected]
   }
   #notification_email_from [email protected]
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_http_port {
    script "    interval 1
    weight -2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.75.130
    }
    track_script {
        chk_http_port
    }
}


2) 配置backup
scp 192.168.75.131:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

vi /etc/keepalived/keepalived.conf
backup的配置文件只需要修改
state MASTER  -> state BACKUP
priority 100 -> priority 99


最后,分别在master和backup上启动keepalived服务,先主后备
/etc/init.d/keepalived start


4. 测试:

在master上停止nginx服务

[root@master ~]# /etc/init.d/nginx stop
停止 nginx:                                               [确定]

在backup上查看日志

[root@backup ~]# tail -f /var/log/messages

May 23 17:21:33 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) forcing a new MASTER election
May 23 17:21:33 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) forcing a new MASTER election
May 23 17:21:34 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) Transition to MASTER STATE
May 23 17:21:35 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) Entering MASTER STATE
May 23 17:21:35 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) setting protocol VIPs.
May 23 17:21:35 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.75.130
May 23 17:21:35 backup Keepalived_healthcheckers[1817]: Netlink reflector reports IP 192.168.75.130 added
May 23 17:21:40 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.75.130

日志显示已经成功切换到备用节点上。


[root@backup ~]# curl 192.168.75.130 -I
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Tue, 23 May 2017 09:34:21 GMT
Content-Type: text/html
Content-Length: 616
Connection: keep-alive
Last-Modified: Mon, 22 May 2017 10:28:07 GMT
ETag: "5922bd37-268"
Accept-Ranges: bytes

master停掉后,用curl访问web服务,一切正常。