一、DRBD的定义:
1、drbd,是分布式磁盘块设备,它的工作模型是,通过将两个节点上磁盘设备建立镜像,完成数据的复制,数据的复制是在数据块级别实现的;
2、drbd工作在内核空间,将所有发往本地磁盘对应drbd磁盘设备的数据通过drbd复制一份到对端drbd设备上;
3、drbd的核心是drbd资源,由资源名称,drbd设备,磁盘设备和网络配置等组成;
4、drbd的数据传输有三种:同步,半同步,异步,为了数据的安全性,最好选择同步协议;
5、drbd资源的工作模型有主从模型和双主模型;
6、主从模型最常见,它可以使用任意的单机文件系统ext3,ext4等,主从模型时2个节点不能使用同一个资源,主节点7、能挂载能读能写,从节点不能挂载,也不能读不能写;
8、可以在集群文件系统中使用分布式文件锁让主从模型中的主节点和从节点都能挂载和读写。

9、DRBD的工作模式图:

DRBD主从模式的实现_安全性

spacer.gif
二、安装配置高可用集群:
1、节点名称:集群每个节点的名称都得能互相解析
       /etc/hosts
       hosts中主机名的正反解析结果必须跟“uname -n”的结果保持一致;
(1)、节点1(172.16.35.1):
vim /etc/sysconfig/network
HOSTNAME=node1.yanhai.com
vim /etc/hosts
172.16.35.1  node1.yanhai.com node1
172.16.35.2  node2.yanhai.com node2
(2)、节点2(172.16.35.2):
vim /etc/sysconfig/network
HOSTNAME=node2.yanhai.com
vim /etc/hosts
172.16.35.2  node2.yanhai.com node2
172.16.35.1  node1.yanhai.com node1
2、时间必须得同步
       使用网络时间服务器同步时间
crontab -e
*/5 * * * * /usr/sbin/ntpdate 172.16.0.1 &> /dev/null   --没5分钟从时间服务器172.16.0.1那儿同步时间
3、并非必须:各节点间能基于ssh密钥认证通信;
(1)172.16.35.1上面:
ssh-keygen -t rsa -P ''    ---不输入密码,生成一对的公钥和私钥,然后把公钥复制到
172.16.35.2上面去: ssh-copy-id  -i ~/.ssh/id_rsa.pub root@node2.yanhai.com
(2)172.16.35.2上面:
ssh-keygen -t rsa -P ''    ---不输入密码,生成一对的公钥和私钥,然后把公钥复制到
172.16.35.1上面去: ssh-copy-id  -i ~/.ssh/id_rsa.pub root@node1.yanhai.com
4、查看内核版本:
uname -r    
2.6.32-431.el6.x86_64
5、下载与自己系统内核版本相匹配的drbd用户空间和内核空间的安装包:
drbd-8.4.3-33.el6.x86_64.rpm                    用户空间工具
drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm     内核空间工具
6、在2个节点都安装这2个包:
rpm -ivh drbd-8.4.3-33.el6.x86_64.rpm drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm
7、vim /etc/drbd.d/global_common.conf :  (在node1上的全局公共配置)
global {
       usage-count no;
       # minor-count dialog-refresh disable-ip-verification
}
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";    (一旦io错误,通过2个脚本通知io有错误,并紧急重启)
               # 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 {  (startup指drbd启动时该做的操作)
               # wfc-timeout degr-wfc-timeout outdated-wfc-timeout wait-after-sb
               #wfc-timeout 120;     等待对端上线连接起来的超时时间为120秒,注释掉,保持默认
               #degr-wfc-timeout 60;   降级等待连接超时时间为60秒,注释掉,保持默认的
       } (wfc-timeout:等待对端上线连接起来的超时时间;degr-wfc-timeout 指降级等待连接超时时间;
outdated-wfc-timeout :指过期的连接等待超时时间;wait-after-sb:脑裂后的等待时间)
       options {
               # cpu-mask on-no-data-accessible
       }
       disk {
               on-io-error detach;   一旦各节点io错误就把磁盘拆掉
               # size max-bio-bvecs 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 :各节点io发生错误时该怎么做;resync-rate:某一节点离线后又上线后2节点之间同步的速率)
       net {
               protocol C;    定义的协议为同步
               cram-hmac-alg "sha1";    使用的加密算法为sha1
               shared-secret "drbd.yanhai.com";  节点通信时共享的密钥
               # 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
       }   ( protocol:协议,包含 A:异步,B:半同步,C:同步;allow-two-primaries:双主模型,而双主模型只能在集群文件系统下的高可用集群才可用;cram-hmac-alg:挑战式握手认证协议使用的加密算法;shared-secret:2节点共享的密钥;)
syncer {
               rate 1000M;    节点之间同步时使用的速率为1000M的带宽
       }
}
8、2个节点各挂一个5G的磁盘分区:
node1: /dev/sda3    5G
node2: /dev/sda3    5G
9、在node1上定义一个资源:
vim /etc/drbd.d/mystore.res  :
resource mystore {   (定义的资源名为mystore)
 on node1.yanhai.com {  
   device    /dev/drbd0;    (定义节点 1 的drbd的设备名称)
   disk      /dev/sda3;       (节点1上的磁盘分区的编号)
   address   172.16.35.1:7789;    (监听的节点1的drbd的IP地址和端口)
   meta-disk internal;        (使用本地磁盘保存drbd语言数据)
 }
 on node2.yanhai.com {
   device    /dev/drbd0;
   disk      /dev/sda3;
   address   172.16.35.2:7789;
   meta-disk internal;
 }
}
10、让2个节点的drbd配置一样,把node1上的文件global_common.conf 和mystore.res复制给node2一份:
cd /etc/drbd.d/
scp global_common.conf mystore.res node2:/etc/drbd.d/
11、初始化资源,在Node1和Node2上分别执行:
drbdadm create-md mystore
12、启动服务,在Node1和Node2上分别执行:
/etc/init.d/drbd start
13、实时查看drbd的情况:
watch -n 1 'cat /proc/drbd'
(secondary表示从节点,inconsistent表示)
14、
也可以使用drbd-overview命令来查看:
drbd-overview
15、将其中一个节点设置为Primary。在要设置为Primary的节点上执行如下命令:
drbdadm primary --force mystore   (将mystore这个资源提升为主节点)

16、而后再次查看状态,可以发现数据同步过程已经开始:

DRBD主从模式的实现_安全性_03

50608221.png
17、
等数据同步完成以后再次查看状态,可以发现节点已经牌实时状态,且节点已经有了主次:
drbd-overview
0:mystore/0  Connected Primary/Secondary UpToDate/UpToDate C r-----
(左边的为primary表示本机几点为主节点)
18、格式化主节点:
mke2fs -t ext4 /dev/drbd0
19、把主节点挂载在mydata目录下:
mkdir /mydata
mount /dev/drbd0 /mydata/
20、卸载mydata,并把当前主节点降级:
umount /mydata/
drbdadm secondary mystore   (当前主节点是node1,把node1降级)
21、把node2提升为主节点:
drbdadm primary mystore
22在node2上创建目录mydata,并把/dev/drbd0挂载到mydata:
mkdir /mydata
mount /dev/drbd0 /mydata/
23、如果要让对端节点升级为主节点,就必须先卸载当前节点挂载的目录mydata,再把当前节点降级为从节点,再把对端节点升级为主节点。
三、使用drbd+pacemaker结合起来实现自动drbd角色切换:
前提条件:主从的挂载点要保持一致。
clone-max:在整个集群中最多运行多少份克隆资源
clone-node-max:每个节点最多运行多少份克隆
notify:一旦停止或启动一个克隆资源时,是否要通知其他运行克隆资源的节点,默认为true,表示必须通知
globally-unique:每个节点都有唯一的名称
ordered:按顺序启动节点
interleave:按照自定义顺序约束保证启动次序
master-max:整个集群中最多能有几份资源是主资源
master-node-max:每个节点上最多允许运行几份主资源
1、node1和node2都安装corosync和pacemaker:
yum -y install corosync pacemaker
2、node1和node2都安装crmsh和pssh包:
yum -y install crmsh-1.2.6-4.el6.x86_64.rpm pssh-2.3.1-2.el6.x86_64.rpm
3、drbd作为集群资源,得把主节点的挂载目录卸载,再把主节点降级,然后关闭drbd服务,并让其不能开机启动:
(1)确保2个节点都为从节点:
drbd-overview
 0:mystore/0  Connected Secondary/Secondary UpToDate/UpToDate C r-----
(2)关闭drbd服务,并让其不能开机启动:
/etc/init.d/drbd stop
chkconfig drbd off
4、
cd /etc/corosync/
ls
corosync.conf.example  corosync.conf.example.udpu  service.d  uidgid.d
cp corosync.conf.example corosync.conf
5、vim corosync.conf:
# Please read the corosync.conf.5 manual page
compatibility: whitetank
totem {
   version: 2
   secauth: off
   threads: 0
   interface {
       ringnumber: 0
       bindnetaddr: 172.16.0.0    (监听的网络)
       mcastaddr: 226.94.35.35      (多播地址)
       mcastport: 5405
       ttl: 1
   }
}
logging {
   fileline: off
   to_stderr: no
   to_logfile: yes
   to_syslog: no
   logfile: /var/log/cluster/corosync.log
   debug: off
   timestamp: on
   logger_subsys {
       subsys: AMF
       debug: off
   }
}
amf {
   mode: disabled
}
service {
   name:    pacemaker
   ver:    0
}
aisexec {
   user:    root
   group:    root
}
6、执行命令:corosync-keygen
Corosync Cluster Engine Authentication key generator.
Gathering 1024 bits for key from /dev/random.
Press keys on your keyboard to generate entropy.
Writing corosync key to /etc/corosync/authkey.
7、给node2复制一份:
cd /etc/corosync
scp authkey corosync.conf node2:/etc/corosync/
8、2个节点都启动corosync服务:
[root@node1 corosync]# service corosync start
Starting Corosync Cluster Engine (corosync):               [  OK  ]
[root@node1 corosync]# ssh node2 'service corosync start'
Starting Corosync Cluster Engine (corosync): [  OK  ]
9、查看2个节点的状态:
[root@node1 ~]# crm status
Last updated: Wed Apr 23 17:21:57 2014
Last change: Wed Apr 23 17:18:20 2014 via crmd on node1.yanhai.com
Stack: classic openais (with plugin)
Current DC: node1.yanhai.com - partition with quorum
Version: 1.1.10-14.el6-368c726
2 Nodes configured, 2 expected votes
0 Resources configured
Online: [ node1.yanhai.com node2.yanhai.com ]
10、使用crm更改属性:
[root@node1 ~]# crm
crm(live)# configure
crm(live)configure# property sto
stonith-action=         stonith-timeout=        stop-orphan-actions=
stonith-enabled=        stop-all-resources=     stop-orphan-resources=
crm(live)configure# property stonith-enabled=false
crm(live)configure# property no-quorum-policy=ignore
crm(live)configure# rsc_defaults resource-stickiness=100
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# show
node node1.yanhai.com
node node2.yanhai.com
property $id="cib-bootstrap-options" \
   dc-version="1.1.10-14.el6-368c726" \
   cluster-infrastructure="classic openais (with plugin)" \
   expected-quorum-votes="2" \
   stonith-enabled="false" \
   no-quorum-policy="ignore"
rsc_defaults $id="rsc-options" \
   resource-stickiness="100"
11、在node1上配置drbd为集群资源:
(1)查看资源代理:
crm(live)# ra
crm(live)ra# classes
lsb
ocf / heartbeat linbit pacemaker
service
stonith
(2)看某一个资源代理有哪些组件:
crm(live)ra# list ocf linbit
drbd  
(3)查看drbd的详细信息:
crm(live)ra# meta ocf:linbit:drbd
(4)定义主资源,主资源名称为mysqlstore:
crm(live)configure# primitive mysqlstore ocf:linbit:drbd params drbd_resource=mystore op monitor role=Master interval=30s timeout=20s op monitor role=Slave interval=60s timeout=20s op start timeout=240s op stop timeout=100s
crm(live)configure# verify
crm(live)configure#
(5)把mysqlstore定义为主从资源:
crm(live)configure# master ms_mysqlstore mysqlstore meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify="True"
crm(live)configure# verify
(6)提交信息:
commit
(7)执行status可以查看到node1为主节点,node2为从节点:
crm(live)# status
Last updated: Wed Apr 23 17:59:17 2014
Last change: Wed Apr 23 17:58:56 2014 via cibadmin on node1.yanhai.com
Stack: classic openais (with plugin)
Current DC: node1.yanhai.com - partition with quorum
Version: 1.1.10-14.el6-368c726
2 Nodes configured, 2 expected votes
2 Resources configured
Online: [ node1.yanhai.com node2.yanhai.com ]
Master/Slave Set: ms_mysqlstore [mysqlstore]
    Masters: [ node1.yanhai.com ]
    Slaves: [ node2.yanhai.com ]
(8)在主节点node1上定义mysql的存储设备的主资源:
crm(live)configure# primitive mysqlfs ocf:heartbeat:Filesystem params device=/dev/drbd0 directory=/mydata fstype=ext4 op monitor interval=30s timeout=40s op start timeout=60s op stop timeout=60s
crm(live)configure# verify
crm(live)configure#
(9)定义排列约束,把存储设备mysqlfs和drbd主从资源的主节点结合在一起:
crm(live)configure# colocation mysqlfs_with_ms_mysqlstore_master inf: mysqlfs ms_mysqlstore:Master
crm(live)configure# verify
(10)定义顺序约束,主节点启动后才能挂载存储设备:
crm(live)configure# order mysqlfs_after_ms_mysqlstore_master mandatory: ms_mysqlstore:promote mysqlfs:start
crm(live)configure# verify
(11)提交定义的信息:
commit
(12)执行status命令查看,主节点和挂载设备都在一台服务器node1上:
crm(live)# status
Last updated: Wed Apr 23 18:26:09 2014
Last change: Wed Apr 23 18:25:17 2014 via cibadmin on node1.yanhai.com
Stack: classic openais (with plugin)
Current DC: node1.yanhai.com - partition with quorum
Version: 1.1.10-14.el6-368c726
2 Nodes configured, 2 expected votes
3 Resources configured
Online: [ node1.yanhai.com node2.yanhai.com ]
Master/Slave Set: ms_mysqlstore [mysqlstore]
    Masters: [ node1.yanhai.com ]
    Slaves: [ node2.yanhai.com ]
mysqlfs    (ocf::heartbeat:Filesystem):    Started node1.yanhai.com
(13)node1上设备已自动挂载:
[root@node1 ~]# ls /mydata/
inittab  lost+found
12、验证:
(1)把主节点node1变为备用的(standby),查看到node2已提升为主节点:
crm(live)# node standby node1.yanhai.com
crm(live)# status
Last updated: Wed Apr 23 18:34:10 2014
Last change: Wed Apr 23 18:34:07 2014 via crm_attribute on node1.yanhai.com
Stack: classic openais (with plugin)
Current DC: node1.yanhai.com - partition with quorum
Version: 1.1.10-14.el6-368c726
2 Nodes configured, 2 expected votes
3 Resources configured
Node node1.yanhai.com: standby
Online: [ node2.yanhai.com ]
Master/Slave Set: ms_mysqlstore [mysqlstore]
    Masters: [ node2.yanhai.com ]
    Stopped: [ node1.yanhai.com ]
mysqlfs    (ocf::heartbeat:Filesystem):    Started node2.yanhai.com
(2)node2上设备已自动挂载:
[root@node2 ~]# ls /mydata/
inittab  lost+found