LVS负载均衡之虚拟主机IP隧道模式
本文包含LVS负载均衡的IP隧道模式的工作原理,以及如何使用它以大大提高服务器集群的扩展性。
IP隧道-IP tunneling
IP隧道(IP封装)是一种将IP数据报封装到IP数据报中的技术,它允许将预定IP地址的数据报包装并重定向到另一个IP地址,IP封装技术目前通常被地用在外联网,移动IP,IP广播,隧道式主机。详情请看NET-3-HOWTO。
如何在LVS上使用IP隧道模式
首先,让我们通过下图了解虚拟主机的IP隧道原理,虚拟主机IP隧道模式与虚拟主机NAT模式最大的不同就是前者是负载均衡器通过IP隧道将请求发送给实服务器,而后者则是通过网络地址翻译将请求发送给实服务器。
当用户访问由服务器集群提供的虚拟服务时,一个包含有虚拟主机地址的数据包到达。负载均衡器检查数据包中的目标地址和端口,如果和虚拟服务的地址和端口相匹配,则通过连接调度算法选择一个实服务器,并将此连接添加进记录连接的Hash表中,然后,负载均衡器将源IP地址封装到数据包中并发送给所选择的实服务器。当该连接上的一个数据包到达,并且该连接仍然在Hash表中,则该数据包仍然会以相同的方式被封装并发送给该实服务器。当实服务器接收到封装过的包,首先解包,然后处理该请求,最后通过自己的路由表将应答直接返回给用户。在连接断开或超时后,连接记录将从Hash表中删除。下图说明了该流程。
注意,实服务器可以是任何网络的任何IP地址,它们可以分布在任意的地理位置,但是必须支持IP封包协议,并且将隧道设备全部配置为启用,以便能够正确地对接收的封装过的包进行解包,并且虚拟IP地址必须配置为不响应ARP,或者系统能被配置为可将数据包从虚拟地址重定到本地socket。
最后,当一个封装过的包到达,实服务器解包,并发现该包的目标地址是虚拟地址,表明“噢,这是我的菜,我来搞定。”它处理该请求然后将请求结果直接返回给用户端。
如何构建内核
首先,获得最新的linux内核拷贝源码。第二,获得正确版本的IPVS补丁并放入内核中,第三,确保内核编译的选项被选中,第四,重新构建内核。一旦你正确构建了内核,更新系统内核并重启系统。
1. 内核2.0.36相匹配的VS 补丁
内核编译选项如下:
Code maturity level options ---> [*] Prompt for development and/or incomplete code/drivers Networking options ---> [*] Network firewalls ... [*] IP: forwarding/gatewaying ... [*] IP: firewalling ... [*] IP: masquerading ... [*] IP: ippfvs(LinuxDirector) masquerading (EXPERIMENTAL) Virtual server request dispatching technique--- ( ) VS-NAT (X) VS-Tunneling ( ) VS-DRouting
你必须选择一个调度算法。
Virtual server scheduling algorithm (X) WeightedRoundRobin ( ) LeastConnection ( ) WeightedLeastConnection [ ] IP: enabling ippfvs with the local node feature
最后,cd到ippfvsadm的源码目录,输入“make install”,在系统目录安装ippfvsadm。
2. 内核2.2.x相匹配的VS补丁
内核编译选项如下:
Code maturity level options ---> [*] Prompt for development and/or incomplete code/drivers Networking options ---> [*] Network firewalls ... [*] IP: forwarding/gatewaying ... [*] IP: firewalling ... [*] IP: masquerading ... [*] IP: masquerading virtual server support (EXPERIMENTAL) (12) IP masquerading table size (the Nth power of 2) <M> IPVS: round-robin scheduling <M> IPVS: weighted round-robin scheduling <M> IPVS: least-connection scheduling <M> IPVS: weighted least-connection scheduling <M> IPVS: locality-based least-connection scheduling <M> IPVS: locality-based least-connection with replication scheduling
最后,cd到ipvsadm的源码目录,输入"make install",在系统目录安装ipvsadm,也可以通过rpm包进行安装。
3. 与内核2.4.x相匹配的IPVS补丁
内核编译选项如下:
Code maturity level options ---> [*] Prompt for development and/or incomplete code/drivers Networking options ---> [*] Network packet filtering (replaces ipchains) [ ] Network packet filtering debugging ... IP: Netfilter Configuration ---> IP: Virtual Server Configuration ---> <M> virtual server support (EXPERIMENTAL) [*] IP virtual server debugging (12) IPVS connection table size (the Nth power of 2) --- IPVS scheduler <M> round-robin scheduling <M> weighted round-robin scheduling <M> least-connection scheduling scheduling <M> weighted least-connection scheduling <M> locality-based least-connection scheduling <M> locality-based least-connection with replication scheduling <M> destination hashing scheduling <M> source hashing scheduling --- IPVS application helper <M> FTP protocol helper
示例说明
举例说明,下表说明了linux中虚拟服务器IP隧道所指定的路由。注意,运行在实服务器上的服务必须使用与虚服务相同的端口,所以没有必要在实服务器上指定服务端口。
Protocol | Virtual IP Address | Port | Real IP Address | Weight |
TCP | 202.103.106.5 | 80 | 202.103.107.2 | 1 |
202.103.106.3 | 2 |
所有访问地址202.103.106.5的80端口的数据包都被负载均衡到地址202.103.107.2的80 端口和202.103.106.3的80端口。
我们可以用下列命令指定上表中显示的路由。
1. 对于内核2.0.x
ippfvsadm -A -t 202.103.106.5:80 -R 202.103.107.2 -w 1 ippfvsadm -A -t 202.103.106.5:80 -R 202.103.106.3 -w 2
2. 对于内核2.2.x /2.4.x /2.6.x
ipvsadm -A -t 202.103.106.5:80 -s wlc ipvsadm -a -t 202.103.106.5:80 -r 202.103.107.2 -i -w 1 ipvsadm -a -t 202.103.106.5:80 -r 202.103.106.3 -i -w 2
测试虚拟服务器示例
这是一个配置示例,用于测试IP隧道的虚拟服务器。下面所示的配置,希望能给读者以启示。负载均衡器地址为172.26.20.111,实服务器地址为172.26.20.112,虚地址为172.26.20.110。在下面的示例中,telnet 172.26.20.110实际访问的将是实服务器。
1. 对于内核2.0.x
负载均衡器,内核版本2.0.36
ifconfig eth0 172.26.20.111 netmask 255.255.255.0 broadcast 172.26.20.255 up route add -net 172.26.20.0 netmask 255.255.255.0 dev eth0 ifconfig eth0:0 172.26.20.110 netmask 255.255.255.255 broadcast 172.26.20.110 up route add -host 172.26.20.110 dev eth0:0 ippfvsadm -A -t 172.26.20.110:23 -R 172.26.20.112
实服务器1,内核2.0.36 (IP forwarding enabled)
ifconfig eth0 172.26.20.112 netmask 255.255.255.0 broadcast 172.26.20.255 up route add -net 172.26.20.0 netmask 255.255.255.0 dev eth0 ifconfig tunl0 172.26.20.110 netmask 255.255.255.255 broadcast 172.26.20.110 up route add -host 172.26.20.110 dev tunl0
当telnet 172.26.20.110时,实际访问的是实服务器1。
2. 对于内核2.2.x
负载均衡器,内核2.2.14
ifconfig eth0 172.26.20.111 netmask 255.255.255.0 broadcast 172.26.20.255 up ifconfig eth0:0 172.26.20.110 netmask 255.255.255.255 broadcast 172.26.20.110 up echo 1 > /proc/sys/net/ipv4/ip_forward ipvsadm -A -t 172.26.20.110:23 -s wlc ipvsadm -a -t 172.26.20.110:23 -r 172.26.20.112 -i
实服务器1, 内核版本2.0.36 (IP forwarding enabled)
ifconfig eth0 172.26.20.112 netmask 255.255.255.0 broadcast 172.26.20.255 up route add -net 172.26.20.0 netmask 255.255.255.0 dev eth0 ifconfig tunl0 172.26.20.110 netmask 255.255.255.255 broadcast 172.26.20.110 up route add -host 172.26.20.110 dev tunl0
更多的配置示例
以下是更多的关于IP隧道虚拟服务器的配置,为了节省空间,仅列举出主要的命令,其他的省略。
1. Real server running kernel 2.2.14 or later with hidden device
负载均衡器,内核版本2.2.14
echo 1 > /proc/sys/net/ipv4/ip_forward ipvsadm -A -t 172.26.20.110:23 -s wlc ipvsadm -a -t 172.26.20.110:23 -r 172.26.20.112 -i
实服务器1, 内核版本2.2.14
echo 1 > /proc/sys/net/ipv4/ip_forward # insert it if it is compiled as module modprobe ipip ifconfig tunl0 0.0.0.0 up echo 1 > /proc/sys/net/ipv4/conf/all/hidden echo 1 > /proc/sys/net/ipv4/conf/tunl0/hidden ifconfig tunl0 172.26.20.110 netmask 255.255.255.255 broadcast 172.26.20.110 up
由于内核2.2版本仅含有一个隧道设备tunl0,所以在配置中只能有一个VIP。如果想要多个VIPs,需要将tunl0设备启用,并且配置别名tunnel/dummy/loopback设备并且隐藏该设备,示例如下:
echo 1 > /proc/sys/net/ipv4/ip_forward # insert it if it is compiled as module modprobe ipip ifconfig tunl0 0.0.0.0 up ifconfig dummy0 0.0.0.0 up echo 1 > /proc/sys/net/ipv4/conf/all/hidden echo 1 > /proc/sys/net/ipv4/conf/dummy0/hidden ifconfig dummy0:0 172.26.20.110 up route add -host 172.26.20.110 dev dummy0:0 ifconfig dummy0:1 <Another-VIP> up ...
2. 具备重定向方法的实服务器(内核2.2.x)
负载均衡器的配置同上,实服务器(内核2.2.x)的配置如下:
echo 1 > /proc/sys/net/ipv4/ip_forward # insert it if it is compiled as module modprobe ipip ifconfig tunl0 0.0.0.0 up ipchains -A input -j REDIRECT 23 -d 172.26.20.110 23 -p tcp ...