Load balancing of LVS-DR mode in four layers

Posted by expertis on Mon, 04 Nov 2019 18:34:19 +0100

Two methods of LVS-DR construction

matters needing attention:
        At this time, there is only one network card for LVS service;  
        The IP forward forwarding function of the route must be enabled; 
        For client access, the gateway must be specified;
        The VS service must be configured with a gateway. If the gateway is not configured, the client will not be able to access it;
        LVS and RS services must maintain the same network segment, and no router can be added in the middle.  

Method 1: VIP is not in the same virtual network segment as RIP and DIP

1. Client client

1) configure gateway   
 [root@centos7 ~]#route add default gw 192.168.10.49 dev ens37
2)Check whether the gateway is configured
 [root@centos7 ~]#route -n 
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 0.0.0.0             192.168.10.49   0.0.0.0         UG    0      0        0 ens37
192.168.10.0    0.0.0.0         255.255.255.0   U     100    0        0 ens37
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

2. Route router

  1)Prepare two network cards and set them up IP
       ens33  192.168.10.49/24 
       ens37  192.168.1.106/24 
  2)NIC ens33 Bind one more IP Access to the public network VIP
      [root@centos7 ~]# ifconfig ens33:1 10.10.0.20   

        link/ether 00:0c:29:ed:81:c3 brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.106/24 brd 192.168.1.255 scope global noprefixroute ens33
        valid_lft forever preferred_lft forever
        inet 10.10.0.20/8 brd 10.255.255.255 scope global ens33:1
  3)Routing enabled ip_forward Forwarding function. If this item is not enabled, the client will not be able to access it. The parameter is set to 1
         [root@centos7 ~]#echo 1 > /proc/sys/net/ipv4/ip_forward

3. RS1 server

     1)Install test server httpd,Default 80 port
            [root@localhost ~]# yum install httpd -y
            [root@localhost ~]# echo www.google.com > /var/www/html/index.html  
            [root@localhost ~]# systemctl start httpd
     2)Be prepared for a meeting with LVS LAN card of the same physical network segment ens33,No other network segment network card is allowed to avoid gateway conflict
             ens33 192.168.1.110
     3)Setting up gateway

             [root@localhost ~]# route add default gw 192.168.1.106
     4)Edit script auto bind VIP Public network, bound to loopback network card

             [root@localhost ~]# vim /data/shell.sh/lvs_dr_rs.sh 

   5) execute script to complete VIP binding
        ~]# bash /data/shell.sh/lvs_dr_rs.sh start 

4. RS2 server

    1)Install test server httpd,Default 80 port
                [root@localhost ~]# yum install httpd -y
                [root@localhost ~]# echo www.china.gov.com > /var/www/html/index.html  
                [root@localhost ~]# systemctl start httpd
    2)Be prepared for a meeting with LVS LAN card of the same physical network segment ens33,No other network segment network card is allowed to avoid gateway conflict
                 ens33 192.168.1.101
    3)Setting up gateway

                 [root@localhost ~]# route add default gw 192.168.1.106
    4)Edit script auto bind VIP The public network is bound to the loopback network card. The content of the editing script is the same as the third step, which is copied and executed directly

                 [root@localhost ~]#  bash /data/shell.sh/lvs_dr_rs.sh start

5. LVS scheduling server

   1)Prepare a network card and set it up IP
       ens33  192.168.1.107/24
   2)Point to the R2 mouth
       [root@centos7 ~]#route add default gw 192.168.1.106
   3)Script to create cluster scheduling environment
       [root@centos7 ~]#vim /data/shell.sh/lvs_dr_vs.sh    

          #!/bin/bash
          vip='10.10.0.10'      #Public network VIP
          iface='lo:1'            #Bind value local loopback network card
          mask='255.255.255.255'        #Independent network
          port='80'        
          rs1='192.168.1.101'         #rs1 services
          rs2='192.168.1.110'        #rs2 services
          scheduler='wrr'           #Weight polling algorithm
          type='-g'  #Working mode is DR mode
          rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null   #Determine whether to install ipvsadm
          case $1 in
          start)
          ifconfig $iface $vip netmask $mask #broadcast $vip up
          iptables -F
          ipvsadm -A -t ${vip}:${port} -s $scheduler        #Create a scheduling cluster environment
          ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1  #Add rs1 service to the scheduling cluster environment
          ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1  #Add rs2 service to the scheduling cluster environment
          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

     4)Script execution complete lvs Environment construction of
                 [root@centos7 ~]# bash /data/shell.sh/lvs_dr_vs.sh start

6. Carry out the access test on the Client, and set the schedule as weighted polling access

                 [root@centos7 ~]# while true;do curl 10.10.0.10;sleep 1;done       

Method 2: VIP, RIP and DIP are in the same network segment

1.Client

 1)Configure the default gateway to route Route Of R1 mouth IP
      [root@centos7 ~]#route del default gw 192.168.10.1 dev ens37

2. Route

 1)Turn on forwarding function of route ip_forward,Parameter set to 1
           [root@centos7 ~]#echo 1 > /proc/sys/net/ipv4/ip_forward

3.RS1 server

  1)take VIP Bind to loopback network card
       [root@localhost ~]# ifconfig lo:1 192.168.1.119/32
  2)Specify default gateway to route Route Of R2 mouth IP 
       [root@localhost ~]# route add default gw 192.168.1.108 dev ens33   
  3)open httpd Test service
         [root@localhost ~]# systemctl start httpd

4.RS2 server

  1)take VIP Bind to loopback network card
       [root@localhost ~]# ifconfig lo:1 192.168.1.119/32
  2)Specify default gateway to route Route Of R2 mouth IP 
       [root@localhost ~]# route add default gw 192.168.1.108 dev ens33
  3)open httpd Test service
         [root@localhost ~]# systemctl start httpd

5.LVS dispatching service

  1)take VIP Bind to loopback network card
       [root@localhost ~]# ifconfig lo:1 192.168.1.119/32
  2)Specify default gateway to route Route Of R2 mouth IP 
       [root@localhost ~]# route add default gw 192.168.1.108 dev ens33
  3)Set up a cluster scheduling environment with the algorithm set as the weight WLC
       [root@centos7 ~]#ipvsadm -A -t 192.168.1.119:80 -s wlc 
  4)take RS1 And RS2 Add to cluster environment and configure weights
       [root@centos7 ~]#ipvsadm -a -t 192.168.1.119:80 -r 192.168.1.110 -w 3
       [root@centos7 ~]#ipvsadm -a -t 192.168.1.119:80 -r 192.168.1.101 -w 1

6. Access test on the client

        [root@centos7 ~]#while true;do curl 192.168.1.119;sleep 1;done

Topics: Linux network shell yum vim