linux下安装drbd详解
关于drbd理论方面的知识本文中不做介绍,本文章主要讲述如何搭建drbd。
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
---------------
-----------------
192.168.1.203 Centos-HA2
Building a new DOS disklabel with disk identifier 0xd59786d2.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261):
Using default value 261
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x9adc5d39.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261):
Using default value 261
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@Centos-HA2 ~]#mkdir /data
# ntpdate -u asia.pool.ntp.org
# 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 KDIR=/usr/src/kernels/2.6.32-431.29.2.el6.x86_64/
# make install
# mkdir -p /usr/local/drbd/var/run/drbd
# cp /usr/local/drbd/etc/rc.d/init.d/drbd /etc/init.d/
# chkconfig --add drbd
FATAL: Module drbd not found.
查看DRBD模块是否加载到内核: (节点1和节点2)
3.参数配置:(节点1和节点2)
# vi /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 Centos-HA1{
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.1.202:7788;
meta-disk internal;
}
on Centos-HA2{
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.1.203:7788;
meta-disk internal;
}
}
(4).创建DRBD设备并激活ro资源:(节点1和节点2)
# mknod /dev/drbd0 b 147 0
等待片刻,显示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]
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.
5.启动DRBD服务:(节点1,节点2)
# service drbd start
(7).将Centos-HA1主机配置为主节点:(节点1)
ro在主从服务器上分别显示 Primary/Secondary和Secondary/Primary
ds显示UpToDate/UpToDate
(8).挂载DRBD:(节点1)
从刚才的状态上看到mounted和fstype参数为空,所以我们这步开始挂载DRBD到系统目录
[root@Centos-HA1 ~]# mkfs.ext4 /dev/drbd0
[root@Centos-HA1 ~]# mount /dev/drbd0 /data
注:Secondary节点上不允许对DRBD设备进行任何操作,包括只读,所有的读写操作只能在Primary节点上进行,只有当Primary节点挂掉时,Secondary节点才能提升为Primary节点继续工作。
(9).模拟DRBD1故障,DRBD2接管并提升为Primary
(节点1)
[root@Centos-HA1 ~]#cd /data
[root@Centos-HA1 ~]# touch a.txt b.txt c.txt d.txt e.txt
[root@Centos-HA1 ~]#cd ..
[root@Centos-HA1 ~]# umount /data
[root@Centos-HA2 ~]# mount /dev/drbd0 /data
[root@Centos-HA2 ~]# cd /data
[root@Centos-HA2 ~]# touch f.txt g.txt h.txt j.txt k.txt
--------------
不过如何保证DRBD主从结构的智能切换,实现高可用,这里就需要Heartbeat来实现了。
注:(摘自酒哥的构建高可用LINUX服务器第2版)
[root@Centos-HA1 ~]# ls
a.txt b.txt c.txt e.txt g.txt j.txt lost+found
[root@Centos-HA2 ~]#ls
aa.txt bbb.txt b.txt cc.txt d.txt f.txt h.txt k.txt
[root@Centos-HA1 ~]# ls
aa.txt bb.txt cc.txt d.txt f.txt h.txt k.txt
a.txt b.txt c.txt e.txt g.txt j.txt lost+found
[root@Centos-HA1 ~]# drbdadm secondary r0
[root@Centos-HA2 ~]# ls
aaa.txt a.txt bb.txt ccc.txt c.txt e.txt g.txt j.txt lost+found
aa.txt bbb.txt b.txt cc.txt d.txt f.txt h.txt k.txt
[root@Centos-HA1 ~]# drbdadm secondary r0
[root@Centos-HA1 ~]# drbdadm disconnect all
[root@Centos-HA1 ~]# drbdadm --discard-my-data connect r0
[root@Centos-HA1 ~]# ls
aaa.txt a.txt bb.txt ccc.txt c.txt e.txt g.txt j.txt lost+found
aa.txt bbb.txt b.txt cc.txt d.txt f.txt h.txt k.txt