Recovery of linux: keepalived+lvs to achieve high available load balancing

Keywords: network vim yum Mac

The complete architecture requires two servers (the role is dir) to install the Keepalived tool respectively, in order to achieve high availability, but Keepalived also has load balancing function, so only one Keepalived can be installed in this use.

Keeping alive has the function of ipvsadm built in, so you don't need to install ipvsadm package, and you don't need to write and execute LVS? Dr script.

Three machines:

There are no two keepalived for high availability. lvs and keepalived can have lvs function built in:
IP: 192.168.8.133; installed

Machine 1:
IP: 192.168.8.134

Machine 2:
IP: 192.168.8.135

VIP: 192.168.8.100

Start building profile

yum install keepalived

//Custom Keepalived profile:
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    #BACKUP on standby server
    state MASTER
    #The network card bound to vip is ens33. Your network card may be different from that of Amin. You need to change it here
    interface ens33
    virtual_router_id 51
    #90 on standby server
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.8.100
    }
}
virtual_server 192.168.8.100 80 {
    #(query realserver status every 10 seconds)
    delay_loop 10
    #(lvs algorithm) 
    lb_algo wlc 
    #Algorithm (DR mode)
    lb_kind DR
    #(the connection of the same IP is allocated to the same RealServer within 60 seconds)
    persistence_timeout 0 
    #(check the state of realserver with TCP protocol)
    protocol TCP 
    real_server 192.168.8.134 80 {
        #(weight) 
        weight 100
        TCP_CHECK {
        #(10 seconds no response timeout)
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }   
    real_server 192.168.8.135 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }  
}    

//To start the preserved service:
[root@director ~]# systemctl start keepalived

//To view network card information:
ip add
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:be:0e:17 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.133/24 brd 192.168.8.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.8.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::592f:39cc:1b50:1d07/64 scope link 
       valid_lft forever preferred_lft forever
#Virtual IP (VIP) on the ens33 network card

//To view ipvsadm rules:
[root@director ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.8.100:80 wlc
  -> 192.168.8.134:80             Route   100    0          0         
  -> 192.168.8.135:80             Route   100    0          0         

Execute this script on two machines 134 and 135

To configure a routing and forwarding script:
vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.8.100
#The purpose of binding vip to lo is to realize rs to directly return the result to the client
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#The following operation is to change the arp kernel parameters so that rs can send the mac address to the client smoothly
#Reference document: www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
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

After configuration, visit VIP: 192.168.8.100 in the browser, refresh the webpage, and the visit result will be replied alternately by real1 and real2

Preserved + LVS effect

  • Keepalived builds high availability to ensure that the server will not be paralyzed after machine failure in LVS
  • If only LVS is used, then when a machine in LVS architecture continues to send requests to it, adding Keepalived will automatically clear the down machine out of rs list.

Posted by Squiggles on Tue, 14 Apr 2020 09:36:45 -0700