DRBD--实现主从双节点的共享储存

简介:

1:DRBD概念:
DRBD : Distributed Replicated Block Device ,分布式的复制块设备
---------------------------------------------------------------------------------------------
有linbit公司所研发出来的,但其完全遵循GPL协定,是一个开源软件,
在linux内核2.6.33以后的版本,DRBD已经做进内核。
----------------------------------------------------------------------------------------------
2:DRBD工作原理图:

 

(1) DRDB工作在内核空间的,图中灰色部分为内核空间。
(2) 数据存储的过程:
我们的应用程序需要储存数据时,通过文件系统调用(File System),经过缓存(Buffer Cache),
来到DRBD上,会复制一份,经过TCP/IP封装打包,经过本节点网卡,到达另一节点的NIC,
NIC Diver,TCP/IP,最后到达另一节点的DRBD,再经过磁盘调度器(Disk Scheduler),磁盘驱动
(Disk Driver),最后保存到其对应的磁盘上(Disk Storage)。
(3) 在另一节点储存数据是没有进入用户空间的。
(4) DRBD是内核的一个子系统。DRBD补充或说是完善了内核中关于磁盘存储的一个子系统。
(5) DRBD 必须在内核中,在linux内核为2.6.33以前的,DRBD官方为其做了rpm的补丁包,它把
DRBD功能做成了内核模块(kmod),只要装上其模块,就可以提供DRBD的功能了。
----------------------------------------------------------------------------------------------------------------------------------
3:DRBD的客户端工具:可以定义把那些设备定义成DRBD模块的。
 drbdadm
 The high-level administration tool of the DRBD program suite
 It obtains all DRBD configuration parameters from the configuration file /etc/drbd.conf
 /etc/drbd.conf 就是其主配置文件。
 drbdsetup
 The program that allows users to configure the DRBD module that has been loaded into the running kernel 
 It is the low-level tool within the DRBD program suite 
 drbdmeta
 The program which allows users to create, dump, restore, and modify DRBD's meta data structures
4:通俗的理解同步与异步的概念:
同步:是需要提前通知其节点,进行数据的传输等操作
异步:是不需要提前通知其节点,就进行数据的传输工作等。
5:DRBD的特征:
 DRBD
 Distributed Replicated Block Device
 A software-based, shared-nothing, replicated storage solution mirroring the content of block devices (hard disks, partitions, logical volumes etc.) between servers
##是一个基于软件的,没有任何东西是共享的,复制的存储解决方案,它是把两个节点的
磁盘,分区,逻辑卷做成了镜像。
 DRBD's core functionality is implemented by way of a Linux kernel module
 DRBD mirrors data ##DRDB实现数据镜像的特点
 In real time ##实时的
 Transparently ##透明的,对于上层应用程序它们是察觉不到的
 Synchronously or asynchronously ##同步或异步
6:DRBD中相关的概念:
(1):Resources 资源
 the collective term that refers to all aspects of a particular replicated storage device
 Include
 Resource name
 This can be any arbitrary, US-ASCII name not containing whitespace by which the resource is referred to
 ##资源的名字只能使用纯粹的ASCII的格式,不能包括空格的
DRBD device
 This is the virtual block device managed by DRBD
 It has a device major number of 147, and its minor numbers are numbered from 0 onwards, as is customary 
 ##主设备号是147,而次设备号从0开始编号的。
 The associated block device is always named /dev/drbdn, where n is the device minor number
 Disk configuration ##磁盘相关的配置信息
 This entails the local copy of the data, and meta data for DRBD's internal use
 ##为了创建DRBD设备,用到那个节点的那个分区。及DRBD的原数据存放的位置。
 Network configuration ##网络相关的配置信息
 This entails all aspects of DRBD's communication with the peer node
 ##在用网络传输数据时是否需要认证,指定可用带宽等等。

## 对于DRBD资源是不可以流转的,可以对比一下HA的资源的相关概念。
## 每一个资源需要靠一个资源名来标配
----------------------------------------------------------------------------------------------------------------
(2)Resource roles 资源角色
 In DRBD, every resource has a role, which may be Primary or Secondary
##对于DRDB,每一个资源都有一个角色,
 Primary
 A DRBD device in the primary role can be used unrestrictedly for read and write operations
 ##在某时刻,被标记为Primary的设备,可以进行无限制的读与写的操作。
 It may be used for creating and mounting file systems, raw or direct I/O to the block device, etc
 ##可以格式化,挂载,支持裸IO设备。
 Secondary
 A DRBD device in the secondary role receives all updates from the peer node's device, but otherwise disallows access completely
 ##对于从节点的RDBD设备来说,只接受来自主节点的数据写入磁盘,但不允许本地的
 任何操作。
 It can not be used by applications, neither for read nor write access
## 不能够挂载,读与写。
## DRBD一般来说是针对两节点的设备。
## 对于DRDB来说,只有主节点才可以,让进行写,读操作。
## 主,从节点的位置可以互换的。
## 对于主,从节点的数据是相同的,当主节点故障时,其从节点可以提供相同的服务。
--------------------------------------------------------------------------------------------------------
(3) DRBD Modes DRBD的工作模式
单主节点:所谓的主从模型
 Single-primary mode
 Any resource is, at any given time, in the primary role on only one cluster member
 Only one cluster node manipulates the data at any moment
 This mode can be used with any conventional file system (ext3, ext4, XFS etc.)
 Deploying DRBD in single-primary mode is the canonical approach for high availability (fail-over capable) clusters
 ## 单主节点主要是为高可用集群解决资源流转的问题的。
双主节点:
Dual-primary mode
 Any resource is, at any given time, in the primary role on both cluster nodes
 Since concurrent access to the data is thus possible, this mode requires the use of a shared cluster file system that utilizes a distributed lock manager
 Examples include GFS and OCFS2
 Deploying DRBD in dual-primary mode is the preferred approach for load-balancing clusters which require concurrent data access from two nodes
 ##提供类似集群负载均衡的功能
 Disabled by default, and must be enabled explicitly in DRBD's configuration file
 Available in DRBD 8.0 and later
##DRBD的官方网站:http://www.drbd.org
## rpm包提供:mirror.sohu.com/centos/5.7/extras/i386/RPMS
-------------------------------------------------------------------------------------------------------------------------
(4):Replication modes 复制的模式
## 是同步还是异步的,有Protocol来定义的。
异步复制:
Protocol A
 Asynchronous replication protocol
 Local write operations on the primary node are considered completed as soon as the local disk write has occurred, and the replication packet has been placed in the local TCP send buffer
 In the event of forced fail-over, data loss may occur
 The data on the standby node is consistent after fail-over, however, the most recent updates performed prior to the crash could be lost
## 异步复制,意味着写的操作在主节点完成就可以了,从节点写的操作是否完成不管,只要RDBD
把复制数据打包,封装成TCP包就可以了。
## 异步可能会丢失部分数据,但是性能好。
-------------------------------------------------------------------------------------
半同步:(内存同步)
 Protocol B
 Memory synchronous (semi-synchronous) replication protocol
 Local write operations on the primary node are considered completed as soon as the local disk write has occurred, and the replication packet has reached the peer node 
 Normally, no writes are lost in case of forced fail-over
 However, in the event of simultaneous power failure on both nodes and concurrent, irreversible destruction of the primary's data store, the most recent writes completed on the primary may be lost
## 只要主节点存储完成了,并且这些数据已经传输到从节点的内存中了,此时就宣告存储完成了。
##此时如果双节点都断电了,从节点的数据同样可能丢失。
------------------------------------------------------------------------------
同步:
 Protocol C
 Synchronous replication protocol
 Local write operations on the primary node are considered completed only after both the local and the remote disk write have been confirmed
 Loss of a single node is guaranteed not to lead to any data loss
 Data loss is, of course, inevitable even with this replication protocol if both nodes (or their storage subsystems) are irreversibly destroyed at the same time
## 主节点存储到磁盘上了,从节点也得存储到磁盘上了。
## 同步的模式,效率较低。
## 权衡一下可以发现,Protocol C对数据的传输较安全,默认情况下,RDBD就是使用Protocol C。
## 对于DRBD模型只能够提供99.5%的在线服务。
7:对于DRBD也可能出现脑裂的现象:
Split brain is a situation where, due to temporary failure of all network links between cluster nodes, and possibly due to intervention by a cluster management software or human error, both nodes switched to the primary role while disconnected
8:DRBD内置的解决脑裂的方法:
   DRBD allows for automatic operator notification (by email or other means) when it detects split brain
 DRBD has several resolution algorithms available for resolving the split brain automaticly
 Discarding modifications made on the “younger” primary
 Discarding modifications made on the “older” primary
 Discarding modifications on the primary with fewer changes
 Graceful recovery from split brain if one host has had no intermediate changes

配置DRBD 设备:
1:要求
1) 两个节点可以通过主机名互相访问对方。主机名和uname –n 的结果一样。
2) 双方节点上准备空间大小一样的磁盘分区或LVM等
3) 两节点上安装相关的软件
4) 下载的内核模块版本和客户端的drbd的版本是对应的。
--------------------------------------------------------------------------------------------------------------
2:前提:
1)本配置共有两个测试节点,分别node1.a.org和node2.a.org,相的IP地址分别为
192.168.0.73和192.168.0.128;
2)node1和node2两个节点上各提供了一个大小相同的分区作为drbd设备;我们这
里为在两个节点上均为/dev/sda5,大小为512M;
3)系统为rhel5.4,x86平台;
3:创建分区:
# fdisk /dev/sda
p
n
e
##按Enter键
##按Enter键 ,其表示把剩余的磁盘空间都创建成扩建分区。
n
##按Enter键
+512M
w ##保存退出
# partprobe /dev/sda ##别忘了,让系统重新读取分区信息。
在Node1和Node2上都执行以上命令。以实现建立分区,且大小相等。
4:实现Node1和Node2之间ssh联机不需要密码:##此步骤非必须的,主要为相互拷贝文件,或执行
一些命令方便。
Node1:
# ssh-keygen -t rsa
# ssh-copy-id -i .ssh/id_rsa.pub root@node2 @@以root身份把私钥拷贝到Node2上。
# ssh node2 -- 'ifconfig'
@@用此命令来验证下,正确的结果应该显示Node2的信息。
Node2:
# ssh-keygen -t rsa
# ssh-copy-id -i .ssh/id_rsa.pub root@node1 @@以root身份把私钥拷贝到Node1上。
# ssh node1-- 'ifconfig'

5:安装相关软件包:
drbd共有两部分组成:内核模块和用户空间的管理工具。
其中drbd内核模块代码已经整合进Linux内核2.6.33以后的版本中,
因此,如果您的内核版本高于此版本的话,你只需要安装管理工具既可;
否则,您需要同时安装内核模块和管理工具两个软件包,并且此两者的版本号一定要保持对应。

目前在用的drbd版本主要有8.0、8.2和8.3三个版本,其对应的rpm包的名字:
分别为drbd, drbd82和drbd83,对应的内核模块的名字分别为kmod-drbd, kmod-drbd82和kmod-drbd83
各版本的功能和配置等略有差异;我们实验所用的平台为x86且系统为rhel5.4
因此需要同时安装内核模块和管理工具。我们这里选用最新的8.3的版本(drbd83-8.3.8-
1.el5.centos.i386.rpm和kmod-drbd83-8.3.8-1.el5.centos.i686.rpm)。
下载地址为:http://mirrors.sohu.com/centos/5.6/extras/i386/RPMS/。

实际使用中,您需要根据自己的系统平台等下载符合您需要的软件包版本,这里不提供各版本的下载地址。 
下载完成后直接安装即可:
# yum -y --nogpgcheck localinstall drbd83-8.3.8-1.el5.centos.i386.rpm kmod-drbd83-8.3.8-1.el5.centos.i686.rpm
## 同样Node2上同样需要安装。
6:配置DRBD:
drbd的主配置文件为/etc/drbd.conf;为了管理的便捷性,目前通常会将些配置文件分成多个部分,
且都保存至/etc/drbd.d目录中,主配置文件中仅使用"include"指令将这些配置文件片断整合起来
通常,/etc/drbd.d目录中的配置文件为global_common.conf和所有以.res结尾的文件。
其中global_common.conf中主要定义global段和common段,而每一个.res的文件用于定义一个资源。

在配置文件中,global段仅能出现一次,且如果所有的配置信息都保存至同一个配置文件中
而不分开为多个文件的话,global段必须位于配置文件的最开始处。
目前global段中可以定义的参数仅有minor-count, dialog-refresh, disable-ip-verification和usage-count。

common段则用于定义被每一个资源默认继承的参数,可以在资源定义中使用的参数都可以在common
段中定义。实际应用中,common段并非必须,但建议将多个资源共享的参数定义为common段中的参数
以降低配置文件的复杂度。

resource段则用于定义drbd资源,每个资源通常定义在一个单独的位于/etc/drbd.d目录中的以.res结尾的文件中。
资源在定义时必须为其命名,名字可以由非空白的ASCII字符组成。每一个资源段的定义中至少要包含两个host
子段, 以定义此资源关联至的节点,其它参数均可以从common段或drbd的默认中进行继承而无须定义。

复制样例配置文件为即将使用的配置文件:
# cd /usr/share/doc/drbd83-8.3.8/
# ls
# cp drbd.conf /etc ##它会提示在这个目录下已经有这个文件是否覆盖,直接覆盖既可,因其文件是
空的。
# cd
# vim /etc/drbd.conf ##仅仅查看一下内容
# cd /etc/drbd.d/
# ls
# cp global_common.conf global_common.conf.bak ##防止修改错了
# vim global_common.conf
global {
usage-count no; ##把此项的值改为no
# minor-count dialog-refresh disable-ip-verification


common {
protocol C;
startup {
wfc-timeout 120;
degr-wfc-timeout 120;
}

disk {
on-io-error detach;
fencing resource-only;
}

net {
cram-hmac-alg "sha1";
shared-secret "mydrbdlab";
}

syncer {
rate 100M;
}

7:定义一个资源配置:
# cd /etc/drbd.d
# vim web.res ##注意资源的文件名必须以.res结尾。
resource web {
on node1.a.org {
device /dev/drbd0;
disk /dev/sda5;
address 192.168.0.73:7789; ##7789是DRBD默认的监听端口
meta-disk internal; ##表示在本地磁盘存储它的原数据
}
on node2.a.org {
device /dev/drbd0;
disk /dev/sda5;
address 192.168.0.128:7789;
meta-disk internal;
}
}
# cd
# scp –r /etc/drbd.conf /etc/drbd.d/ node2:/etc
8: 在两个节点上初始化已定义的资源并启动服务
1) 初始化资源,在Node1和Node2上分别执行:
# drbdadm create-md web ##把web这个资源创建出来,create-md是drbdadm的子命令
2) 启动服务,在Node1和Node2上分别执行:
# /etc/init.d/drbd start ##或者 # servce drbd start 
##在一个节点执行时,会发现………当在另一个节点上启动了,两个会马上都启动起来。
3) 查看启动状态:
# cat /proc/drbd
version: 8.3.8 (api:88/proto:86-94)
GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16
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:b oos:505964

也可以使用drbd-overview命令来查看:
# drbd-overview 
0:web Connected Secondary/Secondary Inconsistent/Inconsistent C r----

从上面的信息中可以看出此时两个节点均处于Secondary状态。于是,
我们接下来需要将其中一个节点设置为Primary。
在要设置为Primary的节点上执行如下命令:
# drbdsetup /dev/drbd0 primary –o
也可以在要设置为Primary的节点上使用如下命令来设置主节点:
# drbdadm -- --overwrite-data-of-peer primary web
而后再次查看状态,可以发现数据同步过程已经开始:
# drbd-overview
0:web SyncSource Primary/Secondary UpToDate/Inconsistent C r---- 
[============>.......] sync'ed: 66.2% (172140/505964)K delay_probe: 35
# watch –n1 ‘cat /proc/drbd’ ##可以查看其详细的数据同步过程。
等数据同步完成以后再次查看状态,可以发现节点已经牌实时状态,且节点已经有了主次:
# drbd-overview
0:web Connected Primary/Secondary UpToDate/UpToDate C r----
9:创建文件系统
文件系统的挂载只能在Primary节点进行,因此,也只有在设置了主节点后才能对drbd设备进行格式化:
# mke2fs -j -L DRBD /dev/drbd0
# mkdir /mnt/drbd
# mount /dev/drbd0 /mnt/drbd
# cd /mnt/drbd
# vim index.html ##建立一个网页文件
<h1>I’m The Primary Storage<\h1>
# drbdadm role web ##可以查看本节点处于什么角色,要指定那个资源。
Primary/Secondary ##在主节点上执行,显示这个,而在从节点上显示:Secondary/Primary
10: 切换Primary和Secondary节点
对主Primary/Secondary模型的drbd服务来讲,在某个时刻只能有一个节点为Primary,
因此,要切换两个节点的角色,只能在先将原有的Primary节点设置为Secondary后,
才能原来的Secondary节点设置为Primary.
Node1:
# cp -r /etc/drbd.* /mnt/drbd @@非必须的
# umount /mnt/drbd
# drbdadm secondary web

查看状态:
# drbd-overview 
0:web Connected Secondary/Secondary UpToDate/UpToDate C r----

Node2:
# drbdadm primary web
# drbd-overview 
0:web Connected Primary/Secondary UpToDate/UpToDate C r---- 
# mkdir /mnt/drbd
# mount /dev/drbd0 /mnt/drbd ##记住一定不要再格式化了,双节点的数据已经同步过了。
使用下面的命令查看在此前在主节点上复制至此设备的文件是否存在:
# ls /mnt/drbd
 










本文转自 kuchuli 51CTO博客,原文链接:http://blog.51cto.com/lgdvsehome/665246,如需转载请自行联系原作者
目录
相关文章
|
6月前
|
NoSQL Redis 容器
Redis集群更换节点IP后如何恢复集群并保留完整集群数据
Redis集群更换节点IP后如何恢复集群并保留完整集群数据
114 0
|
11月前
|
存储 算法 关系型数据库
带你读《存储漫谈:Ceph原理与实践》——2.3.1 PG 数量的选择
带你读《存储漫谈:Ceph原理与实践》——2.3.1 PG 数量的选择
|
11月前
|
存储 Swift 索引
带你读《存储漫谈:Ceph原理与实践》——2.1 数据寻址方案
带你读《存储漫谈:Ceph原理与实践》——2.1 数据寻址方案
|
11月前
|
存储 算法 关系型数据库
带你读《存储漫谈:Ceph原理与实践》——2.2.1 Ceph 寻址流程
带你读《存储漫谈:Ceph原理与实践》——2.2.1 Ceph 寻址流程
|
11月前
|
存储 容灾 安全
带你读《存储漫谈:Ceph原理与实践》——3.1.3 远程复制
带你读《存储漫谈:Ceph原理与实践》——3.1.3 远程复制
带你读《存储漫谈:Ceph原理与实践》——3.1.3 远程复制
|
12月前
|
弹性计算 大数据 数据处理
集群类型之信息共享节点集群
集群类型之信息共享节点集群
102 0
|
存储 NoSQL MongoDB
初始化配置副本集和主节点|学习笔记
快速学习初始化配置副本集和主节点
163 0
|
SQL 关系型数据库 MySQL
如何搭建双 M 结构的主从备份?
关于 MySQL 主从搭建,我们在项目中,更常见一种结构是双 M 结构,即两个 MySQL 实例,每个 MySQL 实例互为主备,这样在主节点突然断电或者不可用的时候,slave 节点可以很快切换为 master,架构图如下: 在这种结构中,两个 MySQL 实例的地位是平等的,互为对方的主备,我们判断谁是主机谁是从机的方式主要是看 readonly,谁是只读的,那谁就是从机,所以这种情况下,主从切换也很方便,只要修改 readonly 属性即可。 接下来我们就来搭建一个 双 M 的主从备份,看看和单纯的 M-S 结 构 的有啥区别。
|
XML 缓存 中间件
Hazelcast双节点改为双集群
Hazelcast双节点改为双集群
Openstack-M版(双节点)热迁移记录
Openstack-M版(双节点)热迁移记录
148 0
Openstack-M版(双节点)热迁移记录