lvs dr模式(rip 、dip 不在同一个网段上)
如图所示:
-
一共3个网段
- 客户端为一个网段:桥接网卡eth0
- 路由器为2个网卡, 桥接网卡eth1、 NAT网卡 eth0
- vs和rs 只有一个NAT 网卡eth0
-
原理:
相比较vip 和dip 都在一个网段而言, 理论原理仍然没有改变。只是router到 lvs 的路由方式发生了改变。在一个网段的话,route 与lvs的服务器组中通常有一个相同的的网关地址,如图中的192.168.7.254 ,而不在一个网段的话,只不过是通过添加静态路由的方式来实现而已,因为他们都连接在一个交换机(vmnet0)上面。
注意: 两个主机之间能不能通信,决定因素是路由,而不是是否在一个网段中。
route上的操作
root@route:~# route add default gw 10.0.0.100 eth0
root@route:~# route add -host 10.0.0.100 eth0
root@route:~# cat /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth1:
dhcp4: no
addresses: [172.16.2.2/20]
gateway4: 172.16.0.1
nameservers:
addresses: [223.6.6.6]
eth0:
dhcp4: no
addresses: [10.0.0.200/8]
gateway4: 0.0.0.0
nameservers:
addresses: [223.6.6.6]
vs 上的操作
网关指向路由器
root@director:~# cat /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses: [192.168.1.103/21]
gateway4: 192.168.7.254
nameservers:
addresses: [223.6.6.6]
root@director:~# route add -host 10.0.0.200 eth0
root@director:~# route add default gw 10.0.0.200 eth0
lvs_dr_vs.sh
#!/bin/bash
vip='10.0.0.100'
iface='lo:1'
mask='255.255.255.255'
port='80'
rs1='192.168.1.104'
rs2='192.168.1.105'
scheduler='wrr'
type='-g'
dpkg-query -l ipvsadm &>/dev/null ||apt-get install ipvsadm &>/dev/null
case $1 in
start)
ifconfig $iface $vip netmask $mask #broadcast $vip up
iptables -F
ipvsadm -A -t ${vip}:${port} -s $scheduler
ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1
ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1
echo "The VS Server is Ready!"
;;
stop)
ipvsadm -C
ifconfig $iface down
echo "The VS Server is Canceled!"
;;
*)
echo "Usage: $(basename $0) start|stop"
exit 1
;;
esac
~
~
root@director:~# bash lvs_dr_vs.sh start
rs 上的操作
root@rs2:~# cat /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses: [192.168.1.105/21]
gateway4: 10.0.0.200
nameservers:
addresses: [223.6.6.6]
root@rs2:~# route add -host 10.0.0.200 eth0
root@rs2:~# route add default gw 10.0.0.200 eth0
root@rs2:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.200 0.0.0.0 UG 0 0 0 eth0
10.0.0.100 0.0.0.0 255.255.255.255 UH 0 0 0 lo
10.0.0.200 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 0 0 0 eth0
lvs_dr_rs.sh
#!/bin/sh
#Zhang Shijie:2017-08-18
LVS_VIP=10.0.0.100
case "$1" in
start)
/sbin/ifconfig lo:0 $LVS_VIP netmask 255.255.255.255 broadcast $LVS_VIP
/sbin/route add -host $LVS_VIP dev lo:0
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
sysctl -p >/dev/null 2>&1
dpkg-query -l apache2 &> /dev/null|| apt-get -y install apache2 >/dev/null && echo "RealServer Start OK"
echo "<h1>`hostname`</h1>" > /var/www/html/index.html
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del $LVS_VIP >/dev/null 2>&1
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
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
root@rs2:~# bash lvs_dr_rs.sh start
实验中遇到的坑
关于vip的绑定
- 通常把VIP 绑定在lo 网卡上面,这是因为lo网卡 ,不会因为物理原因如网线的丢失导致网络配置信息的丢失;而且如果绑在另外一张eth0 上,在rs上会影响正常的通信,因为有arp的设置,但vs 上不涉及此点。
关于rs 上vip 的netmask
例: 此例中 rs 上的vip netmask 设置为255.255.255.0 时,当你运行rs 脚本之后,ping 10.0.0.22 或者其他的10.0.0.0/24 段内的地址都可以ping通 虽然实际并不存在,效果如同ping 127.0.0.1 一样。若设为255.255.255.255时,则不存在这个现象。抓包发现,并没有收到相关的数据。(当然,你设置静态路由后就可以抓到了)。
因为以前网络学的差劲,还不知道是啥原因。如果有知道的老铁请和我说一下
虽然掩码的设置不影响最终结果,但是影响测试。因为当你测试rs 与route 之间是否可以ping 通时,很可能得到的结果是假的。