LVS

目录

一、概述

二、LVS

2.1 术语

2.2 LVS模型

2.2.1 NAT

2.2.2 DR

2.3 调度方式


一、概述

主要为了了解,LVS是一个四层的LB

二、LVS

2.1 术语

  • RS Real server 
  • VS Virtual server
  • VIP Virtual IP
  • RIP
  • DIP

2.2 LVS模型

  • NAT
  • TUN
  • DR

2.2.1 NAT

先看一张示意图:

  1. 报文起始CIP->VIP
  2. 经过VS后,选择一个RS(如RS1), 做DNAT报文变为CIP->RIP1,目的端口也可以在此时改变。
  3. RS处理后,RIP1->CIP
  4. 经过VS,做SNAT, VIP->CIP

没什么好说的,就是做了一个NAT, 因此相应报文还要经过VS一次转换回来,因此NAT模式下,VS很容易成为系统的瓶颈。

接下来考虑,在VS和RS之间具体组网,根据上述分析可以看到,IP变换没有涉及到DIP,因此DIP和RIP没有约束关系,所以二者之间可以用交换机相连,当然是用路由器也没有问题,默认二者处于不同网段,但是基本上没有这么用的。
还有一种full-nat模式,再经过VS后报文也做SNAT,变成 DIP->RIP1,通常RIP和DIP不再同一网络,这样RS的默认路由就不用指向DIP了

总结一下NAT模式的特点:

  • RIP和VIP应该在同一网络,且应是用私网地址,RS网关指向DIP
  • 请求报文和响应报文都要经过VS,容易造成系统瓶颈
  • 支持端口映射,可修改请求报文的目的port

 

2.2.2 DR

DR模式只修改MAC地址,不改变IP

  1. GW_MAC:CIP->VS_MAC:VIP
  2. 上述报文进行二层转发,转发到VS
  3. VS选择一个RS(图中是RS1)做二层转发 GW_MAC:CIP->RS1_MAC:VIP
  4. RS1处理请求,进行响应RS1_MAC:VIP->GW_MAC:CIP,没有经过VS

上述过程中需要注意:

VS和所有的RS都要配置VIP,这是因为VS只修改2层MAC,因此请求报文的VIP必须是VS和RS的本机地址。

同一个二层下有相同IP(VIP)会产生冲突,LVS在2过程中必须先将报文转发至VS,一个可行的方法是对所有的RS的arp_announce和arp_ignore进行配置,使该RS不会主动发送arp,也不会应答arp

具体的描述在Documentation/networking/ip-sysctl.txt

arp_ignore - INTEGER
	Define different modes for sending replies in response to
	received ARP requests that resolve local target IP addresses:
	0 - (default): reply for any local target IP address, configured
	on any interface
	1 - reply only if the target IP address is local address
	configured on the incoming interface
	2 - reply only if the target IP address is local address
	configured on the incoming interface and both with the
	sender's IP address are part from same subnet on this interface
	3 - do not reply for local addresses configured with scope host,
	only resolutions for global and link addresses are replied
	4-7 - reserved
	8 - do not reply for all local addresses

	The max value from conf/{all,interface}/arp_ignore is used
	when ARP request is received on the {interface}

arp_announce - INTEGER
	Define different restriction levels for announcing the local
	source IP address from IP packets in ARP requests sent on
	interface:
	0 - (default) Use any local address, configured on any interface
	1 - Try to avoid local addresses that are not in the target's
	subnet for this interface. This mode is useful when target
	hosts reachable via this interface require the source IP
	address in ARP requests to be part of their logical network
	configured on the receiving interface. When we generate the
	request we will check all our subnets that include the
	target IP and will preserve the source address if it is from
	such subnet. If there is no such subnet we select source
	address according to the rules for level 2.
	2 - Always use the best local address for this target.
	In this mode we ignore the source address in the IP packet
	and try to select local address that we prefer for talks with
	the target host. Such local address is selected by looking
	for primary IP addresses on all our subnets on the outgoing
	interface that include the target IP address. If no suitable
	local address is found we select the first local address
	we have on the outgoing interface or on all other interfaces,
	with the hope we will receive reply for our request and
	even sometimes no matter the source IP address we announce.
  • arp_ignore默认器情况下会回复所有属于本地IP的arp,在DR模式下,要求RS不能对VIP进行响应,一般的通常将VIP配置在RS的lo口上,那么,将RS对应的RIP口上的arp_ignore设置成1即可。
  • arp_announce决定arp request时本地源IP的选择。默认情况下,可以使用任意本地IP,一般选择报文源IP.

总结一下DR模式的特点:

  • VS和所有RS上要配置同一个VIP
  • 确保前端路由器将arp发送给VS (arp_ignore/arp_announce)
  • 每个VS和RS都配有两个地址,私有地址和公网地址,这种情况下对应的网关(GW)上也要有两个地址。另外RIP也可以是公网地址,此时RIP网关不能指向DIP)
  • RS和VS要在同一个网络
  • 请求报文经过VS,响应报文不经过VS,直接返回
  • 不支持端口映射

2.2.3 TUN

VS选择一个RS(这里是RS1),通过隧道将原报文发往RS  DIP->RIP1 | CIP->VIP

RS拨开外层头,处理后直接回复

总结一下TUN模式的特点:

  • VIP,DIP,RIP都应该是公网地址(一般用于服务器分布在各地,跨路由器的情况),RIP是私网地址也没问题,但是又要考虑VIP冲突问题
  • 请求报文指向VS,响应报文直接回应,因此RS网关不能指向DIP
  • 不支持端口映射

2.3 调度方式

IPVS scheduler分为静态方法和动态方法,动态方法要考虑后端server负载情况。

静态调度算法有四种:

  • RR     round robin
  • WRR  weight RR
  • SH     source hashing,源地址hash,用来实现session sticky
  • DH     destination hashing,目的地址hash,典型的应用场景是正向代理缓存场景的负载均衡,如宽带运营商

关于DH可以参考下图(截的屏)

在正向代理的情况下,访问特定网站的数据被存储在特定的缓存服务器上,这样可以加速用户访问速度同时节省运营商带宽,这样就要求按照访问目的地址进行hash

动态调度算法有6种:

  • LC least connection (overhead=activeconns * 256 + inactiveconns)适用于长连接应用
  • WLC weighed LC  (overhead = (activeconns * 256 + inactiveconns) / weight) 默认的调度算法
  • SED shortest expection delay (overhead = (activeconns + 1)* 256  / weight) 初始连接高权重优先
  • NQ   never queue 第一次均匀分配,后续SED
  • LBLC locality-based LC  动态的DH, 根据负载调度
  • LBLCR LBLC with replication 带复制功能的LBLC,解决LBLC负载不均衡的问题

 

三、参考

【1】Linux内核参数之arp_ignore和arp_announce

【2】马哥Linux教程-2019全新LVS负载均衡实战

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值