https://kb.cnblogs.com/page/188170/ <https://kb.cnblogs.com/page/188170/>

Four layer load balancing: Only onceTCP Connect

Seven layer load balancing: The load balancer and the client and the back-end server will establish aTCP Connect. That is the two time.TCP Connect.

( One)

   Simple understanding of four and seven layer load balancing:

  ①  The so-called four layers are based onIP+ Load balancing of ports; The seventh level is based onURL Load balancing of application layer information; Empathy, Also based onMAC Address based load balancing in two layersIP Three layer load balancing of address.
In other words, The second level load balancing will pass a virtualMAC Address receive request, And then reallocate it to the realMAC address; The three-tier load balancing will pass a virtualIP Address receive request, And then reallocate it to the realIP address; Four layers through virtualIP+ Port receive request, And then it's distributed to the real server; Seven layers through virtualURL Or host name receive request, And then it's distributed to the real server.

  ②  So called four to seven layer load balancing, That is, when load balancing the servers in the background, Decide how to forward traffic according to the information of four layers or seven layers.
For example, load balancing of four layers, Through the release of three layersIP address(VIP), Then add the port number of the fourth floor, To determine which traffic needs to be load balanced, For the flow to be processedNAT Handle, Forward to background server, And record thisTCP perhapsUDP Which server handles the traffic of, All subsequent traffic of this connection is also forwarded to the same server for processing. Seven layer load balancing, On the basis of the fourth floor( It's impossible to have seven floors without four), Consider the characteristics of application layer again, Like the sameWeb Load balancing of servers, In addition toVIP plus80 Port identifying traffic to be processed, According to theURL, Browser category, Language to decide whether to load balance. For instance, If yourWeb Servers are divided into two groups, One is in Chinese, One is in English, Then seven layer load balancing can be used when users access your domain name, Automatic identification of user language, Then select the corresponding language server group for load balancing.

  ③ 
Load balancers are often referred to as layer 4 switches or layer 7 switches. Main analysis of layer 4 switchIP Layer andTCP/UDP layer, Realize four layers of traffic load balancing. In addition to supporting layer 4 load balancing, layer 7 switch, And analyze the information of the application layer, asHTTP AgreementURI orCookie information.

  1, Load balancing is divided intoL4 switch( Four level switching), That is, inOSI The first4 Layer work, NamelyTCP Layer. SuchLoad
Balance Do not understand the application protocol( asHTTP/FTP/MySQL Wait). Example:LVS,F5.

  2, The other is calledL7 switch( Seven level switching),OSI The highest level, application layer. here, TheLoad Balancer Understand application protocol. Example:
 haproxy,MySQL Proxy.

   Be careful: A lot of themLoad Balancer We can do four layer exchange, You can also do a seven level exchange.

  ( Two)

   Load balancing devices are also known as" Four to seven layer switch", So what's the difference between the four and seven floors?

   first, Differences in technical principles.

   So called four layer load balancing, That is, it mainly passes through the destination address and port in the message, Plus the server selection mode set by the load balancing device, Determine the internal server of final choice.

   In commonTCP take as an example, The load balancer receives the firstSYN
Request time, Select the best server in the above way, And to the target in the messageIP Address modification( Change to back-end serverIP), Forward directly to this server.TCP Connection establishment of, That is, the three-way handshake is directly established by the client and the server, The load balancer just acts like a router. In some deployment cases, In order to ensure that the packet returned by the server can be returned to the load balancing device correctly, At the same time of forwarding the message, the original source address of the message may be modified.



   So called seven layer load balancing, also known as“ Content exchange”, That is to say, it mainly passes through the real meaningful application layer content in the message, Plus the server selection mode set by the load balancing device, Determine the internal server of final choice.


   In commonTCP take as an example, If the load balancing device chooses the server according to the real application layer content, You can only proxy the final server and client to establish a connection first( The three handshake) after, It is possible to receive the real application layer content message sent by the client, Then according to the specific field in the message, Plus the server selection mode set by the load balancing device, Determine the internal server of final choice. Load balancing equipment in this case, More like a proxy server. Load balancing and front-end clients and back-end servers will be established respectivelyTCP Connect. So in terms of the technical principle, Seven layer load balancing obviously requires higher load balancing equipment, The ability to handle layer 7 is also bound to be lower than the deployment mode of layer 4.

   Second, Requirements of application scenarios.

   Benefits of seven tier application load, Is to make the whole network more" Intellectualization
". For example, the traffic of users visiting a website, It can be done in seven layers, Forward requests for picture classes to specific picture servers and use caching technology; Requests for text classes can be forwarded to specific text servers and compression techniques can be used. Of course, this is just a small case of seven tier application, In terms of technical principle, In this way, the client's request and the server's response can be modified in any sense, Greatly improved the flexibility of the application system in the network layer. Many in the background, for exampleNginx perhapsApache The function of the upper deployment can be moved forward to the load balancing device, For example, theHeader Rewrite, Keyword filtering or content insertion in server response.

   Another feature that is often mentioned is security. The most commonSYN
Flood attack, That is, hackers control many source clients, Use falsehoodIP Address to same destinationSYN attack, Usually this kind of attack will send a lotSYN message, Exhaustion of related resources on the server, In order to achieveDenial
of Service(DoS
) Purpose. It can also be seen from the technical principle, In four tier mode, theseSYN Attacks will be forwarded to back-end servers; And in the 7-tier modeSYN Attacks naturally end on load balancing devices, It will not affect the normal operation of the background server. In addition, the load balancing equipment can set various strategies at the seven levels, Filter specific messages, for exampleSQL Injection Specific attack means at application level, Further improve the overall security of the system from the application level.

   Current7 Layer load balancing, Mainly focus on ApplicationHTTP Agreement, So its application scope is mainly based on many websites or internal information platformsB/S Developed system.
4 Layer load balancing corresponds to otherTCP application, For example, based onC/S DevelopedERP Other systems.

   Third, Problems to be considered in 7-tier application.
<http://hi.baidu.com/aking_roc>

  1: Is it really necessary
, Seven layer application can really improve traffic intelligence, At the same time, it inevitably brings complex equipment configuration, The increase of load balancing pressure and the complexity of troubleshooting. In the design of the system, it is necessary to consider the hybrid situation of simultaneous application of four layers and seven layers.

  2: Does it really improve security. for exampleSYN
Flood attack, The layer 7 model does shield these traffic from the server, But load balancing equipment itself should have strong resistanceDDoS ability, Otherwise, even if the server is normal and the load balancing device as the central scheduling fails, the whole application will crash.

  3: Is there enough flexibility
. The advantage of seven layer application is that it can make the traffic of the whole application intelligent, But load balancing equipment needs to provide perfect seven layer function, Meet customer's application-based scheduling according to different situations. The simplest assessment is whether it can replace the backstageNginx perhapsApache Wait for the scheduling function on the server. A load balancing device that can provide a seven layer application development interface, It allows customers to set functions according to their needs, It's really possible to provide strong flexibility and intelligence.

  ( This section is from “ADC Technology blog” Blog, Be sure to keep this sourcehttp://virtualadc.blog.51cto.com/3027116/591396)

  ( Three)

   Introduction of load balancing layer 4 and 7:

   load balancing(Load
Balance) Build on existing network structure, It provides a cheap, efficient and transparent way to expand the bandwidth of network devices and servers, Increase throughput, Strengthen network data processing capacity, Improve network flexibility and availability.


   Load balancing has two meanings: First, A large number of concurrent accesses or data flows are shared to multiple node devices for separate processing, Reduce user waiting time for response; Secondly, Single heavy load operation is shared to multiple node devices for parallel processing, After each node device processing, Summarize results, Return to user, The processing capacity of the system has been greatly improved.


   The load balancing technology introduced in this paper mainly refers to the application of balancing the traffic load between all servers and applications in the server cluster, At present, most of the load balancing technologies are used to improve the performance ofWeb The server,FTP On servers and other mission critical serversInternet Availability and scalability of server programs.

   Classification of load balancing technology

   At present, there are many different load balancing technologies to meet different application needs, The following equipment objects are used for load balancing, Network level of application( fingerOSI reference model) And applied geographical structure to classify.

   soft/ Hardware load balancing

   Software load balancing solution is to install one or more additional software on the corresponding operating system of one or more servers to achieve load balancing, asDNS Load
Balance,CheckPoint Firewall-1
ConnectControl etc. Its advantage is based on specific environment, Simple configuration, Flexible use, Low cost, Can meet the general load balancing needs.


   Software solutions have many disadvantages, Because installing additional software on each server will consume the system's unlimited resources, More powerful modules, More consumption, So when the connection request is very large, Software itself will be a key to the success of server work; Software scalability is not very good, Limited by operating system; Because of theBug, Often causes safety problems.


   The solution of hardware load balancing is to install load balancing equipment directly between server and external network, This kind of equipment is usually called load balancer, Because of special equipment to complete special tasks, Operating system independent, The overall performance has been greatly improved, Plus a variety of load balancing strategies, Intelligent traffic management, To achieve the best load balancing requirements. 


   Load balancers come in many forms, In addition to being a load balancer in an independent sense, Some load balancers are integrated in switching devices, Placed on server andInternet Link between, Some use two network adapters to integrate this function intoPC in, One piece connected toInternet upper, Connect to the internal network of back-end server cluster.

   generally speaking, Hardware load balancing in function, Better performance than software, But it's expensive.

   local/ Global load balancing
<http://hi.baidu.com/aking_roc>

   Load balancing is divided into local load balancing according to the geographical structure of its application(Local Load Balance) And global load balancing(Global Load
Balance, Also called regional load balancing), Local load balancing refers to load balancing the local server group, Global load balancing refers to the geographical location, Load balancing among servers with different network structures.


   Local load balancing can effectively solve the problem of excessive data flow, The problem of network overload, And you don't have to spend a lot of money on high-performance servers, Make full use of existing equipment, Avoid data traffic loss caused by server single point failure. It has flexible and diverse equalization strategies to distribute data traffic reasonably to the servers in the server cluster. Even expand and upgrade existing servers, Simply add a new server到服务群中,而不需改变现有网络结构,停止现有的服务. 


  全局负载均衡主要用于在一个多区域拥有自己服务器的站点,为了使全球用户只以一个IP地址或域名就能访问到离自己最近的服务器,从而获得最快的访问速度,也可用于子公司分散站点分布广的大公司通过Intranet(企业内部互联网)来达到资源统一合理分配的目的.

  网络层次上的负载均衡

  针对网络上负载过重的不同瓶颈所在,从网络的不同层次入手,我们可以采用相应的负载均衡技术来解决现有问题. 


  随着带宽增加,数据流量不断增大,网络核心部分的数据接口将面临瓶颈问题,原有的单一线路将很难满足需求,而且线路的升级又过于昂贵甚至难以实现,这时就可以考虑采用链路聚合(Trunking)技术.

  链路聚合技术(第二层负载均衡)将多条物理链路当作一条单一的聚合逻辑链路使用,网络数据流量由聚合逻辑链路中所有物理链路共同承担,由此在逻辑上增大了链路的
<http://hi.baidu.com/aking_roc>容量,使其能满足带宽增加的需求.

  现代负载均衡技术通常操作于网络的第四层或第七层.第四层负载均衡将一个Internet上合法注册的IP地址映射为多个内部服务器的IP地址,对每次
TCP连接请求动态使用其中一个内部IP地址,达到负载均衡的目的.在第四层交换机中,此种均衡技术得到广泛的应用,一个目标地址是服务器群VIP(虚拟
IP,Virtual IP
address)连接请求的数据包流经交换机,交换机根据源端和目的IP地址,TCP或UDP端口号和一定的负载均衡策略,在服务器IP和VIP间进行映射,选取服务器群中最好的服务器来处理连接请求.

  第七层负载均衡控制应用层服务的内容,提供了一种对访问流量的高层控制方式,适合对HTTP服务器群的应用.第
七层负载均衡技术通过检查流经的HTTP报头,根据报头内的信息来执行负载均衡任务. 

  第七层负载均衡优点表现在如下几个方面: 

  通过对HTTP报头的检查,可以检测出HTTP400,500和600系列的错误信息,因而能透明地将连接请求重新定向到另一台服务器,避免应用层故障.

  可根据流经的数据类型(如判断数据包是图像文件,压缩文件或多媒体文件格式等),把数据流量引向相应内容的服务器来处理,增加系统性能.

  能根据连接请求的类型,如是普通文本,图象等静态文档请求,还是asp,cgi等的动态文档请求,把相应的请求引向相应的服务器来处理,提高系统的性能及安全性.


  第七层负载均衡受到其所支持的协议限制(一般只有HTTP),这样就限制了它应用的广泛性,并且检查HTTP报头会占用大量的系统资源,势必会影响到系统的性能,在大量连接请求的情况下,负载均衡设备自身容易成为网络整体性能的瓶颈.

  负载均衡策略

  在实际应用中,我们可能不想仅仅是把客户端的服务请求平均地分配给内部服务器,而不管服务器是否宕机.而是想使Pentium III服务器比Pentium
II能接受更多的服务请求,一台处理服务请求较少的服务器能分配到更多的服务请求,出现故障的服务器将不再接受服务请求直至故障恢复等等.


  选择合适的负载均衡策略,使多个设备能很好的共同完成任务,消除或避免现有网络负载分布不均,数据流量拥挤反应时间长的瓶颈.在各负载均衡方式中,针对不同的应用需求,在OSI参考模型的第二,三,四,七层的负载均衡都有相应的负载均衡策略.

  负载均衡策略的优劣及其实现的难易程度有两个关键因素:一,负载均衡算法,二,对网络系统状况的检测方式和能力. 


  考虑到服务请求的不同类型,服务器的不同处理能力以及随机选择造成的负载分配不均匀等问题,为了更加合理的把负载分配给内部的多个服务器,就需要应用相应的能够正确反映各个服务器处理能力及网络状态的
负载均衡算法:

  轮循均衡(Round
Robin):每一次来自网络的请求轮流分配给内部中的服务器,从1至N然后重新开始.此种均衡算法适合于服务器组中的所有服务器都有相同的软硬件配置并且平均服务请求相对均衡的情况.

  权重轮循均衡(Weighted Round
Robin):根据服务器的不同处理能力,给每个服务器分配不同的权值,使其能够接受相应权值数的服务请求.例如:服务器A的权值被设计成1,B的权值是
3,C的权值是6,则服务器A,B,C将分别接受到10%,30%,60%的服务请求.此种均衡算法能确保高性能的服务器得到更多的使用率,避免低性能的服务器负载过重.

  随机均衡(Random):把来自网络的请求随机分配给内部中的多个服务器.

  权重随机均衡(Weighted Random):此种均衡算法类似于权重轮循算法,不过在处理请求分担时是个随机选择的过程.

  响应速度均衡(Response
Time):负载均衡设备对内部各服务器发出一个探测请求(例如Ping),然后根据内部中各服务器对探测请求的最快响应时间来决定哪一台服务器来响应客户端的服务请求.此种均衡算法能较好的反映服务器的当前运行状态,但这最快响应时间仅仅指的是负载均衡设备与服务器间的最快响应时间,而不是客户端与服务器间的最快响应时间.

  最少连接数均衡(Least
Connection):客户端的每一次请求服务在服务器停留的时间可能会有较大的差异,随着工作时间加长,如果采用简单的轮循或随机均衡算法,每一台服务器上的连接进程可能会产生极大的不同,并没有达到真正的负载均衡.最少连接数均衡算法对内部中需负载的每一台服务器都有一个数据记录,记录当前该服务器正在处理的连接数量,当有新的服务连接请求时,将把当前请求分配给连接数最少的服务器,使均衡更加符合实际情况,负载更加均衡.此种均衡算法适合长时处理的请求服务,如FTP. 


  处理能力均衡:此种均衡算法将把服务请求分配给内部中处理负荷(根据服务器CPU型号,CPU数量,内存大小及当前连接数等换算而成)最轻的服务器,由于考虑到了内部服务器的处理能力及当前网络运行状况,所以此种均衡算法相对来说更加精确,尤其适合运用到第七层(应用层)负载均衡的情况下.

  DNS响应均衡(Flash
DNS):在Internet上,无论是HTTP,FTP或是其它的服务请求,客户端一般都是通过域名解析来找到服务器确切的IP地址的.在此均衡算法下,分处在不同地理位置的负载均衡设备收到同一个客户端的域名解析请求,并在同一时间内把此域名解析成各自相对应服务器的IP地址(即与此负载均衡设备在同一位地理位置的服务器的IP地址)并返回给客户端,则客户端将以最先收到的域名解析IP地址来继续请求服务,而忽略其它的IP地址响应.在种均衡策略适合应用在全局负载均衡的情况下,对本地负载均衡是没有意义的.


  尽管有多种的负载均衡算法可以较好的把数据流量分配给服务器去负载,但如果负载均衡策略没有对网络系统状况的检测方式和能力,一旦在某台服务器或某段负载均衡设备与服务器网络间出现故障的情况下,负载均衡设备依然把一部分数据流量引向那台服务器,这势必造成大量的服务请求被丢失,达不到不间断可用性的要求.所以良好的负载均衡策略应有对网络故障,服务器系统故障,应用服务故障的检
测方式和能力:


  Ping侦测:通过ping的方式检测服务器及网络系统状况,此种方式简单快速,但只能大致检测出网络及服务器上的操作系统是否正常,对服务器上的应用服务检测就无能为力了.

  TCP
Open侦测:每个服务都会开放某个通过TCP连接,检测服务器上某个TCP端口(如Telnet的23口,HTTP的80口等)是否开放来判断服务是否正常.

  HTTP URL侦测:比如向HTTP服务器发出一个对main.html文件的访问请求,如果收到错误信息,则认为服务器出现故障.


  负载均衡策略的优劣除受上面所讲的两个因素影响外,在有些应用情况下,我们需要将来自同一客户端的所有请求都分配给同一台服务器去负担,例如服务器将客户端注册,购物等服务请求信息保存的本地数据库的情况下,把客户端的子请求分配给同一台服务器来处理就显的至关重要了.有两种方式可以解决此问题,一是根据IP地址把来自同一客户端的多次请求分配给同一台服务器处理,客户端IP地址与服务器的对应信息是保存在负载均衡设备上的;二是在客户端浏览器
cookie内做独一无二的标识来把多次请求分配给同一台服务器处理,适合通过代理服务器上网的客户端.

  还有一种路径外返回模式(Out of Path
Return),当客户端连接请求发送给负载均衡设备的时候,中心负载均衡设备将请求引向某个服务器,服务器的回应请求不再返回给中心负载均衡设备,即绕过流量分配器,直接返回给客户端,因此中心负载均衡设备只负责接受并转发请求,其网络负担就减少了很多,并且给客户端提供了更快的响应时间.此种模式一般用于HTTP服务器群,在各服务器上要安装一块虚拟网络适配器,并将其IP地址设为服务器群的VIP,这样才能在服务器直接回应客户端请求时顺利的达成三次握手.

  负载均衡实施要素


  负载均衡方案应是在网站建设初期就应考虑的问题,不过有时随着访问流量的爆炸性增长,超出决策者的意料,这也就成为不得不面对的问题.当我们在引入某种负载均衡方案乃至具体实施时,像其他的许多方案一样,首先是确定当前及将来的应用需求,然后在代价与收效之间做出权衡.


  针对当前及将来的应用需求,分析网络瓶颈的不同所在,我们就需要确立是采用哪一类的负载均衡技术,采用什么样的均衡策略,在可用性,兼容性,安全性等等方面要满足多大的需求,如此等等. 


  不管负载均衡方案是采用花费较少的软件方式,还是购买代价高昂在性能功能上更强的第四层交换机,负载均衡器等硬件方式来实现,亦或其他种类不同的均衡技术,下面这几项都是我们在引入均衡方案时可能要考虑的问题:


  性能:性能是我们在引入均衡方案时需要重点考虑的问题,但也是一个最难把握的问题.衡量性能时可将每秒钟通过网络的数据包数目做为一个参数,另一个参数是均衡方案中服务器群所能处理的最大并发连接数目,但是,假设一个均衡系统能处理百万计的并发连接数,可是却只能以每秒2个包的速率转发,这显然是没有任何作用的.性能的优劣与负载均衡设备的处理能力,采用的均衡策略息息相关,并且有两点需要注意:一,均衡方案对服务器群整体的性能,这是响应客户端连接请求速度的关键;二,负载均衡设备自身的性能,避免有大量连接请求时自身性能不足而成为服务瓶颈.有时我们也可以考虑采用混合型负载均衡策略来提升服务器群的总体性能,如DNS负载均衡与NAT负载均衡相结合.另外,针对有大量静态文档请求的站点,也可以考虑采用高速缓存技术,相对来说更节省费用,更能提高响应性能;对有大量ssl/xml内容传输的站点,更应考虑采用ssl/xml加速技术.


  可扩展性:IT技术日新月异,一年以前最新的产品,现在或许已是网络中性能最低的产品;业务量的急速上升,一年前的网络,现在需要新一轮的扩展.合适的均衡解决方案应能满足这些需求,能均衡不同操作系统和硬件平台之间的负载,能均衡HTTP,邮件,新闻,代理,数据库,防火墙和
Cache等不同服务器的负载,并且能以对客户端完全透明的方式动态增加或删除某些资源.

  灵活性:均衡解决方案应能灵活地提供不同的应用需求,满足应用需求的不断变化.在不同的服务器群有不同的应用需求时,应有多样的均衡策略提供更广泛的选择.


  可靠性:在对服务质量要求较高的站点,负载均衡解决方案应能为服务器群提供完全的容错性和高可用性.但在负载均衡设备自身出现故障时,应该有良好的冗余解决方案,提高可靠性.使用冗余时,处于同一个冗余单元的多个负载均衡设备必须具有有效的方式以便互相进行监控,保护系统尽可能地避免遭受到重大故障的损失.


  易管理性:不管是通过软件还是硬件方式的均衡解决方案,我们都希望它有灵活,直观和安全的管理方式,这样便于安装,配置,维护和监控,提高工作效率,避免差错.在硬件负载均衡设备上,目前主要有三种管理方式可供选择:一,命令行接口(CLI:Command
Line
Interface),可通过超级终端连接负载均衡设备串行接口来管理,也能telnet远程登录管理,在初始化配置时,往往要用到前者;二,图形用户接口(GUI:Graphical
User Interfaces),有基于普通web页的管理,也有通过Java Applet
进行安全管理,一般都需要管理端安装有某个版本的浏览器;三,SNMP(Simple Network Management
Protocol,简单网络管理协议)支持,通过第三方网络管理软件对符合SNMP标准的设备进行管理.