首页 > 代码库 > heartbeat
heartbeat
Heartbeat高可用
By:大官人
一.Heartbeat简介
Heartbeat是Linux-HA工程的一个组件,自1999年开始到现在,发布了众多版本,是目前开源Linux-HA项目最成功的一个例子,在行业内得到了广泛的应用,这里分析的是2007年1月18日发布的版本2.0.8。随着Linux在关键行业应用的逐渐增多,它必将提供一些原来由IBM和SUN这样的大型商业公司所提供的服务,这些商业公司所提供的服务都有一个关键特性,就是高可用集群。
heartbeat (Linux-HA)的工作原理:heartbeat最核心的包括两个部分,心跳监测部分和资源接管部分,心跳监测可以通过网络链路和串口进行,而且支持冗余链路,它们之间相互发送报文来告诉对方自己当前的状态,如果在指定的时间内未收到对方发送的报文,那么就认为对方失效,这时需启动资源接管模块来接管运 行在对方主机上的资源或者服务。
二.安装配置heartbeat
2.1安装heartbeat
yum install -y heartbeat
2.2修改权限authkeys
chmod 600 /etc/ha.d/authkeys
注:heartbeat的主从配置文件要保持一致,这里在主从节点上传同样的文件
2.3ha.cf注解
debugfile /var/log/ha-debug logfile /var/log/ha-log logfacility local1 keepalive 2 deadtime 30 warntime 10 initdead 120 #bcast eth1 mcast eth2 225.0.0.111 694 1 0 #采用多播心跳只需要修改eht2为自己的心跳网卡 auto_failback on #主机从宕机状态下回复是否重新接管 node server1 #主服务器的主机名 node server2 #从服务器的主机名 crm no
2.4haresoures注解
#daguanren services servernameIPaddr::vip/netmask/netcar servername主机名 Ipaddr脚本名称 vip参数-虚拟ip netmask 参数-子网掩码 betcar 参数-要添加vip的网卡
三.实战配置vip切换
3.1实验环境
master主机 eth0网卡:192.168.44.128(vip网卡) eth1网卡:192.168.48.128(心跳网卡)
slave主机 eth0网卡:192.168.44.129(vip网卡) eth1网卡:192.168.48.129(心跳网卡)
vip:192.168.44.100
3.2配置ha.cf
root@heartbeat-master /etc/ha.d18:07:49 # cat ha.cf debugfile /var/log/ha-debug logfile /var/log/ha-log logfacility local1 keepalive 2 deadtime 30 warntime 10 initdead 30 #bcast eth1 mcast eth1 225.0.0.111 694 1 0 #才用多播心跳只需要修改eht2为自己的心跳网卡 auto_failback on #主机从宕机状态下回复是否重新接管 node heartbeat-master #主服务器的主机名 node heartbeat-slave #从服务器的主机名 crm no
3.3配置haresources
root@heartbeat-master /etc/ha.d18:08:30 # cat haresources #daguanren services heartbeat-masterIPaddr::192.168.44.100/24/eth0
3.4主从启动heartbeat查看
1)主节点
root@heartbeat-master /etc/ha.d18:11:11 # service heartbeat start Starting High-Availabilityservices: INFO: Resource is stopped Done. root@heartbeat-master /etc/ha.d18:12:15 # ip a 1: lo: <LOOPBACK,UP,LOWER_UP>mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:c2:37:fa brd ff:ff:ff:ff:ff:ff inet 192.168.44.128/24 brd 192.168.44.255 scope global eth0 inet192.168.44.100/24 brd 192.168.44.255 scope global secondary eth0 inet6 fe80::20c:29ff:fec2:37fa/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:37:04 brd ff:ff:ff:ff:ff:ff inet 192.168.48.128/24 brd 192.168.48.255 scope global eth1 inet6 fe80::20c:29ff:fec2:3704/64 scope link valid_lft forever preferred_lft forever
2)从节点
root@heartbeat-slave /etc/ha.d18:07:53 # service heartbeat start Starting High-Availabilityservices: INFO: Resource is stopped Done. root@heartbeat-slave /etc/ha.d18:11:58 # ip a 1: lo: <LOOPBACK,UP,LOWER_UP>mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:b7:b2:cf brd ff:ff:ff:ff:ff:ff inet 192.168.44.129/24 brd 192.168.44.255 scope global eth0 inet6 fe80::20c:29ff:feb7:b2cf/64 scope link valid_lft forever preferred_lft forever 3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:b7:b2:d9 brd ff:ff:ff:ff:ff:ff inet 192.168.48.129/24 brd 192.168.48.255 scope global eth1 inet6 fe80::20c:29ff:feb7:b2d9/64 scope link valid_lft forever preferred_lft forever
3.5模拟主节点宕机测试vip漂移
1)主节点
root@heartbeat-master /etc/ha.d 18:12:22 # service heartbeat stop Stopping High-Availabilityservices: Done. root@heartbeat-master /etc/ha.d18:15:15 # ip a 1: lo: <LOOPBACK,UP,LOWER_UP>mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:37:fa brd ff:ff:ff:ff:ff:ff inet 192.168.44.128/24 brd 192.168.44.255 scope global eth0 inet6 fe80::20c:29ff:fec2:37fa/64 scope link valid_lft forever preferred_lft forever 3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:c2:37:04 brd ff:ff:ff:ff:ff:ff inet 192.168.48.128/24 brd 192.168.48.255 scope global eth1 inet6 fe80::20c:29ff:fec2:3704/64 scope link valid_lft forever preferred_lft forever
2)从节点
root@heartbeat-slave /etc/ha.d18:13:53 # ip a 1: lo: <LOOPBACK,UP,LOWER_UP>mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:b7:b2:cf brd ff:ff:ff:ff:ff:ff inet192.168.44.129/24 brd 192.168.44.255 scope global eth0 inet192.168.44.100/24 brd 192.168.44.255 scope global secondary eth0 inet6 fe80::20c:29ff:feb7:b2cf/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:b7:b2:d9 brd ff:ff:ff:ff:ff:ff inet 192.168.48.129/24 brd 192.168.48.255 scope global eth1 inet6 fe80::20c:29ff:feb7:b2d9/64 scope link valid_lft forever preferred_lft forever
3.6启动主节点heartbeat模拟故障恢复
1)主节点
root@heartbeat-master /etc/ha.d 18:15:19 # service heartbeat start Starting High-Availabilityservices: INFO: Resource is stopped Done. root@heartbeat-master /etc/ha.d18:16:59 # ip a 1: lo: <LOOPBACK,UP,LOWER_UP>mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:37:fa brd ff:ff:ff:ff:ff:ff inet 192.168.44.128/24 brd 192.168.44.255 scope global eth0 inet192.168.44.100/24 brd 192.168.44.255 scope global secondary eth0 inet6 fe80::20c:29ff:fec2:37fa/64 scope link valid_lft forever preferred_lft forever 3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:c2:37:04 brd ff:ff:ff:ff:ff:ff inet 192.168.48.128/24 brd 192.168.48.255 scope global eth1 inet6 fe80::20c:29ff:fec2:3704/64 scope link valid_lft forever preferred_lft forever
2)从节点
root@heartbeat-slave /etc/ha.d18:16:53 # ip a 1: lo: <LOOPBACK,UP,LOWER_UP>mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:b7:b2:cf brd ff:ff:ff:ff:ff:ff inet 192.168.44.129/24 brd 192.168.44.255 scope global eth0 inet6 fe80::20c:29ff:feb7:b2cf/64 scope link valid_lft forever preferred_lft forever 3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:b7:b2:d9 brd ff:ff:ff:ff:ff:ff inet 192.168.48.129/24 brd 192.168.48.255 scope global eth1 inet6 fe80::20c:29ff:feb7:b2d9/64 scope link valid_lft forever preferred_lft forever
四.实战配置heartbeat控制nginx
4.1配置haresources
root@heartbeat-slave /etc/ha.d18:33:14 # cat haresources #daguanren services heartbeat-masterIPaddr::192.168.44.100/24/eth0 nginxd #nginxd文件必须在/etc/init.d/下或者/etc/ha.d/resource.d下
4.2分别重启主从节点的heartbeat
1)主节点
root@heartbeat-master /etc/ha.d18:35:44 # service heartbeat start Starting High-Availabilityservices: INFO: Resource is stopped Done. oot@heartbeat-master /etc/ha.d18:36:05 # /etc/init.d/nginxd status nginx starting [ OK ] 2)从节点 root@heartbeat-slave /etc/ha.d18:35:34 # service heartbeat start Starting High-Availabilityservices: INFO: Resource is stopped Done. root@heartbeat-slave /etc/ha.d18:35:42 # /etc/init.d/nginxd status nginx not starting [FAILED]
4.3停止主节点heartbeat模拟主节点故障
1)主节点
root@heartbeat-master /etc/ha.d18:36:07 # service heartbeat stop Stopping High-Availabilityservices: Done. root@heartbeat-master /etc/ha.d18:39:22 # ip a 1: lo: <LOOPBACK,UP,LOWER_UP>mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:c2:37:fa brd ff:ff:ff:ff:ff:ff inet 192.168.44.128/24 brd 192.168.44.255 scope global eth0 inet6 fe80::20c:29ff:fec2:37fa/64 scope link valid_lft forever preferred_lft forever 3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:c2:37:04 brd ff:ff:ff:ff:ff:ff inet 192.168.48.128/24 brd 192.168.48.255 scope global eth1 inet6 fe80::20c:29ff:fec2:3704/64 scope link valid_lft forever preferred_lft forever root@heartbeat-master /etc/ha.d18:39:25 # /etc/init.d/nginxd status nginx not starting [FAILED]
2)从节点
root@heartbeat-slave /etc/ha.d18:39:03 # ip a 1: lo: <LOOPBACK,UP,LOWER_UP>mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:b7:b2:cf brd ff:ff:ff:ff:ff:ff inet 192.168.44.129/24 brd 192.168.44.255 scope global eth0 inet 192.168.44.100/24 brd 192.168.44.255 scopeglobal secondary eth0 inet6 fe80::20c:29ff:feb7:b2cf/64 scope link valid_lft forever preferred_lft forever 3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:b7:b2:d9 brd ff:ff:ff:ff:ff:ff inet 192.168.48.129/24 brd 192.168.48.255 scope global eth1 inet6 fe80::20c:29ff:feb7:b2d9/64 scope link valid_lft forever preferred_lft forever root@heartbeat-slave /etc/ha.d18:41:05 # /etc/init.d/nginxd status tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5872/nginx nginx starting [ OK ]
4.4启动主节点heartbeat模拟故障恢复
1)主节点
root@heartbeat-master /etc/ha.d18:39:35 # service heartbeat start Starting High-Availabilityservices: INFO: Resource is stopped Done. root@heartbeat-master /etc/ha.d18:48:45 # ip a 1: lo: <LOOPBACK,UP,LOWER_UP>mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:c2:37:fa brd ff:ff:ff:ff:ff:ff inet 192.168.44.128/24 brd 192.168.44.255 scope global eth0 inet192.168.44.100/24 brd 192.168.44.255 scope global secondary eth0 inet6 fe80::20c:29ff:fec2:37fa/64 scope link valid_lft forever preferred_lft forever 3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:c2:37:04 brd ff:ff:ff:ff:ff:ff inet 192.168.48.128/24 brd 192.168.48.255 scope global eth1 inet6 fe80::20c:29ff:fec2:3704/64 scope link valid_lft forever preferred_lft forever root@heartbeat-master /etc/ha.d18:48:51 # /etc/init.d/nginxd status nginx starting [ OK ]
2)从节点
root@heartbeat-slave /etc/ha.d18:49:06 # ip a 1: lo: <LOOPBACK,UP,LOWER_UP>mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:b7:b2:cf brd ff:ff:ff:ff:ff:ff inet 192.168.44.129/24 brd 192.168.44.255 scope global eth0 inet6 fe80::20c:29ff:feb7:b2cf/64 scope link valid_lft forever preferred_lft forever 3: eth1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:b7:b2:d9 brd ff:ff:ff:ff:ff:ff inet 192.168.48.129/24 brd 192.168.48.255 scope global eth1 inet6 fe80::20c:29ff:feb7:b2d9/64 scope link valid_lft forever preferred_lft forever root@heartbeat-slave /etc/ha.d18:49:08 # /etc/init.d/nginxd status nginx not starting [FAILED]
heartbeat