linux下高可用集群之DRBD详解
1.DRBD:Disrtributed Replicated Block Device,分布式复制块设备
DRBD:主要是在不同服务器之间硬盘或分区同步数据,通过网络,按位同步,即镜像!
Raid1主要是将同一服务器硬盘或分区同步数据,通过主板总线,按位同步。即镜像!
DRBD区别于DAS,NAS,SAN,也区别于Raid1
DRBD是primary/secondary,主从设备,主从角色可以互换
primary:可读写执行操作,但secondary不能挂载文件系统
DRBD也是Dual primary,双主设备;首先将DRBD定义成资源,并将两服务器DRBD文件系统格式化成集群文件系统(GFS2/OCFS2),再基于DLM分布式锁管理器将主节点锁信息通过Message Layer通知给从节点,并不是并行读写,且仅允许两节点
DLM:Distributed Lock Manager 分布式锁管理器
2.DRBD协议:
A:asysnc 异步
B: semi sync 半同步
C: sync 同步
DRBD资源:
资源名称:可以是除了空白字符外的任意ACSII码字符;
DRBD设备:在双方节点上,此DRBD设备的设备文件;一般为/dev/drbd{N},其主设备号147
磁盘:在双方节点上,各自提供的存储设备;
网络配置:双方数据同步时所使用的网络属性;
3.配置DRBD必备的3个条件
3.1、时间同步;
3.2、名称解析,且每个主机的主机名与其'uname -n'保持一致;
3.3、配置好每个节点的Yum;
4.安装DRBD,所需两个rpm包drbd83-8.3.8-1.el5.centos.i386.rpm和kmod-drbd83-8.3.8-1.el5.centos.i686.rpm
#yum -y --nogpgcheck localinstall drbd83-8.3.8-1.el5.centos.i386.rpm kmod-drbd83-8.3.8-1.el5.centos.i686.rpm
5.配置drbd
drbd的主配置文件为/etc/drbd.conf;为了管理的便捷性,目前通常会将些配置文件分成多个部分,且都保存至/etc/drbd.d目录中,主配置文件中仅使用"include"指令将这些配置文件片断整合起来。通常,/etc/drbd.d目录中的配置文件为global_common.conf和所有以.res结尾的文件。其中global_common.conf中主要定义global段和common段,而每一个.res的文件用于定义一个资源。
在配置文件中,global段仅能出现一次,且如果所有的配置信息都保存至同一个配置文件中而不分开为多个文件的话,global段必须位于配置文件的最开始处。目前global段中可以定义的参数仅有minor-count, dialog-refresh, disable-ip-verification和usage-count。
common段则用于定义被每一个资源默认继承的参数,可以在资源定义中使用的参数都可以在common段中定义。实际应用中,common段并非必须,但建议将多个资源共享的参数定义为common段中的参数以降低配置文件的复杂度。
resource段则用于定义drbd资源,每个资源通常定义在一个单独的位于/etc/drbd.d目录中的以.res结尾的文件中。资源在定义时必须为其命名,名字可以由非空白的ASCII字符组成。每一个资源段的定义中至少要包含两个host子段,以定义此资源关联至的节点,其它参数均可以从common段或drbd的默认中进行继承而无须定义。
下面的操作在node1.willow.com上完成。
5.1.复制样例配置文件为即将使用的配置文件:
# cp /usr/share/doc/drbd83-8.3.8/drbd.conf /etc
5.2.vim /etc/drbd.d/global-common.conf
global {
usage-count no;
# minor-count dialog-refresh disable-ip-verification
}
common {
protocol C;
handlers {
pri-on-incon-degr "/usr/lib/drbd/notify-pri-on-incon-degr.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f";
pri-lost-after-sb "/usr/lib/drbd/notify-pri-lost-after-sb.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f";
local-io-error "/usr/lib/drbd/notify-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ; halt -f";
# fence-peer "/usr/lib/drbd/crm-fence-peer.sh";
# split-brain "/usr/lib/drbd/notify-split-brain.sh root";
# out-of-sync "/usr/lib/drbd/notify-out-of-sync.sh root";
# before-resync-target "/usr/lib/drbd/snapshot-resync-target-lvm.sh -p 15 -- -c 16k";
# after-resync-target /usr/lib/drbd/unsnapshot-resync-target-lvm.sh;
}
startup {
#wfc-timeout 120;
#degr-wfc-timeout 120;
}
disk {
on-io-error detach;
#fencing resource-only;
}
net {
cram-hmac-alg "sha1";
shared-secret "mydrbdwillow";
}
syncer {
rate 1000M;
}
}
6、定义一个资源/etc/drbd.d/mydrbd.res,内容如下:
resource mydrbd {
on node1.willow.com {
device /dev/drbd0;
disk /dev/sda5;
address 1.1.1.18:7789;
meta-disk internal;
}
on node2.willow.com {
device /dev/drbd0;
disk /dev/sda5;
address 1.1.1.19:7789;
meta-disk internal;
}
}
或者资源定义也可以这样写,将同样属性写在外面,成为默认值,如下
resource mydrbd {
device /dev/drbd0;
disk /dev/sda5;
meta-disk internal;
on node1.willow.com {
address 1.1.1.18:7789;
}
on node2.willow.com {
address 1.1.1.19:7789;
}
}
以上文件在两个节点上必须相同,因此,可以基于ssh将刚才配置的文件全部同步至另外一个节点。
# scp /etc/drbd.* node2:/etc
7、在两个节点上初始化已定义的资源并启动服务:
7.1.初始化资源,在Node1和Node2上分别执行:
# drbdadm create-md mydrbd
7.2.启动服务,在Node1和Node2上分别执行:注意有同步过程,启动一个节点将会一直等待
/etc/init.d/drbd start
7.3.查看启动状态:
# cat /proc/drbd
version: 8.3.8 (api:88/proto:86-94)
GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16
0: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r----
ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:505964
也可以使用drbd-overview命令来查看:
# drbd-overview
0:mydrbd Connected Secondary/Secondary Inconsistent/Inconsistent C r----
从上面的信息中可以看出此时两个节点均处于Secondary状态。
也可以 watch -n 1 'cat /proc/drbd'动状查看同步过程
7.4.将其中一个节点设置为Primary。在要设置为Primary的节点上执行如下命令:
# drbdsetup /dev/drbd0 primary –o
也可以在要设置为Primary的节点上使用如下命令来设置主节点:
# drbdadm -- --overwrite-data-of-peer primary mydrbd
而后再次查看状态,可以发现数据同步过程已经开始:
# drbd-overview
0:mydrbd SyncSource Primary/Secondary UpToDate/Inconsistent C r----
[============>.......] sync'ed: 76.9%(172140/505964)K delay_probe: 35
等数据同步完成以后再次查看状态,可以发现节点已经牌实时状态,且节点已经有了主次:
# drbd-overview
0:mydrbd Connected Primary/Secondary UpToDate/UpToDate C r----
8、创建文件系统
文件系统的挂载只能在Primary节点进行,因此,也只有在设置了主节点后才能对drbd设备进行格式化:
# mke2fs -j -L DRBD /dev/drbd0
# mkdir /mydata
# mount /dev/drbd0 /mydata
9、切换Primary和Secondary节点
对主Primary/Secondary模型的drbd服务来讲,在某个时刻只能有一个节点为Primary,因此,要切换两个节点的角色,只能在先将原有的Primary节点设置为Secondary后,才能原来的Secondary节点设置为Primary:
Node1:
# cp -r /etc/fstab /mydata
# umount /mydata
# drbdadm secondary mydrbd
查看状态:
# drbd-overview
0:mydrbd Connected Secondary/Secondary UpToDate/UpToDate C r----
Node2:
# drbdadm primary mydrbd
# drbd-overview
0:mydrbd Connected Primary/Secondary UpToDate/UpToDate C r----
# mkdir /mydata
# mount /dev/drbd0 /mydata
使用下面的命令查看在此前在主节点上复制至此设备的文件是否存在:
# ls /mnt/drbd
################################################################################
drbd 8.4中第一次设置某节点成为主节点的命令
# drbdadm primary --force resource
配置资源双主模型的示例:
resource mydrbd {
net {
protocol C;
allow-two-primaries yes;
}
startup {
become-primary-on both;
}
disk {
fencing resource-and-stonith;
}
handlers {
# Make sure the other node is confirmed
# dead after this!
outdate-peer "/sbin/kill-other-node.sh";
}
on node1.willow.com {
device /dev/drbd0;
disk /dev/vg0/mydrbd;
address 1.1.1.18:7789;
meta-disk internal;
}
on node2.willow.com {
device /dev/drbd0;
disk /dev/vg0/mydrbd;
address 1.1.1.19:7789;
meta-disk internal;
}
}
9.将drbd作为corosync一个高可用集群资源,主要功能是让其自动切换主从资源
在node1主节点上先卸载文件系统再停止服务,再对从节点node2停止对应服务
# drbd-overview
#umount /mydata
# drbdadm secondary mydrbd
# service drbd stop
# chkconfig drbd off
ssh node2 'service drbd stop'
ssh node2 'chkconfig drbd off'
10.安装corosync,先下载再安装以下安装包
cluster-glue-1.0.6-1.6.el5.i386.rpm
cluster-glue-libs-1.0.6-1.6.el5.i386.rpm
corosync-1.2.7-1.1.el5.i386.rpm
corosynclib-1.2.7-1.1.el5.i386.rpm
heartbeat-3.0.3-2.3.el5.i386.rpm
heartbeat-libs-3.0.3-2.3.el5.i386.rpm
libesmtp-1.0.4-5.el5.i386.rpm
pacemaker-1.1.5-1.1.el5.i386.rpm
pacemaker-cts-1.1.5-1.1.el5.i386.rpm
pacemaker-libs-1.1.5-1.1.el5.i386.rpm
resource-agents-1.0.4-1.1.el5.i386.rpm
# yum --nogpgcheck localinstall *.rpm
# mkdir /var/log/cluster
# ssh node2 'mkdir /var/log/cluster'
然后配置corosync的配置文件以及生成key文件,两节点要求此两文件必须一致,此处省略
如不明白,请查看我前面博客之corosync详解
# crm configure property stonith-enabled=false #禁用stonith
# crm configure property no-quorum-policy=ignore #忽略法定票数
# crm configure rsc_defaults resource-stickiness=200 #将当前节点粘性设为100
# crm configure verify
# crm configure commit
# crm configure show
11.将drbd配置成克隆的主从资源
[root@node1 ~]# crm
crm(live)# configure
crm(live)configure#primitive mysqldrbd ocf:heartbeat:drbd params drbd_resource=mydrbd op start timeout=240 op stop timeout=100 op monitor role=Master interval=10 timeout=20 op monitor role=Slave interval=20 timeout=20
crm(live)configure# ms ms_mysqldrbd mysqldrbd meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# cd
crm(live)# status
============
Last updated: Tue Aug 16 09:45:47 2016
Stack: openais
Current DC: node1.willow.com - partition with quorum
Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f
2 Nodes configured, 2 expected votes
1 Resources configured.
============
Online: [ node2.willow.com node1.willow.com ]
Master/Slave Set: ms_mysqldrbd [mysqldrbd]
Masters: [ node1.willow.com ]
Slaves: [ node2.willow.com ]
crm(live)#
12.定义排列和顺序约束 mystore和ms_mysqldrbd放在同一节点
crm(live)configure# primitive mystore ocf:heartbeat:Filesystem params device=/dev/drbd0 directory=/mydata fstype=ext3 op start timeout=60 op stop timeout=60
crm(live)configure# colocation mystore_with_ms_mysqldrbd inf: mystore ms_mysqldrbd:Master
crm(live)configure# order mystore_after_msmysqldrbd mandatory: ms_mysqldrbd:promote mystore:start
crm(live)configure# verify
crm(live)configure# commit
13.主动设置让主节点成为从节点,查看mystore资源挂载情况
[root@node2 corosync]# crm status
============
Last updated: Tue Aug 16 10:28:47 2016
Stack: openais
Current DC: node1.willow.com - partition with quorum
Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f
2 Nodes configured, 2 expected votes
2 Resources configured.
============
Online: [ node2.willow.com node1.willow.com ]
Master/Slave Set: ms_mysqldrbd [mysqldrbd]
Masters: [ node2.willow.com ]
Slaves: [ node1.willow.com ]
mystore(ocf::heartbeat:Filesystem):Started node2.willow.com
[root@node2 corosync]# ls /mydata/
fstab lost+found
[root@node2 corosync]# crm node standby
[root@node2 corosync]# crm status
============
Last updated: Tue Aug 16 10:31:55 2016
Stack: openais
Current DC: node1.willow.com - partition with quorum
Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f
2 Nodes configured, 2 expected votes
2 Resources configured.
============
Node node2.willow.com: standby
Online: [ node1.willow.com ]
Master/Slave Set: ms_mysqldrbd [mysqldrbd]
Masters: [ node1.willow.com ]
Stopped: [ mysqldrbd:1 ]
mystore(ocf::heartbeat:Filesystem):Started node1.willow.com
[root@node2 corosync]#
14.mysql+drbd+corosync 共享mysql服务 配置命令如下:
crm(live)# configure
crm(live)configure# SHOW
node node1.willow.com \
attributes standby="off"
node node2.willow.com \
attributes standby="off"
primitive vip ocf:heartbeat:IPaddr \
params ip="1.1.1.100" nic="eth0" cidr_netmask="255.255.255.0"
primitive mysqld lsb:mysqld
primitive mysqldrbd ocf:heartbeat:drbd \
params drbd_resource="mydrbd" \
op start interval="0" timeout="240" \
op stop interval="0" timeout="100" \
op monitor interval="20" role="Master" timeout="30" \
op monitor interval="30" role="Slave" timeout="30"
primitive mystore ocf:heartbeat:Filesystem \
params device="/dev/drbd0" directory="/mydata" fstype="ext3" \
op start interval="0" timeout="60" \
op stop interval="0" timeout="60"
ms ms_mysqldrbd mysqldrbd \
meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"
colocation vip_with_ms_mysqldrbd inf: ms_mysqldrbd:Master vip
colocation mysqld_with_mystore inf: mysqld mystore
colocation mystore_with_ms_mysqldrbd inf: mystore ms_mysqldrbd:Master
order mysqld_after_mystore inf: mystore mysqld
order mystore_after_ms_mysqldrbd inf: ms_mysqldrbd:promote mystore:start
property $id="cib-bootstrap-options" \
dc-version="1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f" \
cluster-infrastructure="openais" \
expected-quorum-votes="2" \
stonith-enabled="false" \
no-quorum-policy="ignore" \
last-lrm-refresh="1368438416"
rsc_defaults $id="rsc-options" \
resource-stickiness="100"
转载于:https://blog.51cto.com/willow/1836935