lvs-dr实验

lvs-dr_第1张图片

1、机器四台:

LVS:192.168.41.133
后台web服务器:192.168.41.131 192.168.41.132
VIP:192.168.41.188 VIP需要配置在LVS和两台web上
路由器:172.16.41.151 192.168.41.151
测试机:172.16.41.152
网络是两个:web1是172.16.41.0/24 web2是192.168.41.0/24
所有机器安装网络相关工具:yum -y install tcpdump net-tools

2、测试机:172.16.41.152

路由必须指向172.16.41.151 因为此处无法完全模拟真实环境,因此直接设置默认路由,路由如下:

[root@centos610 22:00 ~]# route add default gw 172.16.41.151;route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.16.41.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         172.16.41.151   0.0.0.0         UG    0      0        0 eth0

3、路由器:外部地址:172.16.41.151 内部地址:192.168.41.151

删除默认路由

[root@centos76 09:49 ~]# route del default  gw 172.16.41.2
[root@centos76 09:49 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 ens33
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 ens37
172.16.41.0     0.0.0.0         255.255.255.0   U     0      0        0 ens33
192.168.41.0    0.0.0.0         255.255.255.0   U     0      0        0 ens37
开启路由转发:
[root@centos76 09:51 ~]# echo 1 >  /proc/sys/net/ipv4/ip_forward

4、web服务器:192.168.41.131 192.168.41.132

安装web服务: yum -y install httpd;systemctl start httpd
加入测试的页面:
web1:echo web131 > /var/www/html/index.html
web2:echo web132 > /var/www/html/index.html
下面的部分两台web操作一致:
配置默认路由:route add default gw 192.168.41.151

[root@centos7ys 22:10 ~]# route add default gw 192.168.41.151
[root@centos7ys 22:10 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.41.151  0.0.0.0         UG    0      0        0 ens37
192.168.41.0    0.0.0.0         255.255.255.0   U     101    0        0 ens37

设置vip: 保存此脚本,修改vip的地址为192.168.41.188,最后执行: bash 脚本位置 start

#!/bin/bash
vip=192.168.41.188
mask='255.255.255.255'
dev=lo:1
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig $dev $vip netmask $mask #broadcast $vip up
#route add -host $vip dev $dev
;;
stop)
ifconfig $dev down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;
*)
echo "Usage: $(basename $0) start|stop"
exit 1
;;
esac

5、LVS设置:192.168.41.133

首先LVS安装ipvsadm:
yum -y install ipvsadm
设置默认路由:route add default gw 192.168.41.254

[root@centos7ys 09:45 network-scripts]# route add default gw 192.168.41.151
[root@centos7ys 09:45 network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.41.151  0.0.0.0         UG    0      0        0 ens37
192.168.41.0    0.0.0.0         255.255.255.0   U     100    0        0 ens37

设置vip: 保存此脚本,修改vip的地址为192.168.41.188,修改rs服务器为两个web对应的地址,最后执行: bash 脚本位置 start

#!/bin/bash
vip='192.168.41.188'
iface='lo:1'
mask='255.255.255.255'
port='80'
rs1='192.168.41.131'
rs2='192.168.41.132'
scheduler='wrr'
type='-g'
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
;;
stop)
ipvsadm -C
ifconfig $iface down
;;
*)
echo "Usage $(basename $0) start|stop"
exit 1
esac

6、测试:172.16.41.152

[root@centos610 10:20 ~]# while true;do curl http://192.168.41.188;sleep 1;done
web132
web131
web132
web131