Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM
[root@heartbeat01 ~]# fdisk -cu /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x198bc436.
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)
Command (m for help): p
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders, total 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x198bc436
Device Boot Start End Blocks Id System
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-4194303, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303):
Using default value 4194303
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks. 4、编辑drbd配置文件
根据drbd的官方文档,drbd的配置文件是/etc/drbd.conf,但是这个文件中只有简单的2行内容,它引用了/etc/drbd.d/下的global_common.conf和*.res文件,这点和nginx、apache的配置文件类似,我们可以把多个resource相同的定义放到global中去,然后添加不同的res文件,从而更有效和方便的管理drbd的配置文件。
下面是/etc/drbd.conf的默认配置:
[root@heartbeat01 ~]# egrep -v "#|^$" /etc/drbd.conf
include "drbd.d/global_common.conf";
include "drbd.d/*.res";
下面是/etc/drbd.d/global_common.conf的默认配置:
[root@heartbeat01 ~]# egrep -v "#|^$" /etc/drbd.d/global_common.conf
global {
usage-count yes;
}
common {
protocol C;
handlers {
}
startup {
}
disk {
}
net {
}
syncer {
}
}
下面是我新建的一个配置文件test.res的内容:
resource test
{
startup {
wfc-timeout 30;
outdated-wfc-timeout 20;
degr-wfc-timeout 30;
}
net {
cram-hmac-alg sha1;
shared-secret sync_disk;
}
syncer {
rate 10M;
al-extents 257;
on-no-data-accessible io-error;
}
on heartbeat01.contoso.com {
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.49.133:7788;
flexible-meta-disk internal;
}
on heartbeat02.contoso.com {
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.49.134:7788;
meta-disk internal;
}
}
然后可以将此配置文件使用scp拷贝到节点2上去,两边的配置文件需要保持一致。
[root@heartbeat01 drbd.d]# scp test.res heartbeat02:/etc/drbd.d/
The authenticity of host 'heartbeat02 (192.168.49.134)' can't be established.
RSA key fingerprint is f9:ce:14:5d:cd:bb:3c:b4:0d:0b:fc:21:3a:92:43:6b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'heartbeat02,192.168.49.134' (RSA) to the list of known hosts.
root@heartbeat02's password:
test.res 100% 444 0.4KB/s 00:00 5、启动drbd
drbdadm create-md clusterdb #初始化metadata数据存储
service drbd start #启动drbd服务 上面的操作需要到两台server上运行,这里以其中一台为例演示结果:
[root@heartbeat01 drbd.d]# drbdadm create-md test
Writing meta data...
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created.
success
[root@heartbeat01 drbd.d]# service drbd start
Starting DRBD resources: [ d(test) s(test) n(test) ]..........
***************************************************************
DRBD's startup script waits for the peer node(s) to appear.
- In case this node was already a degraded cluster before the
reboot the timeout is 30 seconds. [degr-wfc-timeout]
- If the peer was available before the reboot the timeout will
expire after 30 seconds. [wfc-timeout]
(These values are for resource 'test'; 0 sec -> wait forever)
To abort waiting enter 'yes' [ 11]:
.
这里给出很多信息,大多是因为对端节点并没有启动,所以配置文件中的等待时间生效,但是在节点1启动后,我们再启动节点2时的信息就没有这么多了,因为节点1已经启动,所以无须等待。
[root@heartbeat02 drbd.d]# service drbd start
Starting DRBD resources: [ d(test) s(test) n(test) ].
此时,两个节点上都是Secondary的状态,所以我们需要手动的指定哪个节点为primary。
[root@heartbeat01 drbd.d]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
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:2096028
[root@heartbeat02 drbd.d]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
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:2096028
在要作为主节点的server上执行如下命令:
drbdadm -- --overwrite-data-of-peer primary all(是两个--,不是打错) 这里我在节点1上执行该命令。
[root@heartbeat01 drbd.d]# drbdadm -- --overwrite-data-of-peer primary all
再次看一下两个节点的状态:
[root@heartbeat01 drbd.d]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r-----
ns:217088 nr:0 dw:0 dr:217752 al:0 bm:13 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:1878940
[=>..................] sync'ed: 10.6% (1878940/2096028)K
finish: 0:03:01 speed: 10,344 (10,336) K/sec
[root@heartbeat02 drbd.d]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
0: cs:SyncTarget ro:Secondary/Primary ds:Inconsistent/UpToDate C r-----
ns:0 nr:485248 dw:485248 dr:0 al:0 bm:29 lo:1 pe:1 ua:0 ap:0 ep:1 wo:f oos:1610780
[===>................] sync'ed: 23.3% (1610780/2096028)K
finish: 0:02:34 speed: 10,424 (10,324) want: 10,240 K/sec
可以看到现在heartbeat01是Primary的状态,而heartbeat02是Secondary的状态,而且已经开始从主上面同步数据到从节点上了。
[root@heartbeat01 drbd.d]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:2096028 nr:0 dw:0 dr:2096692 al:0 bm:128 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
同步完成,两个节点都是UpToDate的状态。 6、创建文件系统并挂载
下面在主节点上为drbd设置创建文件系统
[root@heartbeat01 drbd.d]# mkfs.ext4 /dev/drbd0
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524007 blocks
26200 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912