LVS/DR
LVS-DR實驗模型如下:
在該模型中VIP和DIP、RIP處於同一物理網段內
實驗平臺
1、本次實驗室在VM虛擬機上完成的,使用的系統爲RHEL 5.8,共三臺設備
2、director和real server都使用橋接模式,每臺設備只需一個網卡即可
DR模型中的arp問題
在如上圖的LVS/DR或LVS/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
配置默認網關,網關地址爲前端router接口的地址
# route add default gw 192.168.0.254
###在eth0口上配置VIP###
#ifconfig eth0:0 192.168.0.1 up
配置專用路由,以使得響應報文首先通過VIP所在的lo上的別名接口
#route add -host 192.168.0.1 dev eth0:0
###開啓路由轉發功能###
#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
###配置RIP###
#ifconfig eth0 192.168.0.100/24 up
配置默認網關
# route add default gw 192.168.0.254
###配置VIP###
#ifconfig lo:0 192.168.0.1 netmask 255.255.255.255 broadcast 192.168.0.1 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
###配置RIP###
#ifconfig eth0 192.168.0.200/24 up
配置默認網關
# route add default gw 192.168.0.254
###配置VIP###
#ifconfig lo:0 192.168.0.1 netmask 255.255.255.255 broadcast 192.168.0.1 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
說明:如果VIP和DIP、RIP不再同一個網段內時,需呀注意的事項
1、RIP可以是私網地址,也可以是公網地址
2、director需要2塊網卡,且DIP與RIP進行通信。
3、real server上的網關不能執行DIP,而是需要指向前端路由器接口