drbd---分布式复制块设备,是一个用软件实现的,无共享的,服务器之间镜像块设备内容的存储复制解决方案,drbd是镜像块设备,是按照数据位镜像成一样的数据块
主节点写操作完成抽就认为操作完成,并不会考虑备节点的写操作是否完成,容易丢失数据
主节点写操作完成且主节点把数据发送给备节点后就认为写操作完成,并不会考虑当数据发送给备节点后,备节点是否完成了写操作,也容易丢失数据
主节点写操作完成且备节点写操作完成,主节点才认为写操作完成,数据不会丢失,保证了数据的一致性和安全性 推荐使用
说明:drbd对网络环境要求比较高,建议主备节点在同一机房中,使用内网环境进行数据同步
1. 两台centos6.9虚拟机
主节点---lb01---内网172.16.1.5
备节点---lb02---内网172.16.1.6
2. 两台机器时间同步正确:
3. 两台机器互信机制:基于ssh密钥可以登录
4. hosts文件可以互相解析
5. 添加硬盘,并创建一个主分区/dev/sdb1 两台服务器的分区大小要一致
[root@lb01 drbd.d]# fdisk -l
Disk /dev/sdb: 106 MB, 106954752 bytes
64 heads, 32 sectors/track, 102 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xdf2b82d7
Device Boot Start End Blocks Id System
/dev/sdb1 1 102 104432 83 Linux
[root@lb02 drbd.d]# fdisk -l
Disk /dev/sdb: 106 MB, 106954752 bytes
64 heads, 32 sectors/track, 102 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb01e26ff
Device Boot Start End Blocks Id System
/dev/sdb1 1 102 104432 83 Linux
[root@lb02 drbd.d]# cat global_common.conf
# DRBD is the result of over a decade of development by LINBIT.
# In case you need professional services for DRBD or have
# feature requests visit http://www.linbit.com
global {
usage-count yes;
# minor-count dialog-refresh disable-ip-verification
# cmd-timeout-short 5; cmd-timeout-medium 121; cmd-timeout-long 600;
}
common {
handlers {
# These are EXAMPLE handlers only.
# They may have severe implications,
# like hard resetting the node under certain circumstances.
# Be careful when chosing your poison.
# pri-on-incon-degr "/usr/lib/drbd/notify-pri-on-incon-degr.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f";
# pri-lost-after-sb "/usr/lib/drbd/notify-pri-lost-after-sb.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f";
# local-io-error "/usr/lib/drbd/notify-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ; halt -f";
# fence-peer "/usr/lib/drbd/crm-fence-peer.sh";
# split-brain "/usr/lib/drbd/notify-split-brain.sh root";
# out-of-sync "/usr/lib/drbd/notify-out-of-sync.sh root";
# before-resync-target "/usr/lib/drbd/snapshot-resync-target-lvm.sh -p 15 -- -c 16k";
# after-resync-target /usr/lib/drbd/unsnapshot-resync-target-lvm.sh;
}
startup {
# wfc-timeout degr-wfc-timeout outdated-wfc-timeout wait-after-sb
}
options {
# cpu-mask on-no-data-accessible
}
disk {
# size on-io-error fencing disk-barrier disk-flushes
# disk-drain md-flushes resync-rate resync-after al-extents
# c-plan-ahead c-delay-target c-fill-target c-max-rate
# c-min-rate disk-timeout
on-io-error detach;
}
net {
# protocol timeout max-epoch-size max-buffers unplug-watermark
# connect-int ping-int sndbuf-size rcvbuf-size ko-count
# allow-two-primaries cram-hmac-alg shared-secret after-sb-0pri
# after-sb-1pri after-sb-2pri always-asbp rr-conflict
# ping-timeout data-integrity-alg tcp-cork on-congestion
# congestion-fill congestion-extents csums-alg verify-alg
# use-rle
after-sb-0pri discard-zero-changes;
}
syncer {
rate 1024M;
}
}
[root@lb01 drbd.d]# cat mysql.res
resource mysql {
protocol C;
meta-disk internal;
device /dev/drbd1;#相关的块设备需命名为/dev/drbdm,其中m是设备的次要号码
syncer {
verify-alg sha1;#支持复制传输数据完整性验证(MD5、SHA-1、CRC-32C)
}
net {
allow-two-primaries;
}
on lb01 {
disk /dev/sdb1; #在lb01创建的分区
address 172.16.1.5:7789;
}
on lb02 {
disk /dev/sdb1; #在lb02创建的分区
address 172.16.1.6:7789;
}
}
[root@lb01 drbd.d]# drbdadm create-md mysql
--== Thank you for participating in the global usage survey ==--
The server's response is:
you are the 8626th user to install this version
initializing activity log
NOT initializing bitmap
Writing meta data...
New drbd meta data block successfully created.
success
[root@lb01 drbd.d]# /etc/init.d/drbd start
Starting DRBD resources:
查看drbd状态的两种方法:
[root@lb01 drbd.d]# cat /proc/drbd
version: 8.4.9-1 (api:1/proto:86-101)
GIT-hash: 9976da086367a2476503ef7f6b13d4567327a280 build by mockbuild@Build64R6, 2016-12-13 18:38:15
1: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r-----
ns:0 nr:0 dw:0 dr:0 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:104392
[root@lb01 drbd.d]# drbd-overview
1:mysql/0 Connected Secondary/Secondary Inconsistent/Inconsistent
说明: Secondary/Secondary表示两个节点都处于Secondary辅助状态
[root@lb01 drbd.d]# drbdadm -- --overwrite-data-of-peer primary mysql
[root@lb01 drbd.d]# drbd-overview
1:mysql/0 Connected Primary/Secondary UpToDate/UpToDate 此时lb01成为主节点
说明:文件系统只能在主节点上进行,只有设置了主节点后才可以创建文件系统
[root@lb01 drbd.d]# mkfs.ext4 /dev/drbd1
进行挂载:
[root@lb01 /]# mount /dev/drbd1 /data/
[root@lb01 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.5G 17G 9% /
tmpfs 112M 0 112M 0% /dev/shm
/dev/sda1 190M 35M 146M 20% /boot
/dev/drbd1 95M 1.6M 89M 2% /data
[root@lb01 data]# touch jiang.txt 在data目录中创建文件用于验证
[root@lb01 data]# echo 123 >jiang.txt
[root@lb01 data]# cat jiang.txt
123
备节点上不允许对drbd设备进行任何操作,即使读操作也不可以,因为此时在备节点上根本不能挂载drbd设备,所有的读操作只能在主节点上进行,只有当主节点挂掉时,备节点才能提升为主节点继续工作
lb01 主节点上
[root@lb01 ~]# umount /data
[root@lb01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.5G 17G 9% /
tmpfs 112M 0 112M 0% /dev/shm
/dev/sda1 190M 35M 146M 20% /boot
[root@lb01 ~]# drbdadm secondary mysql
[root@lb01 ~]# drbd-overview
1:mysql/0 Connected Secondary/Secondary UpToDate/UpToDate
lb02 备节点上
[root@lb02 drbd.d]# drbdadm primary mysql
[root@lb02 drbd.d]# drbd-overview
1:mysql/0 Connected Primary/Secondary UpToDate/UpToDate
[root@lb02 drbd.d]# mkdir /data 共享目录是手工需要创建的
[root@lb02 drbd.d]# mount /dev/drbd1 /data/
[root@lb02 drbd.d]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.6G 17G 10% /
tmpfs 112M 0 112M 0% /dev/shm
/dev/sda1 190M 35M 146M 20% /boot
/dev/drbd1 95M 1.6M 89M 2% /data
[root@lb02 drbd.d]# cd /data/
[root@lb02 data]# ll
total 13
drwx------ 2 root root 12288 Mar 11 12:54 lost+found
-rw-r--r-- 1 root root 4 Mar 11 13:52 jiang.txt
[root@lb02 data]# cat jiang.txt
123 数据同步成功
注意:要想达到drbd主从节点故障的自动切换需要搭配heartbeat来实现,heartbeat可以实现当主节点挂掉后,自动切换备节点为主节点并挂载分区,实现高可用
原文地址:http://blog.51cto.com/13520772/2085127