Get the App
SLTechnology News&Howtos  ›  Network Security  › 

LVS/DR combined with keepalived

Shulou Source: shulou.com Published: 2022-06-01 01:33:36 10月04日 Update

LVS/DR configuration

Prepare 3 machines:

Dir (192.168.220.141)

Rs1 (192.168. 220.143)

Rs2 (192.168.220.144)

Vip (192.168.220.110)

Vim / usr/local/sbin/lvs_dr.sh / / added on Director

#! / bin/bash

Echo 1 > / proc/sys/net/ipv4/ip_forward

Ipv=/sbin/ipvsadm

Vip=192.168.220.110

Rs1=192.168.220.143

Rs2=192.168.220.144

Ifconfig eth0:0$ vip broadcast $vip netmask 255.255.255.255 up

Route add-host $vip dev eth0:0

$ipv-C

$ipv-A-t $vip:80-s rr

$ipv-a-t $vip:80-r $rs1:80-g-w 1

$ipv-a-t $vip:80-r $rs2:80-g-w 1

On two rs: vim / usr/local/sbin/lvs_dr_rs.sh

#! / bin/bash

Vip=192.168.220.110

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

Then execute on director: bash / usr/local/sbin/lvs_dr.sh

Execute on two rs: bash / usr/local/sbin/lvs_dr_rs.sh

Browser test access under Windows

Combined with keepalived

Prepare another machine as a backup dir (192.168.220.129)

On the main dir

Vim / etc/keepalived/keepalived.conf

Vrrp_instance VI_1 {

State MASTER # BACKUP on the standby server

Interface eth0

Virtual_router_id 51

90 on priority 100 # standby server

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass 1111

}

Virtual_ipaddress {

192.168.220.110

}

}

Virtual_server 192.168.220.110 80 {

Delay_loop 6 # (query realserver status every 10 seconds)

Lb_algo wlc # (lvs algorithm)

Lb_kind DR # (Direct Route)

Persistence_timeout 60 # (connections to the same IP are assigned to the same realserver within 60 seconds)

Protocol TCP # (check realserver status with TCP protocol)

Real_server 192.168.220.143 80 {

Weight 100 # (weight)

TCP_CHECK {

Connect_timeout 10 # (10 seconds no response timeout)

Nb_get_retry 3

Delay_before_retry 3

Connect_port 80

}

}

Real_server 192.168.220.144 80 {

Weight 100

TCP_CHECK {

Connect_timeout 10

Nb_get_retry 3

Delay_before_retry 3

Connect_port 80

}

}

}

Scp / etc/keepalived/keepalived.conf 192.168.220.129://etc/keepalived/keepalived.conf

On standby dir

Vim / etc/keepalived/keepalived.conf

Modify the following:

State MASTER-> state BACKUP

Priority 100-> priority 90

After configuring keepalived, you need to enable port forwarding (both master and slave):

Echo 1 > / proc/sys/net/ipv4/ip_forward

Then, execute the / usr/local/sbin/lvs_dr_rs.sh script on both rs

Finally, start the keepalived service (master then slave) on the two director:

/ etc/init.d/keepalived start

In addition, it is important to note that starting the keepalived service automatically generates vip and ipvsadm rules, eliminating the need to execute the / usr/local/sbin/lvs_dr.sh script mentioned above.

Tags: Backup service two server machine status script preparation configuration master-slave first master weight browser port algorithm automatic generation rules allocation query check Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Xiaomi Shulou Technology Linux OPPO Reno