1、实验环境

HA1:192.168.1.17(RHEL5.8_32bit,web server)

HA2:192.168.1.18(RHEL5.8_32bit,web server)

NFS:192.168.1.19(RHEL5.8_32bit,nfs server)

VIP:192.168.1.20


2、准备工作

<1> 配置主机名

节点名称使用/etc/hosts解析;节点名称必须跟uname -n命令的执行结果一致

HA1:

# hostname node1.ikki.com
# vim /etc/sysconfig/network
HOSTNAME=node1.ikki.com

HA2:

# hostname node1.ikki.com
# vim /etc/sysconfig/network
HOSTNAME=node2.ikki.com

<2> 配置节点ssh基于密钥方式互相通信

HA1:

# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

HA2:

# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

<3> 配置各节点基于主机名互相通信

HA1&HA2:

# vim /etc/hosts
192.168.1.17    node1.ikki.com  node1
192.168.1.18    node2.ikki.com  node2

<4> 配置各节点时间同步

HA1&HA2:

# crontab -e
*/5 * * * *     /sbin/ntpdate 202.120.2.101 &> /dev/null


3、安装Heartbeat v2

# yum --nogpgcheck localinstall perl-MailTools-1.77-1.el5.noarch.rpm
#yum--nogpgchecklocalinstall heartbeat-2.1.4-9.el5.i386.rpm heartbeat-gui-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


4、配置Heartbeat v2(各个节点)

# cd /etc/ha.d/
# cp /usr/share/doc/heartbeat-2.1.4/{authkeys,ha.cf,haresources} ./
# chmod 600 authkeys

<1> 配置authkeys

生成随机数:

# dd if=/dev/random count=1 bs=512 | md5sum
# vim authkeys
auth 1
1 md5 cbe6411e54d9bfbeef9cc1c48c7234fa

<2> 配置ha.cf

# vim ha.cf
keepalive 1                        # 设置1秒检测一次心跳
mcast eth0 225.0.100.19 694 1 0    # 设置心跳信息传递方式为多播
node    node1.ikki.com             # 设置各个节点名称
node    node2.ikki.com
ping 192.168.1.1                   # 使用ping通网关来判断存活状态

<3> 配置haresources(设置VIP和主节点服务)

# vim haresources
node1.ikki.com IPaddr::192.168.1.20/24/eth0 httpd

注意以上资源的顺序排列(当前资源:VIP、httpd)


5、配置WEB服务(各个节点)

# echo "

node1.ikki.com

" >> /var/www/html/index.html # service httpd stop # chkconfig httpd off


6、启动Heartbeat v2服务(在主节点HA1上启动服务并远程启动HA2上的服务)

HA1:

# service heartbeat start
# ssh node2 'service heartbeat start'


7、测试

访问http://192.168.1.20/

使用hb_standby将HA1设置为备节点进行调试:

# /usr/lib/heartbeat/hb_standby


8、搭建NFS服务(用于各节点挂载web目录)

NFS:

# vim /etc/exports
/web/htdocs     192.168.1.0/24(ro)
# service nfs start
# showmount -e 192.168.1.19
# mkdir -p /web/htdocs
# echo "

nfs server

" > /web/htdocs/index.html


9、停止Heartbeat v2服务并配置haresources(各个节点)

# service heartbeat stop
# ssh node2 'service heartbeat stop'
# vim /etc/ha.d/haresources
node1.ikki.com IPaddr::192.168.1.20/24/eth0 Filesystem::192.168.1.19:/web/htdocs::/var/www/html::nfs httpd

注意以上资源的顺序排列(当前资源:VIP、Filesystem、httpd)


10、启动Heartbeat v2服务并测试

HA1:

# service heartbeat start
# ssh node2 'service heartbeat start'

访问http://192.168.1.20/