转:http://blog.csdn.net/kkdelta/article/details/39737011
http://myhat.blog.51cto.com/391263/606318/
https://www.centos.bz/2012/03/achieve-drbd-high-availability-with-heartbeat/
主服务器
cat /etc/drbd.d/xserver.res
resource xserver {
meta-disk internal;
device /dev/drbd0; #device指定的参数最后必须有一个数字,用于global的minor-count,
#否则会报错。device指定drbd应用层设备。
on test-VirtualBox{ #注意:drbd配置文件中,机器名大小写敏感!
address 10.77.60.197:7789;
disk /dev/sdd1;
}
on victor_VirtualBox{ #注意:drbd配置文件中,机器名大小写敏感!
address 10.77.60.71:7789;
disk /dev/sdb1;
}
}
868 drbd-overview
869 service drbd restart
870 drbdadm create-md all
871 service drbd restart
872 service drbd start
873 drbd-overview
874 drbdadm create-md all
875 cat /proc/drbd
876 ps aux|grep drbd
877 killall drbd
878 service drbd stop
879 killall drbd
880 ps aux|grep drbd
881 drbdadm create-md all
882 ps aux|grep drbd
883 service drbd start
884 drbd-overview
885 drbdadm -- --overwrite-data-of-peer primary all
886 drbd-overview
887 vim xserver.res
888 ifconfig
889 vim xserver.res
890 service drbd restart
891 drbd-overview
892 watch "drbd-overview"
893 drbd-overview
894 mkfs.ext3 /dev/drbd0
895 mkdir /xserver-storage
896 mount /dev/drbd0 /xserver-storage/
897 mkfs.ext3 /dev/drbd0
897 mkfs.ext3 /dev/drbd0
899 cat /proc/drbd
900 vim xserver.res
901 mkfs.ext3 /dev/sdd1
902 mkfs.ext3 /dev/drbd0
903 mount /dev/drbd0 /xserver-storage/
904 partprobe /dev/sdd
905 cat /proc/partions
906 cat /proc/partitions
907 mkfs.ext3 /dev/drbd0
908 mount /dev/drbd0 /xserver-storage/
909 cd /xserver-storage/
910 ls
911 drbdadm -- --overwrite-data-of-peer primary all
912 drbd-overview
913 mkfs.ext3 /dev/drbd0
914 mount /dev/drbd0 /xserver-storage/
915 ls -rlht
916 pwd
917 ls -rhlt
918 echo "a file created in servertest" >testfile
919 ls -rhlt
920 pwd
921 cd ..
922 umount /xserver-storage/
923 drbdadm secondary all
924 drbdadm primary all
925 mount /dev/drbd0 /xserver-storage/
926 ls -rhlt
927 cd /xserver-storage/
928 ls -rhlt
929 ls -rthl
930 history
931 ls
932 cd /etc/
933 ls -rthl
934 cd drbd.d/
935 ls
936 ls -rlth
937 vim global_common.conf
938 vim xserver.res
939 history
940 history>1.txt
从服务器
cat /etc/drbd.d/xserver.res
resource xserver {
meta-disk internal;
device /dev/drbd0; #device指定的参数最后必须有一个数字,用于global的minor-count,
#否则会报错。device指定drbd应用层设备。
on victor-VirtualBox{ #注意:drbd配置文件中,机器名大小写敏感!
address 10.77.60.71:7789;
disk /dev/sdb1;
}
on test-VirtualBox{ #注意:drbd配置文件中,机器名大小写敏感!
address 10.77.60.197:7789;
disk /dev/sdd1;
}
}
23 apt-get install drbd8-utils
25 cd /etc/drbd.d/
28 ls -rthl
29 rm global_common.conf
30 mv /home/victor/global_common.conf ./
31 mv /home/victor/xserver.res ./
33 uname -n
34 vim xserver.res
35 fdisk -l
36 fdisk /dev/sdb
38 fdisk -l
39 vim xserver.res
40 service drbd restart
41 vim xserver.res
42 service drbd restart
44 vim xserver.res
45 service drbd restart
47 mv /home/victor/xserver.res ./
49 vim xserver.res
50 service drbd restart
51 drbdadm create-md all
53 vim xserver.res
54 drbdadm create-md all
55 service drbd restart
56 vim xserver.res
57 ifconfig
58 vim xserver.res
59 service drbd restart
61 vim global_common.conf
62 vim ../drbd.conf
64 service drbd restart
65 killall drbd
67 netstat -tnlp
69 vim ../drbd.conf
72 vim global_common.conf
73 vim xserver.res
74 service drbd restart
76 vim xserver.res
77 service drbd restart
78 service drbd restartdrbdadm create-md all
79 drbdadm create-md all
80 service drbd restart
81 drbd-overview
82 service drbd restart
83 service drbd start
84 netstat -tnlp
85 ps uax|grep drbd
86 pwd
87 vim /var/log/syslog
88 ls -rlht
89 service drbd start
90 drbdadm create-md all
91 Command 'drbdmeta 0 v08 /dev/sdb1 internal create-md' terminated with exit code 20
92 drbdadm create-md all
93 drbdadm --help
94 drbdadm --h
95 service drbd stop
96 ps axu|grep drbd
97 service drbd start
98 vim xserver.res
99 service drbd restart
100 drbd-overview
101 watch "drbd-overview"
102 drbd-overview
103 ls -rlht
104 mkdir /xserver-storage
105 mount /dev/drbd0 /xserver-storage/
106 drbadmin primary all
107 drbdadm primary all
108 mount /dev/drbd0 /xserver-storage/
109 ls -rlht
110 pwd
111 cd /xserver-storage/
112 ls -rhlt
113 ls 0rhlt
114 ls -rhlt
115 kls -rhlt
116 ls -rthl
117 mkdir 1
118 mkdir 2
119 touch 1
120 touch 2
121 ls -rhlt
122 touch three.txt
123 echo test>three.txt
124 ls -rhlt
125 cd ..
126 cd /
127 umount /xserver-storage/
128 drbdadm secondary all
drbd 工作原理
DRBD是一种块设备,可以被用于高可用(HA)之中.它类似于一个网络RAID-1功能.当你将数据写入本地 文件系统时,数据还将会被发送到网络中另一台主机上.以相同的形式记录在一个文件系统中。 本地(主节点)与远程主机(备节点)的数据可以保证实时同步.当本地系统出现故障时,远程主机上还会 保留有一份相同的数据,可以继续使用.在高可用(HA)中使用DRBD功能,可以代替使用一个共享盘阵.
因为数据同时存在于本地主机和远程主机上,切换时,远程主机只要使用它上面的那份备份数据,
就可以继续进行服务了。
两台机器的环境如下:
hostname:srv5.localdomain
192.168.8.5
hostname:srv6.localdomain
192.168.8.6
准备环境
在两台机器上各新加一块磁盘
fdisk -l 列出所有的磁盘和分区的情况
在实验中可以看到新加的磁盘还没有分区
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
对新加的磁盘分区
[root@srv5 ~]# fdisk /dev/sdb
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-130, default 130):
Using default value 130
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1-130, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-130, default 130):
Using default value 130
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
加载磁盘信息
partprobe /dev/sdb
cat /proc/partions
安装和配置DRBD
1,两台机器上分别安装drbd
yum -y install kmod-drbd83 drbd83
检查是否安装成功
[root@srv5 yum.repos.d]# modprobe -l | grep -i drbd
/lib/modules/2.6.18-53.el5/weak-updates/drbd83/drbd.ko
安装完成后再/sbin 目录下有drbd的命令文件, 在/etc/init.d/目录下有drbd启动脚本
[root@srv5 yum.repos.d]# ls /sbin/drbd*
/sbin/drbdadm /sbin/drbdmeta /sbin/drbdsetup
2,配置drbd
2.1,在两台机器的hosts文件中添加如下的内容:
192.168.8.5 srv5.localdomain
192.168.8.6 srv6.localdomain
2.2,DRBD运行的时候要读取/etc/drbd.conf文件,
将文件的内容保存为如下内容:
include "drbd.d/global_common.conf";
include "drbd.d/*.res";
修改global_common.conf文件内容如下:
- global {
- usage-count no;
- }
- common {
- protocol C;
- startup {
- wfc-timeout 15;
- degr-wfc-timeout 15;
- outdated-wfc-timeout 15;
- }
- disk {
- on-io-error detach;
- fencing resource-only;
- }
- net {
- cram-hmac-alg sha1;
- shared-secret "123456";
- }
- syncer {
- rate 100M;
- }
- }
创建一个xserver.res文件内容如下:
- resource xserver {
- meta-disk internal;
- device /dev/drbd0; #device指定的参数最后必须有一个数字,用于global的minor-count,
- #否则会报错。device指定drbd应用层设备。
- on srv5.localdomain{ #注意:drbd配置文件中,机器名大小写敏感!
- address 192.168.8.5:7789;
- disk /dev/sdb5;
- }
- on srv6.localdomain {
- address 192.168.8.6:7789;
- disk /dev/sdb5;
- }
- }
2.3,假设上面的配置是在92.168.8.5上做的,拷贝上面配置好的3个文件到92.168.8.6上
scp /etc/drbd.conf 192.168.8.6:/etc/drbd.conf
scp /etc/drbd.d/* 192.168.8.6:/etc/drbd.d/
3,在两台机器上创建drbd元数据信息:
[root@srv5 ~]# drbdadm create-md all
Writing meta data...
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created.
4,启动服务,
4.1,在主节点92.168.8.5上
service drbd start
在备份节点92.168.8.6上
service drbd start
在两台机器上用下面的命令drbd-overview或者cat /proc/drbd查看,发现都是Secondary 备份状态
[root@srv5 ~]# drbd-overview
0:xserver Connected Secondary/Secondary Inconsistent/Inconsistent C r-----
[root@srv6 ~]# cat /proc/drbd
version: 8.3.15 (api:88/proto:86-97)
GIT-hash: 0ce4d235fc02b5c53c1c52c53433d11a694eab8c build by mockbuild@builder17.centos.org, 2013-03-27 16:04:08
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:1044092
4.2,在主节点92.168.8.5上执行下面的命令让其成为主节点
drbdadm -- --overwrite-data-of-peer primary all
然后再看状态:
[root@srv5 ~]# drbd-overview
0:xserver SyncSource Primary/Secondary UpToDate/Inconsistent C r---n-
[=============>......] sync'ed: 70.6% (310268/1044092)K
[root@srv5 ~]# drbd-overview
0:xserver SyncSource Primary/Secondary UpToDate/Inconsistent C r---n-
[================>...] sync'ed: 89.1% (117372/1044092)K
[root@srv5 ~]# drbd-overview
0:xserver Connected Primary/Secondary UpToDate/UpToDate C r-----
4.3,将/dev/drbd0格式化并挂载
在主节点192.168.8.5上执行下面的命令
mkfs.ext3 /dev/drbd0
mkdir /xserver-storage
mount /dev/drbd0 /xserver-storage
4.4,测试同步
在主节点192.168.8.5上执行下面的命令
cd /xserver-storage
echo "a file created in server5" > testfile
在备份节点192.168.8.6上执行下面的命令
mkdir /xserver-storage
mount /dev/drbd0 /xserver-storage
#mount 会出错,因为mount只能在Primary一端使用
在主节点192.168.8.5上执行下面的命令变成备份节点
umount /xserver-storage
drbdadm secondary all
在备份节点192.168.8.6上执行下面的命令变为主节点,可以看到192.168.8.5同步过来的内容
drbdadm primary all
mount /dev/drbd0 /xserver-storage
less /xserver-storage/testfile
可以查看到文件的内容为"a file created in server5"
在192.168.8.6新建一个文件
echo "a file created in server6" > testfile2
再将192.168.8.5变为主节点后mount可以看到testfile2的内容也同步了。