Linux-HA实战(1)— Heartbeat安装

接触Heartbeat主要是由于以前项目中使用了TFS,最近想给nameserver作HA,由于TFS官方用的Heartbeat,因此恰好了解下,参考了网络上不少内容,这里简单记录下。node

内容

 

环境和软件包

个人机器是两台64位的CentOS,其它Linux机器应该差很少能够参考官方的说明。linux

从Heartbeat 2.1.4以后原先的项目被拆分红了三个子项目:cluster-glueresource-agentsheartbeat,致使了配置更加复杂。下面列出了我使用的各个软件包版本并提供了下载(后面具体安装提供了官方的下载地址,这里本身作个备份)。c++

  • cluster-glue 1.0.9
  • resource-agents 3.9.2
  • heartbeat 3.0.5

 

Heartbeat源码安装

先最小化系统安装:gcc编译环境等。

# yum install gcc gcc-c++ autoconf automake libtool glib2-devel libxml2-develbzip2-devel e2fsprogs-devel libxslt-devel libtool-ltdl-devel make wget docbook-dtds docbook-style-xsl

 

添加 Haclient 组和 Hacluster 帐户。这个用户主要用来配置respawn的。

# groupadd haclient
# useradd -g haclient hacluster -M -s /sbin/nologin

 

安装libaio

cluster-glue依赖的库。libaio是Linux下的一个异步非阻塞接口,它提供了以异步非阻塞方式来读写文件的方式,读写效率比较高。git

# yum install libaio-devel

 

 安装cluster-glue

 glue(胶水的意思)是用来粘合Heartbeat、Pacemake以及Resource Agent的一系列类库、工具的集合。github

# wget http://hg.linux-ha.org/glue/archive/glue-1.0.9.tar.bz2
# tar jxvf glue-1.0.9.tar.bz2
# cd Reusable-Cluster-Components-glue--glue-1.0.9/
# ./autogen.sh
# ./configure --prefix=/usr/local/heartbeat --sysconfdir=/etc/heartbeat libdir=/usr/local/heartbeat/lib64 LIBS='/lib64/libuuid.so.1'
# make & make install l

 

安装Resource Agents

resource-agents为集群资源的访问提供了一系列标准的接口。shell

# wget https://codeload.github.com/ClusterLabs/resource-agents/zip/v3.9.2
# unzip v3.9.2
# cd resource-agents-3.9.2/
# ./autogen.sh
# ./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'
//创建一个软链接,避免编译时找不到所须要的包
# ln -s /usr/local/heartbeat/lib64/* /lib64/

注意:LDFLAGS的空格,不然configure时不会报错但make时报错。数据库

 

安装Heartbeat

# wget http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/7e3a82377fa8.tar.bz2
# tar jxvf 7e3a82377fa8.tar.bz2
# cd Heartbeat-3-0-7e3a82377fa8/
# ./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'
# vi  /usr/local/heartbeat/include/heartbeat/glue_config.h
 // 删除 glue_config.h 最后一行定义的配置文件路径,避免编译时产生的路径重复定义错误,Shift+g 跳到末行,dd删除
# make && make install

 

配置文件修改

将配置文件复制到 /etc/heartbeat/ 下,并使用sed 修改路径bootstrap

# cp doc/ha.cf /etc/heartbeat/ha.d/
# cp doc/haresources /etc/heartbeat/ha.d/
# cp doc/authkeys /etc/heartbeat/ha.d/
# chkconfig --add heartbeat
# chkconfig heartbeat on
# chmod 600 /etc/heartbeat/ha.d/authkeys
# sed -i 's#/usr/lib/ocf#/usr/local/heartbeat/usr/lib/ocf#g' /etc/heartbeat/ha.d/shellfuncs
# sed -i 's#/usr/lib/ocf#/usr/local/heartbeat/usr/lib/ocf#g' /etc/heartbeat/ha.d/resource.d/hto-mapfuncs
# sed -i 's#/usr/lib/ocf#/usr/local/heartbeat/usr/lib/ocf#g' /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/ocf-shellfuncs

 

创建Resource-Agent 的脚本软链接,避免Heartbeat 找不到路径而没法工做

# ln -s /usr/local/heartbeat /usr/lib/ocf

 

Heartbeat yum安装

推荐经过yum来安装Heartbeat,由于不管是tfs官方仍是网上一些参考资料都是采用这种方式,这样会少不少路径配置方面的问题。但CentOS默认状况经过yum install heartbeat好像找不到相应的包,须要先下载并安装epel包:api

# wget http://mirrors.sohu.com/fedora-epel/6/i386/epel-release-6-8.noarch.rpm
# rpm -ivh epel-release-6-8.noarch.rpm 
# yum install heartbeat*

 

Heartbeat配置

Heartbeat的配置主要涉及到ha.cf、haresources、authkeys这三个文件。其中ha.cf是主配置文件,haresource用来配置要让Heartbeat托管的服务,authkey是用来指定Heartbeat的认证方式,具体参考:http://ixdba.blog.51cto.com/2895551/548625bash

须要注意几点:

  • authkeys的配置方式:

# auth 1      //认证序号1
# 1 md5 password  //序号1 采用MD5 后面是密钥

 auth 后面填写序号,可任意填写,但第二行开头必须为序号名,而后为验证方式,支持三种( crc md5 sha1 )方式验证,最后面是自定义密钥。

 

  • 须要保证authkeys有相应的读写权限:
# chmod 600 /etc/heartbeat/ha.d/authkeys

 

  • 对于HA系统来讲主从节点的机器时间同步时很重要的。

 

  • 服务要想被Heartbeat托管则必须写成能够经过start/stop来启动和关闭的脚本,而后放在/etc/init.d或者Heartbeat本身的ha.d/resource.d目录中。

 

  • HA的备份节点也须要安装Heartbeat,能够同错scp命令来复制配置文件:

# scp –r node1:/etc/heartbeat/ha.d/*  /etc/heartbeat/ha.d/  
//node1为主节点的主机名(uname -n)

 

测试

  • 建立测试脚本 在Heartbeat的ha.d/resource.d下以下创建一个脚本:
# vi /etc/heartbeat/ha.d/resource.d/test1

输入以下内容:

#!/bin/bash
logger $0 called with $1
case "$1" in
start)
# Start commands go here
echo "start!!!";
;;
stop)
# Stop commands go herer
echo "stop!!!";
;;
status)
# Status commands go here
echo "status!!!";
;;
esac 

增长相应的权限:

# chmod 755 test1

能够这样执行该脚本:

# ./test1 start

 此处输入图片的描述

 

  • 配置haresource文件
# vi /etc/heartbeat/ha.d/haresources
# 输入下面的内容
# ydhl-test1 test1

其中ydhl-test1为uname –n输出的结果。

 

  • 配置authkeys,参见上段内容。

 

  • 配置ha.cf

debugfile /var/log/ha-debug
# 用于记录heartbeat的调试信息
logfile /var/log/ha-log
# 用于记录heartbeat的日志信息
logfacility local0
keepalive 2
# 设置心跳间隔
watchdog /dev/watchdog
deadtime 30
# 在30秒后宣布节点死亡
warntime 10
# 在日志中发出“late heartbeat“警告以前等待的时间,单位为秒
initdead 120
 # 网络启动时间
udpport 694
# 广播/单播通信使用的udp端口
#baud 19200
#serial /dev/ttyS0
# 使用串口heartbeat
bcast eth0
# 使用网卡eth0发送心跳检测
auto_failback on
# 当主节点从故障中恢复时,将自动切换到主节点
watchdog /dev/watchdog
# 该指令是用于设置看门狗定时器,若是节点一分钟内都没有心跳,那么节点将从新启动
node HA-01
node HA-02
# 集群中机器的主机名,与“uname –n”的输出相同。
ping 192.168.0.254
# ping 网关或路由器来检测链路正常
respawn hacluster /usr/local/heartbeat/lib64/heartbeat/ipfail
# respawn调用 ipfail 来主动进行切换
apiauth ipfail gid=haclient uid=hacluster
# 设置启动ipfail的用户和组

 

  • 备份节点配置,参见上段内容。

 

  • 同步主从节点系统时间:能够经过ntpdate来同步

 

  • 启动Heartbeat

在启动以前用下面的命令在主从节点上面测试一下配置是否正确,ReourceManager在Heartbeat安装目录的share/heartbeat目录下。

# ./ResourceManager listkeys `/bin/uname -n`
测试经过后启动主节点和从节点Heartbeat:
# service heartbeat start

经过查看 /var/log/messages能够看到Hearbeat的不少信息: 

此处输入图片的描述

能够看到咱们前面的测试脚本test1输出的信息。

当经过service heartbeat stop命令中止一个节点的Heartbeat的时候,从日志中能够看到另一个节点已经感知到了: 

此处输入图片的描述

重启后能够看到:

此处输入图片的描述

 

FAQ

Q:为何在/var/log/messages里看到不少下面这样的警告?

 此处输入图片的描述

A :这种状况是由于部署Heartbeat是直接从其它机器上拷贝过来致使的。直接拷过来会致使两个节点上的uuid冲突,解决方法是强制某个Hearbeat从新生成uuid,先中止Heatbeat而后删除hb_uuid这个文件(能够经过find命令查找)重启就行了:

# rm –rf /usr/local/heartbeat/var/lib/heartbeat/hb_uuid

 

Q:什么是“脑裂”问题?

A :采用keepalive等心跳软件,须要注意“脑裂”问题: "在“双机热备”高可用(HA)系统中,当联系2个节点的“心跳线”断开时,原本为一总体、动做协调的HA系统,就分裂成为2个独立的个体。因为相互失去了联系,都觉得是对方出了故障,2个节点上的HA软件像“裂脑人”同样,“本能”地争抢“共享资源”、争起“应用服务”,就会发生严重后果:或者共享资源被瓜分、2边“服务”都起不来了;或者2边“服务”都起来了,但同时读写“共享存储”,致使数据损坏(常见如数据库轮询着的联机日志出错)。

 

Q:启动heartbaet的时候可能会报不少库找不到的错误:

A:能够先经过find命令查找,而后经过创建软链接就能够解决了:

# ln -s /usr/libexec/pacemaker/* /usr/local/heartbeat/lib64/heartbeat/

 

参考连接

[1]高可用方案之脑裂问题探讨

相关文章
相关标签/搜索