ITPub博客

首页 > Linux操作系统 > Linux操作系统 > CentOS 6.3下DRBD + HeartBeat + NFS配置笔记

CentOS 6.3下DRBD + HeartBeat + NFS配置笔记

Linux操作系统 作者:luashin 时间:2016-03-13 20:31:18 0 删除 编辑
系统版本:CentOS 6.3 x64(内核2.6.32)

DRBD: DRBD-8.4.3

HeartBeat:epel更新源(真坑)

NFS: 系统自带

 

HeartBeat VIP: 192.168.7.90

Primary       DRBD+HeartBeat: 192.168.7.88(drbd1.example.com)

Secondary  DRBD+HeartBeat: 192.168.7.89 (drbd2.example.com)

 

(Primary)为仅主服务器端配置

(Secondary)为仅从服务器端配置

(Primary,Secondary)为主服务器端从服务器端共同配置

 

一.DRBD配置,传送门:http://showerlee.blog.51cto.com/2047005/1211963

二.Hearbeat配置;

这里接着DRBD系统环境及安装配置:

1.安装heartbeat(CentOS6.3中默认不带有Heartbeat包,因此需要从第三方下载)(Primary,Secondary)

# wget ftp://mirror.switch.ch/pool/1/mirror/scientificlinux/6rolling/i386/os/Packages/epel-release-6-5.noarch.rpm

# rpm -ivUh epel-release-6-5.noarch.rpm

# yum --enablerepo=epel install heartbeat -y

 

2.配置heartbeat

(Primary)

# vim /etc/ha.d/ha.cf

---------------

# 日志

logfile /var/log/ha-log

logfacility local0

# 心跳监测时间

keepalive 2

# 死亡时间

deadtime 5

# 指定对方IP:

ucast eth0 192.168.7.89

# 服务器正常后由主服务器接管资源,另一台服务器放弃该资源

auto_failback off

#定义节点

node drbd1.example.com drbd2.example.com

---------------

(Secondary)

# vi /etc/ha.d/ha.cf

---------------

# 日志

logfile /var/log/ha-log

logfacility local0

# 心跳监测时间

keepalive 2

# 死亡时间

deadtime 5

# 指定对方IP:

ucast eth0 192.168.7.88

# 服务器正常后由主服务器接管资源,另一台服务器放弃该资源

auto_failback off

#定义节点

node drbd1.example.com drbd2.example.com

---------------

 

编辑双机互联验证文件:(Primary,Secondary)

# vim /etc/ha.d/authkeys

--------------

auth 1

1 crc

--------------

# chmod 600 /etc/ha.d/authkeys

 

编辑集群资源文件:(Primary,Secondary)

# vim /etc/ha.d/haresources

--------------

drbd1.example.com IPaddr::192.168.7.90/24/eth0 drbddisk::r0 Filesystem::/dev/drbd0::/data::ext4 killnfsd

--------------

该文件内IPaddr,Filesystem等脚本存放路径在/etc/ha.d/resource.d/下

 

编辑脚本文件killnfsd,用来重启NFS服务:

注:因为NFS服务切换后,必须重新mount NFS共享出来的目录,否则会报错。

# vim /etc/ha.d/resource.d/killnfsd

-----------------

killall -9 nfsd; /etc/init.d/nfs restart;exit 0

-----------------

赋予执行权限:

# chmod 755 /etc/ha.d/resource.d/killnfsd

 

创建DRBD脚本文件drbddisk:(Primary,Secondary)

注:

此处又是一个大坑,如果不明白Heartbeat目录结构的朋友估计要在这里被卡到死,因为默认yum安装Heartbeat,不会在/etc/ha.d/resource.d/创建drbddisk脚本,而且也无法在安装后从本地其他路径找到该文件。

此处本人也是因为启动Heartbeat后无法PING通虚IP,最后通过查看/var/log/ha-log日志,找到一行

ERROR: Cannot locate resource script drbddisk

然后进而到/etc/ha.d/resource.d/路径下发现竟然没有drbddisk脚本,最后在google上找到该代码,创建该脚本,终于测试通过:

 

# vim /etc/ha.d/resource.d/drbddisk

-----------------------

#!/bin/bash

#

# This script is inteded to be used as resource script by heartbeat

#

# Copright 2003-2008 LINBIT Information Technologies

# Philipp Reisner, Lars Ellenberg

#

###

DEFAULTFILE="/etc/default/drbd"

DRBDADM="/sbin/drbdadm"

if [ -f $DEFAULTFILE ]; then

. $DEFAULTFILE

fi

 

if [ "$#" -eq 2 ]; then

RES="$1"

CMD="$2"

else

RES="all"

CMD="$1"

fi

 

## EXIT CODES

# since this is a "legacy heartbeat R1 resource agent" script,

# exit codes actually do not matter that much as long as we conform to

# http://wiki.linux-ha.org/HeartbeatResourceAgent

# but it does not hurt to conform to lsb init-script exit codes,

# where we can.

# http://refspecs.linux-foundation.org/LSB_3.1.0/

#LSB-Core-generic/LSB-Core-generic/iniscrptact.html

####

 

drbd_set_role_from_proc_drbd()

{

local out

if ! test -e /proc/drbd; then

ROLE="Unconfigured"

return

fi

 

dev=$( $DRBDADM sh-dev $RES )

minor=${dev#/dev/drbd}

if [[ $minor = *[!0-9]* ]] ; then

# sh-minor is only supported since drbd 8.3.1

minor=$( $DRBDADM sh-minor $RES )

fi

if [[ -z $minor ]] || [[ $minor = *[!0-9]* ]] ; then

ROLE=Unknown

return

fi

 

if out=$(sed -ne "/^ *$minor: cs:/ { s/:/ /g; p; q; }" /proc/drbd); then

set -- $out

ROLE=${5%/**}

: ${ROLE:=Unconfigured} # if it does not show up

else

ROLE=Unknown

fi

}

 

case "$CMD" in

start)

# try several times, in case heartbeat deadtime

# was smaller than drbd ping time

try=6

while true; do

$DRBDADM primary $RES && break

let "--try" || exit 1 # LSB generic error

sleep 1

done

;;

stop)

# heartbeat (haresources mode) will retry failed stop

# for a number of times in addition to this internal retry.

try=3

while true; do

$DRBDADM secondary $RES && break

# We used to lie here, and pretend success for anything != 11,

# to avoid the reboot on failed stop recovery for "simple

# config errors" and such. But that is incorrect.

# Don't lie to your cluster manager.

# And don't do config errors...

let --try || exit 1 # LSB generic error

sleep 1

done

;;

status)

if [ "$RES" = "all" ]; then

echo "A resource name is required for status inquiries."

exit 10

fi

ST=$( $DRBDADM role $RES )

ROLE=${ST%/**}

case $ROLE in

Primary|Secondary|Unconfigured)

# expected

;;

*)

# unexpected. whatever...

# If we are unsure about the state of a resource, we need to

# report it as possibly running, so heartbeat can, after failed

# stop, do a recovery by reboot.

# drbdsetup may fail for obscure reasons, e.g. if /var/lock/ is

# suddenly readonly. So we retry by parsing /proc/drbd.

drbd_set_role_from_proc_drbd

esac

case $ROLE in

Primary)

echo "running (Primary)"

exit 0 # LSB status "service is OK"

;;

Secondary|Unconfigured)

echo "stopped ($ROLE)"

exit 3 # LSB status "service is not running"

;;

*)

# NOTE the "running" in below message.

# this is a "heartbeat" resource script,

# the exit code is _ignored_.

echo "cannot determine status, may be running ($ROLE)"

exit 4 # LSB status "service status is unknown"

;;

esac

;;

*)

echo "Usage: drbddisk [resource] {start|stop|status}"

exit 1

;;

esac

exit 0

-----------------------

赋予执行权限:

# chmod 755 /etc/ha.d/resource.d/drbddisk

 

在两个节点上启动HeartBeat服务,先启动Primary:(Primary,Secondary)

# service heartbeat start

# chkconfig heartbeat on

 

这里能够PING通虚IP 192.168.7.90,表示配置成功


三.配置NFS:(Primary,Secondary)

# vi /etc/exports

-----------------

/data *(rw,no_root_squash)

-----------------

重启NFS服务:

# service rpcbind restart

# service nfs restart

# chkconfig rpcbind on

# chkconfig nfs off

这里设置NFS开机不要自动运行,因为/etc/ha.d/resource.d/killnfsd 该脚本内容控制NFS的启动。

 

四.最终测试

在另外一台LINUX的客户端挂载虚IP:192.168.7.90,挂载成功表明NFS+DRBD+HeartBeat大功告成.

# mount -t nfs 192.168.7.90:/data /tmp

# df -h

---------------

......

192.168.7.90:/data 1020M 34M 934M 4% /tmp

---------------

测试DRBD+HeartBeat+NFS可用性:

1.向挂载的/tmp目录传送文件,忽然重新启动主端DRBD服务,查看变化

经本人测试能够实现断点续传

 

2.正常状态重启Primary主机后,观察主DRBD状态是否恢复Primary并能正常被客户端挂载并且之前写入的文件存在,可以正常再写入文件。

经本人测试可以正常恢复,且客户端无需重新挂载NFS共享目录,之前数据存在,且可直接写入文件。

 

3.当Primary主机因为硬件损坏无法立即使用,需要将Secondary提升为Primary主机,如何操作?

如果设备能够正常启动则按照如下操作,无法启动则强行提升Secondary为Primary,待宕机设备能够正常启动,若“脑裂”,再做后续修复工作。

首先先卸载客户端挂载的NFS主机目录

# umount /tmp

(Primary)

卸载DRBD设备:

# service nfs restart

# umount /data

降权:

# drbdadm secondary r0

查看状态,已降权

# service drbd status

-----------------

drbd driver loaded OK; device status:

version: 8.4.3 (api:1/proto:86-101)

GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@drbd1.example.com, 2013-05-27 20:45:19

m:res cs ro ds p mounted fstype

0:r0 Connected Secondary/Secondary UpToDate/UpToDate C

-----------------

(Secondary)

提权:

# drbdadm primary r0

查看状态,已提权:

# service drbd status

----------------

drbd driver loaded OK; device status:

version: 8.4.3 (api:1/proto:86-101)

GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@drbd2.example.com, 2013-05-27 20:49:06

m:res cs ro ds p mounted fstype

0:r0 Connected Primary/Secondary UpToDate/UpToDate C

----------------

这里还未挂载DRBD目录,让Heartbeat帮忙挂载:

注:若重启过程中发现Heartbeat日志报错:

ERROR: glib: ucast: error binding socket. Retrying: Permission denied

请检查selinux是否关闭

# service heartbeat restart

# service drbd status

-----------------------

drbd driver loaded OK; device status:

version: 8.4.3 (api:1/proto:86-101)

GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@drbd2.example.com, 2013-05-27 20:49:06

m:res cs ro ds p mounted fstype

0:r0 Connected Primary/Secondary UpToDate/UpToDate C /data ext4

------------------------

成功让HeartBeat挂载DRBD目录

 

重新在客户端做NFS挂载测试:

# mount -t nfs 192.168.7.90:/data /tmp

# ll /tmp

------------------

1 10 2 2222 3 4 5 6 7 8 9 lost+found orbit-root

------------------

重启刚刚被提权的主机,待重启查看状态:

# service drbd status

------------------------

drbd driver loaded OK; device status:

version: 8.4.3 (api:1/proto:86-101)

GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@drbd2.example.com, 2013-05-27 20:49:06

m:res cs ro ds p mounted fstype

0:r0 WFConnection Primary/Unknown UpToDate/DUnknown C /data ext4

------------------------

HeartBeat成功挂载DRBD目录,且客户端无缝透明使用NFS挂载点。

 

4.测试最后刚才那台宕机重新恢复正常后,他是否会从新夺取Primary资源?

重启后不会重新获取资源,需手动切换主从权限方可。

注:vi /etc/ha.d/ha.cf配置文件内该参数:

--------------------

auto_failback off

--------------------

表示服务器正常后由新的主服务器接管资源,另一台旧服务器放弃该资源

-------大功告成----------

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9034054/viewspace-2056633/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2015-02-18

  • 博文量
    290
  • 访问量
    1050173