NGINX反代TOMCAT,实现网站动静分离,并通过Keepalived实现网站高可用

两台NGINX主机,每个NGINX反代4个TOMCAT,实现WEB服务器负载均衡,通过Keepalived实现高可用。
NGINX1:192.168.68.128
NGINX2:192.168.68.129
NGINX反代TOMCAT,实现网站动静分离,并通过Keepalived实现网站高可用_第1张图片

一、配置NGINX
NGINX1配置文件,增加:

#http下增加:
upstream tomcat_web {
    server 192.168.68.130:8080 weight=100 max_fails=2 fail_timeout=15;
    server 192.168.68.130:8081 weight=100 max_fails=2 fail_timeout=15;
    server 192.168.68.130:8082 weight=100 max_fails=2 fail_timeout=15;
    server 192.168.68.130:8083 weight=100 max_fails=2 fail_timeout=15;
}

#server 下增加:

location / {
            proxy_pass http://tomcat_web;
            proxy_set_header host $host;
            root   html;
            index  index.html index.htm;
        }

#重启nginx服务
/usr/local/nginx/sbin/nginx -s reload

访问NGINX主机:192.168.68.128,出现TOMCAT主机内容,说明NGINX1反代设定成功,并实现负载均衡。
NGINX反代TOMCAT,实现网站动静分离,并通过Keepalived实现网站高可用_第2张图片
同方法把NGINX2也设定反代TOMCAT.
二、动静分离。
将jorum发布目录拷贝到NGINX1、NGINX2的发布目录,同时配置NGINX1、NGINX2两台主机NGINX配置文件

scp -r 192.168.68.130:/usr/local/tomcat/webapps/ROOT/* /usr/local/nginx/html/

location ~ .*\.(html|htm|jpg|gif|jpeg|png|txt|js|css|doc)$
        {
            root   html;
            expires 30d;
        }

三、配置Keepalived,实现NGINX高可用。
NGINX1与NGINX2上安装Keepalived软件。将NGINX1作为keepalived的MASTER ,NGINX2作为Keepalived的BACKUP

#YUM安装keepalived
yum install keepalived
#配置MASTER
vim keepalived.conf
#global_defs ,全局参数,本地邮件服务器要配置好。
global_defs {
  notification_email {
      [email protected]
 }
    notification_email_from [email protected]
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
 }

#VIP1,vrrp实例,将物理机封装  priority为优先级,MASTER优先级最高。
vrrp_instance VI_1 {
     state MASTER
     interface ens33
     virtual_router_id 151
     priority 100
     advert_int 5   #每5秒向BACKUP发送组播包。
     authentication {
         auth_type  PASS
         auth_pass  1111

     }
     virtual_ipaddress {
         192.168.68.124
     }
}
#BACKUP配置只需把state MASTER修改为state BACKUP; priority 100修改为 priority 90即可。

以上keepalived把NGINX1和NGINX2做了封装,

启动keepalived并查看keepalived启动日志,Transition to MASTER STATE表示成功切换到MASTER

service keepalived restart;tail -fn30 /var/log/messages

NGINX反代TOMCAT,实现网站动静分离,并通过Keepalived实现网站高可用_第3张图片
同方法启动NGINX2上的Keepalive,并查看启动日志,确认keepalived成功切换到BAKCKUP。同时还可以看到,removing protocol VIPS, 没有虚拟IP,虚拟IP正在MASTER,当MASTER主机故障时,BACKUP抢占成为MASTER时,才会自动生成虚拟IP。以保证主机高可用。
NGINX反代TOMCAT,实现网站动静分离,并通过Keepalived实现网站高可用_第4张图片
通过Keepalived虚拟IP192.168.68.124访问效果
NGINX反代TOMCAT,实现网站动静分离,并通过Keepalived实现网站高可用_第5张图片

你可能感兴趣的:(linux,nginx,负载均衡)