共计 5227 个字符,预计需要花费 14 分钟才能阅读完成。
本文由 LinuxProbe.Com 团队成员 逄增宝 整理发布,原文来自:一路向北。
导读 | Distributed Replicated Block Device(DRBD)是一个用软件实现的、无共享的、服务器之间镜像块设备内容的存储复制解决方案。数据镜像:实时、透明、同步(所有服务器都成功后返回)、异步(本地服务器成功后返回)。DRBD 的核心功能通过 Linux 的内核实现,最接近系统的 IO 栈,但它不能神奇地添加上层的功能比如检测到 EXT3 文件系统的崩溃。DRBD 的位置处于文件系统以下,比文件系统更加靠近操作系统内核及 IO 栈。 |
系统版本:CentOS6.5
DRBD 版本:DRBD-8.4.3
node1: 192.168.7.88(drbd1)
node2: 192.168.7.89 (drbd2)
(node1)为仅主节点配置
(node2)为仅从节点配置
(node1,node2)为主从节点共同配置
1. 关闭 iptables 和 SELINUX,避免安装过程中报错
service iptables stop // 关闭 iptables
setenforcing 0 // 暂时关闭 selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux // 永久关闭 selinux
2. 设置 hosts 文件
vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.7.88 drbd1
192.168.7.89 drbd2
3. 在两台虚拟机上添加一块 2G 硬盘 sdb 作为 DRBD,分别分区为 sdb1,大小 1G,并在本地系统创建 /data 目录,不做挂载操作。
fdisk /dev/sdb
----------------
n-p-1- 回车 -"+1G"-wq
----------------
mkdir /data
4. 时间同步:
ntpdate -u asia.pool.ntp.org
1. 安装依赖包:(node1,node2)
yum install gcc gcc-c++ make glibc flex kernel-devel kernel-headers
2. 安装 DRBD:(node1,node2)
wget http://oss.linbit.com/drbd/8.4/drbd-8.4.3.tar.gz
tar zxvf drbd-8.4.3.tar.gz
cd drbd-8.4.3
./configure --prefix=/usr/local/drbd --with-km
make && make install
mkdir -p /usr/local/drbd/var/run/drbd
cp /usr/local/drbd/etc/rc.d/init.d/drbd /etc/rc.d/init.d
chkconfig --add drbd
chkconfig drbd on
加载 DRBD 模块:
modprobe drbd
查看 DRBD 模块是否加载到内核:lsmod |grep drbd
1. 参数配置:(node1,node2)
vim /usr/local/drbd/etc/drbd.conf
清空里面的配置,添加如下配置:resource r0{
protocol C;
startup {wfc-timeout 0; degr-wfc-timeout 120;}
disk {on-io-error detach;}
net{
timeout 60;
connect-int 10;
ping-int 10;
max-buffers 2048;
max-epoch-size 2048;
}
syncer {rate 30M;}
on drbd1.example.com{
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.7.88:7788;
meta-disk internal;
}
on drbd2.example.com{
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.7.89:7788;
meta-disk internal;
}
}
2. 创建 DRBD 设备并激活 r0 资源:(node1,node2)
mknod /dev/drbd0 b 147 0
drbdadm create-md r0
等待片刻,显示 success 表示 drbd 块创建成功
----------------
Writing meta data...
initializing activity log
NOT initializing bitmap
New drbd meta data block successfully created.
--== Creating metadata ==--
As with nodes, we count the total number of devices mirrored by DRBD
at http://usage.drbd.org.
The counter works anonymously. It creates a random number to identify
the device and sends that random number, along with the kernel and
DRBD version, to usage.drbd.org.
http://usage.drbd.org/cgi-bin/insert_usage.pl?
nu=716310175600466686&ru=15741444353112217792&rs=1085704704
* If you wish to opt out entirely, simply enter 'no'.
* To continue, just press [RETURN] // 出现 [RETURN] 按回车
success
----------------
再次输入该命令:# drbdadm create-md r0
成功激活 r0
----------------
[need to type 'yes' to confirm] yes
Writing meta data...
initializing activity log
NOT initializing bitmap
New drbd meta data block successfully created.
3. 启动 DRBD 服务:(node1,node2)
service drbd start
注:需要主从共同启动方能生效
4. 查看状态:(node1,node2)
cat /proc/drbd
version: 8.4.3 (api:1/proto:86-101)
GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@drbd1.example.com,
2013-05-27 20:45:19
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:f oos:1060184
或者
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 Inconsistent/Inconsistent C
注:这里 ro:Secondary/Secondary 表示两台主机的状态都是备机状态,ds 是磁盘状态,显示的状态内容为“不一致”,这是因为 DRBD 无法判断哪一方为主机,应以哪一方的磁盘数据作为标准。
5. 将 drbd1 主机配置为主节点:(node1)
drbdsetup /dev/drbd0 primary --force
分别查看主从 DRBD 状态:
(node1)
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 Primary/Secondary UpToDate/UpToDate C
(node2)
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 Secondary/PrimaryUpToDate/UpToDate C
注:ro 在主从服务器上分别显示 Primary/Secondary 和 Secondary/Primary ds 显示 UpToDate/UpToDate 表示主从配置成功。
6. 挂载 DRBD:(node1)
从刚才的状态上看到 mounted 和 fstype 参数为空,所以我们这步开始挂载 DRBD 到系统目录
mkfs.ext4 /dev/drbd0
mount /dev/drbd0 /data
注:Secondary 节点上不允许对 DRBD 设备进行任何操作,包括只读,所有的读写操作只能
在 Primary 节点上进行,只有当 Primary 节点挂掉时,Secondary 节点才能提升为 Primary 节点继续工作。
(node1)
cd /data
touch 1 2 3 4 5
cd ..
umount /data
drbdsetup /dev/drbd0 secondary
注:这里实际生产环境若 DRBD1 宕机,在 DRBD2 状态信息中 ro 的值会显示为 Secondary/Unknown, 只需要进行 DRBD 提权操作即可。
(node2)
drbdsetup /dev/drbd0 primary
mount /dev/drbd0 /data
cd /data
touch 6 7 8 9 10
ls
--------------
1 10 2 3 4 5 6 7 8 9 lost+found
查看(node1)(node2)DRBD 状态:
(node2)
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
(node1)
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/Primary UpToDate/UpToDate C