Load Balancing
负载均衡(load balancing,LB) 是用于给多个服务充当流量代理的解决方案,它负责分发不同应用流量到不同服务,还负责平衡不同服务器上同一个服务的流量负载以提高整体应用程序性能、稳定性,同时需要保持会话。负载均衡 由 负载均衡器(load balancer,LB) 实现,有专用硬件实现,也有软件实现。
即使后端实例只有一个,也建议部署负载均衡器做一对一转发,因为 负载均衡 有许多重要优势:
- 可用性(Availability)
- 可扩展性(Scalability)
- 安全性(Security)
- 性能(Performance)
负载均衡算法
负载均衡算法是负载均衡器采取的流量分发策略,分为静态和动态两类。
静态负载均衡算法
静态负载均衡算法根据预先设置好的计划转发流量,不会运行时动态调整。因此这种算法可能造成节点过载/节点饥饿。
主要的算法有:
- 客户端随机(client-side random):客户端拥有一串服务器 IP 列表,由客户端随机选择访问哪个服务器
- 轮询(round robin):这种算法使用在 DNS LB 上,在 DNS 服务器上给某个域名配置一串记录,每次应答 DNS 请求时将记录乱序响应
动态负载均衡算法
动态负载均衡算法在运行时根据每个服务器状态(可用性、工作量和健康状况等)调整流量转发。但这种算法配置比较复杂。
主要的算法有:
- 最少连接(least connection):LB 将检测后端服务的连接数,并将新请求转发到连接数最少的节点上
- 响应时间(response time):LB 记录不同节点的平均响应时间,并结合连接数来转发请求
- 资源检测(resource-based):每个节点上运行代理软件收集系统资源信息(CPU、内存等)来辅助 LB 决策将请求发往哪个节点
负载均衡类型
通常根据 OSI 模型 分类 负载均衡器,有四层负载均衡器和七层负载均衡器。
- 四层负载均衡器(layer 4 load balancers):根据五元组(source IP、source port、destination IP、destination port 和 protocol)转发 IP 包。不会做拥塞控制、不会查看数据包内容,因此并不是和客户端、后端服务建立新连接。
- 七层负载均衡器(layer 7 load balancers):与客户端建立连接后,再与后端服务建立新连接,将数据包转发。由于七层负载均衡器能拿到完整的应用层报文,因此可以根据 HTTP path、header 等信息配置不同转发规则。
Load balancing is the practice of distributing computational workloads between two or more computers. On the Internet, load balancing is often employed to divide network traffic among several servers. This reduces the strain on each server and makes the servers more efficient, speeding up performance and reducing latency. Load balancing is essential for most Internet applications to function properly.[1]
A load balancer is a solution that acts as a traffic proxy and distributes network or application traffic across endpoints on a number of servers. Load balancers are used to distribute capacity during peak traffic times, and to increase reliability of applications. They improve the overall performance of applications by decreasing the burden on individual services or clouds, and distribute the demand across different compute surfaces to help maintain application and network sessions.[2]
A load balancing algorithm is the logic that a load balancer uses to distribute network traffic between servers (an algorithm is a set of predefined rules).
There are two primary approaches to load balancing. Dynamic load balancing uses algorithms that take into account the current state of each server and distribute traffic accordingly. Static load balancing distributes traffic without making these adjustments.[3]
There are two types of load-balancing algorithms in terms of how they operate: static and dynamic. Static load balancing measures the incoming load on a server using algorithms that have performance capacity information about the existing servers in the distributed network. Dynamic load balancing can dynamically identify the amount of load that needs to be shed during runtime and which system should bear the load.[2:1]
Static load balancing algorithms distribute workloads without taking into account the current state of the system. A static load balancer will not be aware of which servers are performing slowly and which servers are not being used enough. Instead it assigns workloads based on a predetermined plan. Static load balancing is quick to set up, but can result in inefficiencies.[1:1]
Dynamic load balancing algorithms take the current availability, workload, and health of each server into account. They can shift traffic from overburdened or poorly performing servers to underutilized servers, keeping the distribution even and efficient. However, dynamic load balancing is more difficult to configure. A number of different factors play into server availability: the health and overall capacity of each server, the size of the tasks being distributed, and so on.[1:2]