LVS is short for Linux Virtual Server. It is a Virtual Server cluster system.
In the following case, we use three hosts with IP addresses:
192.168.1.100
192.168.1.101
192.168.1.102
Among them, 192.168.1.100 uses LVS as the server load balancer to distribute requests, and the other two are used as the real service-providing hosts and deployed in DR mode.
First of all, we should deploy Apache or Nginx on 192.168.1.101 and 192.168.1.102, as well as PHP, which is not described here. For details, refer to Nginx + PHP (FastCGI) on this site) install configuration notes (http://www.sunbloger.com/article/242.html ).
To facilitate the test, we place test. php on 192.168.1.101. The code is as follows:
PHP
<? Php
Echo '20140901 ';
?>
And put a file named test. php on 192.168.1.102. The code is as follows:
<? Php
Echo '20140901 ';
?>
Access http: // 192.168.1.101: 80/test. php and http: // 192.168.1.102: 80/test. php through a browser. Normally, 101 and 102 are displayed respectively.
Next, we will add the script for LVS on 192.168.1.101 and 192.168.1.102 respectively. We can add the script to/etc/rc. d/init. d, as a system service started with Linux, the method is as follows:
Vi/etc/rc. d/init. d/lvs-rs // create a script named lvs-rs
Chmod + x/etc/rc. d/init. d/lvs-rs // add execution permission
Chkconfig -- add lvs-rs // add to system service
The lvs-dr script code is as follows:
#! /Bin/bash
# Chkconfig: 35 90 10
# Description: LVS of RealServer
VIP = 192.168.1.100
./Etc/init. d/functions
Case "$1" in
Start)
Ifconfig lo: 0 $ VIP netmask 255.255.255.255
/Sbin/route add-host $ VIP dev 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
Sysctl-p>/dev/null 2> & 1
Echo "LVS of RealServer Start ."
;;
Stop)
Ifconfig lo: 0 down
Route del $ VIP>/dev/null 2> & 1
Echo "0">/proc/sys/net/ipv4/conf/lo/arp_ignore
Echo "0">/proc/sys/net/ipv4/conf/lo/arp_announce
Echo "0">/proc/sys/net/ipv4/conf/all/arp_ignore
Echo "0">/proc/sys/net/ipv4/conf/all/arp_announce
Echo "LVS of RealServer Stoped ."
;;
*)
Echo "Usage: $0 {start | stop }"
Exit 1
Esac
Exit 0
Next, we will start to install LVS for 192.168.1.100.
Yum install ipvsadm
Yum is recommended for CentOS to avoid kernel version impact on LVS.
After ipvsadm is installed, we add a service stop script named lvs-ds for 192.168.1.100. The code is as follows:
#! /Bin/bash
# Chkconfig: 35 90 10
# Description: LVS of DirectorServer
VIP = 192.168.1.100
RIP1 = 192.168.1.101
RIP2 = 192.168.1.102
./Etc/init. d/functions
Case "$1" in
Start)
/Sbin/ipvsadm-A-t $ VIP: 80-s wlc
/Sbin/ipvsadm-a-t $ VIP: 80-r $ RIP1: 80-g-w 1
/Sbin/ipvsadm-a-t $ VIP: 80-r $ RIP2: 80-g-w 1
Touch/var/lock/subsys/ipvsadm>/dev/null 2> & 1
Echo "LVS of DirectorServer start ."
;;
Stop)
/Sbin/ipvsadm-C
Rm-rf/var/lock/subsys/ipvsadm>/dev/null 2> & 1
Echo "LVS of DirectorServer stoped ."
;;
Status)
If [! -E/var/lock/subsys/ipvsadm]; then
Echo "LVS of DirectorServer stoped ."
Exit 1
Else
Echo "LVS of DirectorServer OK ."
Fi
;;
*)
Echo "Usage: $0 {start | stop | status }"
Exit 1
Esac
Exit 0
Add lvs-ds to the system service:
Vi/etc/rc. d/init. d/lvs-ds // create a script named lvs-rs
Chmod + x/etc/rc. d/init. d/lvs-ds // add execution permission
Chkconfig -- add lvs-ds // add to system service
Now, the three hosts are deployed and the Test starts.
Start lvs-rs on 192.168.1.101 and 192.168.1.102 respectively.
Service lvs-rs start
Then start lvs-ds on 192.168.1.100
Service lvs-ds start
Use http: // 192.168.1.100: 80/test. php to test whether LVS works normally. Refresh the page repeatedly and you will see that 101 or 102 is successfully deployed.