Web Load Balancing Based on LVS + heartbeat

Source: Internet
Author: User

Document configuration environment:

Master scheduler A: 192.168.0.111 (eth0) Intranet: 172.16.1.1 (eth1)

From scheduler B: 192.168.0.112 (eth0) Intranet: 172.16.1.2 (eth1)

Real Server R1: 192.168.0.113

Real Server R2: 192.168.0.114

Virtual IP address (VIP): 192.168.0.110

Preparations: Add the following content to the/etc/hosts of schedulers A and B:

Vi/etc/hosts

192.168.0.111 quxinyao

192.168.0.112 slave

Configure master scheduler:

1.install libnet-1.1.2.1.tar.gz

(1) tar-zxvf libnet-1.1.2.1.tar.gz

(2) cd libnet-1.1.2.1

(3)./configure

(4) make & make install

Libnet installation is complete.

2. Install kernel-devel

(1) first check the kernel: uname-

(2) then install the corresponding kernel's kernel-devel

Rpm-ivh kernel-devel-2.6.9-78.EL.i686.rpm

(3) connect the current kernel to/usr/src/linux

Ln-s/usr/src/kernels/2.6.9-78. EL-i686/usr/src/linux

3.install ipvsadm-1.24.tar.gz

(1) tar-zxvf ipvsadm-1.24.tar.gz

(2) cd ipvsadm-1.24

(3) make & make install

Ipvsadm installation is complete.

4. Install pkgconfig

Rpm-ivh/media/cdrom/RedHat/RPMS/pkgconfig-0.15.0-3.i386.rpm

5. Install glib2-devel

Rpm-ivh/media/cdrom/RedHat/RPMS/glib2-devel-2.4.7-1.i386.rpm

6.install heartbeat-2.0.2.tar.gz

(1) tar-zxvf heartbeat-2.0.2.tar.gz

(2) cd heartbeat-2.0.2

(3)./ConfigureMe configure -- disable-swig -- disable-snmp-subagent

(4) make & make install

Heartbeat installation is complete.

7. Configure The lvs Startup Script

This document uses the lvs-DR mode. This mode is characterized by client requests entering and allocating from the scheduler to the real server, and then the responses from the Real Server are directly delivered to the client.

The configuration is as follows:

Vi/etc/init. d/lvs

#! /Bin/sh

# Chkconfig: 2345 20 80

# Description: start LVS of Directorserver

VIP1 = 192.168.0.110 // specify the virtual IP Address

RIP1 = 192.168.0.111

RIP2 = 192.168.0.112

RIP3 = 192.168.0.113

RIP4 = 192.168.0.114

// RIP1-RIP4 for Real Server IP, if more real server can add more, such as RIP5 etc.

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

Case "$1" in

Start)

Echo "start LVS of DirectorServer"

# Set the Virtual IP Address

/Sbin/ifconfig eth0: 0 $ VIP1 broadcast $ VIP1 netmask 255.255.255.255.255 up // eth0: 0

/Sbin/route add-host $ VIP1 dev eth0: 0

# Clear IPVS table

/Sbin/ipvsadm-C

# Set LVS

/Sbin/ipvsadm-A-t $ VIP1: 80-s lc

/Sbin/ipvsadm-a-t $ VIP1: 80-r $ RIP1: 80-g

/Sbin/ipvsadm-a-t $ VIP1: 80-r $ RIP2: 80-g

/Sbin/ipvsadm-a-t $ VIP1: 80-r $ RIP3: 80-g

/Sbin/ipvsadm-a-t $ VIP1: 80-r $ RIP4: 80-g

# Run LVS // use ipvsadm to forward client requests.-s lc is the least-connection algorithm, and-g is the DR mode. There are several real servers to add several records.

/Sbin/ipvsadm

# End

;;

Stop)

Echo "close LVS Directorserver"

/Sbin/ipvsadm-C // clear the ipvsadm table when disabled

;;

*)

Echo "Usage: $0 {start | stop }"

Exit 1

Esac

Note: Do not use chkconfig to manage this resource script, just put it in/etc/init. d.

Use chkconfig to manage and enable it with the System

Chkconfig -- add lvs

Chkconfig -- level 35 lvs on

8. Configure heartbeat

First, create the required users and groups.

Groupadd haclient

Useradd-g haclient hacluster

(1) cd heartbeat-2.0.4

(2) cp doc/ha. cf doc/haresources doc/authkeys/etc/ha. d/
(3) cp ldirectord/ldirectord. cf/etc/ha. d/

The main directory of heartbeat is:/etc/ha. d/

Ha. cf is the master configuration file of heartbeat. The configuration is as follows. You can follow the default configuration for the rest:

(4) vi/etc/ha. d/ha. cf (the options below are all removed from the preceding annotations)

<1> log File Location: logfile/var/log/ha-log

<2> specify how often the Master/Slave server sends a heartbeat keepalive 2

<3> If the heartbeat of the other party is not received within 30 seconds, the other party is deemed to have been DOWN and deadtime 30

<4> If the heartbeat is not received within 10 seconds, an alarm is triggered. Warntime 10

<5> the other party goes DOWN and then checks again in 120 seconds. Initdead 120

<6> specify the listening port udpport 694

<7> heartbeat listening network port, eth1

Bcast eth1 // remove the # before the first bcast in the file, and remove the # linux

<8> resources are automatically withdrawn after the master node recovers. Auto_failback on

<9> specify the Host Name of the master-slave server, that is, the host name specified in the hosts file.

The first node is the master server, and the second node is the slave server.

Node quxinyao // host name of the server

Node slave

<10> when both IP addresses 192.168.0.1 and 192.168.0.111 cannot be pinged, the other party begins to take over the resources.

Ping_group group1 192.168.0.1 192.168.0.111

<11> enable ipfail script respawn root/usr/lib/heartbeat/ipfail

<12> specify the user that runs ipfail. Apiauth ipfail gid = root uid = root

Master configuration file configured

9. Configure the haresources file, which specifies the virtual IP address and the resource script controlled by the host.

The content is as follows:

Vi/etc/ha. d/haresources

# Quxinyao is the master scheduler host name, And 192.168.0.110 is the virtual IP address,

Lvs ldirectord is the resource scripts controlled by heartbeat.

In/etc/init. d.

Quxinyao 192.168.0.110 lvs ldirectord

Resource file configuration is complete.

Use chkconfig to manage and enable it with the System

Chkconfig -- add ldirectord

Chkconfig -- level 35 ldirectord on

10. ldirectord. cf is the configuration file of the ldirectord process. This process is used to monitor the running status of the Real Server. If the Real Server cannot respond to the request, it is excluded from the forwarding list.

Vi/etc/ha. d/ldirectord. cf

<1> set the time-out time of the Real Server checktimeout = 30

<2> interval checkinterval = 10

<3> if all real servers fail, the requests will be forwarded to the local fallback = 127.0.0.1: 80.

<4> change the configuration file content and do not need to re-ldirectord autoreload = yes

<5> specify the log location logfile = "/var/log/ldirectord. log" quiescent = no

<6> specify virtual IP address virtual = 192.168.0.110: 80

<7> specify the Real Server IP address and listening port

Real = 192.168.0.111: 80 gate

Real = 192.168.0.112: 80 gate

Real = 192.168.0.113: 80 gate

Real = 192.168.0.114: 80 gate

Fallback = 127.0.0.1: 80 gate

Service = http

<8> name of the listener test page, which is placed in the root directory of the Real Server web Service

Request = "test.html"

<9> specify the content returned from the test page

Receive = "Test Page"

Virtualhost = v.abc.cn

<10> specify a forwarding algorithm

Scheduler = lc // The algorithm here must be the same as the LVS script algorithm.

Protocol = tcp

Ldirectord. cf is configured.

11. authkeys file configuration

The authkeys file is used to encrypt heartbeat information.

Vi/etc/ha. d/authkeys

Auth 1

1 crc

Here we use crc Cyclic Redundancy verification instead of encryption.

The permission for this file must be 600.

The entire lvs and heartbeat configurations are complete.

12. The configuration of slave scheduler B is exactly the same as that of scheduler.

Manage the Heartbeat application chkconfig and start it randomly.

Chkconfig -- add heartbeat

Chkconfig -- level 35 heartbeat on

13. Configure the real server script and place the realserver script in the/etc/init. d directory of each Real Server. The active and standby schedulers are also real servers.

Vi/etc/init. d/realserver

#! /Bin/bash

#

# Lvsdrrs init script to hide loopback interfaces on LVS-DR

# Real servers. Modify this script to suit

# Your needs. You at least need to set the correct VIP address (es ).

#

# Script to start lvs dr real server.

#

# Chkconfig: 2345 20 80

# Description: lvs dr real server

#

# You must set the VIP address to use here:

# Specify a virtual IP Address

VIP = 192.168.0.110

Host = '/bin/hostname'

Case "$1" in

Start)

# Start LVS-DR real server on this machine.

/Sbin/ifconfig lo down

/Sbin/ifconfig lo up

# Modify related kernel parameters:

Echo "1" & gt;/proc/sys/net/ipv4/conf/lo/arp_ignore

Echo "2" & gt;/proc/sys/net/ipv4/conf/lo/arp_announce

Echo "1" & gt;/proc/sys/net/ipv4/conf/all/arp_ignore

Echo "2" & gt;/proc/sys/net/ipv4/conf/all/arp_announce

/Sbin/ifconfig lo: 0 $ VIP netmask running 255.255.255 up

/Sbin/route add-host $ VIP dev lo: 0

;;

Stop)

# Stop LVS-DR real server loopback device (s ).

/Sbin/ifconfig lo: 0 down

;;

Status)

# Status of LVS-DR real server.

Islothere = '/sbin/ifconfig lo: 0 | grep $ VIP'

Isrothere = 'netstat-rn | grep "lo" | grep $ VIP'

If [! "$ Islothere"-o! "$ Isrothere"]; then

# Either the route or the lo: 0 device

# Not found.

Echo "LVS-DR real server Stopped ."

Else

Echo "LVS-DR Running ."

Fi

;;

*)

# Invalid entry.

Echo "$0: Usage: $0 {start | status | stop }"

Exit 1

;;

Esac

14. The configuration script of the Real Server is complete. Place the script in the/etc/init. d directory of each Real Server (including the Master/Slave Scheduler) and manage it with chkconfig for random startup.

Chkconfig -- add realserver

Chkconfig -- level 35 realserver on

15. At last, heartbeat is deployed on the master-slave scheduler, And the realserver is enabled on the Real Server to test whether switching and forwarding are normal.

Use ipvsadm-ln to view the Server LIST

View access logs with ipvsadm-Lcn


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.