1. Implementation of VIP and DIP in the same network segment
Network topology
Configuration environment
Configure RIP1 of Client gateway pointing to route
[root@client ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.22.27.20 0.0.0.0 UG 100 0 0 ens33 172.22.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens33
Enable inter route forwarding on route
[root@route ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
VS operation
Point the gateway to RIP2 of route on the director
[root@director ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.153.10 0.0.0.0 UG 100 0 0 ens33 192.168.153.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
Execute script configuration VS
[root@director ~]# bash lvs_dr_vs.sh start The VS Server is Ready!
Script content
#!/bin/bash vip='192.168.153.50' iface='lo:1' mask='255.255.255.255' port='80' rs1='192.168.153.30' rs2='192.168.153.40' scheduler='wrr' type='-g' rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null case $1 in start) ifconfig $iface $vip netmask $mask #broadcast $vip up iptables -F ipvsadm -A -t $:$ -s $scheduler ipvsadm -a -t $:$ -r $ $type -w 1 ipvsadm -a -t $:$ -r $ $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
RS operation
Point the gateway to RIP2 of route on two RS
[root@rs1 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.153.10 0.0.0.0 UG 100 0 0 ens33 192.168.153.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
Execute lvs configuration script on RS side
[root@rs1 ~]# bash lvs_dr_rs.sh start The httpd Server is Ready! The RS Server is Ready!
Script content
#!/bin/bash vip=192.168.153.50 mask='255.255.255.255' dev=lo:1 rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null service httpd start &> /dev/null && echo "The httpd Server is Ready!" echo "<h1>`hostname`</h1>" > /var/www/html/index.html 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 echo "The RS Server is Ready!" ;; 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 "The RS Server is Canceled!" ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac
test
[root@client ~]# curl 192.168.153.50 <h1>rs2</h1> [root@client ~]# curl 192.168.153.50 <h1>rs1</h1>
2.VIP and DIP are not in the same network segment
Network topology
Configuration environment
Configure RIP1 of Client gateway pointing to route
[root@client ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.22.27.20 0.0.0.0 UG 100 0 0 ens33 172.22.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens33
Enable inter route forwarding on route
[root@route ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
Bind an IP on the route to receive the corresponding RS message
[root@route ~]# ip a a 192.168.100.100/24 dev ens37
VS terminal operation
Point the gateway to RIP2 of route on the director
[root@director ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.153.10 0.0.0.0 UG 100 0 0 ens33 192.168.153.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
Execute script configuration VS
[root@director ~]# bash lvs_dr_vs.sh start The VS Server is Ready!
Script content
#!/bin/bash vip='192.168.100.50' iface='lo:1' mask='255.255.255.255' port='80' rs1='192.168.153.30' rs2='192.168.153.40' scheduler='wrr' type='-g' rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null case $1 in start) ifconfig $iface $vip netmask $mask #broadcast $vip up iptables -F ipvsadm -A -t $:$ -s $scheduler ipvsadm -a -t $:$ -r $ $type -w 1 ipvsadm -a -t $:$ -r $ $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
RS operation
Point the gateway to RIP2 of route on two RS
[root@rs1 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.153.10 0.0.0.0 UG 100 0 0 ens33 192.168.153.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
Execute lvs configuration script on RS side
[root@rs1 ~]# bash lvs_dr_rs.sh start The httpd Server is Ready! The RS Server is Ready!
Script content
#!/bin/bash vip=192.168.100.50 mask='255.255.255.255' dev=lo:1 rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null service httpd start &> /dev/null && echo "The httpd Server is Ready!" echo "<h1>`hostname`</h1>" > /var/www/html/index.html 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 echo "The RS Server is Ready!" ;; 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 "The RS Server is Canceled!" ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac
test
[root@client ~]# curl 192.168.100.50 <h1>rs2</h1> [root@client ~]# curl 192.168.100.50 <h1>rs1</h1>
3.VIP and DIP are not in the same network segment 2 (route uses an ip address)
Network topology
Configuration environment
Configure RIP1 of Client gateway pointing to route
[root@client ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.22.27.20 0.0.0.0 UG 100 0 0 ens33 172.22.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens33
Enable inter route forwarding on route
[root@route ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
Configure RIP2 on route
[root@route ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens37 IPADDR=192.168.100.100 PREFIX=24
RS configuration
Execute RS configuration script
[root@rs1 ~]# bash lvs_dr_rs.sh start The httpd Server is Ready! The RS Server is Ready!
Script content
#!/bin/bash vip=192.168.100.50 mask='255.255.255.0' dev=ens33:1 rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null service httpd start &> /dev/null && echo "The httpd Server is Ready!" echo "<h1>`hostname`</h1>" > /var/www/html/index.html 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 echo "The RS Server is Ready!" ;; 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 "The RS Server is Canceled!" ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac
Refer to the gateway of VS as RIP2
Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.100.100 0.0.0.0 UG 0 0 0 ens33 192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 192.168.153.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
VS configuration
Configure address for director
[root@director ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 IPADDR=192.168.153.20 PREFIX=24
Execute vs configuration script
[root@director ~]# bash lvs_dr_vs.sh start The VS Server is Ready!
Script content
#!/bin/bash vip='192.168.100.50' iface='ens33:1' mask='255.255.255.255' port='80' rs1='192.168.153.30' rs2='192.168.153.40' scheduler='wrr' type='-g' rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null case $1 in start) ifconfig $iface $vip netmask $mask #broadcast $vip up iptables -F ipvsadm -A -t $:$ -s $scheduler ipvsadm -a -t $:$ -r $ $type -w 1 ipvsadm -a -t $:$ -r $ $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
test
[root@client ~]# curl 192.168.100.50 <h1>rs2</h1> [root@client ~]# curl 192.168.100.50 <h1>rs1</h1> [root@client ~]# curl 192.168.100.50 <h1>rs2</h1> [root@client ~]# curl 192.168.100.50 <h1>rs1</h1>