# Nginx TCP 四层负载均衡
# 1. 四层负载均衡基本概述
# 1.1 什么是四层负载均衡
所谓四层就是基于 * IP+* 端口的负载均衡,它通过用户请求的端口来决定将请求转发至哪台后端服务器。
就是通过三层的 IP 地址并加上四层的端口号,来决定哪些流量需要做负载均衡。对需要负载均衡的流量进行 NAT 转换,然后转发至后端服务器节点,并记录这个 TCP 或者 UDP 的流量是由哪台后端服务器处理的,后续这个连接的所有流量都同样转发到同一台服务器处理。
# 1.2 四层负载均衡应用场景
1. 场景一、端口代理
首先 http 当然是最常用的一种协议,但是还是有很多非 http 的应用(mysql、redis、ssh),只能用四层代理
2. 场景二、四层负载均衡 +* 七层负载均衡,实现大规模集群架构。
其次七层代理需要 CPU 运算,所以单台机器很难做到很高的处理能力,因此需要在七层负载均衡前面再加四层负载均衡。(提高网站的访问效率,并保证了七层负载均衡的高可用性。)
# 1.3 四层负载均衡优缺点
- 1. 四层负载均衡通常用来转发非 http 应用:如 tcp/80tcp/443 tcp/3306 tcp/22 udp/53
- 2. 四层负载均衡可以解决七层负载均衡高可用性的问题。(多个七层负载均衡同时提供服务)
- 3. 四层负载均衡可以解决七层负载均衡端口数限制问题。(七层负载均衡最多能使用的端口是 5w)
- 4. 四层转发效率远比七层代理的效率高的多,但是他只能支持 tcp/ip 协议,所以他的功能较弱,虽然七层效率不高,但他支持 http/https 这样的应用层协议。
# 2. 四层负载均衡场景实践
# 2.1 配置语法示例
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; | |
} | |
} |
# 2.2 实现 HTTP 协议负载均衡
前端四层负载均衡 + 后端七层负载均衡 + 应用节点
1. 配置 nginx 四层负载均衡
[root@lb02 ~]# vim /etc/nginx/nginx.conf | |
# 在 events 层下面,http 层上面配置 include | |
events { | |
.... | |
} | |
include /etc/nginx/conf.c/*.conf; | |
http { | |
..... | |
} | |
#1. 配置四层负载均衡 | |
[root@lb01 conf.c]# rm -f /etc/nginx/conf.d/default.conf #删除 http 的 80 端口 | |
[root@lb01 ~]# mkdir -p /etc/nginx/conf.c | |
[root@lb01 ~]# cd /etc/nginx/conf.c | |
[root@lb01 conf.c]# cat proxy_lb.conf | |
stream { | |
upstream lb { | |
server 172.16.1.5:80 weight=5 max_fails=3 fail_timeout=30s; | |
server 172.16.1.6:80 weight=5 max_fails=3 fail_timeout=30s; | |
server 172.16.1.7:80 weight=5 max_fails=3 fail_timeout=30s; | |
} | |
server { | |
listen 80; | |
proxy_connect_timeout 3s; | |
proxy_timeout 3s; | |
proxy_pass lb; | |
} | |
} | |
#2. 配置七层负载均衡 | |
upstream www { | |
server 172.16.1.10:80; | |
server 172.16.1.11:80; | |
server 172.16.1.12:80; | |
} | |
upstream blog { | |
server 172.16.1.13:80; | |
server 172.16.1.14:80; | |
server 172.16.1.15:80; | |
} | |
upstream bbs { | |
server 172.16.1.16:80; | |
server 172.16.1.17:80; | |
server 172.16.1.18:80; | |
} | |
server { | |
server_name www.hmallleasing.com; | |
listen 80; | |
location / { | |
proxy_pass http://www; | |
include proxy_params; | |
} | |
} | |
server { | |
server_name blog.hmallleasing.com; | |
listen 80; | |
location / { | |
proxy_pass http://blog; | |
include proxy_params; | |
} | |
} | |
server { | |
server_name bbs.hmallleasing.com; | |
listen 80; | |
location / { | |
proxy_pass http://bbs; | |
include proxy_params; | |
} | |
} | |
[root@lb01 conf.c]# cat /etc/nginx/proxy_params | |
proxy_http_version 1.1; | |
proxy_set_header Connectin ""; | |
proxy_set_header Host $http_host; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_connect_timeout 60; | |
proxy_send_timeout 60; | |
proxy_read_timeout 120; | |
proxy_buffering on; | |
proxy_buffer_size 32k; | |
proxy_buffers 4 128k; | |
proxy_temp_file_write_size 10240k; | |
proxy_max_temp_file_size 10240k; | |
#当其中一台返回错误 500,502,503,504 时,分配下一台服务器程序处理,提高平台访问成功率 | |
proxy_next_upstream error timeout http_500 http_502 http_503 http_504; | |
proxy_next_upstream_tries 2; | |
proxy_next_upstream_timeout 3s; |
# 2.3 实现 MySQL 负载均衡
请求负载均衡 5555 ---> 172.16.1.7:22
请求负载均衡 6666 ---> 172.16.1.51:3306
1. Nginx 四层负载均衡配置如下
[root@lb01 ~]# mkdir -p /etc/nginx/conf.c | |
[root@lb01 ~]# vim /etc/nginx/nginx.conf | |
# 在 events 层下面,http 层上面配置 include | |
include /etc/nginx/conf.c/*.conf; | |
# 配置 Nginx 四层转发 | |
[root@lb01 conf.c]# cat proxy_stream.conf | |
stream { | |
#1. 定义转发 tcp/22 端口的虚拟资源池 | |
upstream ssh { | |
server 192.168.40.7:22; | |
} | |
#2. 定义转发 tcp/3306 端口的虚拟资源池 | |
upstream mysql { | |
server 192.168.40.51:3306; | |
} | |
#调用虚拟资源池 | |
server { | |
listen 5555; | |
proxy_connect_timeout 1s; | |
proxy_timeout 300s; | |
proxy_pass ssh; | |
} | |
server { | |
listen 6666; | |
proxy_connect_timeout 1s; | |
proxy_timeout 300s; | |
proxy_pass mysql; | |
} | |
} | |
[root@lb01 conf.c]# nginx -t | |
[root@lb01 conf.c]# systemctl reload nginx |