DRBD数据同步
By:大官人
一、DRBD简介
DRBD的全称为:DistributedReplicatedBlock Device(DRBD)分布式块设备复制,DRBD是由内核模块和相关脚本而构成,用以构建高可用性的集群。其实现方式是通过网络来镜像整个设备。你可以把它看作是一种网络RAID。它允许用户在远程机器上建立一个本地块设备的实时镜像。
二、DRBD是如何工作的呢?
(DRBDPrimary)负责接收数据,把数据写到本地磁盘并发送给另一台主机(DRBD Secondary)。另一个主机再将数据存到自己的磁盘中。目前,DRBD每次只允许对一个节点进行读写访问,但这对于通常的故障切换高可用集群来说已经足够用了。有可能以后的版本支持两个节点进行读写存取。
三、DRBD与HA的关系
一个DRBD系统由两个节点构成,与HA集群类似,也有主节点和备用节点之分,在带有主要设备的节点上,应用程序和操作系统可以运行和访问DRBD设备(/dev/drbd*)。在主节点写入的数据通过DRBD设备存储到主节点的磁盘设备中,同时,这个数据也会自动发送到备用节点对应的DRBD设备,最终写入备用节点的磁盘设备上,在备用节点上,DRBD只是将数据从DRBD设备写入到备用节点的磁盘中。现在大部分的高可用性集群都会使用共享存储,而DRBD也可以作为一个共享存储设备,使用DRBD不需要太多的硬件的投资。因为它在TCP/IP网络中运行,所以,利用DRBD作为共享存储设备,要节约很多成本,因为价格要比专用的存储网络便宜很多;其性能与稳定性方面也不错
四、DRBD复制模式
协议A:
异步复制协议。一旦本地磁盘写入已经完成,数据包已在发送队列中,则写被认为是完成的。在一个节点发生故障时,可能发生数据丢失,因为被写入到远程节点上的数据可能仍在发送队列。尽管,在故障转移节点上的数据是一致的,但没有及时更新。这通常是用于地理上分开的节点
协议B:
内存同步(半同步)复制协议。一旦本地磁盘写入已完成且复制数据包达到了对等节点则认为写在主节点上被认为是完成的。数据丢失可能发生在参加的两个节点同时故障的情况下,因为在传输中的数据可能不会被提交到磁盘
协议C:
同步复制协议。只有在本地和远程节点的磁盘已经确认了写操作完成,写才被认为完成。没有任何数据丢失,所以这是一个群集节点的流行模式,但I / O吞吐量依赖于网络带宽
一般使用协议C,但选择C协议将影响流量,从而影响网络时延。为了数据可靠性,我们在生产环境使用时须慎重选项使用哪一种协议
四、 DRBD工作原理图
DRBD是linux的内核的存储层中的一个分布式存储系统,可用使用DRBD在两台Linux服务器之间共享块设备,共享文件系统和数据。类似于一个网络RAID-1的功能,如图所示:
五.安装drbd
yum --enablerepo=elrepo installdrbd83-utils kmod-drbd83 -y
六.硬盘分区
将硬盘分为两个分区,注意第二个分区不要格式化,具体分区大小一般第二个分区站中大小的10%一般不超过20G(注:在他两台机器都要分区,且两台机器的硬盘大小和分区大小要一致)
root@heartbeat-master ~ 21:30:30 # fdisk /dev/sdb Device contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel withdisk identifier 0xba2ef2e6. Changes will remain in memory only,until you decide to write them. After that, of course, the previouscontent won't be recoverable. Warning: invalid flag 0x0000 ofpartition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode isdeprecated. It's strongly recommended to switch off the mode (command 'c') andchange 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-130, default 1): Using default value 1 Last cylinder, +cylinders or+size{K,M,G} (1-130, default 130): +800M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (104-130, default104): Using default value 104 Last cylinder, +cylinders or+size{K,M,G} (104-130, default 130): Using default value 130 Command (m for help): w3 The partition table has beenaltered! Calling ioctl() to re-readpartition table. Syncing disks. root@heartbeat-master ~ 21:31:05 #lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 398M 0 rom sda 8:0 0 8G 0 disk |-sda1 8:1 0 200M 0 part /boot |-sda2 8:2 0 1G 0 part [SWAP] `-sda3 8:3 0 6.8G 0 part / sdb 8:16 0 1G 0 disk |-sdb1 8:17 0 807.9M 0 part `-sdb2 8:18 0 211.8M 0 part root@heartbeat-master ~ 21:31:10 # mkfs.ext4 /dev/sdb1 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=0blocks 51744 inodes, 206829 blocks 10341 blocks (5.00%) reserved forthe super user First data block=0 Maximum filesystem blocks=213909504 7 block groups 32768 blocks per group, 32768fragments per group 7392 inodes per group Superblock backups stored onblocks: 32768, 98304, 163840 Writing inode tables: done Creating journal (4096 blocks):done Writing superblocks and filesystemaccounting information: done This filesystem will beautomatically checked every 29 mounts or 180 days, whichever comesfirst. Use tune2fs -c or -i to override.
七.drbd配置文件注解
global { # minor-count 64; # dialog-refresh 5; # 5 seconds # disable-ip-verification; usage-count no; } #************************************************************ common { protocol C; disk { on-io-error detach; no-disk-flushes; no-md-flushes; } net { sndbuf-size 512k; # timeout 60; # 6seconds (unit = 0.1 seconds) # connect-int 10; # 10 seconds (unit = 1 second) # ping-int 10; # 10 seconds (unit = 1 second) # ping-timeout 5; # 500 ms (unit = 0.1 seconds) max-buffers 8000; unplug-watermark 1024; max-epoch-size 8000; # ko-count 4; # allow-two-primaries; cram-hmac-alg "sha1"; shared-secret "hdhwXes23sYEhart8t"; after-sb-0pri disconnect; after-sb-1pri disconnect; after-sb-2pri disconnect; rr-conflict disconnect; # data-integrity-alg "md5"; # no-tcp-cork; } syncer { rate 630M; #同步速率 al-extents 517; } } #********************************************************************* resource data { #一个资源实例 on servername1 { #servername是主机名 device /dev/drbd0; #drbd设备 disk data-Partition; address ipaddr1:port; meta-disk meta-Partition [0]; } on servername2 { device /dev/drbd0; disk data-Partition; address ipaddr2:port; meta-disk meta-Partition [0]; } }
八.drbd实例
8.1环境
drbd-masterip:192.168.44.129disk:sdb1为数据盘,sdb2为drdb专用
drbd-slave:192.168.44.129 disk:同master
8.2修改配置文件(主从保持一致)
global { # minor-count 64; # dialog-refresh 5; # 5 seconds # disable-ip-verification; usage-count no; } #************************************************************ common { protocol C; disk { on-io-error detach; no-disk-flushes; no-md-flushes; } net { sndbuf-size 512k; # timeout 60; # 6seconds (unit = 0.1 seconds) # connect-int 10; # 10 seconds (unit = 1 second) # ping-int 10; # 10 seconds (unit = 1 second) # ping-timeout 5; # 500 ms (unit = 0.1 seconds) max-buffers 8000; unplug-watermark 1024; max-epoch-size 8000; # ko-count 4; # allow-two-primaries; cram-hmac-alg "sha1"; shared-secret "hdhwXes23sYEhart8t"; after-sb-0pri disconnect; after-sb-1pri disconnect; after-sb-2pri disconnect; rr-conflict disconnect; # data-integrity-alg "md5"; # no-tcp-cork; } syncer { rate 630M; # al-extents 517; } } #********************************************************************* resource data { onheartbeat-master { #servername device /dev/drbd0; #drbd disk /dev/sdb1; address 192.168.44.128:7788; meta-disk /dev/sdb2[0]; } on heartbeat-slave { device /dev/drbd0; disk /dev/sdb1; address 192.168.44.129:7788; meta-disk /dev/sdb2 [0]; } }
8.3主从配置data实例
在主从上执行以下命令
/etc/init.d/drbd start drbdadm create-md data drbdadm up data
1)主节点
root@heartbeat-master /etc 21:45:06# /etc/init.d/drbd start Starting DRBD resources: [ data Operation only supported for v8internal meta data Command '/sbin/drbdmeta 0 v08/dev/sdb2 0 check-resize' terminated with exit code 255 drbdadm check-resize data: exitedwith code 255 d(data) 0: Failure: (119) No validmeta-data signature found. ==> Use 'drbdadm create-md res' toinitialize meta-data area. <== [data] cmd /sbin/drbdsetup 0 disk/dev/sdb1 /dev/sdb2 0 --set-defaults --create-device --no-md-flushes--no-disk-flushes --on-io-error=detach failed - continuing! s(data) n(data) ]. root@heartbeat-master /etc 21:45:53# drbdadm create-md data Writing meta data... initializing activity log NOT initialized bitmap New drbd meta data blocksuccessfully created. root@heartbeat-master /etc 21:46:15# drbdadm up data
2)从节点
root@heartbeat-master /etc 21:45:06# /etc/init.d/drbd start Starting DRBD resources: [ data Operation only supported for v8internal meta data Command '/sbin/drbdmeta 0 v08/dev/sdb2 0 check-resize' terminated with exit code 255 drbdadm check-resize data: exitedwith code 255 d(data) 0: Failure: (119) No validmeta-data signature found. ==> Use 'drbdadm create-md res' toinitialize meta-data area. <== [data] cmd /sbin/drbdsetup 0 disk/dev/sdb1 /dev/sdb2 0 --set-defaults --create-device --no-md-flushes--no-disk-flushes --on-io-error=detach failed - continuing! s(data) n(data) ]. root@heartbeat-master /etc 21:45:53# drbdadm create-md data Writing meta data... initializing activity log NOT initialized bitmap New drbd meta data blocksuccessfully created. root@heartbeat-master /etc 21:46:15# drbdadm up data
8.4在主节点执行置主并且向从节点同步
root@heartbeat-master /etc 21:47:32# drbdadm -- --overwrite-data-of-peerprimary data root@heartbeat-master /etc 21:47:43# cat /proc/drbd version: 8.3.16(api:88/proto:86-97) GIT-hash:a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-2414:51:37 0: cs:SyncSource ro:Primary/Secondaryds:UpToDate/Inconsistent C r----- ns:164784 nr:0 dw:0 dr:176664 al:0 bm:10 lo:0 pe:4 ua:88 ap:0 ep:1 wo:doos:662964 [===>................] sync'ed: 20.3%(662964/827316)K #通过命令查看drbd开始同步 finish: 0:00:08 speed: 82,176 (82,176)K/sec root@heartbeat-master /etc 21:47:45# cat /proc/drbd version: 8.3.16 (api:88/proto:86-97) GIT-hash:a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-2414:51:37 0: cs:SyncSource ro:Primary/Secondaryds:UpToDate/Inconsistent C r----- ns:596796 nr:0 dw:0 dr:608664 al:0 bm:36 lo:0 pe:3 ua:88 ap:0 ep:1 wo:doos:230836 [=============>......] sync'ed:72.3% (230836/827316)K finish: 0:00:02 speed: 85,208 (85,208)K/sec root@heartbeat-master /etc 21:47:50# cat /proc/drbd version: 8.3.16(api:88/proto:86-97) GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985build by phil@Build64R6, 2014-11-24 14:51:37 0: cs:SyncSource ro:Primary/Secondaryds:UpToDate/Inconsistent C r----- ns:717300 nr:0 dw:0 dr:720664 al:0 bm:43 lo:0 pe:1 ua:22 ap:0 ep:1 wo:doos:110132 [================>...] sync'ed:87.2% (110132/827316)K finish: 0:00:01 speed: 79,684 (79,684)K/sec root@heartbeat-master /etc 21:47:52# cat /proc/drbd version: 8.3.16(api:88/proto:86-97) GIT-hash:a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-2414:51:37 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate Cr----- #出现uptodata同步完成 ns:827316 nr:0 dw:0 dr:827980 al:0 bm:51 lo:0 pe:0 ua:0 ap:0 ep:1 wo:doos:0
8.5挂在drbd设备写文件
root@heartbeat-master /etc 21:47:54# mount /dev/drbd0 /mnt root@heartbeat-master /etc 22:05:49# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 6.6G 1.3G 5.0G 21% / tmpfs 232M 0 232M 0% /dev/shm /dev/sda1 190M 50M 130M 28% /boot /dev/drbd0 780M 824K 739M 1% /mnt root@heartbeat-master /etc 22:05:53# echo "Hello Word" >/mnt/test.txt root@heartbeat-master /etc 22:06:21# cat /mnt/test.txt Hello Word
8.6主从切换验证同步
8.6.1将主置从
root@heartbeat-master /etc 22:09:13# umount /mnt/ root@heartbeat-master /etc 22:09:24# drbdadm secondary data
8.6.2将从置主
root@heartbeat-slave /etc 21:50:58# drbdadm -- --overwrite-data-of-peerprimary data root@heartbeat-slave /etc 22:09:49# cat /proc/drbd version: 8.3.16(api:88/proto:86-97) GIT-hash:a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-2414:51:37 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate Cr----- ns:0 nr:827412 dw:827412 dr:664 al:0 bm:51 lo:0 pe:0 ua:0 ap:0 ep:1 wo:doos:0
8.6.3从挂在验证
root@heartbeat-slave /etc 22:11:20# mount /dev/drbd0 /mnt root@heartbeat-slave /etc 22:11:32# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 6.6G 1.3G 5.0G 21% / tmpfs 232M 0 232M 0% /dev/shm /dev/sda1 190M 50M 130M 28% /boot /dev/drbd0 780M 828K 739M 1% /mnt root@heartbeat-slave /etc 22:11:36# cat /mnt/test.txt Hello Word
九.Heartbeat+drbd高可用实例
9.1环境保证双方都是从
root@heartbeat-slave /etc 22:13:31# cat /proc/drbd version: 8.3.16 (api:88/proto:86-97) GIT-hash:a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-2414:51:37 0: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate Cr----- ns:12 nr:827412 dw:827424 dr:1021 al:1 bm:51 lo:0 pe:0 ua:0 ap:0 ep:1wo:d oos:0
9.2配置haresources
root@heartbeat-master /etc 22:14:05 # cat ha.d/haresources #daguanren services heartbeat-masterIPaddr::192.168.44.100/24/eth0 nginxd drbddisk::data Filesystem::/dev/drbd0::/mnt::ext4
9.3启动heartbeat
root@heartbeat-master/etc 22:20:32 # service heartbeat start StartingHigh-Availability services: INFO: Resource is stopped Done. root@heartbeat-master/etc 22:21:10 # ip a 1:lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stateUP qlen 1000 link/ether 00:0c:29:c2:37:fa brdff:ff:ff:ff:ff:ff inet 192.168.44.128/24 brd 192.168.44.255scope global eth0 inet 192.168.44.100/24brd 192.168.44.255 scope global secondary eth0 inet6 fe80::20c:29ff:fec2:37fa/64 scopelink valid_lft forever preferred_lft forever 3:eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stateUP qlen 1000 link/ether 00:0c:29:c2:37:04 brdff:ff:ff:ff:ff:ff inet 192.168.48.128/24 brd 192.168.48.255scope global eth1 inet6 fe80::20c:29ff:fec2:3704/64 scopelink valid_lft forever preferred_lft forever root@heartbeat-master/etc 22:21:12 # df-h Filesystem Size Used Avail Use% Mounted on /dev/sda3 6.6G 1.3G 5.0G 21% / tmpfs 232M 0 232M 0% /dev/shm /dev/sda1 190M 50M 130M 28% /boot /dev/drbd0 780M 828K 739M 1% /mnt root@heartbeat-master/etc 22:21:15 # cat /mnt/test.txt Hello Word root@heartbeat-master/etc 22:21:45 #
9.4停止主节点heartheat测试切换
1)主节点
root@heartbeat-master/etc 22:21:45 # service heartbeat stop StoppingHigh-Availability services: Done. root@heartbeat-master/etc 22:24:09 # ip a 1:lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stateUP qlen 1000 link/ether 00:0c:29:c2:37:fa brdff:ff:ff:ff:ff:ff inet 192.168.44.128/24 brd 192.168.44.255scope global eth0 inet6 fe80::20c:29ff:fec2:37fa/64 scopelink valid_lft forever preferred_lft forever 3:eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UPqlen 1000 link/ether 00:0c:29:c2:37:04 brdff:ff:ff:ff:ff:ff inet 192.168.48.128/24 brd 192.168.48.255scope global eth1 inet6 fe80::20c:29ff:fec2:3704/64 scopelink valid_lft forever preferred_lft forever root@heartbeat-master/etc 22:24:14 # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 6.6G 1.3G 5.0G 21% / tmpfs 232M 0 232M 0% /dev/shm /dev/sda1 190M 50M 130M 28% /boot
2)从节点
root@heartbeat-slave/etc 22:20:45 # ip a 1:lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stateUP qlen 1000 link/ether 00:0c:29:b7:b2:cf brdff:ff:ff:ff:ff:ff inet 192.168.44.129/24 brd 192.168.44.255scope global eth0 inet 192.168.44.100/24brd 192.168.44.255 scope global secondary eth0 inet6 fe80::20c:29ff:feb7:b2cf/64 scopelink valid_lft forever preferred_lft forever 3:eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stateUP qlen 1000 link/ether 00:0c:29:b7:b2:d9 brdff:ff:ff:ff:ff:ff inet 192.168.48.129/24 brd 192.168.48.255scope global eth1 inet6 fe80::20c:29ff:feb7:b2d9/64 scopelink valid_lft forever preferred_lft forever root@heartbeat-slave/etc 22:25:17 # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 6.6G 1.3G 5.0G 21% / tmpfs 232M 0 232M 0% /dev/shm /dev/sda1 190M 50M 130M 28% /boot /dev/drbd0 780M 828K 739M 1% /mnt root@heartbeat-slave/etc 22:25:22 # cat /mnt/test.txt Hello Word
9.5启动主节点heartbeat测试恢复
1)主节点
root@heartbeat-master/etc 22:24:19 # service heartbeat start StartingHigh-Availability services: INFO: Resource is stopped Done. root@heartbeat-master/etc 22:27:06 # ip a 1:lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stateUP qlen 1000 link/ether 00:0c:29:c2:37:fa brdff:ff:ff:ff:ff:ff inet 192.168.44.128/24 brd 192.168.44.255scope global eth0 inet 192.168.44.100/24brd 192.168.44.255 scope global secondary eth0 inet6 fe80::20c:29ff:fec2:37fa/64 scopelink valid_lft forever preferred_lft forever 3:eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stateUP qlen 1000 link/ether 00:0c:29:c2:37:04 brdff:ff:ff:ff:ff:ff inet 192.168.48.128/24 brd 192.168.48.255scope global eth1 inet6 fe80::20c:29ff:fec2:3704/64 scopelink valid_lft forever preferred_lft forever root@heartbeat-master/etc 22:27:12 #df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 6.6G 1.3G 5.0G 21% / tmpfs 232M 0 232M 0% /dev/shm /dev/sda1 190M 50M 130M 28% /boot /dev/drbd0 780M 828K 739M 1% /mnt root@heartbeat-master/etc 22:27:20 # cat /mnt/test.txt Hello Word
2)从节点
root@heartbeat-slave/etc 22:25:30 # ip a 1:lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stateUP qlen 1000 link/ether 00:0c:29:b7:b2:cf brdff:ff:ff:ff:ff:ff inet 192.168.44.129/24 brd 192.168.44.255scope global eth0 inet6 fe80::20c:29ff:feb7:b2cf/64 scope link valid_lft forever preferred_lft forever 3:eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stateUP qlen 1000 link/ether 00:0c:29:b7:b2:d9 brdff:ff:ff:ff:ff:ff inet 192.168.48.129/24 brd 192.168.48.255scope global eth1 inet6 fe80::20c:29ff:feb7:b2d9/64 scopelink valid_lft forever preferred_lft forever root@heartbeat-slave/etc 22:28:33 # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 6.6G 1.3G 5.0G 21% / tmpfs 232M 0 232M 0% /dev/shm /dev/sda1 190M 50M 130M 28% /boot root@heartbeat-slave/etc 22:28:38 #