四层负载和动静分离

发布于 29 天前  17 次阅读


一、LVS的几种工作模式

1.DR模式
2.NAT模式
3.FULL NAT模式
4.TUN隧道模式

Nginx支持四层负载吗?
支持,但是是假的,不是真四层,只是模拟的四层代理。
可以用LVS来做四层转发。

二、四层负载

1.配置lb02

1.配置nginx仓库
[root@lb02 ~]# scp 10.0.0.7:/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/
2.安装nginx服务
[root@lb02 ~]# yum -y install nginx
3.同步lb01的配置
[root@lb02 ~]# rsync -avz --delete 10.0.0.5:/etc/nginx/ /etc/nginx/

4.启动nginx
[root@lb02 /etc/nginx/conf.d]#systemctl enable --now nginx

2.配置四层负载lb

1.配置官网仓库
[root@lb ~]# scp 10.0.0.7:/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/
2.安装nginx服务
[root@lb ~]# yum -y install nginx
3.配置四层负载
[root@lb ~]# cd /etc/nginx/
[root@lb conf.d]# rm -rf default.conf	# 删除应用层配置
配置四层需要再http区块外配置
[root@lb nginx]# cat nginx.conf 
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}
#配置
stream {
upstream web {
        server 10.0.0.5:80;
	    server 10.0.0.6:80;
}
     server {
     listen 80;
     proxy_pass web;
     }
upstream web01 {
        server 172.16.1.7:22;
        }

     server {
     listen 2222;
     proxy_pass web01;
        }
}
启动nginx
[root@lb /etc/nginx]#systemctl enable --now nginx

3.配置路由转发

配置访问10.0.0.4的2222端口则转发给后端web01的22端口
vim nginx.conf
..
upstream web01 {
        server 172.16.1.7:22;
        }

     server {
     listen 2222;
     proxy_pass web01;
        }
...

[root@lb nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb nginx]# systemctl restart nginx

测试
ssh 10.0.0.4 2222 实际连接的172.16.1.7的22端口

4.配置动静分离

web01配置
1.安装Java运行环境
[root@web01 /etc/nginx/conf.d]#yum -y install java
2.安装tomcat
[root@web01 /etc/nginx/conf.d]#wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.34/bin/apache-tomcat-10.1.34.tar.gz^C
[root@web01 ~]#tar xf apache-tomcat-10.1.34.tar.gz -C /usr/local/

3.启动Tomcat
[root@web01 ~]#/usr/local/apache-tomcat-10.1.34/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/apache-tomcat-10.1.34
Using CATALINA_HOME:   /usr/local/apache-tomcat-10.1.34
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-10.1.34/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/apache-tomcat-10.1.34/bin/bootstrap.jar:/usr/local/apache-tomcat-10.1.34/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
配置反向代理
[root@web01 /etc/nginx/conf.d]#cat tom.conf 
server {
	listen 80;
	server_name www.tom.com;
	
	location / {
	proxy_pass http://10.0.0.7:8080;
}
}
             
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx

5.单台实现动静分离

[root@web01 /etc/nginx/conf.d]#cat tom.conf 
server {
	listen 80;
	server_name www.tom.com;
	
	location / {
	proxy_pass http://10.0.0.7:8080;
}
	location ~ \.(svg|png|jpg|jpeg)$ {
	root /images;
}
}
[root@web01 /etc/nginx/conf.d]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

创建图片目录
[root@web01 /etc/nginx/conf.d]#mkdir /images
拷贝图片到/images目录
[root@web01 /etc/nginx/conf.d]#cp /usr/local/apache-tomcat-10.1.34/webapps/ROOT/tomcat.svg /images/
[root@web01 /etc/nginx/conf.d]#ll /images/
total 68
-rw-r----- 1 root root 67795 Dec 16 15:25 tomcat.svg
[root@web01 /images]#chown www.www tomcat.svg 

三、配置动静分离

web02配置静态页面

[root@web02 /etc/nginx/conf.d]#vim s.conf
server {
        listen 80;
        server_name www.s.com;

        location / {
        root /code/s;
        index index.html;
        }
}
[root@web02 /etc/nginx/conf.d]#mkdir /code/s
[root@web02 /etc/nginx/conf.d]#echo web02..... > /code/s/index.html
[root@web02 /code/s]#ll
total 188
-rw-r--r-- 1 root root 187675 Dec 11 22:25 dog.jpg
-rw-r--r-- 1 root root     11 Dec 16 15:43 index.html

web01配置动态页面

修改默认的首页
[root@web01 ~]# cat /usr/local/apache-tomcat-10.1.34/webapps/ROOT/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>oldboy JSP Page</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>oldboy随机数:<h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>

启动Tomcat
[root@web01 /images]#/usr/local/apache-tomcat-10.1.34/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/apache-tomcat-10.1.34
Using CATALINA_HOME:   /usr/local/apache-tomcat-10.1.34
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-10.1.34/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/apache-tomcat-10.1.34/bin/bootstrap.jar:/usr/local/apache-tomcat-10.1.34/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

配置负载均衡

[root@lb01 conf.d]# cat ds.conf 
upstream static {
        server 172.16.1.8:80;
}
upstream java {
        server 172.16.1.7:8080;
}
server {
	listen 80;
	   server_name www.s.com;
       location ~* \.(jpg|png|gif)$ {
       proxy_pass http://static;
       proxy_set_header Host $http_host;
}
       location ~ \.jsp {
       proxy_pass http://java;
       proxy_set_header Host $http_host;
       }
}

直接通过代理访问动态

直接通过代理访问静态

在负载均衡: 将动态数据和静态数据集成在一个HMTL中

[root@lb01 conf.d]# cat ds.conf 
upstream static {
        server 172.16.1.8:80;
}
upstream java {
        server 172.16.1.7:8080;
}
server {
	listen 80;
	server_name www.s.com;
        root /code;
        index index.html;

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

       location ~ \.jsp {
       proxy_pass http://java;
       proxy_set_header Host $http_host;
      }
}
[root@lb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@lb01 conf.d]# systemctl restart nginx

创建代码目录
[root@lb01 conf.d]# mkdir /code
整合资源
[root@lb01 conf.d]# cat /code/index.html
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>测试ajax和跨域访问</title>
		<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
	</head>
	<script type="text/javascript">
		$(document).ready(function(){
			$.ajax({
				type: "GET",
				url: "http://www.s.com/index.jsp",
				success: function(data){
					$("#get_data").html(data)
				},
				error: function() {
					alert("哎呦喂,失败了,回去检查你服务去~");
				}
			});
		});
	</script>
	<body>
		<h1>测试动静分离</h1>
		<img src="http://www.s.com/dog.jpg">
		<div id="get_data"></div>
	</body>
</html>

访问测试
www.s.com
小棱
最后更新于 2024-12-24