高可用集群之heartbeat

时间:2022-02-25 03:17:36

heartbeat

高可用 热备
Heartbeat 项目是 Linux-HA 工程的一个组成部分,它实现了一个高可用集群系统。心跳服务和集群通信是高可用集群的两个关键组件,在 Heartbeat 项目里,由 heartbeat 模块实现了这两个功能。

环境

系统:redhat6.5
节点:server1
火墙:关闭
selinux:disabled

heartbeat

(1)安装配置
yum install heartbeat-* -y
cd /etc/ha.d/ ##heartbeat主目录
rpm -q heartbeat -d ##查看heartbeat的安装路径

[root@server1 ha.d]# rpm -q heartbeat -d
/usr/share/doc/heartbeat-3.0.4/AUTHORS
/usr/share/doc/heartbeat-3.0.4/COPYING
/usr/share/doc/heartbeat-3.0.4/COPYING.LGPL
/usr/share/doc/heartbeat-3.0.4/ChangeLog
/usr/share/doc/heartbeat-3.0.4/README
/usr/share/doc/heartbeat-3.0.4/apphbd.cf
/usr/share/doc/heartbeat-3.0.4/authkeys
/usr/share/doc/heartbeat-3.0.4/ha.cf
/usr/share/doc/heartbeat-3.0.4/haresources
/usr/share/man/man1/cl_status.1.gz
/usr/share/man/man1/hb_addnode.1.gz
/usr/share/man/man1/hb_delnode.1.gz
/usr/share/man/man1/hb_standby.1.gz
/usr/share/man/man1/hb_takeover.1.gz
/usr/share/man/man5/authkeys.5.gz
/usr/share/man/man5/ha.cf.5.gz
/usr/share/man/man8/apphbd.8.gz
/usr/share/man/man8/heartbeat.8.gz

cd /usr/share/doc/heartbeat-3.0.4 ##源码路径

##复制配置文件,认证文件,资源文件到heartbeat配置目录
cp /usr/share/doc/heartbeat-3.0.4/ha.cf /etc/ha.d/ ##配置文件
cp /usr/share/doc/heartbeat-3.0.4/authkeys /etc/ha.d/ ##认证文件
cp /usr/share/doc/heartbeat-3.0.4/haresources /etc/ha.d/ ##资源文件

##heartbeat基础配置
vim /etc/ha.d/ha.cf ##heartbeat主配置文件

##时间单位:秒
48 keepalive 2 ##响应频率
56 deadtime 30 ##未响应时间自动重启
61 warntime 10 ##警告时间
71 initdead 60 ##启动时间

76 udpport 726 ##UDP接口
91 bcast eth0 ##广播网卡 # Linux
157 auto_failback on ##回跳开启

##结点
211 node server1
212 node server4

220 ping 172.25.32.250 ##ping

##检测和处理网络故障
253 respawn hacluster /usr/lib64/heartbeat/ipfail

##自动生成组和用户
259 apiauth ipfail gid=haclient uid=hacluster

vim /etc/ha.d/authkeys ##认证文件

auth 1      ##使用crc认证   
1 crc
#2 sha1 HI!
#3 md5 Hello!

vim /etc/ha.d/haresources ##资源文件

##虚拟主机 IPaddr::172.25.32.100/24/eth0 
##httpd服务 httpd
##添加的资源
server1 IPaddr::172.25.32.100/24/eth0 httpd
##基本是脚本文件,存在于 /etc/init.d 和 /etc/ha.d/resource.d 目录中

chmod 600 /etc/ha.d/authkeys ##认证文件权限600
scp authkeys haresources ha.cf server4:/etc/ha.d/ ##复制到另一台主机
/etc/init.d/heartbeat start ##启动服务

Starting High-Availability services: INFO:  Resource is stopped
Done.

tail -f /var/log/messages ##查看日志

(2)测试
server1:
/etc/init.d/heartbeat start
/etc/init.d/heartbeat stop
/etc/init.d/heartbeat start

server4:
ip addr

浏览器 172.25.32.100
高可用集群之heartbeat
server1关闭后:

[root@server1 ha.d]# /etc/init.d/heartbeat stop
Stopping High-Availability services: Done.

发现虚拟ip转移到了server4上

[root@server4 ha.d]# ip addr
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 52:54:00:8a:1f:4f brd ff:ff:ff:ff:ff:ff
inet 172.25.32.4/24 brd 172.25.32.255 scope global eth0
inet 172.25.32.100/24 brd 172.25.32.255 scope global secondary eth0
inet6 fe80::5054:ff:fe8a:1f4f/64 scope link
valid_lft forever preferred_lft forever

高可用集群之heartbeat