源码编译安装Heartbeat

一、Heartbeat介绍


官方站点:http://linux-ha.org/wiki/Main_Page
Heartbeat可以资源(VIP地址及程序服务)从一台有故障的服务器快速的转移到另一台正常的服务器提供服务,heartbeat和keepalived相似,heartbeat可以实现failover功能,但不能实现对后端的健康检查。

 

二、简要规划

 

系统环境

系统

OEL6.6

系统位数

x86_64

内核版本

3.8.13-44.1.1.el6uek.x86_64

软件环境

Heartbeat

Heartbeat 3.0.6

Cluster Glue 1.0.12

Resource Agents 3.9.6

Heartbeat下载地址

http://linux-ha.org/wiki/Download

wget http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/958e11be8686.tar.bz2
wget http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2

wget https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz

系统规划

主机名

IP

db-1-1

192.168.192.21    管理IP

10.0.100.21       心跳IP

192.168.192.100   VIP

db-1-2

192.168.192.22    管理IP

10.0.100.22       心跳IP

192.168.192.100   VIP

基本配置

# cat << EOF > /etc/hosts

127.0.0.1         localhost  localhost.localdomain

192.168.192.21    db-1-1

10.0.100.21       priv-1-1

192.168.192.22    db-1-2

10.0.100.22       priv-1-2

192.168.192.100   vip

EOF

--配置服务器间主机路由
#db-1-1:
route add -host 10.0.100.22 dev eth1
#db-1-2:
route add -host 10.0.100.21 dev eth1
--时间同步
crontab -l
*/5 * * * * `which ntpdate` pool.ntp.org >/dev/null 2>&1

 

三、编译安装

 

--依赖包安装(提前检查好):

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

--源码编译安装:

下载地址:http://linux-ha.org/wiki/Download

--Heartbeat 3.0.6:
# wget http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/958e11be8686.tar.bz2
--Cluster Glue 1.0.12:
# wget http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2
--Resource Agents 3.9.6: 
# wget https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz

--用户组及用户配置

# groupadd haclient
# useradd -g haclient hacluster

编译Cluster Glue

# tar -jxvf cluster-clue-1.0.12.tar.bz2 
# cd Reusable-Cluster-Components-glue--0a7add1d9996/
# ./autogen.sh
# 注:32位系统 LIBS='/lib/libuuid.so.1'
# ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
# make && make install

--卸载

# make uninstall && make clean

编译Resource Agents

 # tar -zxvf resource-agents-3.9.6.tar.gz
# cd resource-agents-3.9.6
# ./autogen.sh
# ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
# make && make install

--卸载
# make uninstall && make clean

编译Heartbeat

# tar -jxvf heartbeat-3.0.6.tar.bz2
# cd Heartbeat-3-0-958e11be8686/
# ./bootstrap
# export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"
# ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
# make && make install

--卸载

# make uninstall && make clean
--heartbeat路径检查

# whereis heartbeat
heartbeat: /usr/lib64/heartbeat /usr/include/heartbeat /usr/libexec/heartbeat /usr/share/heartbeat /usr/share/man/man8/heartbeat.8


四、Heartbeat配置

 

# cp doc/{ha.cf,haresources,authkeys} /etc/ha.d/
# chmod 600 /etc/ha.d/authkeys

Heartbeat的配置主要涉 ha.cf、haresources、authkeys 三个文件:

ha.cf:主配置文件;

haresource:用来配置要让Heartbeat托管的服务;

authkey:用来指定Heartbeat的认证方式。

主备节点两端的三个配置文件(ha.cf authkeys haresources)完全相同

 

1)、配置ha.cf文件
# cp /etc/ha.d/ha.cf /etc/ha.d/ha.cf.bak
# > /etc/ha.d/ha.cf
# vi /etc/ha.d/ha.cf

#log configure
#用于记录heartbeat的调试信息
debugfile /var/log/ha-debug
#用于记录heartbeat的日志信息
logfile /var/log/ha-log
#设置heartbeat的日志,这里用的是系统日志
logfacility local1
#options configure
#设定心跳(监测)时间时间为2秒
keepalive 2
#指定若备用节点在30秒内未收到主节点心跳信号,则接管主服务器资源
deadtime 30
#指定心跳延迟告警时间为10秒(最好在2~10秒之间),,10秒内备节点不能接收主节点心跳信号,即往日志写入警告日志,但不会切换服务
warntime 10
#系统启动或重启后预留的忽略时间段,取值至少为deadtime的两倍
initdead 120
##########################
#广播/单播通讯使用的Udp端口
#udpport694
#使用网卡eth1发送心跳检测(Linux)
#bcast eth1
#采用网卡eth1的udp单播来组织心跳,后面跟的IP地址为双机对方IP地址
#ucast eth1 10.0.100.22
##########################
#bcasteth1
#采用网卡eth1的Udp多播来组织心跳,一般在备用节点不止一台时使用。Bcast、ucast和mcast分别代表广播、单播和多播,是组织心跳的的方式,任选其一
mcast eth1 225.0.0.100 694 1 0
#node configure
#定义当主节点恢复后,是否将服务自动切回(DB建议为off,测试阶段设置为on)
auto_failback on
#可选配置,通过Heartbeat监控系统运行状态
#watchdog /dev/watchdo
#主节点名称(主节点主机名),与uname -n显示一致
node db-1-1
#备用节点名称
node db-1-2
crm no
#通过ping网关检测心跳是否正常,仅用来测试网络
#ping 10.0.100.1
#指定和heartbeat一起启动、关闭的进程,可选
#respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail
#设置启动IPfail的用户和组
#apiauth ipfail gid=haclient uid=hacluster

注:

① watchdog /dev/watchdog: 可选配置,通过Heartbeat监控系统运行状态。该特性需在内核中载入"softdog"内核模块,用来生成实际的设备文件,如系统中没有该模块,需进行指定,重新编译内核。编译完成输入 "insmod softdog"加载模块,然后输入"grep misc/proc/devices",输入"cat /proc/misc |grep watchdog",最后生成设备文件:"mknod /dev/watchdog c 10 130" 即可使用

② espawn hacluster /usr/lib/heartbeat/ipfail: 可选配置,指定和heartbeat一起启动、关闭的进程。这些进程一般是和heartbeat集成的插件,遇到故障可自动重启。IPfail进程用于检测和处理网络故障,需配合ping语句指定ping node检测网络连通性;hacluster表示启动IPfail进程的用户。

 

2)、配置authkeys,心跳密钥验证文件
# cp /etc/ha.d/authkeys /etc/ha.d/authkeys.bak
# > /etc/ha.d/authkeys
# vi /etc/ha.d/authkeys

#注:auth后填序号,可任意填写
auth 1
#开头必须为序号名,然后为验证方式,支持三种( crc md5 sha1 )方式验证,最后面是自定义密钥
1 sha1 ccb6d8d12a7c207d7somb6618556d62e4831dfab

 

3)、配置haresources

# cp /etc/ha.d/haresources /etc/ha.d/haresources.bak
# > /etc/ha.d/haresources
# vi /etc/ha.d/haresources

#db-1-1 #预定的主机节点名称,与相应主机$uname -n的结果一致。需保证drbd.conf和ha.cf中节点的名字一致。备机此处写db-1-2。
#IPaddr::192.168.192.100/24/eth0 #规划的VIP。
db-1-1 IPaddr::192.168.192.100/24/eth0
######################
##@说明信息:
##@drbddisk::dbdata_r0<==启动drbd data资源,相当于执行/etc/ha.d/resource.d/drbddisk dbdata_r0 stop/start操作
##@Filesystem::/dev/drbd1::/dbdata::ext4 <==drbd分区挂载到/dbdata目录,相当于执行/etc/ha.d/resource.d/Filesystem /dev/drbd1 /dbdata ext4 stop/start<==设定需要挂载的drbd设备采用的文件系统,相当于系统中执行mount /dev/drbd1 /dbdata
##@mysql <==启动mysql服务脚本,相当于/etc/init.d/mysql stop/start
######################
#db-1-1 IPaddr::192.168.192.100/24/eth0 drbddisk::dbdata_r0 Filesystem::/dev/drbd1::/dbdata::ext4 mysql

注:

Haresources文件用于指定双机系统的主节点、集群IP、子网掩码、广播地址及启动服务集群资源,文件每一行可包含一个或多个资源脚本名,资源间使用空格隔开,参数间使用两个冒号隔开,主节点和备份节点中资源文件haresources要完全一样。

一般格式为: node-name network <resource-group>
node-name表示主节点的主机名,必须和ha.cf文件中指定的节点名一致。network用于设定集群的IP地址、子网掩码和网络设备标识等。resource-group用于指定需Heartbeat托管的服务(即这些服务可由Heartbeat来启动和关闭)。

这里指定的IP地址就是集群对外服务的IP地址,如要托管这些服务,必须将服务写成可通过start/stop来启动或关闭的脚本,放到/etc/init.d/或/etc/ha.d/resource.d/目录下,Heartbeat会根据脚本名称自动去/etc/init.d或者/etc/ha.d/resource.d目录下找到相应脚本进行启动或关闭操作。


4)、同步两个节点的配置文件

# scp /etc/ha.d/{ha.cf,haresources,authkeys} db-1-2:/etc/ha.d/
ha.cf           100%        1385      1.4KB/s 00:00
haresources     100%         781      0.8KB/s 00:00
authkeys        100%          77      0.1KB/s 00:00

 

5)、启动heartbeat

#chkconfig --add heartbeat
#关闭开机自启动,当服务器重启时,需要人工去启动
#chkconfig heartbeat off
#chkconfig heartbeat --list
#两个节点都启动
#service heartbeat start

 

五、VIP切换测试

 

① 启动heartbeat查看当前状态

[root@db-1-1 /etc/ha.d]
#ip addr 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:10:a7:fe brd ff:ff:ff:ff:ff:ff
    inet 10.0.100.21/24 brd 10.0.100.255 scope global eth1
    inet6 fe80::20c:29ff:fe10:a7fe/64 scope link 
       valid_lft forever preferred_lft forever
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:10:a7:08 brd ff:ff:ff:ff:ff:ff
    inet 192.168.192.21/24 brd 192.168.192.255 scope global eth0
    inet 192.168.192.100/24 brd 192.168.192.255 scope global secondary eth0:0
    inet6 fe80::20c:29ff:fe10:a708/64 scope link 
       valid_lft forever preferred_lft forever
[root@db-1-2 /etc/ha.d]
#ip addr          
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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:47:ae:ec brd ff:ff:ff:ff:ff:ff
    inet 192.168.192.22/24 brd 192.168.192.255 scope global eth0
    inet6 fe80::20c:29ff:fe47:aeec/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:47:ae:f6 brd ff:ff:ff:ff:ff:ff
    inet 10.0.100.22/24 brd 10.0.100.255 scope global eth1
    inet6 fe80::20c:29ff:fe47:aef6/64 scope link 
       valid_lft forever preferred_lft forever

上面的结果可以看到db-1-1节点拥有vip地址:192.168.192.100,db-1-2节点没有

② 模拟主节点宕机后的状态

[root@db-1-1 /etc/ha.d]
#service heartbeat stop
Stopping High-Availability services:                       [  OK  ]
[root@db-1-1 /etc/ha.d]
#ip addr|grep eth0
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.192.21/24 brd 192.168.192.255 scope global eth0
[root@db-1-2 /etc/ha.d]
#ip addr|grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.192.22/24 brd 192.168.192.255 scope global eth0
    inet 192.168.192.100/24 brd 192.168.192.255 scope global secondary eth0:0

上面的结果可以看到db-1-1宕机后,vip地址漂移到db-1-2节点上,db-1-2成为了主节点

③ 模拟主节点故障恢复后的状态

[root@db-1-1 /etc/ha.d]
#service heartbeat start
Starting High-Availability services: INFO:  Resource is stopped
                                                           [  OK  ]
[root@db-1-1 /etc/ha.d]
#ip addr|grep eth0
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.192.21/24 brd 192.168.192.255 scope global eth0
    inet 192.168.192.100/24 brd 192.168.192.255 scope global secondary eth0:0
[root@db-1-2 /etc/ha.d]
#ip addr|grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.192.22/24 brd 192.168.192.255 scope global eth0

上面的结果可以看到vip又漂移回节点db-1-1,db-1-1成为了主节点。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章