Load balancing_cluster solution_(summary comparison) Detailed explanation of the advantages and disadvantages of Nginx/LVS/HAProxy load balancing software

 

There are many companies using Haproxy in the domestic production environment, such as Taobao's CDN system:



 

 

from: http://www.ha97.com/5646.html  The blogger looks like a rush! ! !

 

PS: Nginx /LVS/HAProxy are the three most widely used load balancing software at present. I have implemented them in multiple projects. I have referenced some materials and combined some of my own experience to summarize.

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 more expensive commercial load balancers such as F5 and Array. Its advantage is that there is a professional maintenance team to maintain these services. The disadvantage is that it costs too much, so 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, there are generally more reasonable and popular architecture solutions for website architecture: Nginx/ HAProxy +Keepalived is used as the load balancer for the front end of the web ; 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.

1. Nginx

The advantages of Nginx are:

 

1. Working on the 7th layer of the network, it can do some diversion strategies for http applications, such as domain name and directory structure. Its regular rules are more powerful and flexible than HAProxy, which is the main reason for its widespread popularity. First, Nginx can be used in far more occasions than LVS alone.
2. Nginx has very little dependence on network stability . In theory, it can perform load function if it can be pinged. This is also one of its advantages. On the contrary, LVS relies heavily on network stability, which I deeply understand;
3 , Nginx is relatively simple to install and configure, and it is more convenient to test. It can basically print out errors in logs. The configuration and testing of LVS will take a long time, and LVS is relatively dependent on the network.
3. It can bear high load pressure and is stable. It can generally support tens of thousands of concurrency when the hardware is not bad, and the load degree is relatively smaller than that of LVS.
4. Nginx can detect the internal failure of the server through the port, such as the status code returned by the server processing the webpage, timeout, etc., and will resubmit the request that returns the error to another node, but the disadvantage is that it does not support url to detect . 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.
5. 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.
6. 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.
7. Nginx can be used as a middle-level reverse proxy. At this level, Nginx is basically unmatched. The only one that can compare with Nginx is lighttpd . However, lighttpd has not yet achieved the complete function of Nginx, and the configuration is not so clear and easy to read. Community profiles are also far less active than Nginx.
8. 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 Tengine used by Taobao's front-end is a customized version for secondary development based on nginx.

Nginx regular HTTP request and response flow chart:

nginx

 

The disadvantages of Nginx are:
1. Nginx can only support http, https and Email protocols, so the scope of application is smaller. This is its disadvantage.
2. The health check of the back-end server only supports detection by port, not by url. Direct retention of Session is not supported, but it can be solved by ip_hash.

2. LVS

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).

LVS的优点是:
1、抗负载能力强、是工作在网络4层之上仅作分发之用,没有流量的产生,这个特点也决定了它在负载均衡软件里的性能最强的,对内存和cpu资源消耗比较低。
2、配置性比较低,这是一个缺点也是一个优点,因为没有可太多配置的东西,所以并不需要太多接触,大大减少了人为出错的几率。
3、工作稳定,因为其本身抗负载能力很强,自身有完整的双机热备方案,如LVS+Keepalived,不过我们在项目实施中用得最多的还是LVS/DR+Keepalived。
4、无流量,LVS只分发请求,而流量并不从它本身出去,这点保证了均衡器IO的性能不会收到大流量的影响。
5、应用范围比较广,因为LVS工作在4层,所以它几乎可以对所有应用做负载均衡,包括http、数据库、在线聊天室等等。

LVS DR(Direct Routing)模式的网络流程图:

lvs_dr

LVS的缺点是:
1、软件本身不支持正则表达式处理,不能做动静分离;而现在许多网站在这方面都有较强的需求,这个是Nginx/HAProxy+Keepalived的优势所在。
2、如果是网站应用比较庞大的话,LVS/DR+Keepalived实施起来就比较复杂了,特别后面有Windows Server的机器的话,如果实施及配置还有维护过程就比较复杂了,相对而言,Nginx/HAProxy+Keepalived就简单多了。

三、HAProxy

HAProxy的特点是:
1、HAProxy也是支持虚拟主机的。
2、HAProxy的优点能够补充Nginx的一些缺点,比如支持Session的保持,Cookie的引导;同时支持通过获取指定的url来检测后端服务器的状态。
3、HAProxy跟LVS类似,本身就只是一款负载均衡软件;单纯从效率上来讲HAProxy会比Nginx有更出色的负载均衡速度,在并发处理上也是优于Nginx的。
4、HAProxy支持TCP协议的负载均衡转发,可以对MySQL读进行负载均衡,对后端的MySQL节点进行检测和负载均衡,大家可以用LVS+Keepalived对MySQL主从做负载均衡。
5、HAProxy负载均衡策略非常多,HAProxy的负载均衡算法现在具体有如下8种:
① roundrobin,表示简单的轮询,这个不多说,这个是负载均衡基本都具备的;
② static-rr,表示根据权重,建议关注;
③ leastconn,表示最少连接者先处理,建议关注;
④ source,表示根据请求源IP,这个跟Nginx的IP_hash机制类似,我们用其作为解决session问题的一种方法,建议关注;
⑤ ri,表示根据请求的URI;
⑥ rl_param,表示根据请求的URl参数’balance url_param’ requires an URL parameter name;
⑦ hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求;
⑧ rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求。

四、总结

Nginx和LVS对比的总结:
1、Nginx工作在网络的7层,所以它可以针对http应用本身来做分流策略,比如针对域名、目录结构等,相比之下LVS并不具备这样的功能,所以Nginx单凭这点可利用的场合就远多于LVS了;但Nginx有用的这些功能使其可调整度要高于LVS,所以经常要去触碰触碰,触碰多了,人为出问题的几率也就会大。
2、Nginx对网络稳定性的依赖较小,理论上只要ping得通,网页访问正常,Nginx就能连得通,这是Nginx的一大优势!Nginx同时还能区分内外网,如果是同时拥有内外网的节点,就相当于单机拥有了备份线路;LVS就比较依赖于网络环境,目前来看服务器在同一网段内并且LVS使用direct方式分流,效果较能得到保证。另外注意,LVS需要向托管商至少申请多一个ip来做Visual IP,貌似是不能用本身的IP来做VIP的。要做好LVS管理员,确实得跟进学习很多有关网络通信方面的知识,就不再是一个HTTP那么简单了。
3、Nginx安装和配置比较简单,测试起来也很方便,因为它基本能把错误用日志打印出来。LVS的安装和配置、测试就要花比较长的时间了;LVS对网络依赖比较大,很多时候不能配置成功都是因为网络问题而不是配置问题,出了问题要解决也相应的会麻烦得多。
4、Nginx也同样能承受很高负载且稳定,但负载度和稳定度差LVS还有几个等级:Nginx处理所有流量所以受限于机器IO和配置;本身的bug也还是难以避免的。
5、Nginx可以检测到服务器内部的故障,比如根据服务器处理网页返回的状态码、超时等等,并且会把返回错误的请求重新提交到另一个节点。目前LVS中 ldirectd也能支持针对服务器内部的情况来监控,但LVS的原理使其不能重发请求。比如用户正在上传一个文件,而处理该上传的节点刚好在上传过程中出现故障,Nginx会把上传切到另一台服务器重新处理,而LVS就直接断掉了,如果是上传一个很大的文件或者很重要的文件的话,用户可能会因此而恼火。
6、Nginx对请求的异步处理可以帮助节点服务器减轻负载,假如使用apache直接对外服务,那么出现很多的窄带链接时apache服务器将会占用大 量内存而不能释放,使用多一个Nginx做apache代理的话,这些窄带链接会被Nginx挡住,apache上就不会堆积过多的请求,这样就减少了相当多的资源占用。这点使用squid也有相同的作用,即使squid本身配置为不缓存,对apache还是有很大帮助的。
7、Nginx能支持http、https和email(email的功能比较少用),LVS所支持的应用在这点上会比Nginx更多。在使用上,一般最前端所采取的策略应是LVS,也就是DNS的指向应为LVS均衡器,LVS的优点令它非常适合做这个任务。重要的ip地址,最好交由LVS托管,比如数据库的 ip、webservice服务器的ip等等,这些ip地址随着时间推移,使用面会越来越大,如果更换ip则故障会接踵而至。所以将这些重要ip交给 LVS托管是最为稳妥的,这样做的唯一缺点是需要的VIP数量会比较多。Nginx可作为LVS节点机器使用,一是可以利用Nginx的功能,二是可以利用Nginx的性能。当然这一层面也可以直接使用squid,squid的功能方面就比Nginx弱不少了,性能上也有所逊色于Nginx。Nginx也可作为中层代理使用,这一层面Nginx基本上无对手,唯一可以撼动Nginx的就只有lighttpd了,不过lighttpd目前还没有能做到 Nginx完全的功能,配置也不那么清晰易读。另外,中层代理的IP也是重要的,所以中层代理也拥有一个VIP和LVS是最完美的方案了。具体的应用还得具体分析,如果是比较小的网站(日PV小于1000万),用Nginx就完全可以了,如果机器也不少,可以用DNS轮询,LVS所耗费的机器还是比较多的;大型网站或者重要的服务,机器不发愁的时候,要多多考虑利用LVS。

现在对网络负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术:

第一阶段:利用Nginx或HAProxy进行单点的负载均衡,这一阶段服务器规模刚脱离开单服务器、单数据库的模式,需要一定的负载均衡,但是仍然规模较小没有专业的维护团队来进行维护,也没有需要进行大规模的网站部署。这样利用Nginx或HAproxy就是第一选择,此时这些东西上手快, 配置容易,在七层之上利用HTTP协议就可以。这时是第一选择。

第二阶段:随着网络服务进一步扩大,这时单点的Nginx已经不能满足,这时使用LVS或者商用Array就是首要选择,Nginx此时就作为LVS或者Array的节点来使用,具体LVS或Array的是选择是根据公司规模和预算来选择,Array的应用交付功能非常强大,本人在某项目中使用过,性价比也远高于F5,商用首选!但是一般来说这阶段相关人才跟不上业务的提升,所以购买商业负载均衡已经成为了必经之路。

第三阶段:这时网络服务已经成为主流产品,此时随着公司知名度也进一步扩展,相关人才的能力以及数量也随之提升,这时无论从开发适合自身产品的定制,以及降低成本来讲开源的LVS,已经成为首选,这时LVS会成为主流。
最终形成比较理想的基本架构为:Array/LVS — Nginx/Haproxy — Squid/Varnish — AppServer

 

——

-

——

-

——

-

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326943916&siteId=291194637