Lvs enterprise-class load balancing cluster - based on the concept [3 modes of operation with 10 choices of scheduling algorithm]

First, load balancing LVS concepts

1, LB cluster

  • LB cluster: Load balance a cluster of shorthand, Chinese is the load balancing cluster.
    Commonly used open source software load balancing : nginx (TCPIP seven application-layer protocol), lvs (TCPIP protocol four transport layer), haproxy, varnish;
    commercial hardware load balancing device : F5, Netscale. LVS is a way to achieve LB cluster.
  • LB cluster architecture and principles: When a user requests over, will be distributed directly to the dispatch server (Director Server), then it according to the user's request to set a good scheduling algorithm, intelligent evenly distributed to the backend real servers (real server) on.

2, LVS concept

  • LVS: Linux Virtual Server short, that Linux virtual servers. LVS has now been integrated intoLinux kernel modulein. The project is in the Linux kernel implementsBased on IPLoad balancing data request scheduling scheme , which architecture shown in Figure 1, the terminal user from the outside Internet access the company's external server load balancing, the end user requests a Web LVS sent to the scheduler, the scheduler algorithm according to its own preset the decision to request a Web server to the backend. For example polling algorithm may request the external server to the average of all of the rear end, the rear end of the end user access to the server's real scheduler LVS although it will be forwarded to, but if the server is connected to the real memory is the same, the same service is provided services, end users no matter which real server access, services get are the same, the entire cluster is transparent to the user. Finally, according to the different operating modes of the LVS, the real server to choose the manner required by the user to send data to the end user,LVS NAT mode into the operating mode, TUN mode and the DR mode

Here Insert Picture Description

  • LVS related terms
    DS : Director Server. Load balancing refers to the front end node.
    RS : Real Server. The back-end server real work.
    The VIP : user request directly to the outside, as a user requestDestination IP Address.
    DIP : Director Server IP, IP addresses and is mainly used for internal communication with the host.
    RIP : Real Server IP, IP address back-end server.
    CIP : IP Address Client IP, access to the client.

  • lvs attached to netfiler
    Five built-hook function
    PREROUTING -> INPUT (internal flow)
    the PREROUTING ----> the FORWARD -> the POSTROUTING (forward) '
    OUTPUT-> the POSTROUTING (external flow)
    lvs operates INPUT: PREROUTING -> INPUT (lvs Here the forced data flow) -> POSTROUTING

 - 当用户向负载均衡调度器(Director Server)发起请求,调度器将请求发往至内核空间
 - PREROUTING链首先会接收到用户请求,判断目标IP确定是本机IP,将数据包发往INPUT链
 - IPVS是工作在INPUT链上的,当用户请求到达INPUT时,IPVS会将用户请求和自己已定义好的集群服务进行比对,如果用户请求的就是定义的集群服务,那么此时IPVS会强行修改数据包里的目标IP地址及端口,并将新的数据包发往POSTROUTING链
 - POSTROUTING链接收数据包后发现目标IP地址刚好是自己的后端服务器,那么此时通过选路,将数据包最终发送给后端的服务器
  • lvs is consists of two parts
    ipvs (ip virrual server): a piece of code (work input on the hook function in the kernel netfilter) is scheduled code went into effect to achieve
    the ipvsadm :( work in user space command-line tool, responsible for the preparation of rules ipvs kernel framework for managing clusters service)

3, LVS three modes

  • NAT (network address translation)

    definitionDNAT multi-target (port forwarding) By modifying the request packet destination IP (VIP) address (and may modify the destination port), to pick out a RS (back-end servers work) address of the RIP (the back-end server's IP address) to achieve forward.
    effect: By modifying the data header that is located in a private IP address within the enterprise can access the external network , and external users can access located within the company's private IP hosts

    1) RS and DIP private network addresses should be used, and RS gateway to point DIP
    2) request and response messages should be forwarded via the DS,High load scenario, DS may become a bottleneck in the system
    3) RS the RIP and the DS DIP must be in the same IP network: RS gateway to point DIP
    Malpractice : High load scenario, DS may become a bottleneck in the system

Here Insert Picture Description

(a). 当用户请求到达Director Server,此时请求的数据报文会先到内核空间的PREROUTING链。 此时报文的源IP为CIP,目标IP为VIP
(b). PREROUTING检查发现数据包的目标IP是本机,将数据包送至INPUT链
(c). IPVS比对数据包请求的服务是否为集群服务,若是,修改数据包的目标IP地址为后端服务器IP,然后将数据包发至POSTROUTING链。 此时报文的源IP为CIP,目标IP为RIP
(d). POSTROUTING链通过选路,将数据包发送给Real Server
(e). Real Server比对发现目标为自己的IP,开始构建响应报文发回给Director Server。 此时报文的源IP为RIP,目标IP为CIP
(f). Director Server在响应客户端前,此时会将源IP地址修改为自己的VIP地址,然后响应给客户端。 此时报文的源IP为VIP,目标IP为CIP

  • TO DO

    IP tunning definition:在原有的IP报文外再次封装多一层IP首部,内部IP首部(源地址为CIP,目标IP为VIP),外层IP首部(源地址为DIP,目标IP为RIP),不修改请求报文的ip首部(cip—vip),而是通过在原有的ip首部(dip–rip),再封装一个ip首部。IP隧道(IP tunning) 是一种数据包封装技术,它可以将原始数据包封装并添加新的包头(内容包括新的源地址及端口、目标地址及端口),从而实现将一个目标为调度器的VIP地址的数据包封装,通过隧道转发给后端的真实服务器(Real Server),通过将客户端发往调度器的原始数据包封装,并在其基础上添加新的数据包头(修改目标地址为调度器选择出来的真实服务器的IP地址及对应端口),LVS(TUN)模式要求真实服务器可以直接与外部网络连接,真实服务器在收到请求数据包后直接给客户端主机响应数据。

    TUN的定义: 数据请求包往往远小于响应数据包的大小。 因为响应数据包中包含有客户需要的具体数据,所以 LVS(TUN)的思路就是将请求与响应数据分离,让调度器仅处理数据请求,而让真实服务器响应数据包直接返回给客户端。
    1) RIP DIP VIP 全是公网地址
    2)RS的网关不能指向DIP
    3)请求报文必须经由director调度,但响应报文必须不能经由director
    4)RS的os必须支持隧道功能
    弊端: 在LVS(TUN)模式下,由于需要在LVS调度器与真实服务器之间创建隧道连接,这同样会 增加服务器的负担

Here Insert Picture Description

(a) 当用户请求到达Director Server,此时请求的数据报文会先到内核空间的PREROUTING链。 此时报文的源IP为CIP,目标IP为VIP 。
(b) PREROUTING检查发现数据包的目标IP是本机,将数据包送至INPUT链
(c) IPVS比对数据包请求的服务是否为集群服务,若是,在请求报文的首部再次封装一层IP报文,封装源IP为为DIP,目标IP为RIP。然后发至POSTROUTING链。 此时源IP为DIP,目标IP为RIP
(d) POSTROUTING链根据最新封装的IP报文,将数据包发至RS(因为在外层封装多了一层IP首部,所以可以理解为此时通过隧道传输)。 此时源IP为DIP,目标IP为RIP
(e) RS接收到报文后发现是自己的IP地址,就将报文接收下来,拆除掉最外层的IP后,会发现里面还有一层IP首部,而且目标是自己的lo接口VIP,那么此时RS开始处理此请求,处理完成之后,通过lo接口送给eth0网卡,然后向外传递。 此时的源IP地址为VIP,目标IP为CIP
(f) 响应报文最终送达至客户端
  • DR(直接路由模式)

    DR模式: 该模式中 LVS依然仅承担数据的入站请求以及根据算法选出合理的真实服务器,最终由后端真实服务器负责将响应数据包发送返回给客户端。 与隧道模式不同的是,直接路由模式(DR模式)要求调度器与后端服务器必须在同一个局域网内,VIP地址需要在调度器与后端所有的服务器间共享, 因为最终的真实服务器给客户端回应数据包时需要设置源IP为VIP地址,目标IP为客户端IP,这样客户端访问的是调度器的VIP地址,回应的源地址也依然是该VIP地址(真实服务器上的VIP),客户端是感觉不到后端服务器存在的。由于多台计算机都设置了同样一个VIP地址,所以 在直接路由模式中要求调度器的VIP地址是对外可见的,客户端需要将请求数据包发送到调度器主机,而所有的真实服务器的VIP地址必须配置在Non-ARP的网络设备上,也就是该网络设备并不会向外广播自己的MAC及对应的IP地址,真实服务器的VIP对外界是不可见的,但真实服务器却可以接受目标地址VIP的网络请求,并在回应数据包时将源地址设置为该VIP地址。调度器根据算法在选出真实服务器后,在不修改数据报文的情况下,将数据帧的MAC地址修改为选出的真实服务器的MAC地址,通过交换机将该数据帧发给真实服务器。整个过程中,真实服务器的VIP不需要对外界可见。

  1. 保证前端路由器将目标IP为VIP的请求报文发送给director(静态绑定 arptables 修改rs主机内核的参数)
  2. rs的rip可以使用私有地址,但也可以使用公网地址
  3. 请求报文经由director调度,但向英国报文一定不能经由director
  4. rs跟director必须在同一物理网络中(同一物理网络不等于同网段)
  5. RS网关不能指向dip
    Here Insert Picture Description
(a) 当用户请求到达Director Server,此时请求的数据报文会先到内核空间的PREROUTING链。 此时报文的源IP为CIP,目标IP为VIP
(b) PREROUTING检查发现数据包的目标IP是本机,将数据包送至INPUT链
(c) IPVS比对数据包请求的服务是否为集群服务,若是,将请求报文中的源MAC地址修改为DIP的MAC地址,将目标MAC地址修改RIP的MAC地址,然后将数据包发至POSTROUTING链。 此时的源IP和目的IP均未修改,仅修改了源MAC地址为DIP的MAC地址,目标MAC地址为RIP的MAC地址
(d) 由于DS和RS在同一个网络中,所以是通过二层来传输。POSTROUTING链检查目标MAC地址为RIP的MAC地址,那么此时数据包将会发至Real Server。
(e) RS发现请求报文的MAC地址是自己的MAC地址,就接收此报文。处理完成之后,将响应报文通过lo接口传送给eth0网卡然后向外发出。 此时的源IP地址为VIP,目标IP为CIP
(f) 响应报文最终送达至客户端

4、LVS负载均衡的10种调度算法

(1)轮询调度算法
轮询调度(Round Robin 简称’RR’)算法就是按 依次循环 的方式将请求调度到不同的服务器上,该算法最大的特点就是实现简单. 轮询算法假设所有的服务器处理请求的能力都一样的,调度器会 将所有的请求平均分配给每个真实服务器 ,俗话说:你一个,我一个(web1接收一个访问,web2接收一个访问,这样轮询)

(2)加权轮询调度算法
加权轮询(Weight Round Robin 简称’WRR’)算法主要是对轮询算法的一种优化与补充,LVS会考虑每台服务器的性能,并给每台服务器添加一个权值,如果服务器A的权值为1,服务器B的权值为2,则调度器调度到服务器B的请求会是服务器A的两倍,权值越高的服务器,处理的请求越多 ,俗话说:拿钱多的干的活就多(哪个服务器性能好哪个服务器就多接受客户的请求)

(3)最小连接调度算法
最小连接调度(Least Connections 简称’LC’)算法是把新的连接请求分配到当前连接数最小的服务器,最小连接调度是一种动态的调度算法,它通过服务器当前活跃的连接数来估计服务器的情况,调度器需要记录各个服务器已建立连接的数目,当一个请求被调度到某台服务器,其连接数加1;当连接中断或者超时,其连接数减1。俗话说:谁比较闲就让谁干活。集群系统的真实服务器具有相近的系统性能,采用最小连接调度算法可以比较好地均衡负载

(4)加权最小连接调度算法
加权最少连接(Weight Least Connections 简称’WLC’)算法是最小连接调度的超集,各个服务器相应的权值表示其处理性能。服务器的缺省权值为1,系统管理员可以动态地设置服务器的权值。加权最小连接调度在调度新连接时尽可能使服务器的已建立连接数和其权值成比例。 调度器可以自动问询真实服务器的负载情况,并动态地调整其权值,俗话说:根据实际情况给后端服务器分配任务

(5)基于局部的最少连接算法
基于局部的最少连接调度(Locality-Based Least Connections 简称’LBLC’)算法是 针对请求报文的目标IP地址的负载均衡调度
目前主要 用于Cache集群系统 ,因为在Cache集群客户请求报文的目标IP地址是变化的这里假设任何后端服务器都可以处理任一请求,算法的设计目标是在 服务器的负载基本平衡情况下,将相同目标IP地址的请求调度到同一台服务器 ,来提高各台服务器的访问局部性和Cache命中率,从而提升整个集群系统的处理能力,LBLC调度算法先根据请求的目标IP地址找出该目标IP地址最近使用的服务器,若该服务器是可用的且没有超载,将请求发送到该服务器,若服务器不存在或该服务器超载且有服务器处于一半的工作负载,则使用’最少连接’的原则选出一个可用的服务器,将请求发送到服务器

(6)带复制的基于局部性的最少连接算法
带复制的基于局部性的最少连接(Locality-Based Least Connections with Replication 简称’LBLCR’)算法也是针对目标IP地址的负载均衡
目前主要用于Cache集群系统,它与LBLC算法不同之处是它要维护从一个目标IP地址到一组服务器的映射,而LBLC算法维护从一个目标IP地址到一台服务器的映射。按’最小连接’原则从该服务器组中选出一一台服务器,若服务器没有超载,将请求发送到该服务器;若服务器超载,则按’最小连接’原则从整个集群中选出一台服务器, 将该服务器加入到这个服务器组中,将请求发送到该服务器, 同时,当该服务器组有一段时间没有被修改,将最忙的服务器从服务器组中删除,以降低复制的程度

(7) the target address hash scheduling
target address hash schedule (Destination Hashing abbreviation 'DH') algorithm according to the request destination IP address as the hash key (Hash Key) to find the corresponding server from the list of hash static allocation If the server is available and not overloaded, the request is sent to the server, otherwise empty

(8) the source address hash scheduling algorithm
source address hash scheduling (Source Hashing abbreviation 'SH') algorithm to find the corresponding service from the list of hash static allocation request source IP address as the hash key (Hash Key), If the server is available and is not overloaded, the server sends a request to the otherwise empty, the hash function which uses the destination address of the same hash algorithm scheduling, its target address hash algorithm process scheduling algorithm substantially similar

(9) a desired minimum delay algorithm
shortest expected delay scheduling (Shortest Expected Delay referred to as 'SED') algorithm WLC algorithm, for example, right, right ABC three servers weight of 1,2,3, respectively, then if when using WLC algorithm, then a new request to enter it might give any ABC one, after use SED algorithm for a calculation a: (1 + 1) / 1 = 2 B: (1 + 2) / 2 = 3 / 2 C: (1 + 3 ) / 3 = 4/3 puts the request to the server with the smallest calculation result obtained

(10) Minimum queue scheduling
happened queue scheduling (Never Queue referred to as 'NQ') algorithm, without queue. If the number of connections is equal to 0 realserver directly assigned past, no operation is performed SED

Published 102 original articles · won praise 21 · views 5341

Guess you like

Origin blog.csdn.net/ranrancc_/article/details/101871992