首页 > 代码库 > linux负载均衡集群之LVS-DR
linux负载均衡集群之LVS-DR
LVS/DR
LVS-DR实验模型如下:
在该模型中director和real server处于同一物理网段内
实验平台
1、本次实验室在VM虚拟机上完成的,使用的系统为RHEL 5.8,共三台设备
2、director和real server都使用桥接模式,每台设备只需一个网卡即可
DR模型中的arp问题
在如上图的VS/DR或VS/TUN应用的一种模型中(所有机器都在同一个物理网络),所有机器(包括Director和RealServer)都使用了一个额外的IP地址,即VIP。当一个客户端向VIP发出一个连接请求时,此请求必须要连接至Director的VIP,而不能是RealServer的。因为,LVS的主要目标就是要Director负责调度这些连接请求至RealServer的。
因此,在Client发出至VIP的连接请求后,只能由Director将其MAC地址响应给客户端(也可能是直接与Director连接的路由设备),而Director则会相应的更新其ipvsadm table以追踪此连接,而后将其转发至后端的RealServer之一。
如果Client在请求建立至VIP的连接时由某RealServer响应了其请求,则Client会在其MAC table中建立起一个VIP至RealServer的对就关系,并以至进行后面的通信。此时,在Client看来只有一个RealServer而无法意识到其它服务器的存在。
为了解决此问题,可以通过在路由器上设置其转发规则来实现。当然,如果没有权限访问路由器并做出相应的设置,则只能通过传统的本地方式来解决此问题了。这些方法包括:
1、禁止RealServer响应对VIP的ARP请求;
2、在RealServer上隐藏VIP,以使得它们无法获知网络上的ARP请求;
3、基于“透明代理(Transparent Proxy)”或者“fwmark (firewall mark)”;
4、禁止ARP请求发往RealServers;
传统认为,解决ARP问题可以基于网络接口,也可以基于主机来实现。Linux采用了基于主机的方式,因为其可以在大多场景中工作良好,但LVS却并不属于这些场景之一,因此,过去实现此功能相当麻烦。现在可以通过设置arp_ignore和arp_announce,这变得相对简单的多了。
Linux 2.2和2.4(2.4.26之前的版本)的内核解决“ARP问题”的方法各不相同,且比较麻烦。幸运的是,2.4.26和2.6的内核中引入了两个新的调整ARP栈的标志(device flags):arp_announce和arp_ignore。基于此,在DR/TUN的环境中,所有IPVS相关的设定均可使用arp_announce=2和arp_ignore=1/2/3来解决“ARP问题”了。
arp_annouce: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.
2 - Always use the best local address for this target.
arp_announce={0|1|2}:定义自己地址向外通告时的通告级别
其中0表示将本地上的任何地址都向外通告
1表示试图将请求的目标地址和该地址所在接口匹配的网络进行通告。假设某个主机上面有2个网卡分别为eth0和eth1,因此当arp_announce=1时,如果请求的是eth0的mac地址,则将eth0网卡所在的mac地址通告给eth0这个接口所在的网络,且不会将eth1这个网卡的mac地址也向eth0所在接口的网络进行通告。
2表示只将请求的目标地址和该地址所在接口匹配的网络进行通告
arp_ignore: Define different modes for sending replies in response to received ARP requests that resolve local target IP address.
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 address configured with scope host, only resolutions for golbal and link addresses are replied.
4-7 - reserved
8 - do not reply for all local addresses
arp_ignore={0|1|2|3|8}:用来定义接收到arp请求时的响应级别,这里这介绍0和1
其中0表示只要本地有相应的地址则给予响应
1表示仅当请求的目标地址和到达时的接口相匹配时,才给予响应
在RealServers上,VIP配置在本地回环接口lo上。如果回应给Client的数据包路由到了eth0接口上,则arp通告应该通过eth0实现,因此,需要在sysctl.conf文件中定义如下配置:
#vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
以上选项需要在启用VIP之前进行,否则,则需要在Director上清空arp表才能正常使用LVS。
实验步骤如下:
1、在director上的配置
###配置DIP###
#ifconfig eth0 192.168.0.10/24 up
###在lo口上配置VIP###
#ifconfig eth0:0 192.168.0.1 up broadcast 192.168.0.1 netmask 255.255.255.255 up
#route add -host 192.168.0.1 dev eth0:0
#route add -host 192.168.0.10 dev eth0
###开启路由转发功能###
#echo 1 > /proc/sys/net/ipv4/ip_forward
2、在real server1上的配置
###设置内核参数arp_ignore和arp_announce###
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
###配置DIP###
#ifconfig eth0 192.168.0.100/24 up
###配置VIP###
#ifconfig lo:0 192.168.0.1 broadcast 192.168.0.1 netmask 255.255.255.255 up
###添加一条主机路由,确保请求的目标ip为VIP,响应报文时的源ip也是VIP
#route add -host 192.168.0.1 dev lo:0
3、在real server2上的配置
###设置内核参数arp_ignore和arp_announce###
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
###配置DIP###
#ifconfig eth0 192.168.0.200/24 up
###配置VIP###
#ifconfig lo:0 192.168.0.1 broadcast 192.168.0.1 netmask 255.255.255.255 up
###添加一条主机路由,确保请求的目标ip为VIP,响应报文时的源ip也是VIP
#route add -host 192.168.0.1 dev lo:0
LVS/DR的服务脚本
1、Director脚本:
#!/bin/bash
#
# LVS script for VS/DR
#
. /etc/rc.d/init.d/functions
#
VIP=192.168.0.1
DIP=192.168.0.10
RIP1=192.168.0.100
RIP2=192.168.0.200
PORT=80
#
case "$1" in
start)
/sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:1
# Since this is the Director we must be able to forward packets
echo 1 > /proc/sys/net/ipv4/ip_forward
# Clear all iptables rules.
/sbin/iptables -F
# Reset iptables counters.
/sbin/iptables -Z
# Clear all ipvsadm rules/services.
/sbin/ipvsadm -C
# Add an IP virtual service for VIP 192.168.0.219 port 80
# In this recipe, we will use the round-robin scheduling method.
# In production, however, you should use a weighted, dynamic scheduling method.
/sbin/ipvsadm -A -t $VIP:80 -s wlc
# Now direct packets for this VIP to
# the real server IP (RIP) inside the cluster
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g -w 1
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g -w 2
/bin/touch /var/lock/subsys/ipvsadm &> /dev/null
;;
stop)
# Stop forwarding packets
echo 0 > /proc/sys/net/ipv4/ip_forward
# Reset ipvsadm
/sbin/ipvsadm -C
# Bring down the VIP interface
/sbin/ifconfig eth0:1 down
/sbin/route del $VIP
/bin/rm -f /var/lock/subsys/ipvsadm
echo "ipvs is stopped..."
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ]; then
echo "ipvsadm is stopped ..."
else
echo "ipvs is running ..."
ipvsadm -L -n
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
;;
esac
2、RealServer脚本:
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
. /etc/rc.d/init.d/functions
VIP=192.168.0.100
host=`/bin/hostname`
case "$1" in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig lo down
/sbin/ifconfig lo up
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
;;
status)
# Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0 | grep $VIP`
isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
if [ ! "$islothere" -o ! "isrothere" ];then
# Either the route or the lo:0 device
# not found.
echo "LVS-DR real server Stopped."
else
echo "LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac
本文出自 “linux学习之路” 博客,请务必保留此出处http://xslwahaha.blog.51cto.com/4738972/1592086
linux负载均衡集群之LVS-DR