DRBD是一个用软件实现的、无共享的、服务器之间镜像块设备内容的存储复制解决方案。DRBD的功能是通过linux内核实现的,它是通过tcp/ip协议,和网卡通信保持数据同步的。
DRBD有三种协议:
协议A:数据在本地完成写操作且数据已经发送到TCP/IP协议栈的队列中,则认为写操作完成。如果本地节点的写操作完成,此时本地节点发生故障,而数据还处在TCP/IP队列中,则数据不会发送到对端节点上。因此,两个节点的数据将不会保持一致。这种协议虽然高效,但是并不能保证数据的可靠性。
协议B:数据在本地完成写操作且数据已到达对端节点则认为写操作完成。如果两个节点同时发生故障,即使数据到达对端节点,这种方式同样也会导致在对端节点和本地节点的数据不一致现象,也不具有可靠性。
协议C:只有当本地节点的磁盘和对端节点的磁盘都完成了写操作,才认为写操作完成。这是集群流行的一种方式,应用也是最多的,这种方式虽然不高效,但是最可靠。
环境:
192.168.40.12 centosA
192.168.40.19 centosB
修改hosts文件保持两台服务器能互相通信,然后建立ssh互信:
[root@centosa ~]# ssh-keygen [root@centosa ~]# ssh-copy-id centosB
设置时间同步:
[root@centosa ~]# crontab -l */5 * * * * /usr/sbin/ntpdatecn.pool.ntp.org [root@centosb ~]# crontab -l */5 * * * * /usr/sbin/ntpdatecn.pool.ntp.org
安装DRBD:
[root@centosa ~]# rpm --importhttps://www.elrepo.org/RPM-GPG-KEY-elrepo.org [root@centosa ~]# rpm -Uvhhttp://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm [root@centosa ~]# yum install -ykmod-drbd84 drbd84-utils
两个节点都安装。
修改配置文件:
[root@centosa ~]# cat/etc/drbd.d/global_common.conf # DRBD is the result of over a decade ofdevelopment by LINBIT. # In case you need professional servicesfor DRBD or have # feature requests visithttp://www.linbit.com global { usage-countno; #是否参加DRBD使用统计,默认为yes。官方统计drbd的装机量 #Decide what kind of udev symlinks you want for "implicit" volumes #(those without explicit volume{} block, implied vnr=0): #/dev/drbd/by-resource/ / (explicit volumes) #/dev/drbd/by-resource/ (default for implict) udev-always-use-vnr;# treat implicit the same as explicit volumes #minor-count dialog-refresh disable-ip-verification #cmd-timeout-short 5; cmd-timeout-medium 121; cmd-timeout-long 600; } common { protocol C; #使用DRBD的同步协议 handlers{ #These are EXAMPLE handlers only. #They may have severe implications, #like hard resetting the node under certain circumstances. #Be careful when choosing 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; #quorum-lost "/usr/lib/drbd/notify-quorum-lost.sh root"; } startup{ #wfc-timeout degr-wfc-timeout outdated-wfc-timeout wait-after-sb } options{ #cpu-mask on-no-data-accessible #RECOMMENDED for three or more storage nodes with DRBD 9: #quorum majority; #on-no-quorum suspend-io | io-error; } disk{ on-io-errordetach; #配置I/O错误处理策略为分离 #size on-io-error fencing disk-barrier disk-flushes #disk-drain md-flushes resync-rate resync-after al-extents # c-plan-ahead c-delay-targetc-fill-target c-max-rate # c-min-rate disk-timeout } net{ #protocol timeout max-epoch-size max-buffers #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 } syncer{ rate 1024M; #设置主备节点同步时的网络速率 } }
创建配置文件:
[root@centosa ~]# cat /etc/drbd.d/mysql.res resource mysql { ##资源名称 protocol C; ##使用的协议 meta-disk internal; device /dev/drbd1; ##DRBD设备名称 syncer { verify-alg sha1; ##加密算法 } net { allow-two-primaries; } on centosa { disk /dev/sdb1; ##drbd使用的磁盘分区 address 192.168.40.12:7789; ##设置drbd监听地址与端口 } on centosb { disk /dev/sdb1; address 192.168.40.19:7789; } }
将配置文件复制到对端节点:
[root@centosa ~]# scp -rp /etc/drbd.d/*centosb:/etc/drbd.d/
在centosa上启动:
[root@centosa ~]# drbdadm create-md mysql initializing activity log initializing bitmap (64 KB) to all zero Writing meta data... New drbd meta data block successfullycreated.
[root@centosa ~]# modprobe drbd
查看内核是否已加载了模块:
[root@centosa ~]# lsmod | grep drbd drbd 396875 0 libcrc32c 12644 2 xfs,drbd
用drbd启动mysql并设置centosa为主mysql:
[root@centosa ~]# drbdadm up mysql [root@centosa ~]# drbdadm -- --forceprimary mysql
查看状态:
[root@centosa ~]# cat /proc/drbd version: 8.4.10-1 (api:1/proto:86-101) GIT-hash:a4d5de01fffd7e4cde48a080e2c686f9e8cebf4c build by mockbuild@, 2017-09-1514:23:22 1:cs:WFConnection ro:Primary/Unknown ds:UpToDate/DUnknown C r----s ns:0 nr:0 dw:0 dr:912 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:foos:2096028
在对端节点执行操作:
[root@centosb ~]# drbdadm create-md mysql initializing activity log initializing bitmap (64 KB) to all zero Writing meta data... New drbd meta data block successfullycreated. [root@centosb ~]# modprobe drbd [root@centosb ~]# drbdadm up mysql
在对端查看状态:
[root@centosb ~]# cat /proc/drbd version: 8.4.10-1 (api:1/proto:86-101) GIT-hash:a4d5de01fffd7e4cde48a080e2c686f9e8cebf4c build by mockbuild@, 2017-09-1514:23:22 1:cs:SyncTarget ro:Secondary/Primary ds:Inconsistent/UpToDate C r----- ns:0 nr:1748992 dw:1748992 dr:0 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:foos:347036 [===============>....]sync'ed: 83.6% (347036/2096028)K finish:0:00:08 speed: 39,832 (39,748) want: 59,560 K/sec
格式化并挂载:
[root@centosa ~]# mkfs.xfs /dev/drbd1 [root@centosa ~]# mount /dev/drbd1 /data
在数据库添加一个数据库:
MariaDB [(none)]> create databasecentos; Query OK, 1 row affected (0.04 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | centos | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
查看数据库存放位置:
[root@centosa ~]# ls /data aria_log.00000001 aria_log_control centos ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test
停止数据库并卸载:
[root@centosa ~]# systemctl stop mariadb [root@centosa ~]# umount /data
要想从能挂载必须将主降为从:
[root@centosa ~]# drbdadm secondary mysql
然后去到从,将从升为主然后挂载:
[root@centosb ~]# drbdadm primary mysql [root@centosb ~]# mount /dev/drbd1 /data
查看是否有数据库文件:
[root@centosb ~]# cd /data [root@centosb data]# ls aria_log.00000001 aria_log_control centos ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test