首页 > 代码库 > keepalived+nginx+tomcat搭建高性能web服务器集群

keepalived+nginx+tomcat搭建高性能web服务器集群

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

技术分享

搭建环境


准备四台服务器 
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和装在了一台服务上,也可以分开配置。


配置步骤如下:


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


web1,web2上分别安装好tomcat

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

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


在master和backup上配置nginx:

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

加入或修改以下内容:

    upstream web_nginx {
        #ip_hash;
        server 192.168.75.151;
        server 192.168.75.152;
    }

upstream web_tomcat {
    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


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


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

global_defs {
   notification_email {
     #acassen@firewall.loc
     #failover@firewall.loc
     #sysadmin@firewall.loc
   }
   #notification_email_from Alexandre.Cassen@firewall.loc
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_http_port {
    script "</dev/tcp/127.0.0.1/80"
    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
    }
}


配置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



keepalived+nginx+tomcat搭建高性能web服务器集群