Load Balancing
概述
负载均衡通常是基于连接的,负载均衡策略是一个算法或公式用来决定将流量转发到那个节点。常见的负载分配策略主要包含以下几种:
静态负载均衡算法 - 轮询,比率,优先权
动态负载均衡算法 - 最少连接数,最快响应速度,观察方法,预测法,动态性能分配
可编程控制的负载均衡策略 - 通过编程控制应用流量的导向
静态负载均衡算法
轮询算法(Round Robin)
默认负载均衡策略
顺序循环将请求一次顺序循环地连接每个服务器。当其中某个服务器发生第 2 到第 7 层的故障,则将其从顺序循环队列中拿出,不参加下一次的轮询,直到其恢复正常。
Note | 轮询算法通常用于比较简单的情况,有时,在大流量和大量新建连接时,也采用轮 询算法保证服务器接收请求的平均性,避免动态算法的计算间隔带来的单台服务器 压力过大的问题。 |
// 1. create vs and pool
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode round-robin
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test round robin
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
比率算法(Ratio (member) Ratio (node))
给每个服务器分配一个加权值为比例,根椐这个比例,把用户的请求分配到每个服务器。当其中某个服务器发生第二到第 7 层的故障,就把 其从服务器队列中拿出,不参加下一次的用户请求的分配, 直到其恢复正常。
比率(Ratio) 可以设定在 pool member 或 node 上,如果设定在 pool member 上,则选择 Ratio (member) ,如果设定在 node 上则选择 Ratio (node)。
Note | 比率算法通常用于在后台服务器的服务能力不均匀的情况下。可以手工指定每台服务器所接收的请求比率。实现负载的均衡性。 |
// set ratio
modify ltm pool http_pool members modify { 10.1.20.11:tproxy { ratio 5 } 10.1.20.12:tproxy { ratio 2 } 10.1.20.13:tproxy { ratio 1 } } load-balancing-mode ratio-member
// test Ratio (member)
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
示例 - Ratio (node)// update load balance mode
modify ltm pool http_pool members replace-all-with { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode ratio-node
// set node ratio
modify ltm node 10.1.20.11 ratio 5
modify ltm node 10.1.20.12 ratio 2
modify ltm node 10.1.20.13 ratio 1
// test Ratio (node)
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
// set back node ratio
modify ltm node 10.1.20.11 ratio 1
modify ltm node 10.1.20.12 ratio 1
动态负载均衡算法
最小连接数(Least Connections (member) Least Connections (node))
每一台服务器的当前连接数进行统计(L4 层连接数),当有新的请求进入时,将新的请求分配给当前最少连接处理的服务器。当其中某个服务器发生第 2 到第 7 层的故障,就把其从服务器队列中拿出,不参加下一次的用户请求的分配, 直到其恢复正常。
最小连接数为最常用的负载均衡算法之一,在后台服务器处理能力均等的情况下, 使用最小连接数可以得到最为平衡的负载均衡效果。
Note | 最小连接数负载均衡算法根据连接数统计的方式不同,分为两种:Least Connections (member) 和 Least Connections (node),node 和 member 统计连接数的区别: member 连接总数只统计一个 pool 内 member 上的连接,node 连接总数的统计可能跨 pool。 |
// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode least-connections-member
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
示例 - Least Connections (node)// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode least-connections-node
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
最快响应速度(Fastest (node) Fastest (application))
通过观察每台服务器得应用响应速度(统计 7 层的连接数,连接数越少,则认为响应速度越快),当有新的请求进入的时候,将新的请求分配给响应最快的服务器。当其中某个服务器发生第 2 到第 7 层的故障, BIG-IP LTM 就把其从服务器队列中拿出,不参加下一次的用户请求的分配,直到其恢复正常。
Note | 最快响应速度负载均衡算法需要依赖 TCP Profile 和 HTTP profile。 |
Note | 最快响应速度 Fastest 类似于最小连接数 Least Connections,对响应速度的判断也是基于一个 pool member 上 Application,和一个 node 上。 |
// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode fastest-app-response
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp profiles add { http { } }
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
示例 - Fastest (node)// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode fastest-node
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp profiles add { http { } }
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
动态观察(Observed (member) Observed (node))
根据 L4 层的连接数动态设定一个比率,然后按照比率去分发,这个比率会随着时间推进及不同节点上的 L4 层连接情况进行重新设定,同样。当其中某个服务器发生第 2 到第 7 层的故障,BIG-IP LTM 就把其从服务器队列中拿出,不参加下一次的用户请求的分配,直到其恢复正常。
Note | 和最小连接数、最快响应速度类似,动态观察也会基于 member 和 node 去统计 L4 层连接情况。 |
Note | 这种负载均衡算法会带来额外的 CPU 和资源消耗,应尽量避免使用。 |
// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode observed-member
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
示例 - Observed (node)// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode observed-node
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
动态预知(Predictive (member) Predictive (node))
和动态观察类似,也是基于 L4 层的连接数进行动态分配比率,不同之处是动态预知会基于过去一段时间的平均值作为比率升降的判断依据。当其中某个服务器发生第 2 到第 7 层的故障,BIG-IP LTM 就把其从服务器队列中拿出,不参加下一次的用户请求的分配,直>到其恢复正常。
Note | 和最小连接数、最快响应速度类似、动态观察,动态预知也是基于 member 和 node 去统计 L4 层连接情况。 |
Note | 和动态观察类似,这种负载均衡算法会带来额外的 CPU 和资源消耗,应尽量避免使用。 |
// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode predictive-member
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
示例 - Predictive (node)// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode predictive-node
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
加权最小连接数(Weighted Least Connections (member) Weighted Least Connections (node))
类似最小连接数,基于 L4 层连接数分配请求,主要处理后端服务器性能不均的情况,例如 Server 1 上有 20 个连接且最大连接数为 100,Server 2 上有 30 个连接且支持最大连接数为 200,那么该算法会将请求分发到 Server 2。
该算法需要所有 pool member 设定 connection limit 气人大小大于 0。
加权算法:capacity = Current connections / Connection limit
Note | 加权最小连接数也有基于单个 pool member,及 node。 |
// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 connection-limit 100} 10.1.20.12:8081 { address 10.1.20.12 connection-limit 200} 10.1.20.13:8081 { address 10.1.20.13 connection-limit 300} } load-balancing-mode weighted-least-connections-member
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
示例 - Weighted Least Connections (node)modify ltm node 10.1.20.11 connection-limit 200
modify ltm node 10.1.20.12 connection-limit 300
modify ltm node 10.1.20.13 connection-limit 400
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode weighted-least-connections-node
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
Least Sessions
与 persistence table 中的实体个数相关联计算分发策略,新的连接将会转发给 persistence 连接最少的 pool member
不支持 cookie persistence
// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode least-sessions
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
动态比率算法(Dynamic Ratio (member) Dynamic Ratio (node))
类似于比率算法,但比率是动态计算出,通常通过插件获取第三方监控平台服务器的性能等指标值。
示例 - Dynamic Ratio (member)// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode dynamic-ratio-member
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
示例 - Dynamic Ratio (node)// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode dynamic-ratio-member
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
比率最小连接数(Ratio Least Connections (member) Ratio Least Connections (node))
根据每个 pool member 或 node 上连接数的比率动态分配。
示例 - Ratio Least Connections (member)// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode ratio-least-connections-member
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done
示例 - Ratio Least Connections (node)// 1. create vs
create ltm pool http_pool members add { 10.1.20.11:8081 { address 10.1.20.11 } 10.1.20.12:8081 { address 10.1.20.12 } 10.1.20.13:8081 { address 10.1.20.13 } } load-balancing-mode ratio-least-connections-node
create ltm virtual http_vs destination 10.1.10.20:80 pool http_pool ip-protocol tcp
// 2. test
for i in {1..100} ; do curl http://10.1.10.20/server_addr.php ; echo "" ; done