运维之道 | Nginx-keepalived+Nginx 实现高可用集群

Nginx-keepalived+Nginx实现动静分离高可用集群

一、Keepalived+Nginx 动静分离、高可用集群(主从模式)

1、环境准备
Hostname IP 作用
Nginx - 主服务器 192.168.146.129 keepalived - 主服务器
Nginx - 备服务器 192.168.146.132 keepalived - 备服务器
Nginx - web服务器 192.168.146.133 静态资源
Tomcat - web服务器 192.168.146.134 动态资源
Centos - test测试机 192.168.146.131 客户端 - 测试机
2、关闭所有服务器的防火墙、selinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
3、配置后端WEB服务器(Nginx-133、Tomcat-134)
  • Nginx-web
worker_processes  1;

events {
    worker_connections  1024;
}
#############################################
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
#############################################
    server {
        listen       80;
        server_name  localhost;
#############################################
    location ~*.*\.(png|jpg|gif)$ {
            root   html;
          #  index  index.html index.htm;
        }
#############################################
 error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
  • 将静态图片导入Nginx发布目录中
[root@localhost html]# rz 
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring nginx.jpg...
nginx.jpg was skipped

[root@localhost html]# ls
nginx.jpg

  • Tomcat-web(导入jsp动态测试文件)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
    <HEAD>
        <TITLE>JSP Test Page</TITLE>
    </HEAD>
    <BODY>
      <%
        Random rand = new Random();
        out.println("<h1>Random number:</h1>");
	    out.println("<h1>刷新数字即可变化</h1>");
        out.println(rand.nextInt(99)+100);
      %>
    </BODY>
</HTML>
  • 测试静态资源

在这里插入图片描述

  • 测试访问动态资源

在这里插入图片描述


4、配置主、备服务器(Nginx-129、Nginx-132)
  • 两台服务器Nginx.conf配置代码均一致
worker_processes  1;

events {
    worker_connections  1024;
}
################################################
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
###################`upstream`###################
upstream static {
        server 192.168.146.133:80;
}

upstream java {
        server 192.168.146.134:8080;
}

###################`动静分离`####################
    server {
        listen       80;
        server_name  www.zwl.com;
       
        location / {
            root   html;
            index  index.html index.htm;

        location ~* .*\.(png|jpg|gif)$ {
            proxy_pass http://static;
        }


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

        }

#################################################
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
  • 检查配置文件是否正确,并重载数据
[root@#localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@#localhost conf]# /usr/local/nginx/sbin/nginx -s reload

5、在测试机192.168.146.131上配置域名映射Nginx主、备服务器IP
192.168.146.129 www.zwl.com
192.168.146.132 www.zwl.com
6、在两台Nginx服务器上搭建keepalived
[root@#localhost ~]# yum install -y keepalived
  • 配置Nginx - 主服务器 keepalived
! Configuration File for keepalived
global_defs {
        notification_email {
        270330615@qq.com
}
        notification_email_from 270330615@qq.com
        smtp_server 192.168.146.131

        router_id LVS_DEVELBACK
}

vrrp_script chk_http_port {
        script "/usr/local/nginx/nginx_check.sh"
        interval 2
        weight 2
}

vrrp_instance VI_1 {
        state MASTER
#备份服务器上将MASTER改为BACKUP
        interface ens33
#网卡
        virtual_router_id 51
#主、备服务器虚拟ID必须一致
        priority 100
#主、备服务器取不同的优先级,主机值大、备份机小
        advert_int 1
        authentication
        auth_type PASS
        auth_pass 1111
}
        virtual_ipaddress {
        192.168.146.131
#测试机虚拟地址IP
}
}
  • 测试脚本(主备服务器的切换使用)
#!/bin/bash
A=`ps -C nginx -no-header |wc -l`
if [ $A -eq 0 ];then
        /usr/local/nginx/sbin/nginx
        sleep 2
        if [ `ps -C nginx --no-header | wc-l` -eq 0 ];then
                killall keepalived
        fi
   fi
  • 配置Nginx - 备服务器 keepalived
! Configuration File for keepalived
global_defs {
        notification_email {
        270330615@qq.com
}
        notification_email_from 270330615@qq.com
        smtp_server 192.168.146.131

        router_id LVS_DEVELBACK
}

vrrp_script chk_http_port {
        script "/usr/local/nginx/nginx_check.sh"
        interval 2
        weight 2
}

vrrp_instance VI_1 {
        state BACKUP
#备份服务器上将BACKUP改为MASTER
        interface ens33
#网卡
        virtual_router_id 51
#主、备服务器虚拟ID必须一致
        priority 90
#主、备服务器取不同的优先级,主机值大、备份机小
        advert_int 1
        authentication
        auth_type PASS
        auth_pass 1111
}
        virtual_ipaddress {
        192.168.146.131
#测试机虚拟地址IP
}
}
  • 测试脚本(主备服务器的切换使用)
#!/bin/bash
A=`ps -C nginx -no-header |wc -l`
if [ $A -eq 0 ];then
        /usr/local/nginx/sbin/nginx
        sleep 2
        if [ `ps -C nginx --no-header | wc-l` -eq 0 ];then
                killall keepalived
        fi
   fi

7、在测试机192.168.146.131访问资源界面

在这里插入图片描述
在这里插入图片描述

发布了97 篇原创文章 · 获赞 10 · 访问量 3410

猜你喜欢

转载自blog.csdn.net/VillianTsang/article/details/103495026
今日推荐