码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx学习笔记23TCP负载均衡

时间:2017-07-13 01:08:44      阅读:476      评论:0      收藏:0      [点我收藏+]

标签:支持   另一个   href   使用   html   一个   介绍   byte   rem   

TCP负载均衡的配置和HTTP负载均衡的配置很相似。

 

(1)Nginx配置文件。

stream {

 

   upstream myserver{

 

     server 192.168.197.101:2101;

     server 192.168.197.101:2102;

     server 192.168.197.101:2103;

     hash $remote_addr consistent;

  }

 

   server {

    listen 2001;

    proxy_connect_timeout 3s;

    proxy_timeout  10s;

    proxy_pass  myserver;

  }

 

上述配置中,使用upstream配置块定义了一个上游主机服务群myserver,包含3个服务,分别在210121022103端口监听。使用hash分配策略指定了使用客户端地址来作为hash的键值。本例中指定了consistent参数,可以提高hash的稳定性。如果不使用consistent,则在增加一个server或删除一个server之后,可能导致很多客户端连接到一个不同的server

 

在试验过程中,从一个客户端telnetnginx2001端口时,Nginx选择了端口为2103的服务,从另一个客户端telnetnginx2001端口时,Nginx选择了端口为2102的服务。由于使用了consistent参数,使得hash的选择结果比较稳定,多次从同一个客户端telnetnginx2001端口时,选择的都是同一个服务。

 

详解Nginx的日志记录:

2017/07/11 06:56:40 [info] 3459#0: *41 client 20.1.1.11:4242 connected to 0.0.0.0:2001

2017/07/11 06:56:40 [info] 3459#0: *41 proxy 192.168.197.101:46750 connected to 192.168.197.101:2103

2017/07/11 06:56:52 [info] 3459#0: *41 connection timed out (110: Connection timed out) while proxying connection, client: 20.1.1.11, server: 0.0.0.0:2001, upstream: "192.168.197.101:2103", bytes from/to client:7/6, bytes from/to upstream:6/7

2017/07/11 06:57:00 [info] 3459#0: *43 client 20.1.1.11:4250 connected to 0.0.0.0:2001

2017/07/11 06:57:00 [info] 3459#0: *43 proxy 192.168.197.101:46751 connected to 192.168.197.101:2103

2017/07/11 06:57:13 [info] 3459#0: *43 connection timed out (110: Connection timed out) while proxying connection, client: 20.1.1.11, server: 0.0.0.0:2001, upstream: "192.168.197.101:2103", bytes from/to client:6/5, bytes from/to upstream:5/6

2017/07/11 07:08:24 [info] 3459#0: *45 client 192.168.197.101:40306 connected to 0.0.0.0:2001

2017/07/11 07:08:24 [info] 3459#0: *45 proxy 192.168.197.101:60837 connected to 192.168.197.101:2102

2017/07/11 07:08:37 [info] 3459#0: *45 connection timed out (110: Connection timed out) while proxying connection, client: 192.168.197.101, server: 0.0.0.0:2001, upstream: "192.168.197.101:2102", bytes from/to client:7/6, bytes from/to upstream:6/7

 

TCP负载均衡的分配策略还可以有least_connleast_time两种:

(a)least_conn选择当前连接数最少的服务。

Syntax: least_conn;

Default: —

Context: upstream

 

(b)least_time根据时间来选择服务。

本策略为Nginx商业版订阅定容,目前未做测试,本文暂不介绍。

 

使用least_conn策略的一个例子如下所示:

stream {

   upstream myserver{

     server 192.168.197.101:2101;

     server 192.168.197.101:2102;

     server 192.168.197.101:2103;

     least_conn;

  }

 

   server {

    listen 2001;

    proxy_connect_timeout 3s;

    proxy_timeout  300s;

    proxy_pass  myserver;

  }

 

上游主机的TCP服务程序使用前一篇博客中的Java程序,请参考该博客:

http://www.cnblogs.com/coe2coe/p/7157950.html

 

此时从三个客户端程序中连接Nginx2001端口,根据least_conn的分配策略,可知将选择三个不同的服务,使得每个服务的连接数最接近相等。

 

 

TCP负载均衡的upstream中的server支持weightmax_failsfail_timeout等参数,这跟http的负载均衡类似,不再详细介绍。当使用了weight参数时,least_conn将综合考虑weight制定的权值以及连接数来选择服务。

 

关于ngx_stream_upstream_module模块的完整介绍,请参考:

http://nginx.org/en/docs/stream/ngx_stream_upstream_module.html

 

Nginx学习笔记23TCP负载均衡

标签:支持   另一个   href   使用   html   一个   介绍   byte   rem   

原文地址:http://www.cnblogs.com/coe2coe/p/7157957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!