DRBD+Heratbeat+NFS高可用文件共享存储
一、概述
1、架构图
2、软件简介
Heartbeat(Linux-HA)的工作原理:heartbeat最核心的包括两个部分,心跳监测部分和资源接管部分,心跳监测可以通过网络链路和串口进行,而且支持冗余链路,它们之间相互发送报文来告诉对方自己当前的状态,如果在指定的时间内未到对方发送的报文,那么就认为对方失效,这时需启动资源接管模块来接管运 行在对方主机上的资源或者服务。
DRBD:drbd是一个软件来实现数据实时、同步、异步的数据镜像块存储复制解决方案,主要功能是通过Linux内核实现。DRBD类似网络RAID-1功能写入本地的文件会通过网络以相同方式写在另一文件系统。
3、资源分配
系统:CentOS6.5_x64
主机名 |
角色 |
IP地址 |
masternfs |
主NFS |
eth0:192.168.0.202/24 |
backupnfs |
备NFS |
eth0:192.168.0.203/24 |
VIP |
192.168.0.204/24 |
4、配置两台hosts记录
[[email protected] ~]# vi /etc/hosts
192.168.0.202 masternfs
192.168.0.203 backupnfs
二、安装配置DRBD(两台服务器配置基本一样)
安装epel源:
rpm -ivh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
下载drbd软件:http://oss.linbit.com/drbd/
1、安装依赖包
1
|
[[email protected] ~] # yum install -y gcc gcc-c++ make perl kernel-devel kernel-headers flex drbd84-utils kmod-drbd84
|
2、安装DRBD并加载到内核
1
2
3
4
5
6
7
8
9
10
11
12
|
[[email protected] ~] # tar zxvf drbd-8.4.4.tar.gz
[[email protected] ~] # cd drbd-8.4.4
[[email protected] ~] # ./configure --prefix=/usr/local/drbd --with-km #--with-km,启用内核模块
[[email protected] ~] # make KDIR=/usr/src/kernels/2.6.32-358.23.2.el6.x86_64/ #指定内核源码路径
[[email protected] ~] # make install
[[email protected] ~] # chkconfig --add drbd
[[email protected] ~] # chkconfig drbd on
[[email protected] ~] # cp drbd/drbd.ko /lib/modules/`uname -r`/kernel/lib/ #加载DRBD模块到内核中
[[email protected] ~] # modprobe drbd
[[email protected] ~] # lsmod |grep drbd #由此查看drbd模块已经加载
drbd 333755 0 libcrc32c 1246 1 drbd |
3、配置DRBD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
[[email protected] ~] # cd /usr/local/drbd/
[[email protected] ~] # vi /usr/local/drbd/etc/drbd.d/global_common.conf
global { usage-count yes ;
} common { protocol C;
disk { on-io-error detach;
} syncer { rate 100M;
}
} resource web { on masternfs {
device /dev/drbd0 ;
disk /dev/sdb1 ;
address 192.168.0.202:7899;
meta-disk internal;
}
on backupnfs {
device /dev/drbd0 ;
disk /dev/sdb1 ;
address 192.168.0.203:7899;
meta-disk internal;
}
} |
4、创建供DRBD记录信息数据块
1
2
3
4
5
6
7
8
|
[[email protected] ~] # drbd admcreate-md web
Writing meta data... initializing activity log NOT initializing bitmap New drbd meta data blocksuccessfully created. success [[email protected] ~] # mkdir -p /usr/local/drbd/var/run/drbd #创建资源目录
[[email protected] ~] # service drbd start
|
5、默认没有分主备节点的,需要设置
1
2
3
4
5
|
[[email protected] ~] # cat /proc/drbd
version: 8.4.4(api:1 /proto :86-101)
0: cs:Connected ro:Secondary /Secondaryds :UpToDate /UpToDate C r-----
ns:64 nr:112 dw:176 dr:6088 al:1 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1wo:f oos:0 |
1
2
|
[[email protected] ~] # drbdadm -- --overwrite-data-of-peer primary web
[[email protected] ~] # drbdadm secondary web #backupnfs服务器设置从节点
|
#再查看状态已经是主从
1
2
|
[[email protected] ~] # drbd-overview
0:mysql /0 Connected Primary /Secondary UpToDate /UpToDate C r-----
|
说明:
cs:两台数据连接状态
ro:两台主机的状态
ds:磁盘状态是“UpToDate/UpToDate”,同步状态。
6、格式化并挂载分区
1
2
3
|
[[email protected] ~] # mkfs.ext4 /dev/drbd0
[[email protected] ~] # mkdir /web
[[email protected] ~] # mount /dev/drbd0 /web
|
三、安装配置NFS
1
2
3
4
5
6
7
|
[[email protected] ~] # yum install -y nfs-utils
[[email protected] ~] # vi /etc/exports
/web 192.168.0.0 /24 (rw,no_root_squash)
[[email protected] ~] # service rpcbind start
[[email protected] ~] # service nfs start
[[email protected] ~] # chkconfig rpcbind on
[[email protected] ~] # chkconfig nfs on
|
四、安装配置Heartbeat
1
2
3
4
|
[[email protected] ~] # yum install heartbeat* -y
[[email protected] ~] # cd /usr/share/doc/heartbeat-3.0.4/
[[email protected]] # cp ha.cf authkeys haresources /etc/ha.d/
[[email protected]] # cd /etc/ha.d/
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[[email protected] ha.d] # vi ha.cf
logfile /var/log/ha-log
logfacility local0 #如果未定义上述日志,默认写入/var/log/message
keepalive 2 #心跳间隔
deadtime 30 #死亡阀值
warntime 10 #警告时间
initdead 120 #首次启动heartbeat,等待多久才启动主服务资源
udpport 694 #连接端口
bcast eth0 #使用什么方式组织心跳,最好用ucast方式
auto_failback off #当主节点故障恢复后,自动获取资源并取代从节点
node masternfs #主节点主机名
node backupnfs #备份节点主机名
ping 192.168.0.1 #选择ping节点,ping的越好对HA集群越稳定,建议使用网关
|
1
2
3
|
1
|
[[email protected] ~] # masternfs IPaddr::192.168.0.204/24/eth0 drbddisk::web Filesystem::/dev/drbd0::/web::ext4 killnfsd
|
参数说明:
masternfsIPaddr::192.168.0.204/24/eth0 #主机名,后跟虚拟IP地址、接口
drbddisk::web #管理drbd资源
Filesystem::/dev/drbd0::/web::ext4 killnfsd #文件系统,目录及格式,后跟nfs资源脚本
#killnfsd脚本
1
2
|
[[email protected] ~] # echo "killall -9 nfsd ; /etc/init.d/nfs restart ; exit 0" >>/etc/ha.d/resource.d/killnfsd
[[email protected] ~] # chmod +x /etc/ha.d/resource.d/killnfsd
|
1
2
3
|
[[email protected] ~] # chmod 600 /etc/ha.d/authkeys #不修改权限会报错
[[email protected] ~] # service heartbeat start
[[email protected] ~] # chkconfig heartbeat on
|
五、测试高可用性
1、停掉masternfs服务器heartbeat服务,是否虚拟IP正常切换,可通过ip addr命令查看。
2、通过一台测试机, mount -t nfs 192.168.0.204:/web/media,当故障切换时,nfs无间断提供服务,提供完美冗余。
3、通过ha-log日志可以看出主释放资源,备接管资源。