Author: Roast chicken prince

Source: Hang Seng LIGHT Cloud Community

Introduction to the

Nginx as a reverse proxy is very common in our daily life, and we can also use nginx to balance liabilities when we have multiple services on the back end. The upstream module is the core of this module.

Recently, we need to do a test on a cluster of two machines. However, due to the large CPU and memory gap between the two services (one is 4C8G, the other is 8C16G), the original default configuration of the wheel training cannot be used. In order to achieve a weighted load balancing strategy, I have made some notes on the upstream module to share with you

Common parameters in upstream

Commonly used parameters Parameters of the function
server IP address or domain name of the back-end server. The default value is 80 without write port. The domain name is used in high-concurrency scenarios and the DNS is used for load balancing
weight The default weight of the backend server is 1. A larger weight is used to receive more requests. For example, weight=2
max_fails Check the health status of the node and allow the maximum number of failed requests to take the node offline. The default value is 1. 0 indicates that failed attempts are prohibited, for example, max_fails=3
fail_timeout Max_fails Specifies the time for suspending the server on this node after the number of failures reaches the limit. The default value is 10 seconds.
backup In hot backup mode, when all servers in the service pool become faulty, the Backup server automatically goes online.
down Indicates that the server is unavailable and does not participate in load balancing. This parameter is usually used with IP_HASH.
max_conns Limit the maximum number of connections, usually configured for inconsistent back-end server hardware.
keepalive Limits the maximum number of idle long connections.
keepalive_timeout Maximum hold time of idle long connections.
keepalive_requests Maximum number of requests that can be processed per long connection.

The distribution of official support

1, polling

Polling is the default allocation method used by Upstream. Each request is allocated to a different backend server in chronological order and is automatically removed if a backend server goes down.

2, weight

Weight is an enhanced version of polling, that is, you can specify the polling ratio, weight is proportional to the access probability, mainly used in the case of large differences in back-end server configuration, as shown in the following examples.

Upstream backend_developer {ip_hash server 172.27.26.174:8099 weight=2; Server 172.27.26.243:8099 weight = 1; }Copy the code

Search baidu in front of all examples of weight are 1,2,3 ranking, but let a person confused, is the order of 1,2,3 rotation training??

3, ip_hash

Each request is allocated according to the hash result of the access IP (i.e., the front-end server or client IP of Nginx), so that each visitor has a fixed access to the back-end server, which can solve the session consistency problem. For example, if a department is in the same egress IP address segment, the entire department may access a server, resulting in service imbalance

Upstream backend_developer {ip_hash server 172.27.26.174:8099; Server 172.27.26.243:8099; }Copy the code

In actual combat

As mentioned above, we are a 4C8G+ an 8C16G machine, and the result of the single performance pressure test is that the TPS of the 8C machine is twice that of the 4C machine, so we need to set the weight of the two machines to 2:1 (i.e., three requests, one request in 4C machine and two requests in 8C machine).

Nginx configuration

Send the request

We sent it six times in a row using the Postman tool

View the results

According to the results, we can see that the machine with weight=1 made two requests for six times, and the machine with weight=2 rotated four times.

Ok, that’s all for now. We can talk about the implementation principle of upstream if we have time. If you are interested, please leave a comment below

reference

Liverpoolfc.tv: nginx.org/en/docs/htt…