Build an LVS + Keepalived Load Balancing Cluster

Source: Internet
Author: User

I have learned how to build LVS + Keepalived Server Load balancer over the past two days. There are many online tutorials, But I have encountered many problems when I start to work.

Now I want to share with you my own setup process and problems.

Hardware environment:

8 GB Macbook memory, 250 gb ssd, dual-core

Software environment:

Due to limited resources, four virtual machines were built.

Virtual Machine

[Root @ RS-1 work] # uname-

Linux RS-1 2.6.18-238. el5 #1 SMP Thu Jan 13 15:51:15 EST 2011 x86_64 x86_64 x86_64 GNU/Linux

[Root @ RS-1 work] # cat/etc/redhat-release

CentOS release 5.6 (Final)

The IP addresses of the four VMS are allocated as follows:

Master DR: {ip: 172.16.3.89 hostname: lvs-backup}

Slave DR: {ip: 172.16.3.90 hostname: lvs}

Real Server1: {ip: 172.16.3.91 hostname: RS-1}

Real Server2: {ip: 172.16.3.92 hostname: RS-2}

VIP: 172.16.3.199

1. Install ipvsadm (1.24) and keepalived (1.2.12) on the Master DR and Slave DR respectively)

Install ipvsadm

Check whether the IPVS module is installed in the system. It is displayed that the system supports the ipvs module.

[Root @ lvs ~] # Modprobe-l | grep ipvs

/Lib/modules/2.6.18-238. el5/kernel/net/ipv4/ipvs/ip_vs.ko

/Lib/modules/2.6.18-238. el5/kernel/net/ipv4/ipvs/ip_vs_dh.ko

/Lib/modules/2.6.18-238. el5/kernel/net/ipv4/ipvs/ip_vs_ftp.ko

/Lib/modules/2.6.18-238. el5/kernel/net/ipv4/ipvs/ip_vs_lblc.ko

/Lib/modules/2.6.18-238. el5/kernel/net/ipv4/ipvs/ip_vs_lblcr.ko

/Lib/modules/2.6.18-238. el5/kernel/net/ipv4/ipvs/ip_vs_lc.ko

/Lib/modules/2.6.18-238. el5/kernel/net/ipv4/ipvs/ip_vs_nq.ko

/Lib/modules/2.6.18-238. el5/kernel/net/ipv4/ipvs/ip_vs_rr.ko

/Lib/modules/2.6.18-238. el5/kernel/net/ipv4/ipvs/ip_vs_sed.ko

/Lib/modules/2.6.18-238. el5/kernel/net/ipv4/ipvs/ip_vs_sh.ko

/Lib/modules/2.6.18-238. el5/kernel/net/ipv4/ipvs/ip_vs_wlc.ko

/Lib/modules/2.6.18-238. el5/kernel/net/ipv4/ipvs/ip_vs_wrr.ko

Make a soft connection

[Vagrant @ lvs src] $ sudo ln-s/usr/src/kernels/2.6.18-238. el5-x86_64 // usr/src/linux

Compile

[Vagrant @ lvs ipvsadm-1.24] $ make


Install

[Vagrant @ lvs ipvsadm-1.24] $ sudo make install


Check whether the installation is successful

[Root @ lvs ~] # Ipvsadm-v

Ipvsadm v1.24 2005/12/10 (compiled with getopt_long and IPVS v1.2.1)

You can print the version number, indicating that the installation is successful !!!!

Install keepalived

Configure

[Vagrant @ lvs keepalived-1.2.12] $./configure -- sysconf =/etc -- with-kernel-dir =/usr/src/kernels/2.6.18-238. el5-x86_64/

Compile

[Vagrant @ lvs keepalived-1.2.12] $ make


Install

[Vagrant @ lvs keepalived-1.2.12] $ sudo make install


Make a soft connection

[Vagrant @ lvs keepalived-1.2.12] $ sudo ln-s/usr/local/sbin/keepalived/sbin/


Check whether the installation is successful

[Root @ lvs ~] # Keepalived-v

Keepalived v1.2.12 (05/06, 2014)

You can print the version number, indicating that the installation is successful !!

Similarly, install keepalived on lvs-backup.

Check whether the installation is successful

[Root @ lvsbackup ~] # Keepalived-v

Keepalived v1.2.12 (05/06, 2014)


Configure keepalived

! Configuration File for keepalived

# Global_defs {

# Icationication_email {

# Set alert email addresses. You can set multiple email addresses, with one email address per line,

# Enable the mail alarm and Sendmail service on the local machine.

#}

# Icationication_email_from Alexandre.Cassen@firewall.loc

# Smtp_server 192.168.199.1 # Set the SMTP Server address;

# Smtp_connect_timeout 30

# Router_id LVS_DEVEL

#}

######## VRRP Instance ########

Vrrp_instance VI_1 {

State MASTER # specifies the Keepalived role. MASTER is the host server and BACKUP is the BACKUP server.

Interface eth1 # BACKUP is a BACKUP server

Virtual_router_id 51

Priority 100 # defines the priority. A larger number indicates a higher priority. The primary DR must be greater than the standby DR.

Advert_int 1

Authentication {

Auth_type PASS # Set the authentication type, mainly including PASS and AH

Auth_pass 1111 # Set the verification Password

}

Virtual_ipaddress {

172.16.3.199 # Set the virtual IP address (virtual IP address) of the master DR. You can set multiple IP addresses, but one IP address is required per line.

}

}

######## Virtual Server ########

Virtual_server 172.16.3.199 80 {# note that IP addresses and port numbers are separated by Spaces

Delay_loop 6 # Set the health check time in seconds

Lb_algo rr # sets the load scheduling algorithm. The default value is rr, which is the polling algorithm. The best is the wlc algorithm.

Lb_kind DR # Set LVS to implement the LB mechanism. Three modes are available: NAT, TUNN, and DR.

Nat_mask 255.255.255.0

Persistence_timeout 50 # session persistence time, in seconds

Protocol TCP # specifies the forwarding protocol type, which can be TCP or UDP

Real_server 172.16.3.92 80 {

Weight 50 # configure the node weight. The greater the number, the higher the weight.

TCP_CHECK {

Connect_timeout 3 # indicates no response in 3 seconds, then timeout

Nb_get_retry 3 # indicates the number of retries

Delay_before_retry 3 # indicates the Retry Interval.

}

}

Real_server 172.16.3.91 80 {# Configure the server node, that is, the public IP of Real Server2

Weight 50 # configure the node weight. The greater the number, the higher the weight.

TCP_CHECK {

Connect_timeout 3 # indicates no response in 3 seconds, then timeout

Nb_get_retry 3 # indicates the number of retries

Delay_before_retry 3 # indicates the Retry Interval.

}

}


The Slave DR configuration is basically the same as that of the Master DR configuration. There are only two differences:

Change MASTER to BACKUP and priority 100 to priority 80.

Start keepalived

Compile the start. sh (stop. sh, restart. sh) script for easy start.

#! /Bin/sh

/Etc/init. d/keepalived start

Execute scripts

[Root @ lvs work] #./start. sh

Starting keepalived: [OK]

Write the detection script watch. sh.

#! /Bin/sh

Watch 'mongosadm-l-N'

Start Detection

[Root @ lvs work] #./watch. sh


Every 2.0 s: ipvsadm-l-n Tue May 6 12:49:52 2014


IP Virtual Server version 1.2.1 (size = 4096)

Prot LocalAddress: Port sched1_flags

-> RemoteAddress: Port Forward Weight ActiveConn InActConn

TCP 172.16.3.199: 80 rr persistent 50

-> 172.16.3.91: 80 Route 50 0 0

-> 172.16.3.92: 80 Route 50 0 0


We can see that two servers 172.16.3.91 and 172.16.3.92 have been detected.

Perform the same configuration and script on the Slave DR.

2. Install nginx on Real Server1 and Real Server2

Nginx installation is omitted.

After nginx is installed, start nginx.

Configure the realserver. sh script

#! /Bin/bash

SNS_VIP = 172.16.3.199


/Etc/rc. d/init. d/functions


Case "$1" in

Start)

Ifconfig lo: 0 $ SNS_VIP netmask 255.255.255.255 broadcast $ SNS_VIP

/Sbin/route add-host $ SNS_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)

Ifconfig lo: 0 down

Route del $ SNS_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 1

Esac

~

Start script

[Root @ RS-1 work] #./realserver. sh start

RealServer Start


Execute ifconfig and you can see that there is a red box with more content.


Test

Test on Slave DR <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Asn + Cjxicj4KCjxicj4KPHA + 1NpNYXN0ZXIgRFLJz9a00NB3YXRjaC5zaDwvcD4KPHA + pgltzybzcm9 "" alt = "\">

You can see 100 links.

During the configuration process, remember to disable the firewalls of all virtual machines. This is very important !!!

[Root @ lvs work] # service iptables stop


Run the following command to check whether the firewall has been disabled:

[Root @ lvs work] # chkconfig -- list "grep iptables

Iptables 0: off1: off2: off3: off4: off5: off6: off


Reference link:

Http://beyondhdf.blog.51cto.com/229452/1331874

Http://www.it165.net/admin/html/201308/1604.html

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.