一、启动Heartbeat
1、启动主节点的Heartbeat
Heartbeat安装完成后,自动在/etc/init.d目录下生成了启动脚步文件heartbeat,直接输入/etc/init.d/heartbeat可以看到heartbeat脚本的用法,如下所示:
- [root@node1 ~]# /etc/init.d/heartbeat
- Usage: /etc/init.d/heartbeat {start|stop|status|restart|reload|force-reload}
因而启动heartbeat可以通过如下命令进行:
- [root@node1 ~]#service heartbeat start
- 或者通过
- [root@node1 ~]#/etc/init.d/heartbeat start
这样就启动了主节点的heartbeat服务
日志信息如下:
- Feb 5 19:09:48 node1 heartbeat: [22768]: info: glib: ucast: bound send socket to device: eth0
- Feb 5 19:09:48 node1 heartbeat: [22768]: info: glib: ucast: bound receive socket to device: eth0
- Feb 5 19:09:48 node1 heartbeat: [22768]: info: glib: ucast: started on port 694 interface eth0 to 192.168.12.1
- Feb 5 19:09:48 node1 heartbeat: [22768]: info: glib: ping heartbeat started.
- Feb 5 19:09:48 node1 heartbeat: [22768]: info: glib: ping group heartbeat started.
- Feb 5 19:09:48 node1 heartbeat: [22768]: info: Local status now set to: 'up'
- Feb 5 19:09:49 node1 heartbeat: [22768]: info: Link 192.168.12.1:192.168.12.1 up.
- Feb 5 19:09:49 node1 heartbeat: [22768]: info: Status update for node 192.168.12.1: status ping
- Feb 5 19:09:49 node1 heartbeat: [22768]: info: Link group1:group1 up.
- Feb 5 19:09:49 node1 heartbeat: [22768]: info: Status update for node group1: status ping
此段日志是Heartbeat在进行初始化配置,例如,heartbeat的心跳时间间隔、UDP广播端口、ping节点的运行状态等,日志信 息到这里会暂停,等待120秒之后,heartbeat会继续输出日志,而这个120秒刚好是ha.cf中“initdead”选项的设定时间。此时 heartbeat的输出信息如下:
- Feb 5 19:11:48 node1 heartbeat: [22768]: WARN: node node2: is dead
- Feb 5 19:11:48 node1 heartbeat: [22768]: info: Comm_now_up(): updating status to active
- Feb 5 19:11:48 node1 heartbeat: [22768]: info: Local status now set to: 'active'
- Feb 5 19:11:48 node1 heartbeat: [22768]: info: Starting child client "/usr/local/ha/lib/heartbeat/pingd -m 100 -d 5s" (102,105)
- Feb 5 19:11:49 node1 heartbeat: [22768]: WARN: No STONITH device configured.
- Feb 5 19:11:49 node1 heartbeat: [22768]: WARN: Shared disks are not protected.
- Feb 5 19:11:49 node1 heartbeat: [22768]: info: Resources being acquired from node2.
- Feb 5 19:11:49 node1 heartbeat: [22794]: info: Starting "/usr/local/ha/lib/heartbeat/pingd -m 100 -d 5s" as uid 102 gid 105 (pid 22794)
在上面这段日志中,由于node2还没有启动,所以会给出“node2: is dead”的警告信息,接下来启动了heartbeat插件pingd,由于我们在ha.cf文件中没有配置STONITH,所以日志里也给出了“No STONITH device configured”的警告提示。
继续看下面的日志:
- Feb 5 19:11:50 node1 IPaddr[22966]: INFO: Resource is stopped
- Feb 5 19:11:50 node1 ResourceManager[22938]: info: Running /usr/local/ha/etc/ha.d/resource.d/IPaddr 192.168.12.135 start
- Feb 5 19:11:50 node1 IPaddr[23029]: INFO: Using calculated nic for 192.168.12.135: eth0
- Feb 5 19:11:50 node1 IPaddr[23029]: INFO: Using calculated netmask for 192.168.12.135: 255.255.255.0
- Feb 5 19:11:51 node1 pingd: [22794]: info: attrd_lazy_update: Connecting to cluster... 5 retries remaining
- Feb 5 19:11:51 node1 IPaddr[23029]: INFO: eval ifconfig eth0:0 192.168.12.135 netmask 255.255.255.0 broadcast 192.168.12.255
- Feb 5 19:11:51 node1 avahi-daemon[2455]: Registering new address record for 192.168.12.135 on eth0.
- Feb 5 19:11:51 node1 IPaddr[23015]: INFO: Success
- Feb 5 19:11:51 node1 Filesystem[23134]: INFO: Resource is stopped
- Feb 5 19:11:51 node1 ResourceManager[22938]: info: Running /usr/local/ha/etc/ha.d/resource.d/Filesystem /dev/sdf1 /data1 ext3 start
- Feb 5 19:11:52 node1 Filesystem[23213]: INFO: Running start for /dev/sdf1 on /data1
- Feb 5 19:11:52 node1 kernel: kjournald starting. Commit interval 5 seconds
- Feb 5 19:11:52 node1 kernel: EXT3 FS on sdf1, internal journal
- Feb 5 19:11:52 node1 kernel: EXT3-fs: mounted filesystem with ordered data mode.
- Feb 5 19:11:52 node1 Filesystem[23205]: INFO: Success
上面这段日志是进行资源的监控和接管,主要完成haresources文件中的设置,在这里是启用集群虚拟IP和挂载磁盘分区
此时,通过ifconfig命令查看主节点的网络配置,可以看到,主节点已经自动绑定了集群的IP地址,在HA集群之外的主机上通过ping命令检测集群IP地址192.168.12.135,已经处于可通状态,也就是该地址变的可用。
同时查看磁盘分区的挂载情况,共享磁盘分区/dev/sdf1已经被自动挂载。
2、启动备用节点的Heartbeat
启动备份节点的Heartbeat,与主节点方法一样,使用如下命令:
- [root@node2 ~]#/etc/init.d/heartbeat start
- 或者执行
- [root@node2 ~]#service heartbeat start
这样就启动了备用节点的heartbeat服务,备用节点的heartbeat日志输出信息与主节点相对应,通过“tail -f /var/log/messages”可以看到如下输出:
- Feb 19 02:52:15 node2 heartbeat: [26880]: info: Pacemaker support: false
- Feb 19 02:52:15 node2 heartbeat: [26880]: info: **************************
- Feb 19 02:52:15 node2 heartbeat: [26880]: info: Configuration validated. Starting heartbeat 3.0.4
- Feb 19 02:52:15 node2 heartbeat: [26881]: info: heartbeat: version 3.0.4
- Feb 19 02:52:15 node2 heartbeat: [26881]: info: Heartbeat generation: 1297766398
- Feb 19 02:52:15 node2 heartbeat: [26881]: info: glib: UDP multicast heartbeat started for group 225.0.0.1 port 694 interface eth0 (ttl=1 loop=0)
- Feb 19 02:52:15 node2 heartbeat: [26881]: info: glib: ucast: write socket priority set to IPTOS_LOWDELAY on eth0
- Feb 19 02:52:15 node2 heartbeat: [26881]: info: glib: ucast: bound send socket to device: eth0
- Feb 19 02:52:15 node2 heartbeat: [26881]: info: glib: ping heartbeat started.
- Feb 19 02:52:15 node2 heartbeat: [26881]: info: glib: ping group heartbeat started.
- Feb 19 02:52:15 node2 heartbeat: [26881]: info: Local status now set to: 'up'
- Feb 19 02:52:16 node2 heartbeat: [26881]: info: Link node1:eth0 up.
- Feb 19 02:52:16 node2 heartbeat: [26881]: info: Status update for node node1: status active
- Feb 19 02:52:16 node2 heartbeat: [26881]: info: Link 192.168.12.1:192.168.12.1 up.
- Feb 19 02:52:16 node2 heartbeat: [26881]: info: Status update for node 192.168.12.1: status ping
- Feb 19 02:52:16 node2 heartbeat: [26881]: info: Link group1:group1 up.
- Feb 19 02:52:16 node2 harc[26894]: info: Running /usr/local/ha/etc/ha.d//rc.d/status status
- Feb 19 02:52:17 node2 heartbeat: [26881]: info: Comm_now_up(): updating status to active
- Feb 19 02:52:17 node2 heartbeat: [26881]: info: Local status now set to: 'active'
二、测试heartbeat的高可用功能
如何才能得知HA集群是否正常工作,模拟环境测试是个不错的方法,在把Heartbeat高可用性集群放到生产环境中之前,需要做如下几个步骤的测试,从而确定HA是否正常工作:
(1)正常关闭和重启主节点的heartbeat
首先在主节点node1上执行“service heartbeat stop”正常关闭主节点的Heartbeat进程,此时通过ifconfig命令查看主节点网卡信息,正常情况下,应该可以看到主节点已经释放了集群的 服务IP地址,同时也释放了挂载的共享磁盘分区,然后查看备份节点,现在备份节点已经接管了集群的服务IP,同时也自动挂载上了共享的磁盘分区。
在这个过程中,使用ping命令对集群服务IP进行测试,可以看到,集群IP一致处于可通状态,并没有任何延时和阻塞现象,也就是说在正常关闭主节点的情况下,主备节点的切换是无缝的,HA对外提供的服务也可以不间断运行。
接着,将主节点heartbeat正常启动,heartbeat启动后,备份节点将自动释放集群服务IP,同时卸载共享磁盘分区,而主节点将再次接管集群服务IP和挂载共享磁盘分区,其实备份节点释放资源与主节点绑定资源是同步进行的。因而,这个过程也是一个无缝切换。
(2)在主节点上拔去网线
拔去主节点连接公共网络的网线后,heartbeat插件ipfail通过ping测试可以立刻检测到网络连接失败,接着自动释放资源,而就在 此时,备用节点的ipfail插件也会检测到主节点出现网络故障,在等待主节点释放资源完毕后,备用节点马上接管了集群资源,从而保证了网络服务不间断持 续运行。
同理,当主节点网络恢复正常时,由于设置了“auto_failback on”选项,集群资源将自动从备用节点切会主节点。
(3)关闭主节点的系统
在主节点拔去电源后,备用节点的heartbeat进程会立刻收到主节点已经shutdown的消息,备用节点就开始进行资源的接管,这种情况其实和主节点网络故障的现象类似。
(4)让主节点系统内核崩溃
当主节点系统崩溃后,网络也就失去了响应,那么备用节点的heartbeat进程就会立刻检测到主节点网络故障,然后进行资源切换,但是由于主 节点系统内核崩溃,导致自身不能卸载所占有的资源,例如共享磁盘分区、集群服务IP等,那么此时如果没有类似Stonith设备的话,就会出现资源争用的 情况,但是如果有Stonith设备,Stonith设备会首先将故障的主节点电源关闭或者重启此节点等操作,这样就让主节点释放了集群资源,当 Stonith设备完成所有操作时,备份节点才拿到接管主节点资源的所有权,从而接管主节点的资源。