1:准备阶段
两台服务器一主一从
我这里采用了yum的方式进行安装,如想使用rpm安装,请自行下载rpm包
安装epel拓展源
国外的速度是在是太慢,我这里选择了阿里的拓展源,PS  感谢马云。

[root@tudou ~]# rm -rf /etc/yum.repos.d/* 
[root@tudou ~]# wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo

关闭防火墙

[root@tudou ~]# iptables -F

2:安装heartbeat 以及依赖

[root@tudou ~]# yum install -y heartbeat
[root@tudou ~]# yum install -y libnet

3:拷贝样例文件并配置

heartbeat 给我们提供了一些样例配置,在/usr/share/doc/heartbeat

[root@tudou ~]# cd /usr/share/doc/heartbeat-3.0.4/
[root@tudou heartbeat-3.0.4]# ls
apphbd.cf  AUTHORS    COPYING       ha.cf        README
authkeys   ChangeLog  COPYING.LGPL  haresources
[root@tudouheartbeat-3.0.4]# cp authkeys ha.cf haresources /etc/ha.d/

编辑认证文件(authkeys)

auth 3
#1 crc
#2 sha1 HI!
3 md5 Hello!

  authkeys文件用于设定Heartbeat的认证方式,共有3种可用的认证方 式,即crc、md5和sha1。3种认证方式的安全性依次提高,但是占用的系统资源也依次增加。如果Heartbeat集群运行在安全的网络上,可以使 用crc方式;如果HA每个节点的硬件配置很高,建议使用sha1,这种认证方式安全级别最高;如果是处于网络安全和系统资源之间,可以使用md5认证方 式。这里我们使用md5认证方式,设置如下:
PS:确保该文件权限为600

-rw-------. 1 root root  643 Aug  7 14:10 authkeys


配置心跳的监控(haresources)

node1 192.168.1.122/24/eth0:0 nginx

指定 node1调用nginx服务,系统附加一个虚拟IP 192.168.1.122给eth0:0

这里如果node1宕机后,node2可以自动启动nginx服务,并新分配IP 192.168.1.122给node2的eth0:0

配置主配置文件(ha.conf)

logfile /var/log/ha_log/ha-log.log   ## ha的日志文件记录位置。如没有该目录,则需
要手动添加
ucast eth0 192.168.1.107    ##探测对方ip
keepalive 2    ##设定心跳(监测)时间时间为2秒
warntime 10    
deadtime 30    
initdead 120
hopfudge 1
udpport 694    ##使用udp端口694 进行心跳监测
auto_failback on ##当主启动时备切换
node node1  ##节点
node node2  ##节点2
ping 192.168.1.1   ##通过ping 网关来监测心跳是否正常。 
respawn hacluster /usr/lib64/heartbeat/ipfail ##自动拉起进程
apiauth ipfail gid=root uid=root
debugfile /Datas/logs/ha_log/ha-debug.log

拷贝配置文件到备机

[root@tudouha.d]# scp ha.cf haresources authkeys root@192.168.1.107:/etc/ha.d/
root@192.168.1.107's password: 
ha.cf                                         100%  682     0.7KB/s   00:00    
haresources                                   100% 5888     5.8KB/s   00:00    
authkeys                                      100%  643     0.6KB/s   00:00    
[root@tudouha.d]#

启动hearbeat /etc/init.d/heartbeat start
查看日志 tail -f /var/log/ha_log/ha-log.log

heartbeat 第一次启动非常慢,不要着急,耐心等耐,当vip启动完成,基本就没问题了

Aug 08 16:56:46 node1 heartbeat: [1557]: info: **************************
Aug 08 16:56:46 node1 heartbeat: [1557]: info: Configuration validated. Starting heartbeat 3.0.4
Aug 08 16:56:46 node1 heartbeat: [1558]: info: heartbeat: version 3.0.4
Aug 08 16:56:46 node1 heartbeat: [1558]: WARN: change_logfile_ownership: failed to chown dbgfile: No such file or directory
Aug 08 16:56:46 node1 heartbeat: [1558]: info: Heartbeat generation: 1470604473
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ucast: write socket priority set to IPTOS_LOWDELAY on eth0
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ucast: bound send socket to device: eth0
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ucast: set SO_REUSEPORT(w)
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ucast: bound receive socket to device: eth0
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ucast: set SO_REUSEPORT(w)
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ucast: started on port 694 interface eth0 to 192.168.1.107
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ping heartbeat started.
Aug 08 16:56:46 node1 heartbeat: [1558]: info: G_main_add_TriggerHandler: Added signal manual handler
Aug 08 16:56:46 node1 heartbeat: [1558]: info: G_main_add_TriggerHandler: Added signal manual handler
Aug 08 16:56:46 node1 heartbeat: [1558]: info: G_main_add_SignalHandler: Added signal handler for signal 17
Aug 08 16:56:46 node1 heartbeat: [1558]: info: Local status now set to: 'up'
Aug 08 16:56:47 node1 heartbeat: [1558]: info: Link 192.168.1.1:192.168.1.1 up.
Aug 08 16:56:47 node1 heartbeat: [1558]: info: Status update for node 192.168.1.1: status ping
Aug 08 16:58:46 node1 heartbeat: [1558]: WARN: node node2: is dead
Aug 08 16:58:46 node1 heartbeat: [1558]: info: Comm_now_up(): updating status to active
Aug 08 16:58:46 node1 heartbeat: [1558]: info: Local status now set to: 'active'
Aug 08 16:58:46 node1 heartbeat: [1558]: info: Starting child client "/usr/lib64/heartbeat/ipfail " (0,0)
Aug 08 16:58:46 node1 heartbeat: [1558]: WARN: No STONITH device configured.
Aug 08 16:58:46 node1 heartbeat: [1558]: WARN: Shared disks are not protected.
Aug 08 16:58:46 node1 heartbeat: [1558]: info: Resources being acquired from node2.
Aug 08 16:58:46 node1 heartbeat: [1570]: info: Starting "/usr/lib64/heartbeat/ipfail " as uid 0  gid 0 (pid 1570)
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.1.122)[1613]:  2016/08/08_16:58:46 INFO:  Resource is stopped
Aug 08 16:58:46 node1 heartbeat: [1572]: info: Local Resource acquisition completed.
Aug 08 16:58:57 node1 heartbeat: [1558]: info: Local Resource acquisition completed. (none)
Aug 08 16:58:57 node1 heartbeat: [1558]: info: local resource transition completed.
Aug 08 17:05:27 node1 heartbeat: [1558]: WARN: Shutdown delayed until current resource activity finishes.
Aug 08 17:11:19 HaMaster heartbeat: [1241]: info: Pacemaker support: false
Aug 08 17:11:19 HaMaster heartbeat: [1241]: ERROR: Current node [hamaster] not in configuration!
Aug 08 17:11:19 HaMaster heartbeat: [1241]: info: By default, cluster nodes are named by `uname -n` and must be declared with a 'node' directive in the ha.cf file.
Aug 08 17:11:19 HaMaster heartbeat: [1241]: info: See also: http://linux-ha.org/wiki/Ha.cf#node_directive
Aug 08 17:11:19 HaMaster heartbeat: [1241]: WARN: Logging daemon is disabled --enabling logging daemon is recommended
Aug 08 17:11:19 HaMaster heartbeat: [1241]: ERROR: Configuration error, heartbeat not started.
Aug 08 21:16:49 HaMaster heartbeat: [1220]: info: Pacemaker support: false
Aug 08 21:16:49 HaMaster heartbeat: [1220]: ERROR: Current node [hamaster] not in configuration!
Aug 08 21:16:49 HaMaster heartbeat: [1220]: info: By default, cluster nodes are named by `uname -n` and must be declared with a 'node' directive in the ha.cf file.
Aug 08 21:16:49 HaMaster heartbeat: [1220]: info: See also: http://linux-ha.org/wiki/Ha.cf#node_directive
Aug 08 21:16:49 HaMaster heartbeat: [1220]: WARN: Logging daemon is disabled --enabling logging daemon is recommended
Aug 08 21:16:49 HaMaster heartbeat: [1220]: ERROR: Configuration error, heartbeat not started.
Aug 08 21:18:34 node1 heartbeat: [1510]: info: Pacemaker support: false
Aug 08 21:18:34 node1 heartbeat: [1510]: WARN: Logging daemon is disabled --enabling logging daemon is recommended
Aug 08 21:18:34 node1 heartbeat: [1510]: info: **************************
Aug 08 21:18:34 node1 heartbeat: [1510]: info: Configuration validated. Starting heartbeat 3.0.4
Aug 08 21:18:34 node1 heartbeat: [1511]: info: heartbeat: version 3.0.4
Aug 08 21:18:34 node1 heartbeat: [1511]: info: Heartbeat generation: 1470604474
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ucast: write socket priority set to IPTOS_LOWDELAY on eth0
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ucast: bound send socket to device: eth0
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ucast: set SO_REUSEPORT(w)
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ucast: bound receive socket to device: eth0
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ucast: set SO_REUSEPORT(w)
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ucast: started on port 694 interface eth0 to 192.168.1.110
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ping heartbeat started.
Aug 08 21:18:34 node1 heartbeat: [1511]: info: G_main_add_TriggerHandler: Added signal manual handler
Aug 08 21:18:34 node1 heartbeat: [1511]: info: G_main_add_TriggerHandler: Added signal manual handler
Aug 08 21:18:34 node1 heartbeat: [1511]: info: G_main_add_SignalHandler: Added signal handler for signal 17
Aug 08 21:18:34 node1 heartbeat: [1511]: info: Local status now set to: 'up'
Aug 08 21:18:35 node1 heartbeat: [1511]: info: Link 192.168.1.1:192.168.1.1 up.
Aug 08 21:18:35 node1 heartbeat: [1511]: info: Status update for node 192.168.1.1: status ping
Aug 08 21:20:35 node1 heartbeat: [1511]: WARN: node node2: is dead
Aug 08 21:20:35 node1 heartbeat: [1511]: info: Comm_now_up(): updating status to active
Aug 08 21:20:35 node1 heartbeat: [1511]: info: Local status now set to: 'active'
Aug 08 21:20:35 node1 heartbeat: [1511]: info: Starting child client "/usr/lib64/heartbeat/ipfail " (0,0)
Aug 08 21:20:35 node1 heartbeat: [1511]: WARN: No STONITH device configured.
Aug 08 21:20:35 node1 heartbeat: [1511]: WARN: Shared disks are not protected.
Aug 08 21:20:35 node1 heartbeat: [1511]: info: Resources being acquired from node2.
Aug 08 21:20:35 node1 heartbeat: [1521]: info: Starting "/usr/lib64/heartbeat/ipfail " as uid 0  gid 0 (pid 1521)
harc(default)[1522]:    2016/08/08_21:20:35 info: Running /etc/ha.d//rc.d/status status
mach_down(default)[1557]:       2016/08/08_21:20:35 info: /usr/share/heartbeat/mach_down: nice_failback: foreign resources acquired
mach_down(default)[1557]:       2016/08/08_21:20:35 info: mach_down takeover complete for node node2.
Aug 08 21:20:35 node1 heartbeat: [1511]: info: mach_down takeover complete.
Aug 08 21:20:35 node1 heartbeat: [1511]: info: Initial resource acquisition complete (mach_down)
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.1.122)[1595]:  2016/08/08_21:20:35 INFO:  Resource is stopped
Aug 08 21:20:35 node1 heartbeat: [1523]: info: Local Resource acquisition completed.
harc(default)[1696]:    2016/08/08_21:20:35 info: Running /etc/ha.d//rc.d/ip-request-resp ip-request-resp
ip-request-resp(default)[1696]: 2016/08/08_21:20:35 received ip-request-resp 192.168.1.122/24/eth0:0 OK yes
ResourceManager(default)[1717]: 2016/08/08_21:20:35 info: Acquiring resource group: node1 192.168.1.122/24/eth0:0 nginx
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.1.122)[1744]:  2016/08/08_21:20:35 INFO:  Resource is stopped
ResourceManager(default)[1717]: 2016/08/08_21:20:35 info: Running /etc/ha.d/resource.d/IPaddr 192.168.1.122/24/eth0:0 start
IPaddr(IPaddr_192.168.1.122)[1875]:     2016/08/08_21:20:35 INFO: Adding inet address 192.168.1.122/24 with broadcast address 192.168.1.255 to device eth0 (with label eth0:0)
IPaddr(IPaddr_192.168.1.122)[1875]:     2016/08/08_21:20:35 INFO: Bringing device eth0 up
IPaddr(IPaddr_192.168.1.122)[1875]:     2016/08/08_21:20:35 INFO: /usr/libexec/heartbeat/send_arp -i 200 -r 5 -p /var/run/resource-agents/send_arp-192.168.1.122 eth0 192.168.1.122 auto not_used not_used
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.1.122)[1849]:  2016/08/08_21:20:35 INFO:  Success
ResourceManager(default)[1717]: 2016/08/08_21:20:36 info: Running /etc/init.d/nginx  start
Aug 08 21:20:45 node1 heartbeat: [1511]: info: Local Resource acquisition completed. (none)
Aug 08 21:20:45 node1 heartbeat: [1511]: info: local resource transition completed.