LVS + preserved + nginx for high availability

Keywords: Linux Nginx yum SELinux RPM

1. purpose

 lvs is a four-tier load balancing. keepalived provides lvs with highly available services and checks the health status of the backend nginx. Nginx is mainly used for seven tier load balancing

2. topological graph

Server IP address description
 Director master 192.168.3.105 LVS kept alive
 Director standby node 192.168.3.104 LVS kept alive
Real server1:    192.168.3.106:89    nginx
Real server1:    192.168.3.107:89    nginx
 VIP 192.168.3.114 VIP only needs the ip which has not been allocated, and does not need to allocate the disk and cpu

3. software

keepalived-1.4.2.tar.gz
ipvsadm-1.27-7.el7.x86_64.rpm
nginx-1.12.2.tar.gz

4. Installation steps
4.1 change server name

hostnamectl set-hostname lvs01(3.105)
hostnamectl set-hostname lvs02 (3.104)
hostnamectl set-hostname rs01(3.107)
hostnamectl set-hostname rs02 (3.106)

4.2 turn off firewall and selinux

firewall-cmd --state
systemctl stop firewalld.service
systemctl disable firewalld.service
sed -i 's/^ *SELINUX=disabled/SELINUX=disabled/g' /etc/selinux/config
setenforce 0

4.3 IPVS installation (192.168.3.105192.168.3.104)

yum install ipvsadm( yum install --downloadonly ipvsadm(Download the installation package)rpm -ivh ipvsadm*)
ipvsadm
lsmod | grep ip_vs

4.4 configure scripts on two rs (192.168.3.106192.168.3.107)

#! /bin/bash
vip=192.168.3.114
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
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
//Execute bash / usr / local / SBIN / lvs? Dr? Rs.sh on lvs and lvs

4.5keepalived installation

yum -y install openssl openssl-devel  
yum -y install libnl libnl-devel
yum install -y libnfnetlink-devel

./configure --prefix=/usr/local/keepalived
make
make install

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/etc/init.d/keepalived /etc/init.d/

4.6 keepalived configuration

192.168.3.105
vrrp_instance VI_1 {
    state MASTER
interface ens160
virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.3.114
    }
}

virtual_server 192.168.3.114 89 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 0
    protocol TCP

    real_server 192.168.3.106 89 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 89
        }
    }

    real_server 192.168.3.107 89 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 89
        }
    }
}
192.168.3.104
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.3.114
    }
}

virtual_server 192.168.3.114 89 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 0
    protocol TCP

    real_server 192.168.3.106 89 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 89
        }
    }

    real_server 192.168.3.107 89 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 89
        }
    }
}
keepalived The two nodes of the execute the following command to enable the forwarding function
echo 1 > /proc/sys/net/ipv4/ip_forward
//Start keepalive from master to slave
service keepalived start

5. validation
5.1 LVS load balancing

Manually close nginx of 192.168.3.107
 No more 107

Reopen nginx
 107 again

5.2 verify keepalived high availability

At this time, vip192.168.3.114 is on 192.168.3.1105

Simulate downtime, close the keepalived of 192.168.3.105, and 192.168.3.114 drifts to the standby node 192.168.3.104

Still accessible

6. reference
https://www.cnblogs.com/liwei0526vip/p/6370103.html

Posted by natalieG on Thu, 05 Dec 2019 17:41:03 -0800