Deploying LVS (DR) + keepalived in RHEL 5.4 to achieve high-performance and high-availability Load Balancing

Source: Internet
Author: User
Tags reflector

Address: http://www.cnblogs.com/mchina/archive/2012/05/23/2514728.html

I. Introduction

LVS is short for Linux virtual server, which is a virtual server cluster system. This project was established by Dr. Zhang Wenyu in May 1998 and is one of the earliest free software projects in China.

Currently, three IP Server Load balancer technologies are available (Vs/NAT, VS/TUN, and VS/DR ); ten scheduling algorithms (RR | WRR | LC | wlc | lblc | lblcr | DH | sh | sed | NQ ).

Keepalived is mainly used for health check of RealServer and Failover implementation between loadbalance host and backup host.

Functions implemented by LVS + keepalived: Use the Master/Slave Mode of The LVS controller to avoid spof and automatically delete the faulty web server node and add it to the cluster after it is restored.

Topology:

Ii. System Environment

System Platform: RHEL 5.4

LVS version: ipvsadm-1.24

Keepalived version: keepalived-1.1.15

Iii. Installation

1. Install LVS and keepalived software (lvs_master & lvs_backup) on two LVS servers)
LVS install -------------

[[Email protected] ~] # Wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

[[Email protected] ~] # Ln-S/usr/src/kernels/2.6.18-194. el5-i686 // usr/src/Linux/

[[Email protected] ~] # Tar zxvf ipvsadm-1.24.tar.gz

[[Email protected] ~] # Cd ipvsadm-1.24

[[Email protected] ipvsadm-1.24] # Make & make install

Keepalived install -------------

[[Email protected] ~] # Wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

[[Email protected] ~] # Tar zxvf keepalived-1.1.15.tar.gz

[[Email protected] ~] # Cd keepalived-1.1.15

[[Email protected] keepalived-1.1.15] #./configure & make install

######## Make keepalived a startup service for convenient management ##########

[[Email protected] ~] # Cp/usr/local/etc/rc. d/init. d/keepalived/etc/init. d/

[[Email protected] ~] # Cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/

[[Email protected] ~] # Mkdir/etc/keepalived/

[[Email protected] ~] # Cp/usr/local/etc/keepalived. CONF/etc/keepalived/

[[Email protected] ~] # Cp/usr/local/sbin/keepalived/usr/sbin/

[[Email protected] ~] # Service keepalived START | stop

2. keepalived Configuration

################### Master ###################

! Configuration file for keepalivedglobal_defs {icationication_email {[email protected] # set alarm email addresses. You can set multiple email addresses and one email address per line. [Email protected] # sendmail service on the local machine must be enabled} icationication_email_from [email protected] # Set the mail sending Address smtp_server 127.0.0.1 # Set the SMTP server address smtp_connect_timeout 30 # Set the timeout time for connecting to SMTP Server router_id lvs_devel # indicates an identifier for running the keepalived server. Information displayed on the subject of the email} vrrp_instance vi_1 {state master # specifies the keepalived role. The master indicates that the host is the master server, backup indicates that the host is the backup server interface eth0 # specifies the interface virtual_router_id 51 of the HA Monitoring Network # virtual route ID, which is a number and the same vrrp instance uses a unique identifier. That is, in the same vrrp_instance, the master and backup must be consistent with the priority 100 # defined priority. The larger the number, the higher the priority. In the same vrrp_instance, the master priority must be higher than the backup priority advert_int 1 # Set the interval between the master and Backup Server Load balancer synchronization check, the Unit is second authentication {# Set the authentication type and password auth_type pass # Set the authentication type, mainly including pass and Ah auth_pass 1111 # Set the authentication password, under the same vrrp_instance, master and backup must use the same password for normal communication} virtual_ipaddress {# Set a virtual IP address. You can set multiple virtual IP addresses with one 10.0.0.148} virtual_server 10.0.0.148 80 {# Set a virtual server You must specify the virtual IP address and service port. The IP address and port are separated by spaces. delay_loop 6 # Set the runtime check time, in the unit of second lb_algo RR # Set the load scheduling algorithm, here it is set to RR, that is, the polling algorithm lb_kind Dr # sets the LVS mechanism for load balancing. There are three modes: Nat, Tun, and Dr: persistence_timeout 50 # session persistence time, in seconds. This option is very useful for dynamic web pages and provides a good solution for session sharing in the cluster system. # With this session persistence function, user requests are distributed to a service node until the session persistence time is exceeded. # Note that the session persistence time is the maximum no response timeout time. That is to say, if the user does not perform any operation within 50 seconds when operating the dynamic page, # The next operation will be distributed to another node, but if the user is still operating on the dynamic page, protocol TCP is not limited by 50 seconds # specify the forwarding protocol type, there are TCP and UDP real_server 10.0.0.20.80 {# configure Service Node 1. You must specify the real IP address and port of the Real Server, separate the IP address and port with spaces weight 3 # configure the weight of the service node. The weight is represented by a number. The larger the number, the higher the weight, you can assign different loads to servers with different performance values. You can set a higher weight for servers with high performance values and a lower weight for servers with low performance values, in this way, the system resource tcp_check {# RealServer status detection settings can be properly used and allocated, unit: seconds connect_timeout 10 # No response timeout for 3 seconds nb_get_retry 3 # Number of Retries 3 # Retry Interval connect_port 80} real_server 10.0.0.139 80 {weight 3 tcp_check {connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 }}}

#################### Backup ###################

! Configuration File for keepalivedglobal_defs {   notification_email {     [email protected]     [email protected]   }   notification_email_from [email protected]   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id LVS_DEVEL}vrrp_instance VI_1 {    state BACKUP    interface eth0    virtual_router_id 51    priority 99    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        10.0.0.148    }}virtual_server 10.0.0.148 80 {    delay_loop 6    lb_algo rr    lb_kind DR    persistence_timeout 50    protocol TCP    real_server 10.0.0.137 80 {        weight 3        TCP_CHECK {            connect_timeout 10            nb_get_retry 3            delay_before_retry 3            connect_port 80        }    }    real_server 10.0.0.139 80 {        weight 3        TCP_CHECK {            connect_timeout 10            nb_get_retry 3            delay_before_retry 3            connect_port 80        }    }}

3. Run the RealServer. Sh script on the two web servers to bind lo: 0 to the VIP address 10.0.0.148 and suppress ARP broadcast.

[[Email protected] ~] # Cat RealServer. Sh

#!/bin/bash#description: Config realserverVIP=10.0.0.148 /etc/rc.d/init.d/functions case "$1" instart)       /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP       /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 "RealServer Start OK"       ;;stop)       /sbin/ifconfig lo:0 down       /sbin/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 "RealServer Stoped"       ;;*)       echo "Usage: $0 {start|stop}"       exit 1esac exit 0

[[Email protected] ~] # Sh RealServer. Sh start

4. Run service keepalived start on LVS-master and LVS-BACKUP respectively to start keepalived to realize load balancing and high availability cluster;

[[Email protected] keepalived] # service keepalived start

[[Email protected] keepalived] # define SADM-l
IP Virtual Server version 1.2.1 (size = 4096)
Prot localaddress: Port sched1_flags
-> Remoteaddress: port forward weight activeconn inactconn
TCP 10.0.0.148: http rr persistent 50
-> 10.0.0.139: HTTP Route 3 0 0
-> 10.0.0.133: HTTP Route 3 0 0
[[Email protected] keepalived] #

View logs/var/log/messages

[[Email protected] keepalived] # tail-N 30/var/log/messages
Apr 19 03:59:58 LVS-MASTER avahi-daemon [2845]: Withdrawing address record for10.0.0.148 on eth0.
Apr 19 04:00:19 LVS-MASTER keepalived: Starting keepalived V1.2.1 (04/17, 2012)
Apr 19 04:00:19 LVS-MASTER keepalived: Starting healthcheck child process, pid = 5960
Apr 19 04:00:19 LVS-MASTER keepalived_healthcheckers: Netlink reflector reportsip 10.0.0.136 added
Apr 19 04:00:19 LVS-MASTER keepalived_healthcheckers: Netlink reflector reportsip 10.0.0.159 added
Apr 19 04:00:19 LVS-MASTER keepalived_healthcheckers: Registering kernelnetlink Reflector
Apr 19 04:00:19 LVS-MASTER: keepalived_healthcheckers: Registering kernelnetlink Command Channel
Apr 19 04:00:19 LVS-MASTER keepalived_healthcheckers: Opening File '/etc/keepalived. conf '.
Apr 19 04:00:19 LVS-MASTER keepalived_healthcheckers: configuration is using: 12257 bytes
Apr 19 04:00:19 LVS-MASTER keepalived_healthcheckers: Using linkwatch kernelnetlink reflector...
Apr 19 04:00:19 LVS-MASTER keepalived_healthcheckers: Activating healtcheckerfor service [10.0.0.137: 80]
Apr 19 04:00:19 LVS-MASTER keepalived_healthcheckers: Activating healtcheckerfor service [10.0.0.139: 80]
Apr 19 04:00:19 LVS-MASTER keepalived_vrrp: Netlink reflector reports ip10.0.0.136 added
Apr 19 04:00:19 LVS-MASTER keepalived_vrrp: Netlink reflector reports ip10.0.0.159 added
Apr 19 04:00:19 LVS-MASTER keepalived_vrrp: Registering kernel netlinkreflector
Apr 19 04:00:19 LVS-MASTER keepalived_vrrp: Registering kernel Netlink commandchannel
Apr 19 04:00:19 LVS-MASTER keepalived_vrrp: Registering gratutious ARP sharedchannel
Apr 19 04:00:19 LVS-MASTER keepalived_vrrp: Opening File '/etc/keepalived. conf '.
Apr 19 04:00:19 LVS-MASTER keepalived_vrrp: configuration is using: 36698 bytes
Apr 19 04:00:19 LVS-MASTER keepalived_vrrp: Using linkwatch kernel netlinkreflector...
Apr 19 04:00:19 LVS-MASTER keepalived_vrrp: vrrp sockpool: [ifindex (2), proto (112), FD ()]
Apr 19 04:00:19 LVS-MASTER keepalived: Starting vrrp child process, pid = 5961
Apr 19 04:00:20 LVS-MASTER keepalived_vrrp: vrrp_instance (vi_1) transition tomaster state
Apr 19 04:00:21 LVS-MASTER keepalived_vrrp: vrrp_instance (vi_1) entering masterstate
Apr 19 04:00:21 LVS-MASTER keepalived_vrrp: vrrp_instance (vi_1) settingprotocol VIPs.
Apr 19 04:00:21 LVS-MASTER MAID: vrrp_instance (vi_1) sendinggratuitous Arps on eth0 for 10.0.0.148
Apr 19 04:00:21 LVS-MASTER keepalived_healthcheckers: Netlink reflector reportsip 10.0.0.136 added
Apr 19 04:00:21 LVS-MASTER avahi-daemon [2845]: registering new address recordfor 10.0.0.148 on eth0.
Apr 19 04:00:21 LVS-MASTER keepalived_vrrp: Netlink reflector reports ip10.0.0.136 added
Apr 19 04:00:26 LVS-MASTER MAID: vrrp_instance (vi_1) sendinggratuitous Arps on eth0 for 10.0.0.148
[[Email protected] keepalived] #

Iv. Test

Next, we will test high availability and Failover ......

#### High availability test ####

Simulate a fault, stop the keepalived service on the LVS-MASTER, and then observe the log on the LVS-BACKUP, the information is as follows

[[Email protected] keepalived] # tail-F/var/log/messages
Apr 19 03:33:04 LVS-BACKUP keepalived_vrrp: vrrp_instance (vi_1) Transition to master state
Apr 19 03:33:05 LVS-BACKUP keepalived_vrrp: vrrp_instance (vi_1) entering masterstate
Apr 19 03:33:05 LVS-BACKUP keepalived_vrrp: vrrp_instance (vi_1) settingprotocol VIPs.
Apr 19 03:33:05 LVS-BACKUP MAID: vrrp_instance (vi_1) sendinggratuitous Arps on eth0 for 10.0.0.148
Apr 19 03:33:05 LVS-BACKUP keepalived_vrrp: Netlink reflector reports ip10.0.0.157 added
Apr 19 03:33:05 LVS-BACKUP keepalived_healthcheckers: Netlink reflector reportsip 10.0.0.157 added
Apr 19 03:33:05 LVS-BACKUP avahi-daemon [2825]: registering new address recordfor 10.0.0.148 on eth0.
Apr 19 03:33:10 LVS-BACKUP MAID: vrrp_instance (vi_1) sendinggratuitous Arps on eth0 for 10.0.0.148

The log shows that after the host fails, the slave immediately detects that the slave is changed to the master role and takes over the virtual IP Address Resources of the host, finally, bind the virtual IP address to the etho device.

The log status of the LVS-MASTER after the keepalived service on the LVS-BACKUP is turned on.

Apr 19 03:34:02 LVS-BACKUP keepalived_vrrp: vrrp_instance (vi_1) hosted ed higher PRIO advert
Apr 19 03:34:02 LVS-BACKUP keepalived_vrrp: vrrp_instance (vi_1) entering backup state
Apr 19 03:34:02 LVS-BACKUP keepalived_vrrp: vrrp_instance (vi_1) Removing protocol VIPs.
Apr 19 03:34:02 LVS-BACKUP keepalived_vrrp: Netlink reflector reports IP 10.0.0.157 removed
Apr 19 03:34:02 LVS-BACKUP keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.157 removed
Apr 19 03:34:02 LVS-BACKUP avahi-daemon [2825]: Withdrawing address record for 10.0.0.148 on eth0.

The log shows that after the slave detects that the host is restored to normal, the virtual IP resource is released and becomes the backup role again.

#### Failover test ####

Failover is to test whether the keepalived monitoring module can detect and block faulty nodes in time when a node fails, and transfer the server to a normal node for execution.

Disable the service of the web2 node. If the node fails, the logs of the master and slave nodes are as follows:

Apr 19 03:35:04 LVS-MASTER keepalived_healthcheckers: TCP connection to [10.0.0.139: 80] failed !!!
Apr 19 03:35:04 LVS-MASTER MAID: removing service [10.0.0.139: 80] From vs [10.0.0.148: 80]
Apr 19 03:35:04 LVS-MASTER keepalived_healthcheckers: remote SMTP server [127.0.0.1: 25] connected.

[[Email protected] keepalived] # define SADM-l
IP Virtual Server version 1.2.1 (size = 4096)
Prot localaddress: Port sched1_flags
-> Remoteaddress: port forward weight activeconn inactconn
TCP 10.0.0.148: http rr persistent 50
-> 10.0.0.133: HTTP Route 3 0 0
[[Email protected] keepalived] #

As can be seen from the above, the keepalived monitoring module detects the failure of the host 10.0.0.139 and removes some web2 from the cluster system. Access http: // 10.0.0.148 and only web1 is displayed)

Restart the service on the web2 node. The log information is as follows:

Apr 19 03:38:22 LVS-MASTER keepalived_healthcheckers: TCP connection to [10.0.0.139: 80] success.
Apr 19 03:38:22 LVS-MASTER keepalived_healthcheckers: adding service [10.0.0.139: 80] To vs [10.0.0.148: 80]
Apr 19 03:38:22 LVS-MASTER keepalived_healthcheckers: remote SMTP server [127.0.0.1: 25] connected.

[[Email protected] # define SADM-l
IP Virtual Server version 1.2.1 (size = 4096)
Prot localaddress: Port sched1_flags
-> Remoteaddress: port forward weight activeconn inactconn
TCP 10.0.0.148: http rr persistent 50
-> 10.0.0.139: HTTP Route 3 0 0
-> 10.0.0.133: HTTP Route 3 0 0
[[Email protected] keepalived] #

After the keepalived monitoring module detects that the host is restored to normal, it adds the node to the cluster system and can access the web2 page again)

 


Deploy LVS (NAT) + keepalived in centos 6.3 to achieve high-performance and high-availability Load Balancing

Http://www.cnblogs.com/mchina/archive/2012/08/27/2644391.html

Deploying LVS (DR) + keepalived in RHEL 5.4 to achieve high-performance and high-availability Load Balancing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.