Heartbeat+ldirectord的实现负载均衡

时间:2021-10-12 03:07:02
实验图: Heartbeat+ldirectord的实现负载均衡

 

环境:

VMWare

CentOS 5.2

Heartbeat- 2.1.4-4.1

heartbeat-ldirectord- 2.1.4-4.1

http

arptables_jf

 

VIP: 10.0.10.60

LD Server1: eth0 10.0.10.209  #ha.server09

                  eth1 172.16.0.3

LD Server2: eth0 10.0.10.208     #ha.server08

                  eth1 172.16.0.2

 

Real Server1: 10.0.10.207

Real Server2: 10.0.10.240

 

ldirectord工作原理:
ldirectord
需要你在真实服务器内启用apache服务器,并在每台真实服务器web服务器的根目录下建立你在配置文件所指定的文件与内容,然后ldirectord 通过循环检查此文件来确定真实服务器是否存活,如果不存活,则自动将其权重设为0,以确保后面客户的连接不会再导向失效的真实服务器,如果真实服务修复上线后,则其又将它的权重设为以使它能够继续为客端连接提供服务。
ldirectord
主要是通过调用ipvsadm来创建ipvs虚拟服务器表。

 

LD Server Heartbeat的配置

 

配置文件列表:

/etc/ha.d/authkeys/etc/ha.d/ha.cf /etc/ha.d/haresources /etc/ha.d/ldirectord.cf   # cat  /etc/ha.d/authkeys

auth 1

1 crc

# cat /etc/ha.d/ha.cf

logfile         /var/log/ha_log/ha-log.log

logfacility     local0

bcast           eth1

#ucast          eth1 172.16.0.2

keepalive       2

warntime        10

deadtime        30

initdead        120

hopfudge        1

udpport         694

auto_failback   on

node            ha.server09

node            ha.server08

ping            10.0.10.254

respawn hacluster /usr/lib/heartbeat/ipfail

apiauth         ipfail uid=hacluster gid=haclient

debugfile       /var/log/ha_log/ha-debug.log

 

# cat /etc/ha.d/haresources

ha.server09 \

IPaddr2::10.0.10.60/24/eth0/10.0.10.255 \

ldirectord::ldirectord.cf LVSSyncDaemonSwap::master

 

# cat /etc/ha.d/ldirectord.cf

# Global Directives

checktimeout=3

checkinterval=1

#fallback=127.0.0.1:80

autoreload=yes

logfile="/var/log/ha_log/ldirectord.log"

logfile="local0"

quiescent=no

 

# Sample for an http virtual service

virtual=10.0.10.60:80

        real=10.0.10.207:80 gate

        real=10.0.10.240:80 gate

#       real=192.168.6.6:80 gate

        fallback=127.0.0.1:80

        service=http

        request="test.html"

        receive="work"

#       virtualhost=some.domain.com.au

        scheduler=wrr

#       persistent=600

        #netmask=255.255.255.255

        protocol=tcp

        checktype=negotiate

以上将以上的配置文档复制到LD server2/etc/ha.d目录中,并把cat /etc/ha.d/haresources配置中的ha.server09改为ha.server08

到此,LD Server配置完成,确保启用net.ipv4.ip_forward

# vim /etc/sysctl.conf

net.ipv4.ip_forward=0改为:

net.ipv4.ip_forward=1

保存退出,使其马上生效:

# sysctl -p

 

启动heartbeat服务:

# service heartbeat start

 

Real Server 配置

Real Server 需要安装arptables_jf

RealServer1RealServer2中直接运行如下脚本:

# cat /root/lvs-rs.sh

#!/bin/bash

#

 

echo -n "Input Virtaul IP:"

read VIP

echo -n "Input Real IP:"   # 输入本机IP地址

read RIP

/etc/init.d/arptables_jf stop

/sbin/arptables -A IN -d $VIP -j DROP

/sbin/arptables -A OUT -s $VIP -j mangle --mangle-ip-s $RIP

service arptables_jf save

 

cat >>/etc/sysconfig/network-scripts/ifcfg-lo:0<<EOF

DEVICE=lo:0

IPADDR=$VIP

NETMASK="255.255.255.255"

ONBOOT=yes

NAME=loopback

 

EOF

 

/sbin/ifup lo

 

 

或者使用startup脚本来完成RealServer的配置,如下是RealServer1startup脚本:

# cat /etc/init.d/lvsRS

 

#!/bin/sh

# decription:start realserver

# chkconfig 235 26 26

 

#echo -n "Input Local Real IP:"

#_RIPADD

 

#echo -n "Input The Vitaul IP(VIP):"

#_VIP

 

VIP=10.0.10.60

#VIP2=10.0.10.61

RIP1=10.0.10.207

RIP2=10.0.10.208

RIP3=10.0.10.210

RIP4=10.0.10.240

#RIP3=10.0.10.x

 

GW=10.0.10.254

 

SER_PORT=80

SMTP_PORT=25

HTTP_CON_INTERFACE=lo:6721

SMTP_CON_INTERFACE=lo:6725

ATTEM=wrr

 

. /etc/rc.d/init.d/functions

 

case "$1" in

        start)

        echo " start LVS of REALServer"

        /sbin/ifconfig $HTTP_CON_INTERFACE $VIP broadcast $VIP netmask 255.255.255.255 up

        /sbin/route add -host $VIP dev $HTTP_CON_INTERFACE

        echo "0" > /proc/sys/net/ipv4/ip_forward

        /etc/init.d/arptables_jf stop

        /sbin/arptables -A IN -d $VIP -j DROP

        /sbin/arptables -A OUT -s $VIP -j mangle --mangle-ip-s $RIP1

        /etc/init.d/arptables_jf save

#       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 $HTTP_CON_INTERFACE down

        echo " stop LVS service"

        /sbin/arptables -D IN -d $VIP -j DROP

        /sbin/arptables -D OUT -s $VIP -j mangle --mangle-ip-s $RIP1

#       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

 

RealServer2 startup 脚本:

本文出自 “网络记忆棒” 博客,请务必保留此出处http://netocool.blog.51cto.com/61250/154478