Load balancing principle (vertical expansion Scale Up, scale Scale Out)

 
Original link: https://mp.weixin.qq.com/s/NUFRX51D9Yf9yzRQnPeWlg

Transfer: Code AGRICULTURAL stand (Micro Signal: coderising)

This is an ordinary morning in 1998.

A work, the boss took Zhang big fat called into the office, drinking tea while comfortably side took the lead: "? Big fat ah, we developed this site, now how more and more slow."

Fortunately, Zhang big fat also noticed this problem, he was well prepared, a look of frustration, said: "Well, I checked yesterday about the system, and now the traffic has been growing, whether it is CPU, or hard drives, memory They are overwhelmed, the peak of the response speed more slowly. "

Paused, he tentatively asked: "Boss, can now buy a good machine to 'break the old small' server to replace IBM's server I've heard very good, strong performance, or to a?. station?"

(Agricultural turn code Note: This is called a vertical extension Scale Up)

"Well your head, you know that the machine is much more expensive?! Our small company can not afford ah!" Stingy boss rejected immediately.

"This is ..." Big Fat represent the end of their rope.

"You go down and CTO Bill discuss tomorrow to get me out of the program."

Boss no matter the process, as long as the result.

1, hide the real server

Taipan bitterly to find Bill.

He will instruct extremely rich boss did convey.

Bill smiled: "Recently, I also think about it, wanted to discuss with you, see if you can buy a few inexpensive server, the management system more than several deploy, scale (Scale Out) it."

Scale? Zhang big fat heart wondering, if the system is to be deployed on several servers, user access requests will be distributed to each server, a single server that pressure on much smaller.

"But," Zhang Taipan asked, "more than a machine, each machine an IP, users probably confused, in the end what a visit?"

 

 "Definitely not exposed to these servers, from the customer point of view, the best is only one server ." Bill said. Zhang big fat themselves, suddenly had an idea: "With us there is a middle layer ah, right, that is, DNS, we can set and let the domain name of our site is mapped to multiple IP servers, users face is! the domain of our system, then we can use a polling mode, the machine user to do 1 domain name resolution time, DNS returns IP1, user machine 2 when doing DNS, DNS returns IP2 ... this can be achieved without the individual the machine load is relatively balanced yet? "

 

 Bill thinking for a moment, found a loophole: "This has a very terrible problem, because the DNS hierarchical system has the cache , the client machine also has a cache, if a machine fails, the DNS will still return to the problem the machine's IP, then all users access the machine will go wrong, even if we put this machine's IP is deleted from the DNS does not work, which is in trouble. "

Zhang big fat really did not expect to bring this cache problem, he scratched his head:. "Then harder."

2, The Italian Job

"Why do not we own realization of how to develop a software load balancing?" Bill another way.

To demonstrate their ideas, he drew up a map on a whiteboard, "to see the middle of the blue servers do not, we can call it Load Balancer (referred to as LB), the user's requests are sent to him, then again it distributed to the server. "

 

 Zhang Taipan closer look at this chart.

Load Balancer referred LB , there are two IP, a Foreign (115.39.19.22), a internal (192.168.0.100). The user sees is that external IP. The server actually served behind the three, called RS1, RS2, RS3 , their gateway point LB.

"But how to forward the request it? Ah, the user's request in the end is what?" Zhang Tai Pan confused.

"You have to forget about the computer network? Is sent by the user packets Well! You see the layers of encapsulated packet, send a request to a user's HTTP want to visit the home page of our website, this HTTP request is put to a TCP packet, and then was placed in an IP datagram, the final destination is our Load Balancer (115.39.19.22). "

 

 (Note: LB packets sent to the client, not shown frame data link layer)

"But this is a look at the packet sent to the Load Balancer, how sent to the back of the server?"

Bill said: "The Italian Job can, for example, Load Balancer want this packet to the RS1 (192.168.0.10), you can do hands and feet, put the packet into this, then that IP packets can be forwarded to the RS1 to deal a. "

 

 (LB move the hands and feet, the destination IP and port to the RS1) "RS1 treatment is over, to return home in HTML, but also the HTTP packet encapsulation layers:" Zhang Taipan understand how it happens:

 

 (RS1 treatment is over, to send the results to the client) "Since LB is a gateway, it will receive the packet, means it can play again, the source address and source port are replaced with their own, and then to the customer on it. "

 

 (LB hands and feet again, the source address and port into their own, so that the client was unaware)

Zhang big fat sums up the flow of data:
Client -> Load Balancer -> RS - > Load Balancer -> Client

He said excitedly: "This is really wonderful ah trick to deceive, the client simply do not feel there are several servers at work behind, it has been thought that only the Load Balancer at work."

Bill Load Balancer now thinking about how can choose various real servers behind, can have a variety of strategies, he wrote on the board:

轮询: 这个最简单,就是一个挨一个轮换。
加权轮询: 为了应对某些服务器性能好,可以让他们的权重高一点,被选中的几率大一点。
最少连接: 哪个服务器处理的连接少,就发给谁。
加权最少连接:在最少连接的基础上,也加上权重

还有些其他的算法和策略,以后慢慢想。

3、四层还是七层?

张大胖却想到了另外一个问题: 对于用户的一个请求来说,可能会被分成多个数据包来发送,如果这些数据包被我们的Load Balancer发到了不同的机器上,那就完全乱套了啊! 他把自己的想法告诉了Bill。

Bill说:“这个问题很好啊,我们的Load Balancer必须得维护一个表,这个表需要记录下客户端的数据包被我们转发到了哪个真实的服务器上, 这样当下一个数据包到来时,我们就可以把它转发到同一个服务器上去。”

“看来这个负载均衡软件需要是面向连接的,也就是OSI网络体系的第4层, 可以称为四层负载均衡”Bill做了一个总结。

“既然有四层负载均衡,那是不是也可以搞个七层的负载均衡啊?” 张大胖突发奇想。

“那是肯定的,如果我们的Load Balancer把HTTP层的报文数据取出来,根据其中的URL,浏览器,语言等信息,把请求分发到后面真实的服务器去,那就是七层的负载均衡了。不过我们现阶段先实现一个四层的吧,七层的以后再说。”

Bill 吩咐张大胖组织人力把这个负载均衡软件给开发出来。

张大胖不敢怠慢,由于涉及到协议的细节问题,张大胖还买了几本书:《TCP/IP详解》 卷一,卷二,卷三, 带着人快速复习了C语言, 然后开始疯狂开发。

4、责任分离

三个月后,Load Balancer的第一版开发出来了,这是运行在Linux上的一个软件, 公司试用了一下,管理系统运行流畅,感觉还真是不错,仅仅用几台便宜的服务器就可以实现负载均衡了。

老板看到没花多少钱就解决了问题,非常满意,给张大胖所在的开发组发了1000块钱奖金,组织大家出去搓了一顿。

张大胖他们看到老板很抠门,虽略有不满,但是想到通过这个软件的开发,学到了很多底层的知识,尤其是TCP协议,也就忍了。

可是好景不长,张大胖发现这个Load Balancer存在这瓶颈:所有的流量都要通过它,它要修改客户发来的数据包, 还要修改发给客户的数据包

网络访问还有个极大的特点,那就是请求报文较短而响应报文往往包含大量的数据。这是很容易理解的,一个HTTP GET请求短得可怜,可是返回的HTML却是极长,这就进一步加剧了Load Balancer修改数据包的工作。

张大胖赶紧去找Bill ,Bill说:“这确实是个问题,我们把请求和响应分开处理吧,让Load Balancer只处理请求,让各个服务器把响应直接发给客户端,这样瓶颈不就消除了吗?”

“怎么分开处理?”

“首先让所有的服务器都有同一个IP, 我们把他称为VIP吧(如图中115.39.19.22)。”

 

 张大胖通过第一版Load Balancer的开发,积累了丰富的经验。

他问道:“你这是把每个实际服务器的loopback都绑定了那个VIP, 不过有问题啊,这么多服务器都有同样的IP , 当IP数据包来的时候,到底应该由哪个服务器来处理?”

“注意,IP数据包其实是通过数据链路层发过来的,你看看这个图。”

 

 张大胖看到了客户端的HTTP报文再次被封装储层TCP报文,端口号是80, 然后IP数据报中的目的地是115.39.19.22(VIP)。

图中的问号是目的地的MAC地址, 该怎么得到呢?

对, 是使用ARP协议,把一个IP地址(115.39.19.22)给广播出去,然后具有此IP机器就会回复自己的MAC地址。 但是现在有好几台机器都有同一个IP(115.39.19.22), 怎么办?

Bill 说道:“我们只让Load Balancer 响应这个VIP地址(115.39.19.22)的ARP请求,对于RS1,RS2,RS3, 抑制住对这个VIP地址的ARP响应,不就可以唯一地确定Load Balancer了? ”

原来如此!张大胖恍然大悟。

既然Load Balancer得到了这个IP数据包, 它就可以用某个策略从RS1, RS2,RS3中选取一个服务器,例如RS1(192.168.0.10),把IP数据报原封不动, 封装成数据链路层的包(目的地是RS1的MAC地址),直接转发就可以了。

 

RS1(192.168.0.10)这个服务器收到了数据包,拆开一看,目的地IP是115.39.19.22,是自己的IP, 那就可以处理了。

处理完了以后,RS1可以直接响应发回给客户端,完全不用再通过Load Balancer。因为自己的地址就是115.39.19.22。

对于客户端来说,它看到的还是那个唯一的地址115.39.19.22, 并不知道后台发生了什么事情。

Bill补充到:“由于Load Balancer 根本不会修改IP数据报,其中的TCP的端口号自然也不会修改,这就要求RS1, RS2,RS3上的端口号必须得和Load Balancer一致才行。”

像之前一样,张大胖总结了一下数据的流向:

客户端 --> Load Balancer --> RS --> 客户端

Bill 说道:“怎么样? 这个办法还可以吧?”

张大胖又想了想,这种方式似乎没有漏洞,并且效率很高,Load Balancer只负责把用户请求发给特定的服务器就万事大吉了, 剩下的事由具体的服务器来处理,和它没有关系了。

他高兴地说:“不错,我着手带人去实现了。”

后记: 本文所描述的,其实就是著名开源软件LVS的原理,上面讲的两种负载均衡的方式,就是LVS的NAT和DR。

LVS是章文嵩博士在1998年5月成立的自由软件项目,现在已经是Linux内核的一部分。想想那时候我还在不亦乐乎地折腾个人网页,学会安装和使用Linux 没多久 , 服务器端开发也仅限于ASP,像LVS这种负载均衡的概念压根就没有听说过。

编程语言可以学,差距也能弥补,但是这种境界和眼光的差距,简直就是巨大的鸿沟,难以跨越啊!

Guess you like

Origin www.cnblogs.com/skycto/p/11469794.html