This paper is participating in the30 years of Linux”Topic essay activity

Hello everyone, I am Glacier ~~

Today, we are going to talk about the LVS DR mode in Linux and how to achieve load balancing using the LVS network topology.

The LVS DR model

Building environment (Centon 6.5)

  • The LVS – server IP: 192.168.0.145
  • Real-server1 IP: 192.168.0.146 Install the Web server. Prepare to test the site content
  • Real-server2 IP: 192.168.0.147 Install the Web server. Prepare to test the site content
  • Test machine: XP/Windows 7/8 can be, as long as you can ping 192.168.0.*

LVS server deployment

(1) the LVS – server configuration

A. VIP Settings:

[root@binghe ~]# ifconfig eth0:0 192.168.0.100 netmask 255.255.255.255 
[root@binghe ~]# route add-host 192.168.0.100 dev eth0:0
Copy the code

B. install Ipvsadm

[root@binghe ~]# yum -y install ipvsadm* 
Copy the code

C.l vs configuration:

[root@binghe ~]# ipvsadm -C
[root@binghe ~]# ipvsadm -At 192.168.0.100:80 -s rr
[root@binghe ~]# ipvsadm -at 192.168.0.100:80 -r 192.168.0.146:80 -g
[root@binghe ~]# ipvsadm -at 192.168.0.100:80 -r 192.168.0.147:80 -g
[root@binghe ~]# ipvsadm -L -n
Copy the code

(2) Real – Server1 configuration

[root@binghe ~]# ifconfig lo:0 192.168.0.100 netmask 255.255.255.255
[root@binghe ~]# route add-host 192.168.0.100 dev lo:0
[root@binghe ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore 
[root@binghe ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce 
[root@binghe ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore 
[root@binghe ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
Copy the code

(3) Real – Server2 configuration

Same as real-server1.

test

Now through the test machine, direct access to LVS VIP http://192.168.0.100 normal access to the prepared test site. LVS are configured in round robin mode. First access to real-server1, then access real-server2 after refreshing, and then access real-server1 after refreshing

The network topology implements load balancing

The experiment plan

Three CentOS VMS

  • VIP: 192.168.0.145
  • Sip: 192.168.0.146 192.168.0.147

1. The VIP program

192.168.0.145 is used as the LVS Server(LVS Server). Add another virtual NIC whose IP address is 192.168.106.130 and the two IP addresses are the IP addresses of two different network segments. 192.168.106.130 is the external network. That is, the IP address that can communicate with the real PC. 192.168.0.145 serves as the gateway of the other two SIP VMS.

Note: 192.168.0.145 is bridged connection, gateway is 192.168.0.1(the same as real PC), 192.168.106.130 is NAT connection;

The IP address is as follows:

[root@binghe145 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet UUID=4f9aa259-ab85-4f26-9b54-adbd5b9d3af3 ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=192.168.0.145 NETMASK=255.255.255.0 BROADCAST=192.168.0.255 GATEWAY=192.168.0.1 DNS1=114.114.114 DNS2=8.8.8.8  DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0"
PREFIX=24
HWADDR=00:50:56:3A:D4:B5
LAST_CONNECT=1471798199
[root@binghe145 ~]#
Copy the code

2. The sip planning

192.168.0.146 and 192.168.0.147 Set the gateway IP address to 192.168.0.145 as follows:

192.168.0.146 IP address:

[root@binghe146 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet UUID=4f9aa259-ab85-4f26-9b54-adbd5b9d3af3 ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=192.168.0.146 NETMASK=255.255.255.0 BROADCAST=192.168.0.255 GATEWAY=192.168.0.145 DNS1=114.114.114 DNS2=8.8.8.8 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0"
PREFIX=24
HWADDR=00:50:56:2B:B0:B8
LAST_CONNECT=1471798199
[root@binghe146 ~]#
Copy the code

192.168.0.147 IP information

[root@binghe147 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet UUID=4f9aa259-ab85-4f26-9b54-adbd5b9d3af3 ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=192.168.0.147 NETMASK=255.255.255.0 BROADCAST=192.168.0.255 GATEWAY=192.168.0.145 DNS1=114.114.114 DNS2=8.8.8.8 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0"
PREFIX=24
HWADDR=00:50:56:26:00:3D
LAST_CONNECT=1471798199
[root@binghe147 ~]#
Copy the code

The specific Settings

1. The VIP set up

1) install ipvsadm

To install ipvsadm on the VIP VM (192.168.0.145), run the following command:

yum -y install ipvsadm*
Copy the code

2) Configure the forwarding script

To forward load balancing from PCS accessing VIP 192.168.106.130 to SIP (192.168.0.146 and 192.168.0.147), the ipvsadm command is required to set the forwarding rules, so we create script files on the VIP VM: LVS. Sh, as follows:

#! /bin/bash
#lvs.shIpvsadm -c ipvsadm -At 192.168.106.130:80 -s rr ipvsadm -At 192.168.106.130:80 -r 192.168.0.146:80 -m ipvsadm -At 192.168.106.130:80 -r 192.168.0.147:80 -m ipvsadm -l -nCopy the code

To ensure that the lvs.sh script file can be properly executed, you need to set corresponding permissions and run the following command to make it an executable file

chmod a+x lvs.sh
Copy the code

Run the following command on the CLI to start the script

./lvs.sh
Copy the code

The following command output is displayed:

[root@binghe145 ~]# ./lvs.sh IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.106.130:80 RR -> 192.168.0.146:80 Masq 10 0 -> 192.168.0.147:80 Masq 10 0 [root@binghe145 ~]# 
Copy the code

The script is successfully executed

3) Set the VIP VM to the routing mode

CentOS is in non-routing mode by default. To change it to the routing mode, you need to modify the /proc/sys/net/ipv4/ip-forward file. The default value of the file is 0, that is, the non-routing mode.

vim /etc/sysctl.conf
Copy the code

the

 net.ipv4.ip_forward = 0
Copy the code

to

 net.ipv4.ip_forward = 1
Copy the code

If this option is not present in the file, add it. Then execute the command:

sysctl -p
Copy the code

Make it effective, as shown below.

[root@binghe145 ~]# sysctl -pnet.ipv4.ip_forward = 1 net.ipv4.conf.default.rp_filter = 2 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq =  0 kernel.core_uses_pid = 1 net.ipv4.tcp_syncookies = 1 kernel.msgmnb = 65536 kernel.msgmax = 65536 kernel.shmmax = 68719476736 kernel.shmall = 4294967296 vm.min_free_kbytes = 65536 kernel.panic_on_oops = 1 kernel.panic = 60Copy the code

Check it out again

[root@binghe145 ~]# cat /proc/sys/net/ipv4/ip_forward 
 1 
Copy the code

The original 0 becomes a 1.

2. The sip Settings

We install Nginx on THE SIP virtual machine to deploy static web pages, access the VIP through the real PC, and the VIP is evenly forwarded to SIP, and access the STATIC web pages of SIP to achieve load balancing.

3. The test

With the environment deployed, we typed the link http://192.168.106.130 into the PC browser and it looked like this:

At this point, the LVS Net load balancing environment is set up.

Ok, that’s enough for today. I’m Glacier. See you next time