- 论坛徽章:
- 0
|
一:概述:
DRBD 是由内核模块和相关脚本而构成,用以构建高可用性的集群。其实现方式是通过网络来镜像整个设备。您可以把它看作是一种网络RAID。 Drbd 负责接收数据,把数据写到本地磁盘,然后发送给另一个主机。另一个主机再将数据存到自己的磁盘中。
源码下载http://oss.linbit.com/drbd/0.7/drbd-0.7.19.tar.gz
核心参考文档:http://www.drbd.org/drbd-howto.html
二:主要实现
假设有两台机器nannan:192.168.0.136 需要镜像的硬盘:/dev/hdc3
IXDBA.NET社区论坛
root:192.168.0.139 需要镜像的硬盘:/dev/hdc2
主服务器为192.168.0.136 简称为136
备份服务器为192.168.0.139 简称为139
平常对数据读写都在136上实现,当136 down掉后可以启动139,实现数据的热备份。
真正的热切换需要才用HA 来实现。
三:下载安装
安装环境:Red Hat Enterprise Linux AS release 4,内核版本:2.6.9-22.EL
确认内核源码存在,可到http://oss.linbit.com/drbd/去载.
下载源码注意:当前最新的drbd-8.0pre3,配置文件无法正常配置,出现一大堆错误,所以下载以前的稳定版本。tar源码解包后
运行:
A、make KDIR=/usr/src/linux /*内核所在的位置*/
/*如果你没有更改内核可以直接运行make,软件会直接到/lib/module里边去寻找系统环境,如果是新的内核需要对内核进行编译安装,
否则make时候会错误中断掉*/
B、make install
安装完主要生成命令:drbdsetup ,drbdadmin
和配置文件:/etc/drbd.conf ,启动文件,/etc/init.d/drbd
模块文件:drbd.ko(在编译好的安装包目录下的drbd下可以找到)
所有命令和配置文件都可以在源码包编译成功的目录下面找到。
./scripts/drbd.conf是最原始的配置文件,当/etc/drbd.conf被破坏,可以直接拷贝覆盖掉。
C、创建硬件设备drbd
mknod /dev/drbd0 b 147 0
mknod /dev/drbd1 b 147 1
mknod /dev/drbd2 b 147 2
或者用shell来建立多个:
#for i in $(seq 0 15) ; do mknod /dev/drbd$i b 147 $i ; done
D、DRBD 协议说明
A 数据一旦写入磁盘并发送到网络中就认为完成了写入操作。
B 收到接收确认就认为完成了写入操作。
C 收到写入确认就认为完成了写入操作。
您还可以选择其它参数来将数据传输给磁盘和网络选项。更多详情,请参见drbdsetup 手册页。
四:配置drbd
修改/etc/drbd.conf
主要修改了:机器名和设备名ip地址
==================================================
on web1{
device /dev/drbd0;
disk /dev/hdc3;
address 192.168.0.136:7788;
meta-disk internal;
# meta-disk is either 'internal' or '/dev/ice/name [idx]'
#
# You can use a single block device to store meta-data
# of multiple DRBD's.
# E.g. use meta-disk /dev/hde6[0]; and meta-disk /dev/hde6[1];
# for two different resources. In this case the meta-disk
# would need to be at least 256 MB in size.
#
# 'internal' means, that the last 128 MB of the lower device
# are used to store the meta-data.
# You must not give an index with 'internal'.
}
on web2 {
device /dev/drbd0;
disk /dev/hdc2;
address 192.168.0.139:7788;
meta-disk internal;
}
==================================================
下面是整个drbd.conf的配置文件:
注意:配置的版本是drbd-0.7.19.tar.gz,不同版本无法兼容。修改的地方就以上几个地方和注释,还有把除了resource r0 外的其他配置块如resource r1等全部去掉。
也就是说
/****
on root {
device /dev/drbd0;
disk /dev/hdc2;
address 192.168.0.139:7788;
meta-disk internal;
}
}
****/
后面的所有内容全部去掉,
下面是一个完整的配置好的drbd.conf
#
# drbd.conf example
#
skip {
As you can see, you can also comment chunks of text
with a 'skip[optional nonsense]{ skipped text }' section.
This comes in handy, if you just want to comment out
some 'resource <some name> {...}' section:
just precede it with 'skip'.
The basic format of option assignment is
<option name><linear whitespace><value>;
It should be obvious from the examples below,
but if you really care to know the details:
<option name> :=
valid options in the respective scope
<value> := <num>|<string>|<choice>|...
depending on the set of allowed values
for the respective option.
<num> := [0-9]+, sometimes with an optional suffix of K,M,G
<string> := (<name>|\"([^\"\\\n]*|\\.)*\")+
<name> := [/_.A-Za-z0-9-]+
}
# global {
# use this if you want to define more resources later
# without reloading the module.
# by default we load the module with exactly as many devices
# as configured mentioned in this file.
#
# minor-count 5;
# The user dialog counts and displays the seconds it waited so
# far. You might want to disable this if you have the console
# of your server connected to a serial terminal server with
# limited logging capacity.
# The Dialog will print the count each 'dialog-refresh' seconds,
# set it to 0 to disable redrawing completely. [ default = 1 ]
#
# dialog-refresh 5; # 5 seconds
# You might disable one of drbdadm's sanity check.
# disable-ip-verification;
# }
#
# this need not be r#, you may use phony resource names,
# like "resource web" or "resource mail", too
#
resource r0 {
protocol C;
# what should be done in case the cluster starts up in
# degraded mode, but knows it has inconsistent data.
incon-degr-cmd "echo '!DRBD! pri on incon-degr' | wall ; sleep 60 ; halt -f";
startup {
degr-wfc-timeout 120; # 2 minutes.
}
disk {
}
net {
}
syncer {
rate 10M;
group 1;
al-extents 257;
}
on web1{
device /dev/drbd0;
disk /dev/hdc3;
address 192.168.0.136:7788;
meta-disk internal;
}
on web2 {
device /dev/drbd0;
disk /dev/hdc2;
address 192.168.0.139:7788;
meta-disk internal;
}
}
=================================================
五、启动drbd
先确认两台要镜像的机器是否正常,之间的网络是否通畅,需要加载的硬盘是否处于umount状态。
A、 drbd采用的是模块控制的方式
所以先要加载drbd.ko 模块
在136服务器执行:
#insmod drbd.ko 或者modprobe drbd
drbd.ko可以在编译好的源码包里找到。
判断是否加载成功可以使用lsmod来查看:
#lsmod
Module size Used by
drbd 143088 -
有的话表示加载模块成功
#drbdadm up all
启动drbd服务,使他挂在后台状态下运行.
可以使用命令netstat -an查看
有启动端口7788,同时也监听对方的7788端口,来实现数据交换。
netstat -ant的输出结果里有一行:
#netstat -ant
tcp 0 0 192.168.0.136:7789 0.0.0.0:* LISTEN
B:在139服务器执行
#modprobe drbd
#/etc/rc.d/init.d/drbd start
netstat -atn的输出结果,说明两台虚拟机的drbd服务已经连接上了:
#netstat -ant
tcp 0 0 192.168.0.136:7789 192.168.0.139:32845 ESTABLISHED
tcp 0 0 192.168.0.136:32770 192.168.0.139:7789 ESTABLISHED
六:设置权限
drbd的基本服务都起来了,现在需要对主的服务器也就使192.168.0.136这台服务器进行配置,让他能够对drbd0设备进行读写。
在136机器上运行
#drbdadm -- --do-what-I-say primary all #设置136服务器为同步主目录,也就是同步以136的这个分区为准.
注意命令格式需要一样,没有任何提示的话表示基本上成功了
#sfdisk -s
可以看见有一个硬件设备:/dev/drbd0
此时,两台设备之间就建立起一个镜像,您可以查看/proc/drbd 进行核实。
# cat /proc/drbd
如果原来硬盘没有文件系统的话,现在您可以在设备/dev/drbd0上创建一个文件系统,然后把它加载到136上。
#mkfs.ext3 -j /dev/drbd0
#mkdir /mnt/gaojf
# mount /dev/drbd /mnt/gaojf
现在/dev/drbd0就等于你服务器上面的一个硬件设备,你可以对他进行任何的读写操作。
七:drbd测试:
1:在136主服务器上:
#drbdadm primary all
#touch /mnt/gaojf/gaojf
完后,在执行
#umount /mnt/gaojf
#drbdadm secondary all
2:接着到139备用服务器上执行
#drbdadm primary all
#mkdir -p /mnt/gaojf
#mount /dev/drbd0 /mnt/gaojf
#ls -l /mnt/gaojf/gaojf
gaojf
Ok,没问题,可以看到数据在136服务器写入,在139马上可以看到.
几点注意的地方:
1. mount drbd设备以前必须把设备切换到primary状态。
2. 两个节点中,同一时刻只能有一台处于primary状态,另一台处于secondary状态。
3. 处于secondary状态的服务器上不能加载drbd设备。
4. 主备服务器同步的两个分区大小最好相同,这样不至于浪费磁盘空间,因为drbd磁盘镜像相当于网络raid 1.
5: 在配置过程中,如果出现下面错误
Missing argument 'meta_data_dev'
USAGE:
disk lower_dev meta_data_dev meta_data_index [{--size|-d} 0 ... 8587575296]
[{--on-io-error|-e} {pass_on|call-local-io-error|detach}]
[{--fencing|-f} {dont-care|resource-only|resource-and-stonith}] [{--use-bmbv|-b}]
那么就还要初始化meta-data area
drbdadm create-md r0
更详细的参考:http://www.ixdba.net/article/5c/125.html |
|