83374255 2013-06-07
-----------------------闲 扯-------------------------------
最近准备更新点负载均衡高可用的文档,所以把之前一直想攻克的DRBD今天抽空给搞定了。
DRBD(Distributed Replicated Block Device) 我们可以理解为它其实就是个网络RAID-1,两台服务器间就算某台因断电或者宕机也不会对数据有任何影响,而真正的热切换可以通过Heartbeat方案解决,不需要人工干预。
例如:DRBD+Heartbeat+Mysql进行主从结构分离,作为DRBD+HeartBeat+NFS的备份存储解决方案。
--------------------废话不多说,开搞---------------------------
系统版本:CentOS6.3 x64(内核2.6.32)
DRBD:DRBD-8.4.3
Primary: 192.168.7.88(drbd1.example.com)
Secondary: 192.168.7.89 (drbd2.example.com)
(Primary)为仅主服务器端配置
(Secondary)为仅从服务器端配置
(Primary,Secondary)为主服务器端从服务器端共同配置
一.准备环境:(Primary,Secondary)
1.关闭iptables和SELINUX,避免安装过程中报错。
# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
---------------
2.设置hosts文件
# vi /etc/hosts
-----------------
192.168.7.88 drbd1.example.com drbd1
192.168.7.89 drbd2.example.com drbd2
-----------------
3.在两台虚拟机分别添加一块2G硬盘sdb作为DRBD,分别分区为sdb1,大小2G,并在本地系统创建/data目录,不做挂载操作。
# fdisk /dev/sdb
----------------
n-p-1-1-"+1G"-w
----------------
# mkdir /data
4.时间同步:
# ntpdate -u asia.pool.ntp.org
5.更改主机名:
(Primary)
# vi /etc/sysconfig/network
----------------
HOSTNAME=server.example.com
----------------
(Secondary)
# vi /etc/sysconfig/network
----------------
HOSTNAME=client.example.com
----------------
二.DRBD的安装配置:
1.安装依赖包:(Primary,Secondary)
# yum install gcc gcc-c++ make glibc flex kernel-devel kernel-headers
2.安装DRBD:(Primary,Secondary)
# wget http://oss.linbit.com/drbd/8.4/drbd-8.4.3.tar.gz
# tar zxvf drbd-8.4.3.tar.gz
# cd drbd-8.4.3
# ./configure --prefix=/usr/local/drbd --with-km
# make KDIR=/usr/src/kernels/2.6.32-279.el6.x86_64/
# make install
# mkdir -p /usr/local/drbd/var/run/drbd
# cp /usr/local/drbd/etc/rc.d/init.d/drbd /etc/rc.d/init.d
# chkconfig --add drbd
# chkconfig drbd on
加载DRBD模块:
# modprobe drbd
查看DRBD模块是否加载到内核:
# lsmod |grep drbd
3.参数配置:(Primary,Secondary)
# vi /usr/local/drbd/etc/drbd.conf
清空文件内容,并添加如下配置:
---------------
resource r0{
protocol C;
startup { wfc-timeout 0; degr-wfc-timeout 120;}
disk { on-io-error detach;}
net{
timeout 60;
connect-int 10;
ping-int 10;
max-buffers 2048;
max-epoch-size 2048;
}
syncer { rate 30M;}
on drbd1.example.com{
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.7.88:7788;
meta-disk internal;
}
on drbd2.example.com{
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.7.89:7788;
meta-disk internal;
}
}
---------------
4.创建DRBD设备并激活ro资源:(Primary,Secondary)
# mknod /dev/drbd0 b 147 0
# drbdadm create-md r0
等待片刻,显示success表示drbd块创建成功
----------------
Writing meta data...
initializing activity log
NOT initializing bitmap
New drbd meta data block successfully created.
--== Creating metadata ==--
As with nodes, we count the total number of devices mirrored by DRBD
at http://usage.drbd.org.
The counter works anonymously. It creates a random number to identify
the device and sends that random number, along with the kernel and
DRBD version, to usage.drbd.org.
http://usage.drbd.org/cgi-bin/insert_usage.pl?
nu=716310175600466686&ru=15741444353112217792&rs=1085704704
* If you wish to opt out entirely, simply enter 'no'.
* To continue, just press [RETURN]
success
----------------
再次输入该命令:
# drbdadm create-md r0
成功激活r0
----------------
[need to type 'yes' to confirm] yes
Writing meta data...
initializing activity log
NOT initializing bitmap
New drbd meta data block successfully created.
----------------
5.启动DRBD服务:(Primary,Secondary)
# service drbd start
注:需要主从共同启动方能生效
6。查看状态:(Primary,Secondary)
# cat /proc/drbd
----------------
# cat /proc/drbd
version: 8.4.3 (api:1/proto:86-101)
GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by [email protected],
2013-05-27 20:45:19
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:1060184
----------------
或
# service drbd status
----------------
drbd driver loaded OK; device status:
version: 8.4.3 (api:1/proto:86-101)
GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by [email protected],
2013-05-27 20:45:19
m:res cs ro ds p mounted fstype
0:r0 Connected Secondary/Secondary Inconsistent/Inconsistent C
----------------
这里ro:Secondary/Secondary表示两台主机的状态都是备机状态,ds是磁盘状态,显示的状态内
容为“不一致”,这是因为DRBD无法判断哪一方为主机,应以哪一方的磁盘数据作为标准。