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

一、环境说明

#uname -r3.10.0-862.9.1.el7.x86_64# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core)

双节点互备集群:

节点1:node1 node1.pjy.com 192.168.146.141

节点2:node2 node2.pjy.com 192.168.146.150

VIP地址:192.168.146.222

二、前提条件准备

在每个节点上做以下配置:

1.关闭firewalld

2.关闭selinux

3.同步时间

4.配置主机名

5.配置主机间ssh互信,免密钥认证

以下配置是在节点1(141)主机上的配置,节点2(150)省略:

systemctl stop firewalldsystemctl disable firewalldsetenforce 0sed -i s#SELINUX=enforcing#SELINUX=disabled# /etc/selinux/configntpdate 10.0.0.100hostname node1.pjy.comecho "node1.pjy.com" >> /etc/hostnamessh-keygen -q -t rsa -N '' -f ~/.ssh/id_rsassh-copy-id root@192.168.146.150echo -e "192.168.146.141 node1.pjy.com node1\n192.168.146.150 node2.pjy.com node2" >> /etc/hosts

三、开始安装

1.安装基础环境包:

yum install gcc gcc-c++ autoconf automake libtool glib2-devel libxml2-devel bzip2 bzip2-devel e2fsprogs-devel libxslt-devel libtool-ltdl-devel asciidoc

2.创建用户和组

groupadd haclientuseradd -g haclient hacluster

3.下载软件包:Reusable-Components-glue、resource-agents、heartbeat

http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/958e11be8686.tar.bz2http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz

4.安装glue

tar xf 0a7add1d9996.tar.bz2cd Reusable-Cluster-Components-glue--0a7add1d9996/./autogen.sh./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'make && make installecho $?cd ..

5.安装Resource Agents

tar xf v3.9.6.tar.gzcd resource-agents-3.9.6/./autogen.sh ./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'make && make installecho $?cd ..

6.安装HeartBeat

tar xf 958e11be8686.tar.bz2cd Heartbeat-3-0-958e11be8686/./bootstrapexport CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'make && make installecho $?

7.配置网卡支持插件文件

mkdir -pv /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/cp /usr/lib/ocf/lib/heartbeat/ocf-* /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/

#注意:一般启动时会报错因为 ping和ucast这些配置都需要插件支持 需要将lib64下面的插件软连接到lib目录 才不会抛出异常

ln -svf /usr/local/heartbeat/lib64/heartbeat/plugins/RAExec/* /usr/local/heartbeat/lib/heartbeat/plugins/RAExec/ln -svf /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/

#以上在节点1上安装完成,在节点2上执行以上同样的步骤,此处省略...

#下面开始配置

四、配置heartbeat

#拷贝三个模版配置文件到 /usr/local/heartbeat/etc/ha.d 目录下 

cp doc/{ha.cf,haresources,authkeys} /usr/local/heartbeat/etc/ha.d/

1.配置ha.cf配置文件

#该配置文件用于配置 心跳的核心配置

vim /usr/local/heartbeat/etc/ha.d/ha.cf debugfile /var/log/ha-debug  #表示调试的日志文件 一般测试建议开启logfile /var/log/ha-log  #表示系统的的日志文件路径logfacility     local0  #表示使用系统日志与上面只能开启一个keepalive 2  #主备之间的心跳间隔时间单位:sdeadtime 30  #表示如果连接对方30s还无法连接,表示节点死亡需要考虑vip转移warntime 10  #表示10s时间未收到心跳时发出警告日志initdead 120  #有时机器启动后需要一段时间网卡才能正常工作 需要预留一定的时间后,再开始判断心跳检测udpport 694  #多播的udp端口#baud   19200  #串行端口的波特率#serial /dev/ttyS0      # Linux  #串口的接口名#serial /dev/cuaa0      # FreeBSD#serial /dev/cuad0      # FreeBSD 6.x#serial /dev/cua/a      # Solaris#bcast  eth0            # Linux #传播心跳的广播网卡信息#bcast  eth1 eth2       # Linux#bcast  le0             # Solaris#bcast  le1 le2         # Solaris#mcast eth0 225.0.0.1 694 1 0  #多播传送心跳的网卡 多播组 端口 跃点数 是否回环内传送ucast ens33 192.168.146.150 #设置单播心跳,设置对方的ip地址,此处使用单播auto_failback on  #表示如果主机停止后,从机接管设置为on当主机从新启动后,主机立即接管vip off从机不会释放vip给主机node    node1.pjy.com  #配置主从的节点信息,要与uname -n保持一致node    node2.pjy.com##############################################使用ping模式 有时当主机挂掉或者heartbeat挂掉后vip才会转移  有时出现某个进程挂掉 切换需要使用脚本 #ping模式用于测试 如果网卡ping不同 某个主机 就认为当前断网 需要转移vip #respawn root     /usr/local/heartbeat/libexec/heartbeat/ipfail 表示当ping不通时 自动调用 ipfail这个脚本#apiauth ipfail gid=haclient uid=hacluster 表示有权限操作ipfail脚本的组和用户############################################ping 192.168.146.2#ping组的所有主机#ping_group group1 10.10.10.254 10.10.10.253#respawn userid /path/name/to/run#指定与heartbeat一同启动和关闭的进程,该进程被自动监视,遇到故障则重新启动。最常用的进程是ipfail,该进程用于检测和处理网络故障,需要配合ping语句指定的ping node来检测网络连接。如果你的系统是64bit,请注意该文件的路径。#respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail#apiauth ipfail gid=haclient uid=hacluster

2.配置authkeys配置文件

#该文件表示发送心跳时 机器用于验证的key的hash算法,节点之间必须配置成一致的密码

vim /usr/local/heartbeat/etc/ha.d/authkeys
auth 2  #表示使用id为2的验证 下边需要定义一个2的验证算法 2 sha1 1a2b3c  #ID 2的验证加密为shal,并添加密码

#更改权限为600

chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys

3.配置haresources配置文件

#该文件表示资源的管理,如果是主机,当主机启动后自动加载该文件中配置的所有启动资源,资源脚本默认在haresources同级目录下的resource.d目录下

vim /usr/local/heartbeat/etc/ha.d/haresources#指定节点主机名,和VIP地址,以双冒号分隔资源,此处以apache为例进行配置node1.pjy.com  192.168.146.222 apache::/etc/httpd/conf/httpd.conf

4.节点2上准备配置文件

#拷贝三个配置好的文件到节点2上,只需修改ha.cf配置文件中的单播地址为对方地址即可(ucast ens33 192.168.146.141)。

scp authkeys ha.cf haresources root@node2:/usr/local/heartbeat/etc/ha.d/

5.安装httpd资源服务

#在每个节点上安装httpd服务并测试

yum install httpdecho "

node1.pjy.com

" >>/var/www/html/index.htmlsystemctl start httpdcurl 192.168.146.141

#测试httpd服务正常后关闭httpd服务并关闭自启动

systemctl stop httpdsystemctl disable httpd

6.启动服务

#启动每个节点上heartbeat服务

systemctl enable heartbeatsystemctl start heartbeatssh node2 'systemctl start heartbeat'

7.测试结果

# curl http://192.168.146.222

node1.pjy.com

#使用heartbeat自带脚本切换主备节点# /usr/local/heartbeat/share/heartbeat/hb_standbyGoing standby [all].# curl http://192.168.146.222

node2.pjy.com

8.配置NFS共享存储

#在建立NFS的服务器上建立NFS存储:

yum install nfs-utils rpcbindsystemctl start rpcbindsystemctl start nfscat /etc/exports  #建立NFS共享存储/data 192.168.146.0/24(ro)mkdir /data  #建立测试文件echo '

nfs server

' > /data/index.html

#然后修改haresources配置文件添加自动挂载nfs资源:

node1.pjy.com IPaddr::192.168.146.222/24/ens33 Filesystem::192.168.146.151:/data::/var/www/html::nfs::ro apache::/etc/httpd/conf/httpd.conf

#重启heartbeat服务加载配置

systemctl restart heartbeatssh node2 'systemctl restart heartbeat'

#测试是否加载资源:

ip addrnetstat -lntupmount

#资源转移测试:

/usr/local/heartbeat/share/heartbeat/hb_standby