项目背景:
公司需要我们实现本地主机和远程主机上面的重要的数据实时同步,保证数据的安全!
实验环境:
vmware workstation 11
主服务器:centos6.5 ip:192.168.0.19 hostname:Master-drbd 镜像磁盘分区:/dev/sdb1
内核:2.6.32-573.18.1.el6.x86_64 (更新过的,不然会报错)
两块磁盘:
/dev/sda用来做系统盘,我们不动它
/dev/sdb是我们新添加的,我们在上面创建分区/dev/sdb1用来测试。
setenforce 0
iptables关闭
备用服务器:centos6.5 ip:192.168.0.23 hostname:Slave-drbd 镜像磁盘分区:/dev/sdb1
内核:2.6.32-573.18.1.el6.x86_64 (更新过的,不然会报错)
两块磁盘:
/dev/sda用来做系统盘,我们不动它
/dev/sdb是我们新添加的,我们在上面创建分区/dev/sdb1用来测试。
setenforce 0
iptables关闭
SecureCRT (ssh远程连接软件)方便我们测试用。
实验过程:
环境设置、软件安装、配置文件 这些都需要在两台主机上进行操作!!!有什么不明白的可以在下面留言,谢谢!
一、环境配置(其实我们安装任何一种比较复杂的应用的话,环境其实是最麻烦的。)
1:yum源设置(使用163源)
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
2、epel源设置(epel官方的源)
rpm -Uvh http://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm
3、内核升级(我的系统没升级会报错!)
[root@localhost yum.repos.d]# modprobe drbd
FATAL: Module drbd not found.
内核升级:
yum install -y kernel*
内核升级完成以后重启服务器主机。
4、DRBD软件安装
yum -y install drbd83-utils kmod-drbd83
5、加载DRBD模块到内核
[root@Slave-drbd ~]# modprobe drbd
默认会没输出,。
6:检查DRBD是否成功安装
[root@Master-drbd ~]# lsmod |grep -i drbd
drbd 332493 4
7、配置文件修改:(记住复制一份到从服务器)
[root@Master-drbd ~]# cat /etc/drbd.conf
# You can find an example in /usr/share/doc/drbd.../drbd.conf.example
#include "drbd.d/global_common.conf"; #原来的我注释掉了
#include "drbd.d/*.res"; #原来的我注释掉了
global {
usage-count no;
}
common {
syncer { rate 200M; }
}
resource r0 {
protocol C;
handlers {
pri-on-incon-degr "echo o > /proc/sysrq-trigger ; halt -f";
pri-lost-after-sb "echo o > /proc/sysrq-trigger ; halt -f";
local-io-error "echo o > /proc/sysrq-trigger ; halt -f";
fence-peer "/usr/lib64/heartbeat/drbd-peer-outdater -t 5";
pri-lost "echo pri-lost. Have a look at the log files. | mail -s 'DRBD Alert' root";
split-brain "/usr/lib/drbd/notify-split-brain.sh root";
out-of-sync "/usr/lib/drbd/notify-out-of-sync.sh root";
}
net {
cram-hmac-alg "sha1";
shared-secret "MySQL-HA";
}
disk {
on-io-error detach;
fencing resource-only;
}
startup {
wfc-timeout 120;
degr-wfc-timeout 120;
}
device /dev/drbd0;
on Master-drbd { #必须是你的主机名字,不然会报错
disk /dev/sdb1;
address 192.168.0.19:7788; #主服务器的ip和端口
meta-disk internal;
}
on Slave-drbd { #同上
disk /dev/sdb1;
address 192.168.0.23:7788; #同上
meta-disk internal;
}
}
8、第一次启动需要创建供DRBD记录信息的数据块
两台主机都执行:
[root@Master-drbd drbd.d]# drbdadm create-md r0
WARN:
You are using the 'drbd-peer-outdater' as fence-peer program.
If you use that mechanism the dopd heartbeat plugin program needs
to be able to call drbdsetup and drbdmeta with root privileges.
You need to fix this with these commands:
chgrp haclient /sbin/drbdsetup
chmod o-x /sbin/drbdsetup
chmod u+s /sbin/drbdsetup
chgrp haclient /sbin/drbdmeta
chmod o-x /sbin/drbdmeta
chmod u+s /sbin/drbdmeta
Writing meta data...
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created.
上面是主服务器上上面操作的输出。
9、在两个节点启动服务
[root@Master-drbd ~]# /etc/init.d/drbd start
[root@Slave-drbd ~]# /etc/init.d/drbd start
10、查看任意节点状态
我看的是主服务器节点
[root@Master-drbd ~]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
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:f oos:8032184
11、设置主节点
[root@Master-drbd ~]# drbdsetup /dev/drbd0 primary -o
设置完主节点以后,两台服务器数据会同步。
[root@Master-drbd ~]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r-----
ns:1015808 nr:0 dw:0 dr:1016480 al:0 bm:61 lo:0 pe:2 ua:0 ap:0 ep:1 wo:f oos:7016632
[=>..................] sync'ed: 12.7% (6852/7840)M
finish: 0:02:49 speed: 41,260 (40,620) K/sec
你等一会・・・・・ 等它们同步玩上面那个是进度条
12、同步完成的状态查看
[root@Master-drbd ~]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:8032184 nr:0 dw:0 dr:8032856 al:0 bm:491 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
13、挂载DRBD设备
[root@Master-drbd ~]# mkfs.ext3 /dev/drbd0
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=0 blocks
502944 inodes, 2008046 blocks
100402 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2059403264
62 block groups
32768 blocks per group, 32768 fragments per group
8112 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@Master-drbd ~]# mount /dev/drbd0 /mnt
[root@Master-drbd ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 18208184 3555276 13721324 21% /
tmpfs 502380 72 502308 1% /dev/shm
/dev/sda1 289293 82601 191332 31% /boot
/dev/drbd0 7905880 148868 7355404 2% /mnt
14、在主节点/mnt目录下创建个测试文件
[root@Master-drbd ~]# dd if=/dev/zero of=/mnt/testdrbd.tmp bs=10M count=20
20+0 records in
20+0 records out
209715200 bytes (210 MB) copied, 4.45266 s, 47.1 MB/s
[root@Master-drbd ~]# ls -al /mnt/testdrbd.tmp
-rw-r--r--. 1 root root 209715200 Mar 12 21:12 /mnt/testdrbd.tmp
15、查看文件是否同步(从服务器进行的操作)
[root@Slave-drbd ~]# /etc/init.d/drbd stop
Stopping all DRBD resources: .
[root@Slave-drbd ~]# mount /dev/sdb1 /mnt
[root@Slave-drbd ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 18208184 3198456 14078144 19% /
tmpfs 502380 72 502308 1% /dev/shm
/dev/sda1 289293 82502 191431 31% /boot
/dev/sdb1 7905880 353872 7150400 5% /mnt
[root@Slave-drbd ~]# cd /mnt/
[root@Slave-drbd mnt]# ll
total 205020
drwx------. 2 root root 16384 Mar 12 21:10 lost+found
-rw-r--r--. 1 root root 209715200 Mar 12 21:12 testdrbd.tmp
可以看到已经同步过来了
16、重启备用节点上的DRBD服务
[root@Slave-drbd mnt]# cd ~
[root@Slave-drbd ~]# umount /dev/sdb1
[root@Slave-drbd ~]# /etc/init.d/drbd start
二、DRBD主备节点切换
一:停止DRBD切换
1、停止主用节点服务
[root@Master-drbd ~]# /etc/init.d/drbd stop
Stopping all DRBD resources:
.
2、在备用节点执行切换成主命令:
[root@Slave-drbd ~]# drbdsetup /dev/drbd0 primary -o
[root@Slave-drbd ~]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
0: cs:WFConnection ro:Primary/Unknown ds:UpToDate/Outdated C r-----
ns:0 nr:16 dw:16 dr:672 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
3、在新的主用节点上挂载DRBD设备完成主备节点的切换
[root@Slave-drbd ~]# mount /dev/drbd0 /mnt
[root@Slave-drbd ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 18208184 3198460 14078140 19% /
tmpfs 502380 72 502308 1% /dev/shm
/dev/sda1 289293 82502 191431 31% /boot
/dev/drbd0 7905880 353872 7150400 5% /mnt
二、正常切换
1、在主节点卸载磁盘分区
[root@Slave-drbd ~]# umount /mnt
2、执行下面命令
[root@Slave-drbd ~]# drbdadm secondary all
WARN:
You are using the 'drbd-peer-outdater' as fence-peer program.
If you use that mechanism the dopd heartbeat plugin program needs
to be able to call drbdsetup and drbdmeta with root privileges.
You need to fix this with these commands:
chgrp haclient /sbin/drbdsetup
chmod o-x /sbin/drbdsetup
chmod u+s /sbin/drbdsetup
chgrp haclient /sbin/drbdmeta
chmod o-x /sbin/drbdmeta
chmod u+s /sbin/drbdmeta
[root@Slave-drbd ~]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
0: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r-----
ns:8 nr:16 dw:24 dr:1025 al:1 bm:1 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
可以阿卡尼岛现在俩个都是从节点。
3、指定一个主节点,把原来的备用节点制定成主节点(因为上面一个测试,我进行了转换,所以换回来!!!)
[root@Master-drbd ~]# drbdadm primary all
WARN:
You are using the 'drbd-peer-outdater' as fence-peer program.
If you use that mechanism the dopd heartbeat plugin program needs
to be able to call drbdsetup and drbdmeta with root privileges.
You need to fix this with these commands:
chgrp haclient /sbin/drbdsetup
chmod o-x /sbin/drbdsetup
chmod u+s /sbin/drbdsetup
chgrp haclient /sbin/drbdmeta
chmod o-x /sbin/drbdmeta
chmod u+s /sbin/drbdmeta
[root@Master-drbd ~]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:0 nr:8 dw:8 dr:672 al:0 bm:1 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
[root@Master-drbd ~]# mount /dev/drbd0 /mnt
[root@Master-drbd ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 18208184 3555500 13721100 21% /
tmpfs 502380 72 502308 1% /dev/shm
/dev/sda1 289293 82601 191332 31% /boot
/dev/drbd0 7905880 353872 7150400 5% /mnt
可以看到又成功的转换回来了。yes!!!!成功
总结:DRBD这个高可用块设备相信大家会在以后的项目中常用,怎么说呢它最近几年会比较火,它可以跟多种开源软件组合,用来满足我们的高可用的要求!!!
谢谢大家,希望对你们有帮助!!!!