1.2 TCP Load Balancing

Problem

You need to distribute load between two or more TCP servers.

问题

将请求分发到 2 台以上 TCP 服务器。


Solution

Use NGINX’s stream module to load balance over TCP servers

using the upstream block:

stream {
    upstream mysql_read {
        server read1.example.com:3306 weight=5;
        server read2.example.com:3306;
        server 10.10.12.34:3306 backup;
    }
    server {
        listen 3306;
        proxy_pass mysql_read;
    }
}

The server block in this example instructs NGINX to listen on TCP

port 3306 and balance load between two MySQL database read rep‐

licas, and lists another as a backup that will be passed traffic if the

primaries are down.

解决方案

在 NGINX 的 stream 模块内使用 upstream 块级指令实现多台 TCP 服务器负载

均衡:

stream {
    upstream mysql_read {
        server read1.example.com:3306 weight=5;
        server read2.example.com:3306;
        server 10.10.12.34:3306 backup;
    }
    server {
        listen 3306;
        proxy_pass mysql_read;
    }
}

例中的 server 块级指令指定 NGINX 监听 3306 端口的多台 MySQL 数据库

实现负载均衡,其中 10.10.12.34:3306 作为备用数据库服务器当负载请

求分发失败时会被启用。


Discussion

TCP load balancing is defined by the NGINX stream module. The

stream module, like the HTTP module, allows you to define upstream

pools of servers and configure a listening server. When configuring

a server to listen on a given port, you must define the port it’s to lis‐

ten on, or optionally, an address and a port. From there a destina‐

tion must be configured, whether it be a direct reverse proxy to

another address or an upstream pool of resources.

The upstream for TCP load balancing is much like the upstream for

HTTP, in that it defines upstream resources as servers, configured

with Unix socket, IP, or FQDN; as well as server weight, max num‐

ber of connections, DNS resolvers, and connection ramp-up peri‐

ods; and if the server is active, down, or in backup mode.

NGINX Plus offers even more features for TCP load balancing.

These advanced features offered in NGINX Plus can be found

throughout Part I of this book. Features available in NGINX Plus,

such as connection limiting, can be found later in this chap‐

ter. Health checks for all load balancing will be covered in Chapter 2.

Dynamic reconfiguration for upstream pools, a feature available in

NGINX Plus, is covered in Chapter 8.

结论

TCP 负载均衡在 stream 模块中配置实现。stream 模块类似于 http 模块。

配置时需要在 server 块中使用 listen 指令配置待监听端口或 IP 加端口。

接着,需要明确配置目标服务,目标服务可以使代理服务或 upstream 指令

所配置的连接池。 TCP 负载均衡实现中的 upstream 指令配置和 HTTP 负载

均衡实现中的 upstream 指令配置相似。TCP 服务器在 server 指令中配置,

格式同样为 UNIX 套接字、IP地址或 FQDN(Fully Qualified Domain Name:

全限定域名);用于精细化控制的 weight 权重参数、最大连接数、DNS 解析

器、判断服务是否可用和启用为备选服务的 backup 参数一样能在 TCP 负载

均衡中使用。NGINX Plus offers even more features for TCP load balan-

cing.These advanced features offered in NGINX Plus can be found

throughout Part I of this book. Features available in NGINX Plus,

such as connection limiting, can be found later in this chap‐

ter. Health checks for all load balancing will be covered in Chapter 2.

Dynamic reconfiguration for upstream pools, a feature available in

NGINX Plus, is covered in Chapter 8.

results matching ""

    No results matching ""