2009年2月18日 星期三

What is DRBD


http://www.drbd.org/
http://www.allhabit.net/dedo_siteindex.php?q=centos+nfs+drbd+ha

DRBD® refers to block devices designed as a building block to form high availability (HA) clusters. This is done by mirroring a whole block device via an assigned network. DRBD can be understood as network based raid-1.

In the illustration above, the two orange boxes represent two servers that form an HA cluster. The boxes contain the usual components of a Linux™ kernel: file system, buffer cache, disk scheduler, disk drivers, TCP/IP stack and network interface card (NIC) driver. The black arrows illustrate the flow of data between these components.

The orange arrows show the flow of data, as DRBD mirrors the data of a high availably service from the active node of the HA cluster to the standby node of the HA cluster.

DRBD

http://gator.hoja.idv.tw/post/2/52


DRBD是一個Distributed Replicated Block Device的縮寫,簡單的說,他是一個遠端資料備份的機制,不同rsync,他是透過網路realtime的mirror Local端與遠端的Device。

本次測試的軟硬體如下:


  • CPU: Transmeta TM5600 533MHz
  • Memory: 128M
  • HD: 實際測試資料是寫入透過USB1.1連接的硬碟
  • (這會使測試資料較不正確,但是礙於時間,暫時如此)
  • OS: CentOS4.1 Linux kernel升級至 2.6.15.5
  • DRBD: drbd-0.7.16 tarball

(上述機器共兩台)

安裝與設定

如果沒有特殊說明,表示兩台機器都要作。
假設
primary IP= 192.168.55.1 hostname=ha1
secondary IP= 192.168.55.2 hostname=ha2

1. 升級kernel 至 2.6.15.5
解壓縮kernel source於 /usr/src下,並建一個link,link到 /usr/src/linux。然後重新編譯kernel

2. 安裝DRBD
將drbd-0.7.16.tar.gz解壓縮至/usr/src/然後執行以下指令

>cd /usr/src/drbd-0.7.16
>make; make install
> modprobe drbd
>lsmod #正確的話,應該可以看到drbd module已經安裝好了
>ls /dev/drbd*
/dev/drbd0 /dev/drbd1

(ps: source code中有INSTALL說明檔,但是不需要照著作。直接make即可)

3. 編輯 /etc/drbd.conf

resource r0 {
protocol C;
incon-degr-cmd "halt -f";

on ha1 {
device /dev/drbd0;
disk /dev/sda6; #要寫入的device
meta-disk internal;
address 192.168.55.1:7788;
}
on ha2 {
device /dev/drbd0;
disk /dev/sda2; #要寫入的device
meta-disk internal;
address 192.168.55.2:7788;
} }

PS: Device 必須大於128MB


4. 在入module,與啟動brbd

執行以下指令
> drbdadm up all
> dmesg | tail #正確的話,應該可以看到以下類似訊息
drbd: initialised. Version: 0.7.0 svn $Rev: 1442 $ (api:74/proto:74)
drbd: registered as block device major 147

nb: to have it register as 43 (NBD) you can say
modprobe drbd use_nbd_major

drbd0: Creating state block
drbd0: resync bitmap: bits=1250 words=40
drbd0: size = 5000 KB
drbd0: Assuming that all blocks are out of sync (aka FullSync)
drbd0: 5000 KB now marked out-of-sync by on disk bit-map.
drbd0: Handshake successful: DRBD Network Protocol version 74
drbd0: Connection established.
drbd0: I am inconsistent, but there is no sync? BOTH nodes inconsistent!
drbd0: Secondary/Unknown --> Secondary/Secondary
> cat /proc/drbd #正確的話,應該可以看到以下類似訊息
version: 0.7.0 svn $Rev: 1442 $ (api:74/proto:74)

0: cs:Connected st:Secondary/Secondary ld:Inconsistent
ns:0 nr:0 dw:0 dr:0 al:0 bm:1 lo:0 pe:0 ua:0 ap:0

以下的步驟只需在Primary作
>drbdadm primary all #如果有錯誤訊息,你也可以執行以下那行指令
>drbdadm -- --do-what-I-say primary all
>drbdadm -- connect all
>cat /proc/drbd #你將會看到sync的畫面如下
0: cs:SyncSource st:Primary/Secondary ld:Consistent
ns:12940824 nr:0 dw:87492 dr:13690591 al:109 bm:1668 lo:1000 pe:1876 ua:1000 ap:0
[========>...........] sync'ed: 44.4% (15858/28487)M
finish: 0:09:20 speed: 28,933 (25,160) K/sec

不需要等到sync完成即可以作測試了!!

測試與結果

首先,我先在ha1(primary)上建立一個mount point,並將/dev/drbd0 mount上去。然後執行我寫好的touch去產生檔案。產生完後,umount 並且執行 drbdadm secondary all 將Primary變成 secondry。

#在ha1執行,並記得先mke2fs /dev/sda6以及 ha2上的/dev/sda2
>mkdir /mnt/test
>mount /dev/drbd0 /mnt/test
>touch test.txt
>umount /mnt/test
>drbdadm secondary all

接著移至ha2,現在同樣把他變成primary,並mount起/dev/drbd0,如果可以看到之前建立的檔案即成功了。

#在ha2執行
>drbdadm primary all
>mkdir /mnt/test
>mount /dev/drbd0 /mnt/test
>ls /mnt/test
test.txt

另外,我也試過,如果,secondary異常時(例如,斷線),primary會正常運作,當secondary恢復後,資料也會自動備援過去。

接著,我分別在ha1本機硬碟,ha1上的/dev/sda6(USB1.1 上的硬碟),以及DRBD執行下(含secondary 連線正常 與secondary 斷線的情況下,執行我的php scrip(touch.php)產生出以下數據。

執行狀態檔案大小測試次數平均執行時間(sec)備註
本機硬碟 100MB 50 8.846169
USB1.1連接硬碟 100MB 50 75.870175 [1]
DRDB 100MB 10 94.990632 [2]
DRDB ( Secondry fail) 100MB 10 79.909951 當網路恢復,DRBD約以240K/sec速度進行sync
100MB資料約7min多中完成sync
此資訊可由 watch cat /proc/drbd 取得

根據以上的資料顯是,DRBD大約會造成效率降低25% ( ( [2]- [1]) / [1] *100%)。

PS: 我的touch.php 如下:(他會產生以1K為單位的檔案,檔案大小與執行次數由參數決定)

#!/usr/bin/php -q