RHEL6上配置基于heartbeat+lvs+ldirectord的高可用服务

 

Heartbeat+ipvsadm+ldirectord组建linux高可用集群

 

1. 系统环境:

RHEL6,组建选择base--默认,developmenttools—选择所有可选包。

对外虚拟IP:192.168.50.179

网关:192.168.50.254

负载节点:(eth0为外网卡)master-192.168.50.170

负载心跳:(eth1为心跳检查)192.168.94.10

备用节点:(eth0为外网卡)backup-192.168.50.171

备用节点心跳:(eth1为心跳检查)192.168.94.11

真实节点1rs1-192.168.50.172

真实节点2rs2-192.168.50.173

确保hostname==uname –n

编辑各机器/etc/hosts 文件,添加解析:

192.168.50.170 master

192.168.50.171 backup

192.168.50.172 rs1

192.168.50.173rs2

2.所需软件

Heartbeat2.1.3ipvsadm1.24

安装heartbeat时需要:

Libnet  (此包系统中未包含需要下载,我下载的是libnet-1.1.5.tar.gz

glib2-devel (系统自带)

libxml2-devel (系统自带)

bzip2-devel (系统自带)

Ldirector服务需要的依赖几个perl包(CPAN下载):

Socket6-0.23.tar.gz

IO-Socket-INET6-2.65.tar.gz

MailTools-1.74.tar.gz

依次安装以上各包以后应该可以安装heartbeat了。官网最新版的heartbeat3.0已分成3个组件包,安装时每个包时都需要去INTERNET验证大量XML文件,耗费的时间惊人,依赖包也比较多。Heartbeat2.1.4版本在rhe6上安装时需要大量依赖包,而且make install时报错,在网上也没找到相关的解决方案,所以退而求其次选择了heartbeat2.1.3版,此版安装时比较顺利,下面记录了我的安装过程。其中大部分配置都能从网上找到,我也只是借来用的,此文档的目的只是自己备忘,以及提示一下在安装过程中和我有同样错误的菜鸟们。涉及到版权问题还请见谅。并给我留言。

3.安装ipvsadm

#ln -s/usr/src/kernels/`uname -r` /usr/src/linux

#tar zxvfipvsadm-1.24.tar.gz

#cdipvsadm-1.24

#make;make install

---------------------------------------------------------------------------------------------------------------------------

4. 安装heartbeat

[root@mastersrc]# tar zxvf heartbeat-2.1.3.tar.gz

[root@mastersrc]# cd heartbeat-2.1.3

[[email protected]]# ./ConfigureMe configure

[[email protected]]#groupadd haclient

[[email protected]]# useradd -g haclient hacluster

[[email protected]]# ./ConfigureMe make --enable-fatal-warnings=no

[[email protected]]# make install

[[email protected]]# cp doc/{authkeys,haresources,ha.cf} /etc/ha.d/

[root@master heartbeat-2.1.3]# cp ldirectord/ldirectord.cf /etc/ha.d/

配置heartbeat

-------------------------------------------------------------------------------------

1)编辑authkeys文件:

[root@master heartbeat-2.1.3]# sed -e /^#/d /etc/ha.d/authkeys  

auth 3

3 md5 Hello!

[[email protected]]#chmod 600 /etc/ha.d/authkeys

--------------------------------------------------------------------------------------

2)编辑ha.cf文件:

 [root@master heartbeat-2.1.3]# sed -e /^#/d /etc/ha.d/ha.cf

debugfile/var/log/ha-debug

logfile/var/log/ha-log

logfacility     local0

keepalive 2

deadtime 5

initdead 15   #此数值大于等于deadtime的两倍

udpport 694

ucast eth1192.168.94.11

auto_failback on   #负载节点重新恢复正常后从备用节点自动接管

node    master

node    backup

crm no    #禁用crm

-----------------------------------------------------------------------------------------

 3)编辑haresources文件

 [[email protected]]# sed -e /^#/d /etc/ha.d/haresources 

master 192.168.50.179 cluster ldirectord 

#负载节点主机名 虚拟IP地址 要有heartbeat管理的程序

---------------------------------------------------------------------------------------------------------------------------

    4)编辑ldirectord文件

[[email protected]]# sed -e /^#/d /etc/ha.d/ldirectord.cf 

 

checktimeout=3

checkinterval=1

autoreload=yes

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

logfile="local0"

quiescent=no  #自动删除down掉的真实节点

 

virtual=192.168.50.179:80

                real=192.168.50.172:80 gate

                real=192.168.50.173:80 gate  #此项在有更多真实节点时自由添加

                fallback=127.0.0.1:80 gate

                service=http

                scheduler=rr

                #persistent=600

                #netmask=255.255.255.255

                protocol=tcp

                checktype=negotiate

                checkport=80

                request="index.html"   #向真实服务器请求的网页文件名

                receive="Test Page"    #从真实服务器返回的网页内容,当检测不到此网页文件或返回的内容不正确时即认为真实 节点down

                #virtualhost=www.x.y.z

5)编辑cluster脚本文件(此脚本要放在/etc/init.d)下

 [[email protected]]# vi /etc/init.d/cluster

#!/bin/bash

#description:start LVS of DirectorServer

#Writtenby qiu

# define used ip.

GW=192.168.50.254

VIP=192.168.50.179

RIP1=192.168.50.172

RIP2=192.168.50.173

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

 

case"$1" in

start)

#Clear all ipvsadmrules.

/sbin/ipvsadm -C

#set lvs vip fordr

/sbin/ipvsadm--set 30 5 60

/sbin/ifconfigeth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up

/sbin/route add-host $VIP dev eth0:0

/sbin/ipvsadm -A-t $VIP:80 -s rr -p 3  #-p选项设置session保持时间

/sbin/ipvsadm -a-t $VIP:80 -r $WEB_RIP1:80 -g -w 1

/sbin/ipvsadm -a-t $WEB_VIP:80 -r $RIP2:80 -g -w 1

touch/var/lock/subsys/ipvsadm > /dev/null 2>&1

# set Arp 当一台VS接管LVS服务时,可能会网络不通,这是因为路由器的MAC缓存表里无法及时刷新MAC.关于vip这个地址的MAC地址还是替换的VSMAC,解决方法是使用arping命令.

/sbin/arping -Ieth0 -c 5 -s $VIP $GW > /dev/null 2>&1

;;

 

stop)

/sbin/ipvsadm -C

/sbin/ipvsadm -Z

ifconfig eth0:0down

route del $VIP

rm -rf /var/lock/subsys/ipvsadm> /dev/null 2>&1

/sbin/arping -Ieth0 -c 5 -s $VIP $GW

echo "ipvsadmstoped"

;;

 

status)

if [ ! -e/var/lock/subsys/ipvsadm ];then

echo "ipvsadmis stoped"

status)

if [ ! -e/var/lock/subsys/ipvsadm ];then

echo "ipvsadmis stoped"

exit 1

else

ipvsadm -ln

echo"..........ipvsadm is OK."

fi

;;

restart

$0 stop

$0 start

;;

*)

echo "Usage:$0{start|stop|restart|status}"

exit 1

esac

exit 0

 

[[email protected]]#chmod 755 /etc/init.d/cluster

[[email protected]]#chkconfig -add heartbeat

至此负载节点已经安装配置完毕,把一下文件拷贝到相应备用节点的目录:

 authkeys,ha.cf,haresources,ldirectord.cf,cluster

备用几点只需要改动ha.cf文件中 “ucast eth1 192.168.94.11”中的ip地址为负载节点的eth1所对应的ip即可。其他配置完全相同。

 

5.真实节点的相关操作

1)在真实节点上安装lamp环境,或者其他应用程序,在制定存放网页的目录建立index.html文件,确保内容为“Test Page

2)在/etc/init.d下建立脚本文件:

#cat /etc/init.d/lvs_rs

#!/bin/sh

# chkconfig: 234572 08

# description:Config realserver lo:0 port and apply arp patch

VIP=192.168.50.179

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

case $1 in

            start)

            echo "lo:0 port starting"

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

            /sbin/ifconfig lo:0 $VIP broadcast $VIPnetmask 255.255.255.255 up

            /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

            ;;

            stop)

            echo "lo:0 port closing"

            ifconfig lo:0 down

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

            echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce

            ;;

            *)

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

            exit 1

esac

 

 

    添加为系统启动服务:

    #chkconfig –add lvs_rs

    #chkconfig –level345 on

6.测试

在负载节点上运行  watch ipvsadm -ln  命令,此命令现实lvs的转发列表。

交替停掉或者全部停掉两台真实节点上的httpd服务,查看转发列表的变化。

停掉负载节点,查看备用节点是否接管任务。访问虚拟IP看是否能访问到两台真实节点的web服务。

7.维护

主要是查看 /var/log/messages;/var/log/ha-log;/var/log/ldirectord.log 中相关的日志信息做出调整

你可能感兴趣的:(LVS,HA,heartbeat,休闲,ldirectord)