下面,就要使用Linux Heartbeat,在CentOS上可以通过yum的安装:
[root@www.linuxidc.com1 ~]# yum install heartbeat
Linux Heartbeat通过配置文件配置,配置文件均位于/etc/ha.d/目录下,一个Heartbeat集群节点需要3个配置文件:“ha.cf”、”authkeys”、“haresources”,这都需要手动创建,让我们分别来看这3个配置文件的内容:
“ha.cf”:
serial /dev/ttyS0 #使用串口,串口为/dev/ttyS0
baud 19200 #通讯使用的波特率
auto_failback on
autojoin none
warntime 5
deadtime 15
initdead 60
keepalive 1
node www.linuxidc.com1.localdomain #节点1
node www.linuxidc.com2.localdomain #节点2
这个文件详细怎么配置可以参考:
“authkeys”:
auth 1
1 crc
注意,这个文件必须是600权限,所以新建文件后需要运行命令:
[root@www.linuxidc.com1 ~]# chmod 600 authkeys
这个文件详细怎么配置可以参考:
http://www.linux-ha.org/doc/users-guide/_the_literal_authkeys_literal_file.html
http://www.linux-ha.org/authkeys
“haresources”:
www.linuxidc.com1.localdomain 192.168.2.100 httpd
这是www.linuxidc.com1这个虚拟机的配置,表明Heartbeat使用的集群IP为“192.168.2.100”,并且,Heartbeat管理的服务是httpd,因为我们希望将两台服务器的Apache进行一个互备。
最后我们还需要把Heartbeat注册为系统服务:
[root@www.linuxidc.com1 ~]# chkconfig --list heartbeat
[root@www.linuxidc.com1 ~]# chkconfig heartbeat on
修改完Heartbeat的配置文件后,通过reload命令重新加载:
[root@www.linuxidc.com1 ~]# service heartbeat reload
我在www.linuxidc.com1和www.linuxidc.com2下的/var/www/html下放置了不同的index.html文件,如果我直接访问Heartbeat集群IP:http://192.168.2.100,看到的页面是www.linuxidc.com2上的页面:
这时,强行杀死www.linuxidc.com2虚拟机进程,刷新页面马上变成了www.linuxidc.com1上的页面:
再把www.linuxidc.com2虚拟机启动起来后,刷新页面也会再切回到www.linuxidc.com2的页面,因为www.linuxidc.com1将资源重新交还到www.linuxidc.com2手中,这个时候在后台日志中可以看到这样的信息:
Mar 24 11:23:46 www.linuxidc.com1 heartbeat: [19364]: info: Heartbeat restart on node www.linuxidc.com2.localdomain
Mar 24 11:23:46 www.linuxidc.com1 heartbeat: [19364]: info: Link www.linuxidc.com2.localdomain:/dev/ttyS0 up.
Mar 24 11:23:46 www.linuxidc.com1 heartbeat: [19364]: info: Status update for node www.linuxidc.com2.localdomain: status up
Mar 24 11:23:46 www.linuxidc.com1 harc[20082]: [20088]: info: Running /etc/ha.d/rc.d/status status
Mar 24 11:23:48 www.linuxidc.com1 heartbeat: [19364]: info: Status update for node www.linuxidc.com2.localdomain: status active
Mar 24 11:23:48 www.linuxidc.com1 harc[20094]: [20100]: info: Running /etc/ha.d/rc.d/status status
Mar 24 11:23:48 www.linuxidc.com1 heartbeat: [19364]: info: remote resource transition completed.
Mar 24 11:23:48 www.linuxidc.com1 heartbeat: [19364]: info: www.linuxidc.com1.localdomain wants to go standby [foreign]
Mar 24 11:23:49 www.linuxidc.com1 heartbeat: [19364]: info: standby: www.linuxidc.com2.localdomain can take ourforeign resources
Mar 24 11:23:49 www.linuxidc.com1 heartbeat: [20106]: info: give up foreign HA resources (standby).
Mar 24 11:23:49 www.linuxidc.com1 heartbeat: [20106]: info: foreign HA resource release completed (standby).
Mar 24 11:23:49 www.linuxidc.com1 heartbeat: [19364]: info: Local standby process completed [foreign].


