OS:centos 5.6
SERVER01:
eth0:172.18.10.201
eth1:10.10.10.1

SERVER02:
eth0:172.18.10.202
eth1:10.10.10.2

Vip:172.18.10.200

下载装备资源包

heartbeat-2.1.4-11.el5.x86_64.rpm
heartbeat-devel-2.1.4-11.el5.x86_64.rpm
heartbeat-pils-2.1.4-11.el5.x86_64.rpm
heartbeat-stonith-2.1.4-11.el5.x86_64.rpm
libnet-1.1.5-1.el5.i386.rpm
libnet-1.1.5-1.el5.x86_64.rpm
libtool-ltdl-1.5.22-7.el5_4.x86_64.rpm
openhpi-libs-2.14.0-5.el5.x86_64.rpm

cat <<EOD >/etc/ha.d/ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility  local0
keepalive 2
deadtime 20
warntime 10
initdead 100
udpport 694
ucast eth2 10.0.0.2
auto_failback off
node SERVER01
node SERVER02
ping 172.18.10.201
respawn hacluster /usr/lib64/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
crm on
EOD

cat <<EOD >/etc/ha.d/haresources
SERVER01 IPaddr::172.18.10.201/16/eth0 httpd
EOD

#这里的httpd是用来控制apache的停止和启动和监控

echo -e "auth 1\n1 crc" >/etc/ha.d/authkeys
chmod 600 /etc/ha.d/authkeys
/usr/lib64/heartbeat/haresources2cib.py
#在/var/lib/heartbeat/下会生成cib.xml文件,但该文件需要进行部分调整
只需修改cib.xml文件中资源部分:


<resources>
<group id="group_1">
<primitive class="ocf" id="IPaddr_172_18_10_200" provider="heartbeat" type="IPaddr">
<operations>
<op id="IPaddr_172_18_10_200_mon" interval="5s" name="monitor" timeout="5s"/>
</operations>
<instance_attributes id="IPaddr_172_18_10_200_inst_attr">
<attributes>
<nvpair id="IPaddr_172_18_10_200_attr_0" name="ip" value="172_18_10_200"/>
<nvpair id="IPaddr_172_18_10_200_attr_1" name="nic" value="24"/>
<nvpair id="IPaddr_172_18_10_200_attr_2" name="cidr_netmask" value="eth0"/>
</attributes>
</instance_attributes>
</primitive>
<primitive class="heartbeat" id="httpd_2" provider="heartbeat" type="Mysql">
<operations>
<op id="httpd_2_mon" interval="120s" name="monitor" timeout="60s"/>
</operations>
</primitive>
</group>
</resources>
修改为:

<resources>
<group id="group_1">
<primitive class="ocf" id="IPaddr_172_18_10_200" provider="heartbeat" type="IPaddr">
<operations>
<op id="IPaddr_172_18_10_200_mon" interval="5s" name="monitor" timeout="5s"/>
</operations>
<instance_attributes id="IPaddr_172_18_10_200_inst_attr">
<attributes>
<nvpair id="IPaddr_172_18_10_200_attr_0" name="ip" value="172_18_10_200"/>
<nvpair id="IPaddr_172_18_10_200_attr_1" name="nic" value="eth0"/>
<nvpair id="IPaddr_172_18_10_200_attr_2" name="cidr_netmask" value="24"/>

</attributes>
</instance_attributes>
</primitive>
<primitive class="heartbeat" id="httpd_2" provider="heartbeat" type="Mysql">
<operations>
<op id="httpd_2_mon" interval="30s" name="monitor" timeout="15s"/>
</operations>
</primitive>
</group>
</resources>

调整的内容:

value="eth0"与value="24"的位置.

interval="30s"
timeout="15s"
即每30秒检测资源运行情况,如果发现资源不在,则尝试启动资源,如果15s后还未启动成功,则资源切换向另节点


注意:IPaddr使用的是ocf格式的控制脚本,路径为:/usr/share/ocf/resource.d/heartbeat
这里的apache没有指定使用什么风格的控制脚本,默认应该也是ocf,可在这里因为ocf下只有apache,没有httpd这个脚本
heartbeat会去/etc/ha.d/resource.d/下寻找httpd,在这里我使用了LSB风格,但使用很正常,这一点有些不解.

介绍一下ocf和lsb格式的区别:

LSB格式的脚本必须支持status功能,必须能接收start,stop,status,三个参数;而如果是OCF格式,则必须支持start,stop,monitor三个参数.其中status和monitor参数是用来监控资源的,非常重要.

例如LSB风格的脚本,运行./httpd status时候,
返回值包含OK或则running则表示资源正常
返回值包含stopped或者No则表示资源不正常。

假如是OCF风格的脚本,运行./httpd monitor时候,
返回0表示资源是正常的,
返回7表示资源出现问题.

ocf格式的启动脚本在/usr/share/ocf/resource.d/heartbeat
lsb格式的启动脚本在/usr/share/lsb/resource.d/heartbeat


cd /etc/ha.d/ && scp authkeys haresources ha.cf 172.18.10.202:/etc/ha.d/
cd /var/lib/heartbeat/crm
rm cib.xml.* -f
ssh 172.18.10.202"rm /var/lib/heartbeat/crm/* -f"
scp cib.xml 172.18.10.202:/var/lib/heartbeat/crm/

note:heartbeat2.x不使用haresources,如果它的存在的话,在heartbeat启动时log中会出现一条WARN的信息:
heartbeat[30304]: 2012/04/02_20:02:02 WARN: File /etc/ha.d/haresources exists.
heartbeat[30304]: 2012/04/02_20:02:02 WARN: This file is not used because crm is enabled
但不影响使用.


启动heartbeat:
/etc/init.d/heartbeat start
#没有主次之分,谁先启动谁先获取资源.

监控资源运行情况:

#每3秒更新一次,显示当前资源的情况.
crm_mon -i3

#查看所有资源
[root@SERVER01 crm]# crm_resource -L
Resource Group: group_1
IPaddr_172_18_10_200
(ocf::heartbeat:IPaddr)
httpd_2 (heartbeat:httpd)
[root@SERVER01 crm]#

#查看资源跑在哪个节点上
[root@SERVER01 crm]# crm_resource -W -r httpd_2
resource httpd_2 is running on: SERVER01