Detailed explanation of LVS and Nginx load mechanism

Foreword:

     Both LVS and Nginx can be used as a multi-machine load solution. They have their own advantages and disadvantages. In the production environment, they need to analyze the actual situation and use them.

 1. Advantages of LVS

  1. Strong anti-load capability, because the logic of the lvs working method is very simple, and it works on the 4th layer of the network, only for request distribution, no traffic, so there is basically no need to consider too much in terms of efficiency. LVS generally rarely fails, and even if it fails, it is generally due to problems in other places (such as memory, CPU, etc.) that cause problems with LVS.
  2. Low configurability, which is usually a major disadvantage and also a major advantage, because there are not many configurable options, so in addition to adding or removing servers, you don't need to touch it frequently, which greatly reduces the chance of human error.
  3. The work is stable. Because of its strong anti-load ability, high stability is a matter of course. In addition, all kinds of lvs have complete dual-system hot backup solutions, so there is no need to worry about what will happen to the balancer itself and the node failure If so, lvs will automatically determine, so the overall system is very stable.
  4. No traffic, lvs only distributes requests, and traffic does not go out from itself, so it can be used for some line diversion purposes. Without traffic, the IO performance of the equalizer is not affected by large traffic.
  5. lvs can basically support all applications, because lvs works on the 4th layer, so it can do load balancing for almost all applications, including http, databases, chat rooms, etc.

2. Comparison of Nginx and LVS

  1. Nginx works on the 7th layer of the network, so it can be used for offloading strategies for the http application itself, such as domain names, directory structures, etc. In contrast, lvs does not have such a function, so nginx can use this alone There are far more occasions than lvs; but these useful functions of nginx make it more adjustable than lvs, so it is often touched. Judging from the second advantage of lvs, there are too many touches and artificial problems The odds will also be great.
  2. Nginx is less dependent on the network. In theory, as long as the ping works and the webpage access is normal, nginx can be connected. Nginx can also distinguish the internal and external networks. If it is a node with both internal and external networks, it is equivalent to a single machine with a backup Line; LVS is more dependent on the network environment. At present, the server is in the same network segment and LVS uses the direct method to divert, the effect can be guaranteed. In addition, note that lvs needs to apply for at least one ip to the host to do visual ip.
  3. Nginx is relatively simple to install and configure, and it is also very convenient to test, because it can basically print out errors in logs. The installation, configuration, and testing of lvs will take a long time, because as mentioned above, lvs is relatively dependent on the network. In many cases, the failure to configure successfully is due to network problems rather than configuration problems. If problems occur, they must be resolved accordingly. Will be more troublesome.
  4. Nginx can also withstand high loads and is stable, but there are several levels of poor load and stability of LVS: nginx handles all traffic and is limited by machine IO and configuration; its own bugs are still inevitable; nginx is not readily available The dual-system hot backup solution is relatively risky when running on a single machine, and it is difficult to say everything on a single machine.
  5. Nginx can detect the internal failure of the server, such as the status code returned by the server processing the webpage, timeout, etc., and will resubmit the request that returned the error to another node. At present, ldirectd in lvs can also support monitoring for the internal situation of the server, but the principle of lvs makes it impossible to resend the request. For example, the user is uploading a file, and the node processing the upload happens to fail during the upload process, nginx will cut the upload to another server for reprocessing, and the lvs will be directly disconnected.

3. Combination of the two

  • nginx is used as a reverse proxy for http, and upsteam can achieve balanced forwarding of http requests in multiple ways. Because of the asynchronous forwarding, if a server request fails, immediately switch to other servers until the request succeeds or the last server fails. This can maximize the system's request success rate.
  • lvs adopts the strategy of synchronous request forwarding. Here is the difference between synchronous forwarding and asynchronous forwarding. Synchronous forwarding is to redirect to a back-end server immediately after the lvs server receives the request, and the client directly establishes a connection with the back-end server. Asynchronous forwarding means that nginx initiates a new request with the same content to the backend while maintaining the client connection. After the backend returns the result, nginx returns to the client.
  • To go further: when nginx and lvs serving as load balancing servers process the same request, all request and response traffic will pass through nginx; but when using lvs, only the request traffic passes through the lvs network, and the response traffic is determined by the backend server network return. That is, when the scale of the back-end server is huge, the network bandwidth of nginx becomes a huge bottleneck.
  • But if you only use lvs as a load balancer, once the backend receives the request and the server has a problem, the request will fail. But if you add a layer of nginx (multiple) to the back end of lvs, and each nginx back end has several application servers, then combining the advantages of both can avoid the bottleneck of single nginx traffic concentration and avoid single LVS is a problem of one-off trading.

Guess you like

Origin blog.csdn.net/yeyslspi59/article/details/108365000