I. Environment Description:
LVS-DR-MASTER: 10.3.0.82
LVS-DR-Backup: 10.3.0.70
VIP: 10.3.0.60
Realserver1: 10.3.0.69
Realserver1: 10.3.0.83
Centos: 5.7
Ii. Install the LVS and keepalvied software packages
1. Download related software packages
Mkdir/usr/local/src/LVS
CD/usr/local/src/LVS
Wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
Wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
2. Install LVS and keepalived
Lsmod | grep ip_vs
Uname-R
2.6.18-53. el5pae
Ln-S/usr/src/kernels/2.6.18-53. el5PAE-i686 // usr/src/Linux
Tar zxvf ipvsadm-1.24.tar.gz
CD ipvsadm-1.24
Make & make install
Find/-name ipvsadm # view the location of ipvsadm
Or use Yum install ipvsadm
Tar zxvf keepalived-1.1.15.tar.gz
CD keepalived-1.1.15
./Configure & make install
Find/-name keepalived # view the location of keepalived
CP/usr/local/etc/rc. d/init. d/keepalived/etc/rc. d/init. d/
CP/usr/local/etc/sysconfig/keepalived/etc/sysconfig/
Mkdir/etc/keepalived
CP/usr/local/etc/keepalived. CONF/etc/keepalived/
CP/usr/local/sbin/keepalived/usr/sbin/
Service keepalived START | stop # makes the system startup service easy to manage.
Note: During keepalived installation, the first version is too high. I downloaded version 1.2,
The above is my kernel version. During the installation process, there is always an error and no relevant instructions are found on the Internet. Therefore, the download result of a lower version is OK.
Three: LVS-DR, configure LVS script to achieve Load Balancing
Vim/etc/init. d/lvsdr
#! /Bin/bash
Rip1 = 10.3.0.69
Rip2 = 10.3.0.83
VIP = 10.3.0.60
./Etc/rc. d/init. d/functions
Case "$1" in
Start)
Echo "Start LVS of directorserver"
# Set the virtual IP address and sysctl Parameter
/Sbin/ifconfig eth0: 0 $ VIP broadcast $ VIP netmask 255.255.255.255 up
/Sbin/route add-host $ VIP Dev eth0: 0
Echo "1">/proc/sys/NET/IPv4/ip_forward
# Clear ipvs table
/Sbin/ipvsadm-C
# Set LVS
# Web Apache
/Sbin/ipvsadm-a-t $ VIP: 80-s rr
/Sbin/ipvsadm-a-t $ VIP: 80-r $ rip1: 80-G
/Sbin/ipvsadm-a-t $ VIP: 80-r $ rip2: 80-G
# Run LVS
/Sbin/ipvsadm
;;
Stop)
Echo "Close LVS directorserver"
Echo "0">/proc/sys/NET/IPv4/ip_forward
/Sbin/ipvsadm-C
/Sbin/ifconfig eth0: 0 down
;;
*)
Echo "Usage: $0 {START | stop }"
Exit 1
Esac
Chmod A + x/etc/init. d/lvsdr
Service lvsdr START | stop
View the command:
Ipvsadm-ln
Ipvsadm-lcn view request forwarding status
Note: This script must be executed on LVS-DR-master and backup.
4. Configure the RealServer script
Vim/etc/init. d/RealServer
#! /Bin/bash
VIP = 10.3.0.60
./Etc/rc. d/init. d/functions
Case "$1" in
Start)
Echo "Start LVS of RealServer"
/Sbin/ifconfig lo: 0 $ VIP broadcast $ VIP netmask 255.255.255.255 up
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
;;
Stop)
/Sbin/ifconfig lo: 0 down
Echo "Close LVS directorserver"
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 "Usage: $0 {START | stop }"
Exit 1
Esac
Chmod A + x/etc/init. d/RealServer
Service RealServer START | stop
You can run the ifconfig or IP add command to check whether the IP address is bound.
Note: This script must be executed on both realservers.
5. Use keepalvied to achieve load balancing and high availability
Vim/etc/keepalived. conf
! Configuration file for keepalived
Global_defs {
Notification_email {
[Email protected]
}
Notification_email_from [email protected]
Smtp_server 127.0.0.1
Router_id lvs_devel
}
Vrrp_instance vi_1 {
State master # change master to backup on the backup server
Interface eth0
Virtual_router_id 51
Priority 100 # Change Backup Server 100 to 98
Advert_int 1
Authentication {
Auth_type pass
Auth_pass 1111
}
Virtual_ipaddress {
10.3.0.60
}
}
Virtual_server 10.3.0.60 80 {
Delay_loop 6
Lb_algo rr
Lb_kind Dr
Protocol TCP
Real_server 10.3.0.69 80 {
Weight 3 # (weight)
Tcp_check {
Connect_timeout 10 # (no response timeout in 10 seconds)
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80
}
}
Real_server 10.3.0.83 80 {
Weight 3 # (weight)
Tcp_check {
Connect_timeout 10 # (no response timeout in 10 seconds)
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80
}
}
}
Service keepalived START | stop
Note: the backup server is configured as above. You only need to change the red part.
Vi. Test
1. One of the RealServer services is stopped:
I use two nginx servers.
PS-Aux | grep nginx # Find the nginx main thread PID
Kill-Quit 4195
Observe the Dr situation:
10.3.0.83 the server has been cleared. It indicates OK;
2. Start nginx that just stopped 83
Continue to observe the Dr situation:
83 has been successfully added to the RealServer group.
3: Stop the keepalived of the master to check whether the backup can take over:
Visit 10.3.0.60 to observe the backup Dr status:
It is found that the backup Dr receives the request sent by the client and is successfully forwarded. You can also view the log information.
Tail-F/var/log/message
4: restart keepalived of the master to check whether the client request will be received and forwarded by the master again:
Visit: http: // 10.3.0.60
MASTER:
The request is successfully received and forwarded.
Backup log:
Tail-F/var/log/message
The backup server log indicates that it is in the backup status and has not received any request from the client;
7. The above is a self-operated process. If you have any questions, please correct them. If your machine's firewall is enabled, the test is impossible, unless you set the firewall's filtering rules correctly, because this time we mainly test the Server Load balancer, so I will directly stop all the machine's iptables, service iptables stop;
From: http://www.cnblogs.com/czh-liyu/archive/2011/12/01/2270973.html