简体   繁体   English

负载均衡服务器,我该如何实现呢?

[英]Load balancing server, how can I implement it?

I googled for load balancing but the only thing I can find is the working theory, which at the moment, is the "easy" part for me. 我用谷歌搜索负载平衡,但我唯一能找到的是工作理论,目前,这对我来说是“容易”的部分。 But zero examples of how to implement one. 但是如何实现一个的零例子。

I have several questions pertaining load balancing: 我有几个与负载平衡有关的问题:

  1. I have a domain (example.com) and I behind it I have a load balancing server (lets call it A ) which, according to the theory, will ask the client to close the connection with A, and connect to B, a sub-server and carry on the request with B. Will the client, in a web browser stop seeing "example.com/page.html" in the address bar and start seeing "B_ip_address/page.html" instead? 我有一个域(example.com),我背后有一个负载均衡服务器(让我们称之为A ),根据理论,它将要求客户端关闭与A的连接,并连接到B,一个子-server并用B继续请求。客户端,在Web浏览器中停止在地址栏中看到“example.com/page.html”并开始看到“B_ip_address / page.html”吗?

  2. How can I implement a simple load balancer from scratch? 如何从头开始实现简单的负载均衡器? My doubt targets the HTTP part. 我怀疑的目标是HTTP部分。 Is there some specific message or set of messages I need to send the client which will make him disconnect from me and connect to a sub-server? 我需要发送一些特定的消息或消息集来发送客户端,这会使他与我断开连接并连接到子服务器吗?

  3. What about lower level protocols than HTTP, such as TCP/IP, are there any standard packets to tell the client he just connected to a load balancer server and now he needs to connect to xxx.xxx.xxx.xxx to carry on the request? 那些比HTTP更低级别的协议,比如TCP / IP,是否有任何标准数据包告诉客户端他刚刚连接到负载均衡器服务器,现在他需要连接到xxx.xxx.xxx.xxx来进行请求?

  4. What method is the most used? 最常用的方法是什么? (1) client connects to load balancing server, and it asks the client to directly connect to one of the sub-servers, or (2) the load balancing server starts bridging all traffic from the client to the sub-server and vice-versa in a transparent fashion? (1)客户端连接到负载均衡服务器,它要求客户端直接连接到其中一个子服务器,或者(2)负载均衡服务器开始桥接从客户端到子服务器的所有流量,反之亦然以透明的方式?

So question 2, 3 and 4 regard the load balancing protocol, and the 1st one the way a domain name can be connected to a load balancer and what are the underlying consequences. 因此问题2,3和4是负载平衡协议,第一个是域名可以连接到负载均衡器的方式,以及潜在的后果。

Your approach is a kind of static load balancing by redirect the calls to another server. 通过将调用重定向到另一台服务器,您的方法是一种静态负载平衡。 All following calls may use this other server or are send to the load balancer again for redirect. 以下所有呼叫都可以使用此其他服务器,或者再次发送到负载均衡器以进行重定向。

An implementation depends on the implementation of your system. 实现取决于系统的实现。 A load balancer works best for independent requests with no session state. 负载均衡器最适用于没有会话状态的独立请求。 You need to sync the session state otherwise between the "end" servers. 您需要在“结束”服务器之间同步会话状态。 Or use a shared session store to provide the session state to all servers. 或使用共享会话存储为所有服务器提供会话状态。

There exists a simple and transparent solution for HTTP server load balancing. 存在一种简单透明的HTTP服务器负载平衡解决方案。 You can use the load balancing module of an nginx server ( http://nginx.org/en/docs/http/load_balancing.html ). 您可以使用nginx服务器的负载平衡模块( http://nginx.org/en/docs/http/load_balancing.html )。 This can be used for HTTP and HTTPS requests. 这可用于HTTP和HTTPS请求。 And it may be extended with extra servers dynamically if the load increases. 如果负载增加,它可以动态扩展额外的服务器。 You need to edit the nginx configuration and restart the server. 您需要编辑nginx配置并重新启动服务器。 This can be transparent to existing connections. 这对现有连接是透明的。 And nginx does not cause problems with changing domain or host names. 并且nginx不会导致更改域名或主机名的问题。

Other protocols need some support by the client and the server. 其他协议需要客户端和服务器的一些支持。 Load balancing may be transparent if a specialized device is between the client and server. 如果专用设备位于客户端和服务器之间,则负载平衡可能是透明的。 Or the communication protocol needs to support connection redirects. 或者通信协议需要支持连接重定向。

Edit: Load balancing can be implemented by DNS round robin too. 编辑:负载均衡也可以通过DNS循环实现。 Each DNS lookup call returns another IP address for the same domain name. 每个DNS查找调用都返回同一域名的另一个IP地址。 The client choose an IP and connects to this server. 客户端选择IP并连接到此服务器。 Another client can use the next IP. 另一个客户端可以使用下一个IP。 The address bar name is the same all the time. 地址栏名称始终相同。

Example: 例:

Non-authoritative answer:
Name:    www.google.com
Addresses:  2a00:1450:4001:80f::1010
      173.194.116.209
      173.194.116.210
      173.194.116.212
      173.194.116.211
      173.194.116.208

Non-authoritative answer:
Name:    www.google.com
Addresses:  2a00:1450:4001:80f::1010
      173.194.116.210
      173.194.116.212
      173.194.116.211
      173.194.116.208
      173.194.116.209

Non-authoritative answer:
Name:    www.google.com
Addresses:  2a00:1450:4001:80f::1010
      173.194.116.212
      173.194.116.211
      173.194.116.208
      173.194.116.209
      173.194.116.210

The IP address range rotates. IP地址范围旋转。 Most HTTP load balancers work as transparent load balancer like nginx or other reverse proxy implementations. 大多数HTTP负载均衡器都像nginx或其他反向代理实现一样工作为透明负载均衡器。 A redirecting load balancer is more a low tech implementation I think. 我认为重定向负载均衡器更像是一种低技术实现。

TCP/IP is not a protocol. TCP / IP不是协议。 It's the transport layer used to transfer data implementing a specific communication protocol. 它是用于传输实现特定通信协议的数据的传输层。 While TCP/IP itself is a protocol for the network components. 而TCP / IP本身是网络组件的协议。 But not the applications. 但不是应用程序。 You may check https://en.wikipedia.org/wiki/OSI_model . 您可以查看https://en.wikipedia.org/wiki/OSI_model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM