利用heartbear软件实现多Director之间高可用性的群集

一、heartbeat实现多Director间的高可用性

//heartbeat软件包的安装

[root@mail tool]#yum localinstall heartbeat-2.1.4-9.el5.i386.rpm

heartbeat-pils-2.1.4-10.el5.i386.rpm

heartbeat-stonith-2.1.4-10.el5.i386.rpm

libnet-1.1.4-3.el5.i386.rpm

perl-MailTools-1.77-1.el5.noarch.rpm –nogpgcheck

//Director1系统环境的基本设置

[root@node1 ~]# vim /etc/hosts

5 192.168.1.253 node1.xcu.com

6 192.168.1.254 node2.xcu.com

[root@node1 ~]# vim /etc/sysconfig/network

3 HOSTNAME=node1.xcu.com

//配置heartbeat主要配置文档

[root@mail ~]# cd /etc/ha.d/

[root@mail ha.d]# cp /usr/share/doc/heartbeat-2.1.4/ha.cf ./

[root@mail ha.d]# cp /usr/share/doc/heartbeat-2.1.4/authkeys ./

[root@mail ha.d]# cp /usr/share/doc/heartbeat-2.1.4/haresources ./

[root@mail ha.d]# cp /etc/init.d/ipvsadm /etc/ha.d/resource.d/

[root@mail ha.d]# vim ha.cf //主配置文档

48 keepalive 2

56 deadtime 30

61 warntime 10

91 bcast eth1 # Linux //监听心跳的端口

214 node node1.xcu.com

215 node node2.xcu.com

[root@node1 ha.d]# vim haresources

45 node1.xcu.com 192.168.1.100 ipvsadm //设置Director间共用的vip地址

//产生随机数认证密钥,这个要求各个Director上必须一致。

[root@node1 ~]# dd=/dev/urandom bs=512 count=1|openssl md5

d41d8cd98f00b204e9800998ecf8427e

[root@node1 ha.d]# vim authkeys

27 auth 3

28 3 md5 d41d8cd98f00b204e9800998ecf8427e

//Director上ipvsadm的配置

[root@node1 ha.d]# ipvsadm -A -t 192.168.1.100:80 -s rr

[root@node1 ha.d]# ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.10 -g

[root@node1 ha.d]# ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.20 -g

[root@node1 ha.d]# service ipvsadm save

//把Director1上的配置复制给Director2

[root@node1 ha.d]# scp ha.cf 192.168.1.254:/etc/ha.d/

[root@node1 ha.d]# scp authkeys 192.168.1.254:/etc/ha.d/

[root@node1 ha.d]# scp haresources 192.168.1.254:/etc/ha.d/

[root@node1 ha.d]# scp /etc/sysconfig/ipvsadm 192.168.1.254:/etc/sysconfig/

[root@node1 ha.d]# service ipvsadm stop

[root@node1 ha.d]# chkconfig ipvsadm off

[root@node1 ha.d]# chmod 600 authkeys

注意!启动Director之前,要保证各个Director的ipvsadm服务处于关闭状态。然后,配置完成后,在各个Director上同时执行以下命令:

[root@node1 heartbeat]# service heartbeat start

二、构建WWW服务器

//首先,服务器环境的基本设置

[root@mail ~]# ifconfig eth0 192.168.1.10

[root@mail ~]# sysctl -a |grep ignore|head -2 >>/etc/sysctl.conf

[root@mail ~]# sysctl -a |grep announce|head -2 >>/etc/sysctl.conf

[root@mail ~]# vim /etc/sysctl.conf

image

[root@mail ~]#ifconfig lo:0 192.168.1.100 netmask 255.255.255.255

[root@mail ~]#route add -host 192.168.1.100 dev lo:0

//安装Apache软件包

[root@mail ~]# yum install httpd*

//编辑一个简单的网页用于测试

[root@mail ~]# cd /var/www/html/

[root@mail html]# vim index.html

image

[root@mail html]# service httpd start

//首先,在本机上测试一下网页是否能正常打开

[root@mail html]# links http://192.168.1.10

image

以上配置在另外一个WWW服务器上做相同配置,这里就不作描述。

三、测试

如果以上Director和WWW服务器配置结果正确无误,应该会在一个Director出口上产生我们配置的vip以及ipvsadm,而在另一个Director上将什么都不显示。如图:

image

image

image

image

这个时候我们访问http://192.168.1.100,并连续刷新几次网页。

image

此时观察Director1上ipvsadm条目,发现已经有多条匹配了rr调度算法。

image

//接下来我们模拟其中一台Director出现故障,并且查看该Director的端口及ipvsadm状态

[root@node1 heartbeat]# cd /usr/lib/heartbeat

[root@node1 heartbeat]# ./hb_standby //使活动的Director变为备份

image

image

//查看网络连通性和站点访问情况

image

image

image

//查看原来的备份Director状态

image

image

//再模拟原来主Director恢复活动后,网络联通状态和网站访问情况

[root@node1 heartbeat]# ./hb_takeover //抢回优先Director的权限

image

image

image

image

image

从上可以看到,网络并没有因Director的主、备份切换或者其中一台Director不工作而造成网络中断的情况,从而实现了集群服务的简单的高可用性。