(一) 网络架构
两台CentOS6.5
nfsmaster:heartbeatmaster:C1  eth1: 192.168.1.111 (和外网通信提供文件服务)  eth3(192.168.4.10 heartbeat心跳监控)
nfsslave:heartbeatslave:C2    eth2: 192.168.1.113 (和外网通信提供文件服务)     eth3(192.168.4.20 heartbeat心跳监控)

(二) 配置时间同步 (两台服务器同时执行)

1)配置时区

[root@localhost yum.repos.d]# rm -f /etc/localtime
[root@localhost yum.repos.d]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

2)配置时间同步

[root@localhost network-scripts]# ntpdate pool.ntp.org
29 Dec 15:40:14 ntpdate[3232]: adjust time server 202.112.29.82 offset 0.033253 sec

(三) host文件配置(两台服务器配置一样)

[root@localhost network-scripts]# vi /etc/hosts  (添加下面两行)
192.168.4.10 heartbeatmaster
192.168.4.20 heartbeatslave
192.168.1.111 C1.centos.sx
192.168.1.113 C2.centos.sx

修改hostname (C1上修改为HOSTNAME=C1.centos.sx,C2上修改为HOSTNAME=C2.centos.sx)

[root@localhost ha.d]# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=C1.centos.sx
GATEWAY=192.168.1.1
NETWORKING_IPV6=off
临时修改
hostname C1.centos.sx

(四) 添加两条路由规则,使心跳检测和数据同步不受干扰

C1端:
[root@localhost network-scripts]# route add -host 192.168.4.20 dev eth3
开机启动时添加这条规则
[root@localhost network-scripts]# echo '/sbin/route add -host 192.168.4.20 dev eth3' >> /etc/rc.local
C2端:
[root@localhost network-scripts]# route add -host 192.168.4.10 dev eth3
[root@localhost network-scripts]# echo '/sbin/route add -host 192.168.4.10 dev eth3' >> /etc/rc.local

(五) 部署heartbeat(只演示C1端,C2端同样)
C1端:
1) 增加epel源  官方yum源上没有heartbeat

[root@localhost ~]# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm 1>/dev/null 
[root@localhost ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
[root@localhost yum.repos.d]# yum clean all

2) yum安装heartbeat

[root@localhost yum.repos.d]# yum -y install heartbeat

3) 修改配置文件

[root@localhost yum.repos.d]# cd /usr/share/doc/heartbeat-3.0.4/
[root@localhost heartbeat-3.0.4]# cp ha.cf authkeys haresources /etc/ha.d/
[root@localhost ha.d]# vi ha.cf  #(C1,C2配置相同)
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility     local0
keepalive 2
deadtime 10
warntime 6
#initdead 120
udpport 694
#bcast eth3
mcast eth3 239.0.0.192 694 1 0 # 心跳监控时使用的网卡、组播地址、和端口
auto_failback on
respawn hacluster /usr/lib64/heartbeat/ipfail #(C1,C2配置相同)
node    C1.centos.sx # 主节点
node    C2.centos.sx # 从节点
ping 192.168.1.1 # (对外网关的ip地址,主要用来测试当前节点是否可以对外网络是否正常。作为是否要主从切换的一个条件)
[root@localhost ha.d]# vi authkeys # C1,C2配置相同
auth 1
1 crc
[root@localhost ha.d]# chmod 600 authkeys  #该文件必须设置为600权限,不然heartbeat启动会报错

C1:

[root@localhost ha.d]# vi haresources   
C1.centos.sx IPaddr::192.168.1.119/24/eth1  # 这里的C1.centos.sx代表主节点C1,C2配置相同,主节点都为C1。这里的ip是对外提供的浮动ip的地址 (这里C1,C2浮动ip必须写成一样 网卡则根据具体来定 在C2中我写成了eth2)

C2:

[root@localhost ha.d]# vi haresources   
C1.centos.sx IPaddr::192.168.1.119/24/eth2

## 如果配置成上面这样则heartbeat是根据heartbeat这项服务来做主从切换 如果C1宕机或者C1的heartbeat关闭则对外的浮动ip则被C2占用

#(该文件列出集群所提供的服务以及服务的默认所有者。 注意:两个集群节点上的该文件必须相同。集群的IP地址是该选项是必须配置的,不能在haresources文件以外配置该地址, haresources文件用于指定双机系统的主节点、集群IP、子网掩码、广播地址以及启动的服务等。其配置语句格式如下:
node-name network-config
#其中node-name指定双机系统的主节点,取值必须匹配ha.cf文件中node选项设置的主机名中的一个,node选项设置的另一个主机名成为从节点。network-config用于网络设置,包括指定集群IP、子网掩码、广播地址等。resource-group用于设置heartbeat启动的服务,该服务最终由双机系统通过集群IP对外提供。在本文末中我们假设要配置的HA服务为nginx和apache。 )

(六) 启动heartbeat (C1,C2执行相同)

[root@localhost ha.d]# service heartbeat start  # 启动
[root@localhost ha.d]# chkconfig heartbeat on  # 设置开机时启动

(七) 测试heartbeat启动停止时浮动ip资源的切换
1) 正常状态下
C1:

[root@localhost ha.d]# ip a | grep eth1
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.1.111/24 brd 192.168.1.255 scope global eth1
    inet 192.168.1.119/24 brd 192.168.1.255 scope global secondary eth1

C2:

[root@C2 ha.d]# ip a | grep eth2
2: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.1.113/24 brd 192.168.1.255 scope global eth2
# 此时浮动ip(192.168.1.119)属于C1

2) 关闭C1上的heartbeat
C1:

[root@localhost ha.d]# ip a | grep eth1
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.1.111/24 brd 192.168.1.255 scope global eth1

C2:

[root@C2 ha.d]# ip a | grep eth2
2: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.1.113/24 brd 192.168.1.255 scope global eth2
    inet 192.168.1.119/24 brd 192.168.1.255 scope global secondary eth2

# 此时浮动ip属于C2

(八) 测试heartbeat启动停止时控制其他服务(这里是nginx apache)的启动或停止
C1:

[root@C1 ha.d]# vi haresources  (更改heartbeat的资源定义文件)
C1.centos.sx 192.168.1.119 nginx httpd
[root@C1 ha.d]# ln -s /etc/rc.d/init.d/nginx /etc/ha.d/resource.d/
[root@C1 ha.d]# ln -s /etc/rc.d/init.d/httpd /etc/ha.d/resource.d/

C2:

[root@C2 ha.d]# vi haresources  (更改heartbeat的资源定义文件)
C1.centos.sx 192.168.1.119 nginx httpd
[root@C2 ha.d]# ln -s /etc/rc.d/init.d/nginx /etc/ha.d/resource.d/
[root@C2 ha.d]# ln -s /etc/rc.d/init.d/httpd /etc/ha.d/resource.d/

C1:

[root@C1 ha.d]# service heartbeat restart

C2:

[root@C2 ha.d]# service heartbeat restart


默认的状态下:
C1:

[root@C1 ha.d]# service httpd status
httpd (pid  1219) is running...
[root@C1 ha.d]# service nginx status
nginx (pid  19215) is running...

C2:

[root@C2 ha.d]# service nginx status
nginx is stopped
[root@C2 ha.d]# service httpd status
httpd is stopped

停止C1上的heartbeat服务
C1:

[root@C1 ha.d]# service heartbeat stop

再次查看状态
C1:

[root@C1 ha.d]# service nginx status
nginx is stopped
[root@C1 ha.d]# service httpd status
httpd is stopped

C2:

[root@C2 ha.d]# service nginx status
nginx (pid  14433) is running...
[root@C2 ha.d]# service httpd status
httpd (pid  14469) is running...

说明实验已经成功