四层负载均衡

1.什么是四层  OSI  传输层  TCP/IP     UDP/TCP
	四层是基于转发方式:
	
2.四层负载均衡使用场景
	1.四层负载均衡 + 七层负载均衡
	2.dns + 多机房 + 四层负载均衡+七层负载均衡
	3.SOA 松耦合架构
		登录		passport.jd.com
		注册		reg.jd.com
		商品详情	pro.jd.com

4.基于端口的转发
				nginx 7层		web01		MySQL
nginx 4层  + 					web02		NFS
				nginx 7层		web03		Redis
				10.0.0.6


10.0.0.4
	nginx是1.9版本以后才引入的四层负载均衡
	stream模块实现,但stream不能出现在http层
		--with-stream
		-with-stream_ssl_module
		-with-stream_realip_module
		
	
	stream {
		upstream backend {
			hash $remote_addr consistent;
			server backend1.example.com:12345 weight=5;
			server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
			server unix:/tmp/backend3;
		}
		server {
			listen 12345;
			proxy_connect_timeout 1s;
			proxy_timeout 3s;
			proxy_pass backend;
		}
	}
	
nginx四层+nginx七层+web集群--->场景
	
1.定义四层配置文件路径:
[root@lb-4 nginx]# vim /etc/nginx/nginx.conf
include /etc/nginx/conf.c/*.conf;   

2.进行初始化操作
[root@lb-4 ~]# rm -f /etc/nginx/conf.d/default.conf
[root@lb-4 nginx]# mkdir /etc/nginx/conf.c

3.配置四层负载均衡
[root@lb-4 ~]# cat /etc/nginx/conf.c/all.conf 
stream {
	upstream blog {
		server 172.16.1.5:80;
		server 172.16.1.6:80;
	}

	server {
		listen 80;
		proxy_pass blog;
		proxy_timeout 3s;
		proxy_connect_timeout 3s;
	}
}
	
2.基于端口的转发:   
需求: 用户连接10.0.0.4的6666端口,其实连接的是172.16.1.7的22/TCP端口
需求: 用户连接10.0.0.4的5555端口,其实连接的是172.16.1.51的3306/TCP端口

[root@lb-4 conf.c]# cat blog.oldxu.com.conf 
	stream {
		upstream ssh {
			server 172.16.1.7:22;
		}
		upstream mysql {
			server 172.16.1.51:3306;
		}
		
		server {
			listen 6666;
			proxy_pass ssh;
		}

		server {
			listen 5555;
			proxy_pass mysql;
		}
	}
	
4.四层负载均衡怎么记录日志   必须在stream层,不能出现在http层?
log_format  proxy '$remote_addr -  [$time_local]  $status $protocol'
            '   "$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;
    access_log /var/log/nginx/tcp.log proxy;
		 
3.配置阿里云四层负载均衡   实现端口转发  
		公网666转到内网的22
		公网80 转到内网的多台7层负载均衡的80  ?
posted @   老王教你学Linux  阅读(214)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示