面试官:给我讲讲Nginx如何实现四层负载均衡?

服务器
负载均衡可以分为静态负载均衡和动态负载均衡,接下来,我们就一起来分析下Nginx如何实现四层静态负载均衡和四层动态负载均衡。

 作者个人研发的在高并发场景下,提供的简单、稳定、可扩展的延迟消息队列框架,具有精准的定时任务和延迟队列处理功能。自开源半年多以来,已成功为十几家中小型企业提供了精准定时调度方案,经受住了生产环境的考验。为使更多童鞋受益,现给出开源框架地址:https://github.com/sunshinelyz/mykit-delay

写在前面

这次又被问到Nginx四层负载均衡的问题了,别慌,我们一起来细细分析这个看似简单的问题。

负载均衡可以分为静态负载均衡和动态负载均衡,接下来,我们就一起来分析下Nginx如何实现四层静态负载均衡和四层动态负载均衡。

[[340892]]

静态负载均衡

Nginx的四层静态负载均衡需要启用ngx_stream_core_module模块,默认情况下,ngx_stream_core_module是没有启用的,需要在安装Nginx时,添加--with-stream配置参数启用,如下所示。

./configure --prefix=/usr/local/nginx-1.17.2 --with-openssl=/usr/local/src/openssl-1.0.2s --with-pcre=/usr/local/src/pcre-8.43 --with-zlib=/usr/local/src/zlib-1.2.11 --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-cc-opt=-O3 --with-stream  --with-http_ssl_module 
  • 1.

配置四层负载均衡

配置HTTP负载均衡时,都是配置在http指令下,配置四层负载均衡,则是在stream指令下,结构如下所示.

stream { 
 upstream mysql_backend { 
  ...... 
 } 
 server { 
  ...... 
 } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

配置upstream

 

upstream mysql_backend { 
 server 192.168.175.201:3306 max_fails=2 fail_timeout=10s weight=1; 
 server 192.168.175.202:3306 max_fails=2 fail_timeout=10s weight=1; 
 least_conn; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

配置server

server { 
 #监听端口,默认使用的是tcp协议,如果需要UDP协议,则配置成listen 3307 udp; 
 listen 3307; 
 #失败重试 
 proxy_next_upstream on
 proxy_next_upstream_timeout 0; 
 proxy_next_upstream_tries 0; 
 #超时配置 
 #配置与上游服务器连接超时时间,默认60s 
 proxy_connect_timeout 1s; 
 #配置与客户端上游服务器连接的两次成功读/写操作的超时时间,如果超时,将自动断开连接 
 #即连接存活时间,通过它可以释放不活跃的连接,默认10分钟 
 proxy_timeout 1m; 
 #限速配置 
 #从客户端读数据的速率,单位为每秒字节数,默认为0,不限速 
 proxy_upload_rate 0; 
 #从上游服务器读数据的速率,单位为每秒字节数,默认为0,不限速 
 proxy_download_rate 0; 
 #上游服务器 
 proxy_pass mysql_backend; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

配置完之后,就可以连接Nginx的3307端口,访问数据库了。

Nginx完整配置

完整的Nginx配置如下:

user  hadoop hadoop; 
worker_processes  auto; 
  
error_log  logs/error.log; 
#error_log  logs/error.log  notice; 
#error_log  logs/error.log  info; 
  
#pid        logs/nginx.pid; 
  
  
events { 
 use epoll; 
    worker_connections  1024; 

  
stream { 
 upstream mysql_backend { 
  server 192.168.175.100:3306 max_fails=2 fail_timeout=10s weight=1; 
  least_conn; 
 } 
 server { 
  #监听端口,默认使用的是tcp协议,如果需要UDP协议,则配置成listen 3307 udp; 
  listen 3307; 
  #失败重试 
  proxy_next_upstream on
  proxy_next_upstream_timeout 0; 
  proxy_next_upstream_tries 0; 
  #超时配置 
  #配置与上游服务器连接超时时间,默认60s 
  proxy_connect_timeout 1s; 
  #配置与客户端上游服务器连接的两次成功读/写操作的超时时间,如果超时,将自动断开连接 
  #即连接存活时间,通过它可以释放不活跃的连接,默认10分钟 
  proxy_timeout 1m; 
  #限速配置 
  #从客户端读数据的速率,单位为每秒字节数,默认为0,不限速 
  proxy_upload_rate 0; 
  #从上游服务器读数据的速率,单位为每秒字节数,默认为0,不限速 
  proxy_download_rate 0; 
  #上游服务器 
  proxy_pass mysql_backend; 
 } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

动态负载均衡

配置Nginx四层静态负载均衡后,重启Nginx时,Worker进程一直不退出,会报错,如下所示。

nginx: worker process is shutting down; 
  • 1.

这是因为Worker进程维持的长连接一直在使用,所以无法退出,只能杀掉进程。可以使用Nginx的四层动态负载均衡解决这个问题。

使用Nginx的四层动态负载均衡有两种方案:使用商业版的Nginx和使用开源的nginx-stream-upsync-module模块。注意:四层动态负载均衡可以使用nginx-stream-upsync-module模块,七层动态负载均衡可以使用nginx-upsync-module模块。

使用如下命令为Nginx添加nginx-stream-upsync-module模块和nginx-upsync-module模块,此时,Nginx会同时支持四层动态负载均衡和HTTP七层动态负载均衡。

git clone https://github.com/xiaokai-wang/nginx-stream-upsync-module.git 
git clone https://github.com/weibocom/nginx-upsync-module.git 
git clone https://github.com/CallMeFoxie/nginx-upsync.git 
cp -r nginx-stream-upsync-module/* nginx-upsync/nginx-stream-upsync-module/ 
cp -r nginx-upsync-module/* nginx-upsync/nginx-upsync-module/ 
  
./configure --prefix=/usr/local/nginx-1.17.2 --with-openssl=/usr/local/src/openssl-1.0.2s --with-pcre=/usr/local/src/pcre-8.43 --with-zlib=/usr/local/src/zlib-1.2.11 --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-cc-opt=-O3 --with-stream --add-module=/usr/local/src/nginx-upsync --with-http_ssl_module 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

配置四层负载均衡

配置HTTP负载均衡时,都是配置在http指令下,配置四层负载均衡,则是在stream指令下,结构如下所示,

stream { 
 upstream mysql_backend { 
  ...... 
 } 
 server { 
  ...... 
 } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

配置upstream

upstream mysql_backend { 
 server 127.0.0.1:1111; #占位server 
 upsync 192.168.175.100:8500/v1/kv/upstreams/mysql_backend upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off
 upsync_dump_path /usr/local/nginx-1.17.2/conf/mysql_backend.conf; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • upsync指令指定从consul哪个路径拉取上游服务器配置;
  • upsync_timeout配置从consul拉取上游服务器配置的超时时间;
  • upsync_interval配置从consul拉取上游服务器配置的间隔时间;
  • upsync_type指定使用consul配置服务器;
  • strong_dependency配置nginx在启动时是否强制依赖配置服务器,如果配置为on,则拉取配置失败时Nginx启动同样失败。
  • upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出现问题,本地还有一个备份。

配置server

server { 
 #监听端口,默认使用的是tcp协议,如果需要UDP协议,则配置成listen 3307 udp; 
 listen 3307; 
 #失败重试 
 proxy_next_upstream on
 proxy_next_upstream_timeout 0; 
 proxy_next_upstream_tries 0; 
 #超时配置 
 #配置与上游服务器连接超时时间,默认60s 
 proxy_connect_timeout 1s; 
 #配置与客户端上游服务器连接的两次成功读/写操作的超时时间,如果超时,将自动断开连接 
 #即连接存活时间,通过它可以释放不活跃的连接,默认10分钟 
 proxy_timeout 1m; 
 #限速配置 
 #从客户端读数据的速率,单位为每秒字节数,默认为0,不限速 
 proxy_upload_rate 0; 
 #从上游服务器读数据的速率,单位为每秒字节数,默认为0,不限速 
 proxy_download_rate 0; 
 #上游服务器 
 proxy_pass mysql_backend; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

从Consul添加上游服务器

curl -X PUT -d "{\"weight\":1, \"max_fails\":2, \"fail_timeout\":10}" http://192.168.175.100:8500/v1/kv/upstreams/mysql_backend/192.168.175.201:3306 
curl -X PUT -d "{\"weight\":1, \"max_fails\":2, \"fail_timeout\":10}" http://192.168.175.100:8500/v1/kv/upstreams/mysql_backend/192.168.175.202:3306 
  • 1.
  • 2.

从Consul删除上游服务器

curl -X DELETE http://192.168.175.100:8500/v1/kv/upstreams/mysql_backend/192.168.175.202:3306 
  • 1.

配置upstream_show

server { 
 listen 13307; 
 upstream_show; 

  • 1.
  • 2.
  • 3.
  • 4.

配置upstream_show指令后,可以通过curl http://192.168.175.100:13307/upstream_show查看当前动态负载均衡上游服务器列表。

Nginx完整配置

Nginx的完整配置如下:

user  hadoop hadoop; 
worker_processes  auto; 
  
error_log  logs/error.log; 
#error_log  logs/error.log  notice; 
#error_log  logs/error.log  info; 
  
#pid        logs/nginx.pid; 
  
  
events { 
 use epoll; 
    worker_connections  1024; 

  
stream { 
 upstream mysql_backend { 
  server 127.0.0.1:1111; #占位server 
  upsync 192.168.175.100:8500/v1/kv/upstreams/mysql_backend upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off
  upsync_dump_path /usr/local/nginx-1.17.2/conf/mysql_backend.conf; 
 } 
 server { 
  #监听端口,默认使用的是tcp协议,如果需要UDP协议,则配置成listen 3307 udp; 
  listen 3307; 
  #失败重试 
  proxy_next_upstream on
  proxy_next_upstream_timeout 0; 
  proxy_next_upstream_tries 0; 
  #超时配置 
  #配置与上游服务器连接超时时间,默认60s 
  proxy_connect_timeout 1s; 
  #配置与客户端上游服务器连接的两次成功读/写操作的超时时间,如果超时,将自动断开连接 
  #即连接存活时间,通过它可以释放不活跃的连接,默认10分钟 
  proxy_timeout 1m; 
  #限速配置 
  #从客户端读数据的速率,单位为每秒字节数,默认为0,不限速 
  proxy_upload_rate 0; 
  #从上游服务器读数据的速率,单位为每秒字节数,默认为0,不限速 
  proxy_download_rate 0; 
  #上游服务器 
  proxy_pass mysql_backend; 
 } 
 server { 
  listen 13307; 
  upstream_show; 
 } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.

 

 

责任编辑:武晓燕 来源: 冰河技术
相关推荐

2023-10-31 16:38:02

注册中心负载均衡器

2019-12-25 11:22:19

负载均衡集群算法

2022-04-29 08:17:38

RPC远程代理代理模式

2020-11-06 07:11:40

内存虚拟Redis

2020-10-15 06:26:24

高并发场景冰河

2010-05-10 18:11:24

负载均衡机

2020-07-28 00:58:20

IP地址子网TCP

2023-11-20 10:09:59

2024-09-11 22:51:19

线程通讯Object

2024-02-20 14:10:55

系统缓存冗余

2020-09-14 06:57:30

缓存穿透雪崩

2021-08-02 17:21:08

设计模式订阅

2023-07-13 08:19:30

HaspMapRedis元素

2024-10-22 16:39:07

2022-06-15 08:01:39

负载均衡面试OSI

2024-01-26 13:16:00

RabbitMQ延迟队列docker

2024-01-19 14:03:59

Redis缓存系统Spring

2024-04-09 10:40:04

2014-07-24 09:38:34

2020-11-02 07:02:10

加载链接初始化
点赞
收藏

51CTO技术栈公众号