[Advantages and disadvantages of Nginx, LVS and HAProxy load balancing software]

Load balancing (Load Balancing) is based on the existing network structure, it provides a cheap, effective and transparent method to expand the bandwidth of network devices and servers, increase throughput, strengthen network data processing capabilities, and improve network flexibility. and availability.

 

Nginx/LVS/HAProxy are the three most widely used load balancing software.

 

Generally, the use of load balancing is to use different technologies according to different stages as the scale of the website increases. The specific application requirements have to be analyzed in detail. If it is a small and medium-sized web application, for example, the daily PV is less than 10 million, you can use Nginx; if there are many machines, you can use DNS polling, but LVS still consumes a lot of machines. If there are large websites or important services, and there are many servers, LVS can be considered.

 

One is through hardware. Common hardware includes the more expensive commercial load balancers such as F5 and Array. Its advantage is that there is a professional maintenance team to maintain these services. For small-scale network services, there is no need to use it yet; the other is Linux-based open source free load balancing software similar to Nginx/LVS/HAProxy. These are implemented at the software level, so the cost is very low.

 

At present, the website architecture is generally more reasonable and popular: the front end of the web uses Nginx/HAProxy+ Keepalived as the load balancer; the back end uses the MySQL database with one master and multiple slaves and separation of read and write, and adopts the LVS+Keepalived architecture. Of course, a plan should be developed according to the specific needs of the project.

 

The following are the characteristics and applicable occasions of each.

 

The advantages of Nginx are:

 

Working on the 7th layer of the network, it can do some diversion strategies for http applications, such as domain names and directory structures. Its regular rules are more powerful and flexible than HAProxy, which is one of the main reasons for its widespread popularity. Nginx can be used in far more occasions than LVS alone.

 

Nginx's dependence on network stability is very small. In theory, it can perform load function if it can be pinged. This is also one of its advantages; on the contrary, LVS relies more on network stability.

 

Nginx is relatively simple to install and configure, and it is more convenient to test. It can basically print errors in logs. The configuration and testing of LVS will take a long time, and LVS is relatively dependent on the network.

 

It can bear high load pressure and is stable, and can generally support tens of thousands of concurrency when the hardware is not bad, and the load degree is relatively smaller than LVS.

 

Nginx can detect internal server failures through ports, such as status codes, timeouts, etc. returned by the server processing web pages, and will resubmit requests that return errors to another node, but the disadvantage is that it does not support url detection. For example, if the user is uploading a file, and the node processing the upload happens to fail during the upload process, Nginx will switch the upload to another server for reprocessing, and LVS will be cut off directly. If a large file is uploaded Or very important files, the user may be dissatisfied.

 

Nginx is not only an excellent load balancer/reverse proxy software, it is also a powerful web application server. LNMP is also a very popular web architecture in recent years, and it is also very stable in high-traffic environments.

 

Nginx is now more and more mature as a Web reverse acceleration cache, and its speed is faster than the traditional Squid server. It can be considered as a reverse proxy accelerator.

 

Nginx can be used as a middle-level reverse proxy. At this level, Nginx is basically unmatched. The only one that can compare Nginx is lighttpd. However, lighttpd has not yet achieved the complete functions of Nginx, and the configuration is not so clear and easy to read. Community information It is also far less active than Nginx.

 

Nginx can also be used as a static web page and image server, and its performance is unmatched. There is also a very active Nginx community, and there are many third-party modules.

 

The disadvantages of Nginx are:

 

Nginx can only support http, https and Email protocols, so the scope of application is smaller, which is its disadvantage.

 

The health check of the backend server only supports detection by port, not by url. Direct retention of Session is not supported, but it can be solved by ip_hash.

 

LVS: Use Linux kernel cluster to implement a high-performance, high-availability load balancing server, which has good scalability (Scalability), reliability (Reliability) and manageability (Manageability).

 

The advantages of LVS are:

 

It has strong anti-load ability and works on the 4th layer of the network only for distribution purposes, without generating traffic. This feature also determines that it has the strongest performance in load balancing software, and consumes less memory and cpu resources.

 

The configuration is relatively low, which is a disadvantage and an advantage. Because there is not much configuration, it does not require much contact, which greatly reduces the chance of human error.

 

The work is stable, because it has a strong anti-load capability, and it has a complete dual-machine hot backup solution, such as LVS+Keepalived.

 

No traffic, LVS only distributes requests, and traffic does not go out from itself, which ensures that the performance of the balancer IO will not be affected by large traffic.

 

It has a wide range of applications. Because LVS works at Layer 4, it can load balance almost all applications, including http, databases, online chat rooms, and so on.

 

The disadvantages of LVS are:

 

The software itself does not support regular expression processing, and cannot do dynamic and static separation; and now many websites have strong requirements in this regard, which is the advantage of Nginx/HAProxy+Keepalived.

 

If the website application is relatively large, the implementation of LVS/DR+Keepalived is more complicated, especially if there is a Windows Server machine behind it, if the implementation and configuration and maintenance process are more complicated, relatively speaking, Nginx/HAProxy+ Keepalived is much simpler.

 

The features of HAProxy are:

 

HAProxy also supports virtual hosts.

 

The advantages of HAProxy can complement some of the shortcomings of Nginx, such as support for session retention and cookie guidance; at the same time, it supports detecting the status of the back-end server by obtaining the specified url.

 

Similar to LVS, HAProxy itself is just a load balancing software; purely in terms of efficiency, HAProxy will have better load balancing speed than Nginx, and it is also better than Nginx in concurrent processing.

 

HAProxy supports load balancing forwarding of the TCP protocol. It can load balance MySQL reads, detect and load balance MySQL nodes at the back end. You can use LVS+Keepalived to load balance MySQL master and slave.

 

There are many HAProxy load balancing strategies, and HAProxy's load balancing algorithms currently include the following eight:

① roundrobin, which means simple polling, this is not much to say, this is basically what load balancing has;

② static-rr, indicating that according to the weight, it is recommended to pay attention;

③ leastconn, which means that the least connected ones are processed first, and it is recommended to pay attention;

④ source, indicating that according to the request source IP, this is similar to the IP_hash mechanism of Nginx, we use it as a method to solve the session problem, it is recommended to pay attention;

⑤ ri, indicating the URI according to the request;

⑥ rl_param, indicating that 'balance url_param' requires an URL parameter name according to the requested URl parameter;

⑦ hdr(name), which means to lock each HTTP request according to the HTTP request header;

⑧ rdp-cookie(name), which means to lock and hash each TCP request according to cookie(name).

 

Summary of the comparison between Nginx and LVS:

Nginx works on the 7th layer of the network, so it can do the shunting strategy for the http application itself, such as for the domain name, directory structure, etc. In contrast, LVS does not have such a function, so Nginx can be used only by this point. It is much more than LVS; but these useful functions of Nginx make it more adjustable than LVS, so it is often necessary to touch and touch, and the probability of artificial problems will be greater.

 

Nginx has less dependence on network stability. In theory, as long as the ping works and the web page access is normal, Nginx can connect. This is a major advantage of Nginx! Nginx can also distinguish between internal and external networks. If it is a node with internal and external networks at the same time, it is equivalent to having a backup line for a single machine; 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 traffic. The effect is more guaranteed. In addition, note that LVS needs to apply for at least one more IP from the hoster to be used as a Visual IP. It seems that it cannot use its own IP as a VIP. To be a good LVS administrator, you really have to follow up and learn a lot of knowledge about network communication, and it is no longer as simple as HTTP.

 

Nginx is relatively simple to install and configure, and it is also very convenient to test, because it can basically print errors in logs. The installation, configuration and testing of LVS will take a long time; LVS is relatively dependent on the network, and many times the failure to configure successfully is due to network problems rather than configuration problems. It will be much more troublesome to solve if there are problems. .

 

Nginx can also withstand high load and is stable, but the load degree and stability are poor. There are several levels of LVS: Nginx handles all traffic, so it is limited by machine IO and configuration; its own bugs are still unavoidable.

 

Nginx can detect internal server failures, such as status codes, timeouts, etc. returned by the server processing web pages, and resubmit requests that return errors 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, if the user is uploading a file, and the node processing the upload happens to fail during the upload process, Nginx will switch the upload to another server for reprocessing, and LVS will be cut off directly. If a large file is uploaded Or very important files, the user may be annoyed by this.

 

The asynchronous processing of requests by Nginx can help the node server to reduce the load. If apache is used to directly serve the outside world, the apache server will occupy a lot of memory and cannot be released when there are many narrowband links. If one more Nginx is used as the apache proxy, these narrowband links will It will be blocked by Nginx, and there will be no accumulation of too many requests on apache, which reduces a considerable amount of resource usage. Using squid for this has the same effect. Even if squid itself is configured not to cache, it is still very helpful to apache.

 

Nginx can support http, https and email (the function of email is less used), and the applications supported by LVS will be more than Nginx in this regard. In terms of use, the strategy adopted at the front end should be LVS, that is, the point of DNS should be the LVS equalizer. The advantages of LVS make it very suitable for this task. Important ip addresses are best managed by LVS, such as database ip, webservice server ip, etc. These ip addresses will be used more and more over time. If the ip is replaced, failures will follow. Therefore, it is safest to hand over these important IPs to LVS for hosting. The only disadvantage of this is that the number of VIPs required will be relatively large. Nginx can be used as an LVS node machine. One is to use the functions of Nginx, and the other is to use the performance of Nginx. Of course, squid can also be used directly at this level. The function of squid is much weaker than that of Nginx, and its performance is also inferior to Nginx. Nginx can also be used as a middle-level proxy. At this level, Nginx is basically unmatched. The only one that can shake Nginx is lighttpd. However, lighttpd has not yet achieved the complete functions of Nginx, and the configuration is not so clear and easy to read. In addition, the IP of the middle-level agent is also important, so it is the most perfect solution for the middle-level agent to have a VIP and LVS. The specific application has to be analyzed in detail. If it is a relatively small website (the daily PV is less than 10 million), it is completely fine to use Nginx. If there are many machines, DNS polling can be used. LVS still consumes a lot of machines. ; Large websites or important services, when the machine is not worried, consider using LVS more.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326364780&siteId=291194637