Centos5.5文件实时同步(DRBD)的实现

     Distributed Replicated Block Device(DRBD)是一个用软件实现的、无共享的、服务器之间镜像块设备内容的存储复制解决方案。数据镜像:实时、透明、同步,异步。DRBD的核心功能通过Linux的内核实现。下面我用Centos5.5系统搭建服务,希望能对大家有帮助。

复制模式:3种模式:
协议A:异步复制协议。本地写成功后立即返回,数据放在发送buffer中,可能丢失。
协议B:内存同步(半同步)复制协议。本地写成功并将数据发送到对方后立即返回,如果双机掉电,数据可能丢失。
协议C:同步复制协议。本地和对方写成功确认后返回。如果双机掉电或磁盘同时损坏,则数据可能丢失。

1.配置两台服务器的网络参数

服务器A:

[root@centos ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:C0:BB:83
inet addr:2.2.2.135  Bcast:2.2.2.255  Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fec0:bb83/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:46877 errors:0 dropped:0 overruns:0 frame:0
TX packets:2334 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3481575 (3.3 MiB)  TX bytes:259845 (253.7 KiB)
lo        Link encap:Local Loopback
inet addr:127.0.0.1  Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING  MTU:16436  Metric:1
RX packets:1065 errors:0 dropped:0 overruns:0 frame:0
TX packets:1065 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1957592 (1.8 MiB)  TX bytes:1957592 (1.8 MiB)
[root@centos ~]# hostname
centos
[root@centos ~]# vim /etc/hosts
2.2.2.135 centos         --添加以下两行
2.2.2.145 centos1

服务器B:

[root@centos1 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:EC:53:12
inet addr:2.2.2.145  Bcast:2.2.2.255  Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:feec:5312/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:3894 errors:0 dropped:0 overruns:0 frame:0
TX packets:108 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:292745 (285.8 KiB)  TX bytes:11858 (11.5 KiB)
lo        Link encap:Local Loopback
inet addr:127.0.0.1  Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING  MTU:16436  Metric:1
RX packets:1470 errors:0 dropped:0 overruns:0 frame:0
TX packets:1470 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3399758 (3.2 MiB)  TX bytes:3399758 (3.2 MiB)
[root@centos1 ~]# hostname
centos1
[root@centos1 ~]# vim /etc/hosts
2.2.2.135  centos   --添加以下两行
2.2.2.145  centos1
[root@centos1 ~]# ping 2.2.2.135 -c 2
PING 2.2.2.135 (2.2.2.135) 56(84) bytes of data.
64 bytes from 2.2.2.135: icmp_seq=1 ttl=64 time=3.61 ms
64 bytes from 2.2.2.135: icmp_seq=2 ttl=64 time=3.05 ms
--- 2.2.2.135 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 3.054/3.332/3.611/0.284 ms
[root@centos1 ~]#

2.磁盘分区(两台服务器分相同大小 的分区)

[root@centos ~]# fdisk  /dev/sdb     --在两台服务器分别添加一个同步分区
Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-64, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-64, default 64): +500M
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
[root@centos ~]#

2.下载安装DRBD软件

     服务器A:

[root@centos /]# wget http://vault.centos.org/5.5/extras/i386/RPMS/drbd83-8.3.8-1.el5.centos.i386.rpm
[root@centos /]# wget http://vault.centos.org/5.5/extras/i386/RPMS/kmod-drbd83-8.3.8-1.el5.centos.i686.rpm
[root@centos1 /]# rpm -ivh drbd83-8.3.8-1.el5.centos.i386.rpm
Preparing...                ########################################### [100%]
1:drbd83                 ########################################### [100%]
[root@centos1 /]# rpm -ivh kmod-drbd83-8.3.8-1.el5.centos.i686.rpm
Preparing...                ########################################### [100%]
1:kmod-drbd83            ########################################### [100%]
[root@centos ~]# vim /etc/drbd.conf
:r /usr/share/doc/drbd83-8.3.8/drbd.conf        --导入配置文件
:wq               --保存
[root@centos ~]# vim /etc/drbd.d/web.res     --添加如下代码
resource web{
on centos {
device /dev/drbd0;
disk  /dev/sdb1;
address  2.2.2.135:7781;
meta-disk internal;
}
on centos1 {
device /dev/drbd0;
disk  /dev/sdb1;
address 2.2.2.145:7781;
meta-disk  internal;
}
}

    服务器B:

[root@centos1 /]# rpm -ivh drbd83-8.3.8-1.el5.centos.i386.rpm
Preparing...                ########################################### [100%]
1:drbd83                 ########################################### [100%]
[root@centos1 /]# rpm -ivh kmod-drbd83-8.3.8-1.el5.centos.i686.rpm
Preparing...                ########################################### [100%]
1:kmod-drbd83            ########################################### [100%]
[root@centos ~]# vim /etc/drbd.conf
:r /usr/share/doc/drbd83-8.3.8/drbd.conf        --导入配置文件
:wq               --保存
[root@centos ~]# vim /etc/drbd.d/web.res     --添加如下代码
resource web{
on centos {
device /dev/drbd0;
disk  /dev/sdb1;
address  2.2.2.135:7781;
meta-disk internal;
}
on centos1 {
device /dev/drbd0;
disk  /dev/sdb1;
address 2.2.2.145:7781;
meta-disk  internal;
}
}

3.初始配置和启动服务

   服务器A:

[root@centos ~]# drbdadm create-md web
.....................
....................
New drbd meta data block successfully created.         --有这个结尾表示成功

   服务器B:

[root@centos1 ~]# drbdadm create-md web
.....................
....................
New drbd meta data block successfully created.         --有这个结尾表示成功

   服务器A:

[root@centos ~]# /etc/init.d/drbd start    --启动服务
Starting DRBD resources: [
web
Found valid meta data in the expected location, 509927424 bytes into /dev/sdb1.
d(web) n(web) ].
[root@centos ~]#

   服务器B:

[root@centos1 ~]# /etc/init.d/drbd start
Starting DRBD resources: [
web
Found valid meta data in the expected location, 509927424 bytes into /dev/sdb1.
d(web) n(web) ]..........
***************************************************************
DRBD's startup script waits for the peer node(s) to appear.
- In case this node was already a degraded cluster before the
reboot the timeout is 0 seconds. [degr-wfc-timeout]
- If the peer was available before the reboot the timeout will
expire after 0 seconds. [wfc-timeout]
(These values are for resource 'web'; 0 sec -> wait forever)
To abort waiting enter 'yes' [  10]:
.
[root@centos1 ~]#

  查看drbd的状态

[root@centos ~]# cat /proc/drbd
version: 8.3.8 (api:88/proto:86-94)
GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 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:497928
[root@centos ~]#

4.设置服务器A为主设备

[root@centos ~]# drbdadm -- --overwrite-data-of-peer  primary web  --设主设备
[root@centos ~]# cat /proc/drbd    --查看同步信息
version: 8.3.8 (api:88/proto:86-94)
GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16
0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r----
ns:2208 nr:0 dw:0 dr:2208 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:495720
[>....................] sync'ed:  0.9% (495720/497928)K delay_probe: 6685
finish: 0:20:39 speed: 368 (368) K/sec
[root@centos ~]#......................      --等一下看是否同步完毕
[root@centos ~]# cat /proc/drbd      --同步完成了
version: 8.3.8 (api:88/proto:86-94)
GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16
0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----
ns:497928 nr:0 dw:0 dr:497928 al:0 bm:31 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
[root@centos ~]# mkfs.ext3 /dev/drbd0    --格式化设备
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
124928 inodes, 497928 blocks
24896 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
61 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@centos ~]# mount /dev/drbd0  /mnt/
[root@centos ~]# df -TH
Filesystem    Type     Size   Used  Avail Use% Mounted on
/dev/sda2     ext3      11G   3.6G   6.2G  37% /
/dev/sda1     ext3     510M    17M   467M   4% /boot
tmpfs        tmpfs     264M      0   264M   0% /dev/shm
/dev/drbd0    ext3     494M    11M   458M   3% /mnt
[root@centos ~]#

 5.测试主从切换功能

    服务器A:

[root@centos ~]# cp -a /etc/passwd /etc/shadow  /mnt/    --在主设备考备文件
[root@centos ~]# ll /mnt/
total 17
drwx------ 2 root root 12288 Jan 14 17:16 lost+found
-rw-r--r-- 1 root root  1601 Nov 19 13:45 passwd
-r-------- 1 root root   994 Nov 19 13:45 shadow
[root@centos ~]# umount /mnt/
[root@centos ~]# drbdadm  secondary web

    服务器B:

[root@centos1 ~]# drbdadm  primary web    --将从设备设置为主设备
[root@centos1 ~]# mount /dev/drbd0  /mnt/   --挂载
[root@centos1 ~]# ll /mnt/     --查看文件存在
total 17
drwx------ 2 root root 12288 Jan 14 17:16 lost+found
-rw-r--r-- 1 root root  1601 Nov 19 13:45 passwd
-r-------- 1 root root   994 Nov 19 13:45 shadow
[root@centos1 ~]#

 

本文出自 “一起走过的日子” 博客,谢绝转载!

你可能感兴趣的:(Centos5.5文件实时同步(DRBD)的实现)