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

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

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

写在前面

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

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

[[340892]]

静态负载均衡

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

  1. ./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 

配置四层负载均衡

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

  1. stream { 
  2.  upstream mysql_backend { 
  3.   ...... 
  4.  } 
  5.  server { 
  6.   ...... 
  7.  } 

配置upstream

 

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

配置server

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

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

Nginx完整配置

完整的Nginx配置如下:

  1. user  hadoop hadoop; 
  2. worker_processes  auto; 
  3.   
  4. error_log  logs/error.log; 
  5. #error_log  logs/error.log  notice; 
  6. #error_log  logs/error.log  info; 
  7.   
  8. #pid        logs/nginx.pid; 
  9.   
  10.   
  11. events { 
  12.  use epoll; 
  13.     worker_connections  1024; 
  14.   
  15. stream { 
  16.  upstream mysql_backend { 
  17.   server 192.168.175.100:3306 max_fails=2 fail_timeout=10s weight=1; 
  18.   least_conn; 
  19.  } 
  20.  server { 
  21.   #监听端口,默认使用的是tcp协议,如果需要UDP协议,则配置成listen 3307 udp; 
  22.   listen 3307; 
  23.   #失败重试 
  24.   proxy_next_upstream on
  25.   proxy_next_upstream_timeout 0; 
  26.   proxy_next_upstream_tries 0; 
  27.   #超时配置 
  28.   #配置与上游服务器连接超时时间,默认60s 
  29.   proxy_connect_timeout 1s; 
  30.   #配置与客户端上游服务器连接的两次成功读/写操作的超时时间,如果超时,将自动断开连接 
  31.   #即连接存活时间,通过它可以释放不活跃的连接,默认10分钟 
  32.   proxy_timeout 1m; 
  33.   #限速配置 
  34.   #从客户端读数据的速率,单位为每秒字节数,默认为0,不限速 
  35.   proxy_upload_rate 0; 
  36.   #从上游服务器读数据的速率,单位为每秒字节数,默认为0,不限速 
  37.   proxy_download_rate 0; 
  38.   #上游服务器 
  39.   proxy_pass mysql_backend; 
  40.  } 

动态负载均衡

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

  1. nginx: worker process is shutting down; 

这是因为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七层动态负载均衡。

  1. git clone https://github.com/xiaokai-wang/nginx-stream-upsync-module.git 
  2. git clone https://github.com/weibocom/nginx-upsync-module.git 
  3. git clone https://github.com/CallMeFoxie/nginx-upsync.git 
  4. cp -r nginx-stream-upsync-module/* nginx-upsync/nginx-stream-upsync-module/ 
  5. cp -r nginx-upsync-module/* nginx-upsync/nginx-upsync-module/ 
  6.   
  7. ./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 

配置四层负载均衡

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

  1. stream { 
  2.  upstream mysql_backend { 
  3.   ...... 
  4.  } 
  5.  server { 
  6.   ...... 
  7.  } 

配置upstream

  1. upstream mysql_backend { 
  2.  server 127.0.0.1:1111; #占位server 
  3.  upsync 192.168.175.100:8500/v1/kv/upstreams/mysql_backend upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off
  4.  upsync_dump_path /usr/local/nginx-1.17.2/conf/mysql_backend.conf; 
  • upsync指令指定从consul哪个路径拉取上游服务器配置;
  • upsync_timeout配置从consul拉取上游服务器配置的超时时间;
  • upsync_interval配置从consul拉取上游服务器配置的间隔时间;
  • upsync_type指定使用consul配置服务器;
  • strong_dependency配置nginx在启动时是否强制依赖配置服务器,如果配置为on,则拉取配置失败时Nginx启动同样失败。
  • upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出现问题,本地还有一个备份。

配置server

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

从Consul添加上游服务器

  1. 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 
  2. 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 

从Consul删除上游服务器

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

配置upstream_show

  1. server { 
  2.  listen 13307; 
  3.  upstream_show; 

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

Nginx完整配置

Nginx的完整配置如下:

  1. user  hadoop hadoop; 
  2. worker_processes  auto; 
  3.   
  4. error_log  logs/error.log; 
  5. #error_log  logs/error.log  notice; 
  6. #error_log  logs/error.log  info; 
  7.   
  8. #pid        logs/nginx.pid; 
  9.   
  10.   
  11. events { 
  12.  use epoll; 
  13.     worker_connections  1024; 
  14.   
  15. stream { 
  16.  upstream mysql_backend { 
  17.   server 127.0.0.1:1111; #占位server 
  18.   upsync 192.168.175.100:8500/v1/kv/upstreams/mysql_backend upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off
  19.   upsync_dump_path /usr/local/nginx-1.17.2/conf/mysql_backend.conf; 
  20.  } 
  21.  server { 
  22.   #监听端口,默认使用的是tcp协议,如果需要UDP协议,则配置成listen 3307 udp; 
  23.   listen 3307; 
  24.   #失败重试 
  25.   proxy_next_upstream on
  26.   proxy_next_upstream_timeout 0; 
  27.   proxy_next_upstream_tries 0; 
  28.   #超时配置 
  29.   #配置与上游服务器连接超时时间,默认60s 
  30.   proxy_connect_timeout 1s; 
  31.   #配置与客户端上游服务器连接的两次成功读/写操作的超时时间,如果超时,将自动断开连接 
  32.   #即连接存活时间,通过它可以释放不活跃的连接,默认10分钟 
  33.   proxy_timeout 1m; 
  34.   #限速配置 
  35.   #从客户端读数据的速率,单位为每秒字节数,默认为0,不限速 
  36.   proxy_upload_rate 0; 
  37.   #从上游服务器读数据的速率,单位为每秒字节数,默认为0,不限速 
  38.   proxy_download_rate 0; 
  39.   #上游服务器 
  40.   proxy_pass mysql_backend; 
  41.  } 
  42.  server { 
  43.   listen 13307; 
  44.   upstream_show; 
  45.  } 

 

 

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

2023-10-31 16:38:02

注册中心负载均衡器

2019-12-25 11:22:19

负载均衡集群算法

2020-11-06 07:11:40

内存虚拟Redis

2022-04-29 08:17:38

RPC远程代理代理模式

2020-10-15 06:26:24

高并发场景冰河

2010-05-10 18:11:24

负载均衡机

2024-02-20 14:10:55

系统缓存冗余

2020-07-28 00:58:20

IP地址子网TCP

2023-11-20 10:09:59

2020-09-14 06:57:30

缓存穿透雪崩

2022-06-15 08:01:39

负载均衡面试OSI

2021-08-02 17:21:08

设计模式订阅

2024-01-19 14:03:59

Redis缓存系统Spring

2024-01-26 13:16:00

RabbitMQ延迟队列docker

2023-07-13 08:19:30

HaspMapRedis元素

2010-03-24 10:35:02

Nginx负载均衡器

2012-11-12 11:26:44

2014-07-24 09:38:34

2020-11-02 07:02:10

加载链接初始化

2021-05-20 08:54:16

Go面向对象
点赞
收藏

51CTO技术栈公众号