Building of load balancing in LVS DR mode (single network segment)

Keywords: Linux vim network RPM yum

The experiment needs five virtual machines, all of which are centos7

Client Router lvs server
172.22.144.188 1 interface 172.22.144.17 2 interface 192.168.49.5 172.22.144.17 192.168.49.5
rs1 server rs2 server
192.168.49.3 192.168.49.4
**Client configuration**
vim /etc/sysconfig/nerwork-scripts/ifcfg-ens33
DEVICE=ens33
NAME=ens33
BOOTPROTO=static
IPADDR=172.22.144.188
PREFIX=16
ONBOOT=yes
GATEWAY=172.22.144.17
//Because the client IP and VIP are not in the same network segment, the gateway needs to point to the router
Router configuration
vim /etc/sysconfig/nerwork-scripts/ifcfg-ens33
DEVICE=ens33
NAME=ens33
BOOTPROTO=static
IPADDR=172.22.144.17
ONBOOT=yes
PREFIX=16
 Router configuration 1 interface network card to connect to the external network

vim /etc/sysconfig/nerwork-scripts/ifcfg-ens37
DEVICE=ens37
IPADDR=192.168.49.5
PREFIX=24
 Router is configured with 2 interface network card to connect the Intranet

vim /sysctl.conf
net.ipv4.ip_forward=1
 Add this line to the routing profile

sysctl -p
 Start routing forwarding service
lvs Configuration
vim /etc/sysconfig/network-scripts/ifcfg-ens33
DEVICE=ens33
NAME=ens33
BOOTPROTO=static
IPADDR=192.168.49.2
PREFIX=24
ONBOOT=yes
GATEWAY=192.168.49.5
//Point lvs ip gateway to router 2 interface
rs Server configuration
DEVICE=ens33
NAME=ens33
BOOTPROTO=static
IPADDR=192.168.49.3
PREFIX=24
ONBOOT=yes
GATEWAY=192.168.49.5
//Point all the gateways of rs service 1 and 2 to router 2 interface
vs One click installation script
vim lvs_dr_vs.sh 
#!/bin/bash
#Author:wangxiaochun
#Date:2017-08-13
vip='192.168.49.100'
iface='lo:1'
mask='255.255.255.255'
port='80'
rs1='192.168.49.3'
rs2='192.168.49.4'
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 ${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
rs1 Server one click installation script
#!/bin/bash
#Author:wangxiaochun
#Date:2017-08-13
vip=192.168.49.100
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>rs1</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
rs2 Server one click installation script

#!/bin/bash
#Author:wangxiaochun
#Date:2017-08-13
vip=192.168.49.100
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>rs2</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
bash lvs_dr_rs.sh start/stop
 This is the start and stop command of these three scripts!
Start all scripts and test on the client!

while true;do curl 192.168.49.100;sleep 0.5;done
111
lvyao
111
lvyao
111
lvyao
111
lvyao
111
lvyao
.....
DR The construction of load balancing mode (single network segment) has been completed!!!!!!!!!!!

Posted by isurgeon on Tue, 05 Nov 2019 06:51:20 -0800