heartbeat 编译安装配置

时间:2025-03-20 10:35:37

一、heartbeat介绍

heartbeat是HA高可用集群的一个重要组件,heartbeat实现了资源转移和心跳信息传递。它的常用组合方式为heartbeat v1,heartbeat v2+crm,heartbeat v3 + pacemaker,目前版本为v3版本。

二、编译前准备

heartbeat官方站点http://hg.linux-ha.org/

  Cluster Glue官方站点https://github.com/ClusterLabs/cluster-glue

  Resource Agents官方站点 https://github.com/ClusterLabs/resource-agents

node1:192.168.0.15

node2:192.168.0.16

配置集群前提:

(1)各节点时间一致,便于心跳信息传递,使用ntp实现

(2)节点间需要通过主机名互相通信,必须解析主机至IP地址

(a)建议名称解析功能使用hosts文件来实现

(b)通信中使用的名字与节点名字必须保持一致 “uname -n” 或hostname展示出的名字保持一致

(3)考虑仲裁设备是否会用到

(4)建立各节点之间的root用户能够给予密钥认证

(5)定义为集群中的资源,不能开机启动

#使用ntpdate命令同步时间,并建立周期性任务
#可使用任意节点服务器作为ntp时间服务器,如各节点可上公网,可直接指定公网ntp服务器 、安装ntp
[root@node2 ~]# yum install -y ntp
[root@node2 ~]# vim /etc/ntp.conf #修改配置文件允许本网段客户端获取地址
将下面的语句
restrict default kod nomodify notrap nopeer noquery
修改为
restrict default nomodify
restrict 192.168.0.0 mask 255.255.255.0 nomodify [root@node2 ~]# service ntpd start
Starting ntpd: [ OK ] 查看同步过程
[root@node2 ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*202.118.1.81 202.118.1.47 u 92.249 8.602 0.714
202.112.31.197 .INIT. u - 0.000 0.000 0.000 、客户端创建周期任务,每3秒同步时间
[root@node1 ~]# crontab -e
*/ * * * * /usr/sbin/ntpdate 192.168.0.16 &> /dev/null
[root@node1 ~]# service crond start #手动同步成功,因ntp一般为自动,手动前kill掉所有ntp进程即可
[root@node1 ~]# ntpdate 192.168.0.16
Nov :: ntpdate[]: adjust time server 192.168.0.16 offset -0.004440 sec 、时间同步
[root@node2 ~]# date; ssh 192.168.0.15 'date'
Mon Nov :: CST
root@192.168.0.15's password:
Mon Nov :: CST [root@node2 ~]# vim /etc/hosts
192.168.0.15 node1
192.168.0.16 node2 、生成密钥对
[root@node1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
a8:ad:2c::::ff:::9d::::ae:da:c9 root@centfils
The key's randomart image is:
+--[ RSA ]----+
| |
| |
| |
| . = |
| * S |
|.. o o |
|+ . . o o o |
|+ ooo*.. + |
| o +*E+ |
+-----------------+ 、把公钥传输至远程服务器对应用户的家目录
[root@node1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.0.16
The authenticity of host '192.168.0.16 (192.168.0.16)' can't be established.
RSA key fingerprint is e5::6c:f7:c0::3d:0b::b6:1e::0d::8b:.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.16' (RSA) to the list of known hosts.
root@192.168.0.16's password:
Now try logging into the machine, with "ssh 'root@192.168.0.16'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. 、测试
[root@node2 ~]# date; ssh root@192.168.0.15 'date'
Mon Nov :: CST
Mon Nov :: CST

准备工作

三、编译安装  

 1、安装依赖包(node1和node2同步安装)

[root@node2 ~]# yum -y install autoconf automake gcc-c++ asciidoc libxslt-devel libtool libtool-ltdl-devel libxml2 libxml2-devel bzip2-devel glib2-devel mercurial *openssl* net-snmp OpenIPMI flex bison e2fsprogs-devel

  2、源码编译安装

下载地址http://linux-ha.org/wiki/Download
#下载heartbeat
[root@node2 ~]# wget http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/958e11be8686.tar.bz2
#下载cluster glue
[root@node2 ~]# wget http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2
#下载cluster resource agents 注意:需要*
[root@node1 ~]# wget https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz

  3、创建用户与组

[root@node1 ~]# groupadd haclient
[root@node1 ~]# useradd -g haclient hacluster -M -s /sbin/nologin

 4、编译

cluster glue
[root@node1 ~]# tar xf 0a7add1d9996.tar.bz2
[root@node1 ~]# cd Reusable-Cluster-Components-glue--0a7add1d9996/
[root@node1 Reusable-Cluster-Components-glue--0a7add1d9996]# ./autogen.sh
]# ./configure --prefix=/usr/local/heartbeat --sysconfdir=/etc/heartbeat libdir=/usr/local/heartbeat/lib64 LIBS='/lib64/libuuid.so.1' --with-daemon-user=hacluster --with-daemon-group=haclient
#LIBS如果是32位系统自行更改
]# make && make install resource agents
[root@node1 ~]# tar xf resource-agents-3.9..tar.gz
[root@node1 ~]# cd resource-agents-3.9.
[root@node1 resource-agents-3.9.]# ./autogen.sh
[root@node1 resource-agents-3.9.]# ./configure --prefix=/usr/local/heartbeat --sysconfdir=/etc/heartbeat libdir=/usr/local/heartbeat/lib64 CFLAGS=-I/usr/local/heartbeat/include LDFLAGS=-L/usr/local/heartbeat/lib64 LIBS='/lib64/libuuid.so.1' --with-daemon-user=hacluster --with-daemon-group=haclient
[root@node1 resource-agents-3.9.]# make && make install heartbeat
[root@node1 ~]# tar xf 958e11be8686.tar.bz2
[root@node1 ~]# cd Heartbeat---958e11be8686/
[root@node1 Heartbeat---958e11be8686]# ./bootstrap
]# ./configure --prefix=/usr/local/heartbeat --sysconfdir=/etc/heartbeat CFLAGS=-I/usr/local/heartbeat/include LDFLAGS=-L/usr/local/heartbeat/lib64 LIBS='/lib64/libuuid.so.1' --with-daemon-user=hacluster --with-daemon-group=haclient #编译报错,路径重复,google后得出删除 glue_config.h 中配置文件路径即可
[root@node1 Heartbeat---958e11be8686]# make && make install
../include/config.h::: error: this is the location of the previous definition
gmake[]: *** [strlcpy.lo] Error
gmake[]: Leaving directory `/root/Heartbeat---958e11be8686/replace'
make: *** [all-recursive] Error [root@node1 Heartbeat---958e11be8686]# vim /usr/local/heartbeat/include/heartbeat/glue_config.h
define HA_HBCONF_DIR "/usr/local/heartbeat/etc/ha.d/"
#删除最后一行即上行内容

  5、复制配置文件至/etc//heartbeat/ha.d中

[root@node1 Heartbeat---958e11be8686]# cp doc/ha.cf /etc/heartbeat/ha.d/
[root@node1 Heartbeat---958e11be8686]# cp doc/haresources /etc/heartbeat/ha.d/
[root@node1 Heartbeat---958e11be8686]# cp doc/authkeys /etc/heartbeat/ha.d/

  6、将heartbeat加入系统服务,并开机启动

[root@node1 ~]# chkconfig --add heartbeat
#之后可以用service来进行start|stop操作了
[root@node1 ~]# chkconfig heartbeat on

7、修改认证文件权限为600,不然heartbeat无法工作

[root@node1 ~]# chmod   /etc/heartbeat/ha.d/authkeys

  8、为resource-agents建立脚本软连接

[root@node1 ~]# ln -s /usr/local/heartbeat/usr/lib/ocf /usr/lib/ocf

四、配置文件

、配置authkeys文件,指明启用何种算法,使用何种密钥,本文件须更改权限为400
auth
# crc
sha1 2SIEok+gXAvB6G4seA8mhw
# md5 Hello! 生成随机字符串作为密钥
[root@node2 ~]# openssl rand -base64
2SIEok+gXAvB6G4seA8mhw== 、配置ha.cf文件,定义高可用集群的基本工作方式 定义日志文件位置(二选一) logfacility为将日志交由syslog管理
logfile /var/log/ha-log
#logfacility local0
多长时间发送一次心跳信息,默认为2秒
#keepalive
多长时间宣布某节点死亡,默认30秒
#deadtime
多长时间警告对方心跳信息延迟了,默认10秒
#warntime
第一次死去时间,避免因网络问题导致宣布死亡
#initdead
使用udp694端口传递心跳,并选择哪种方式传递心跳
#udpport
串行线缆传递心跳
#serial /dev/ttyS0 # Linux
#serial /dev/cuaa0 # FreeBSD
#serial /dev/cuad0 # FreeBSD .x
#serial /dev/cua/a # Solaris
串行线缆的工作频率
#baud
广播传递心跳
#bcast eth0 # Linux
#bcast eth1 eth2 # Linux
#bcast le0 # Solaris
#bcast le1 le2 # Solaris
多播传递心跳,网卡必须支持多播,ifconfig | grep MULTICAST
mcast eth0 225.0.0.1 #端口694,TTL为1,不允许回传为0
#启用网卡支持多播
[root@node1 ha.d]# ip link set eth0 multicast on 单薄传递心跳
#ucast eth0 192.168.1.2
自动故障转回
auto_failback on
指明节点
#node ken3
#node kathy
node node1
node node2
指明网关为ping node设备(仲裁设备)
#ping 10.10.10.254
ping 192.168.0.1
指明一个组为ping node设备(仲裁设备)
#ping_group group1 10.10.10.254 10.10.10.253
指明节点间传送的压缩算法
compression bz2
指明节点间传送数据压缩的最小数据为2KB
compression_threshold 、配置haresources文件,定义集群资源
直接加入资源
node1 192.168.0.17//eth0/192.168.0.255 httpd 、将httpd设置为开机不启动
[root@node1 ha.d]# chkconfig httpd off 、启动服务
[root@node1 ~]# service heartbeat start

以上所有配置均所有节点一致