drbd學習總結

嗯,有些是直接摘自別人的博客,更多的是manpage,不違法吧。我的就沒什麼版權聲明瞭,GPL

DistributedReplicatedBlockDevice(DRBD)是一種基於軟件的,無共享,複製的存儲解決方案,在服務器之間的對塊設備(硬盤,分區,邏輯卷等)進行鏡像。DRBD工作在內核當中的,類

似於一種驅動模塊。DRBD工作的位置在文件系統的buffercache和磁盤調度器之間,通過tcp/ip發給另外一臺主機到對方的tcp/ip最終發送給對方的drbd,再由對方的drbd存儲在本地對應磁

盤上,類似於一個基於網絡的RAID-1。

DRBD需要構建在底層設備之上,然後構建出一個塊設備出來。對於用戶來說,一個DRBD設備,就像是一塊物理的磁盤,可以在上面內創建文件系統。DRBD所支持的底層設備有以下這些類:
1、一個磁盤,或者是磁盤的某一個分區;
2、一個softraid設備;
3、一個LVM的邏輯卷;
4、一個EVMS(EnterpriseVolumeManagementSystem,企業卷管理系統)的卷;
5、其他任何的塊設備。


由於單機文件系統的限制,drbd只能在主節點上被掛載。
Inspiteofthislimitation,therearestillafewwaystoaccessthedataonthesecondnode:

UseDRBDonlogicalvolumesanduseLVM'scapabilitiestotakesnapshotsonthestandbynode,andaccessthedataviathesnapshot.
DRBD'sprimary-primarymodewithashareddiskfilesystem(GFS,OCFS2).Thesesystemsareverysensitivetofailuresofthereplicationnetwork.


DRBD的配置:
範例:
環境:
1、兩個節點,分別爲node1(10.0.9.1),node2(10.0.9.2),這裏node11,node2即是主機名,和'uname-n'的輸出相同。
2、每個節點各提供一個大小精確相同的塊設備(我這裏使用的是一個lv),分別爲/dev/centos_vg/web和/dev/myvg/web,均爲100M。
3、我不知道是不是要求drbd的版本一致,我的兩臺虛擬機操作系統版本和drbd的版本都是一致的。centos6.4x86_64

操作步驟:
1、確保兩臺drbd的主機時間是同步的,這個要求跟配置集羣節點是相同的。
2、兩個節點的主機名應當可以正確解析,且主機名應該和'uname-n'的結果相同,解析出來的IP應當是用於兩個節點之間通信、同步數據的IP。建議配置hosts文件,比DNS靠譜。
3、安裝軟件包
drbd共有兩部分組成:內核模塊和用戶空間的管理工具。其中drbd內核模塊代碼已經整合進Linux內核2.6.33以後的版本中,因此,如果您的內核版本高於此版本的話,你只需要安裝

管理工具即可;否則,您需要同時安裝內核模塊和管理工具兩個軟件包,並且模塊和管理工具的版本號一定要保持對應。
由於我所使用的centos內核不自帶drbd模塊,所以需要安裝。

目前適用CentOS5的drbd版本主要有8.0、8.2、8.3三個版本,其對應的rpm包的名字分別爲drbd,drbd82和drbd83,對應的內核模塊的名字分別爲kmod-drbd,kmod-drbd82和kmod-

drbd83。而適用於CentOS6的版本爲8.4,其對應的rpm包爲drbd和drbd-kmdl,但在實際選用時,要切記兩點:drbd和drbd-kmdl的版本要對應;另一個是drbd-kmdl的版本要與當前系統的內核

版本相對應。我這裏選用8.4的版本(drbd-8.4.3-33.el6.x86_64.rpm和drbd-kmdl-2.6.32-358.el6-8.4.3-33.el6.x86_64.rpm),下載地址爲ftp://rpmfind.net/linux/atrpms/
下載完成後直接安裝即可:
#rpm-ivhdrbd-8.4.3-33.el6.x86_64.rpmdrbd-kmdl-2.6.32-358.el6-8.4.3-33.el6.x86_64.rpm

4、配置
drbd的主配置文件爲/etc/drbd.conf.內容是:
include"drbd.d/global_common.conf";
include"drbd.d/*.res";

global_common.conf中定義global段和common段,而每一個.res的文件用於定義一個資源。
在配置文件中,global段僅能出現一次,且如果所有的配置信息都保存至同一個配置文件中而不分開爲多個文件的話,global段必須位於配置文件的最開始處。目前global段中可以定義的參

數僅有minor-count,dialog-refresh,disable-ip-verification和usage-count。

common段則用於定義被每一個資源缺省繼承的參數,可以在資源定義中使用的參數都可以在common段中定義。實際應用中,common段並非必須,但建議將多個資源共享的參數定義爲

common段中的參數以降低配置文件的複雜度。

resource段則用於定義drbd資源,資源在定義時必須爲其命名,名字可以由非空白的ASCII字符組成。每一個資源段的定義中要包含兩個(或更多)host子段,以定義此資源關聯至的

節點。

兩個節點關於同一drbd資源的配置應當完全相同,之所以這麼說,是考慮到可能存在存在多個drbd節點、多個drbd資源的情況:比如node1上有資源A,node2上有資源B,C,node3上有

資源C。這裏且不考慮這種情況
先在node1上配置:
global_common.conf文件

global {
usage-count no;
# minor-count dialog-refresh disable-ip-verification
}
common {
protocol C;
handlers {
# These are EXAMPLE handlers only.
# They may have severe implications,
# like hard resetting the node under certain circumstances.
# Be careful when chosing your poison.
pri-on-incon-degr "/usr/lib/drbd/notify-pri-on-incon-degr.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f";
pri-lost-after-sb "/usr/lib/drbd/notify-pri-lost-after-sb.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f";
local-io-error "/usr/lib/drbd/notify-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ; halt -f";
# fence-peer "/usr/lib/drbd/crm-fence-peer.sh";
# split-brain "/usr/lib/drbd/notify-split-brain.sh root";
# out-of-sync "/usr/lib/drbd/notify-out-of-sync.sh root";
# before-resync-target "/usr/lib/drbd/snapshot-resync-target-lvm.sh -p 15 -- -c 16k";
# after-resync-target /usr/lib/drbd/unsnapshot-resync-target-lvm.sh;
}
startup {
# wfc-timeout degr-wfc-timeout outdated-wfc-timeout wait-after-sb
}
options {
# cpu-mask on-no-data-accessible
}
disk {
on-io-error detach;
# size max-bio-bvecs on-io-error fencing disk-barrier disk-flushes
# disk-drain md-flushes resync-rate resync-after al-extents
# c-plan-ahead c-delay-target c-fill-target c-max-rate
# c-min-rate disk-timeout
}
net {
cram-hmac-alg "sha1";
shared-secret   "mydrbd";
# protocol timeout max-epoch-size max-buffers unplug-watermark
# connect-int ping-int sndbuf-size rcvbuf-size ko-count
# allow-two-primaries cram-hmac-alg shared-secret after-sb-0pri
# after-sb-1pri after-sb-2pri always-asbp rr-conflict
# ping-timeout data-integrity-alg tcp-cork on-congestion
# congestion-fill congestion-extents csums-alg verify-alg
# use-rle
}
syncer {
rate 30M;
}
}

web.res的內容:

resource web {
meta-disk internal;
on node1 {
device /dev/drbd0;  #等價於 device minor 0
disk    /dev/centos_vg/web;
address 10.0.9.1:7789;
}
on node2 {
device /dev/drbd0;  #在node2上生成的 drbd 設備名
disk    /dev/myvg/web;  #使用node2上哪個塊設備作爲drbd的底層設備
address 10.0.9.2:7789;  #node2使用該socket與對方節點通信
}
}

也可以使用floatingnode-ip的形式代替onnode-name的形式:
這裏是manpage中的一個示例:

resourcer2{
protocolC;
deviceminor2;
disk/dev/sda7;
meta-diskinternal;

#shortform,device,diskandmeta-diskinherited
floating10.1.1.31:7802;

#longerform,onlydeviceinherited
floating10.1.1.32:7802{
disk/dev/sdb;
meta-disk/dev/sdc8;
}
}

5、將以上配置複製到對方節點
6、初始化資源,分別在兩邊節點上創建/var/lib/drbd目錄(該目錄用於放置類似drbd-minor-0.lkbd的文件),執行:
#drbdadmcreate-mdweb
7、啓動drbd服務
#/etc/init.d/drbdstart

8、查看啓動狀態
#cat/proc/drbd
或者
#drbd-overview
此時兩個節點均處於secondary狀態
9、根據需要將其中一個節點設置爲主節點,在需要成爲主節點的機器上執行
#drbdadmprimary--forceweb
查看drbd的狀態,從節點會從主節點進行逐位同步
10、在主節點上,爲drbd設備創建文件系統(也可以將drbd設備創建成LVM設備),然後掛載
drbd集羣中,只有主節點是可以讀的,從節點的drbd設備是不能讀寫的,所有節點的drbd底層設備上不能執行任何文件系統操作(在drbd在上面運行時),包括dumpe2fs等命令。

11、切換主從節點
在主節點上:
將掛載的drbd設備卸載,然後執行
drbdadmsecondaryweb
在從節點上:
drbdadmprimaryweb
然後掛載drbd設備,注意,不要格式化,因爲drbd從設備在同步的時候是逐位進行同步的,同步的時候就已經格式化了。

關於配置的一些解釋:

protocolprot-id
OntheTCP/IPlinkthespecifiedprotocolisused.ValidprotocolspecifiersareA,B,andC.

ProtocolA:writeIOisreportedascompleted,ifithasreachedlocaldiskandlocalTCPsend
buffer.

ProtocolB:writeIOisreportedascompleted,ifithasreachedlocaldiskandremotebuffer
cache.

ProtocolC:writeIOisreportedascompleted,ifithasreachedbothlocalandremotedisk.

handlers:inthissectionyoucandefinehandlers(executables)thatarestartedbytheDRBDsysteminresponsetocertainevents

devicenameminornr
Thenameoftheblockdevicenodeoftheresourcebeingdescribed.Youmustusethisdevicewith
yourapplication(filesystem)andyoumustnotusethelowlevelblockdevicewhichisspecified
withthediskparameter.

Onecanetheromitthenameorminorandtheminornumber.Ifyouomitthenameadefaultof
/dev/drbdminorwillbeused.

Udevwillcreateadditionalsymlinksin/dev/drbd/by-resand/dev/drbd/by-disk.

diskname
DRBDusesthisblockdevicetoactuallystoreandretrievethedata.Neveraccesssuchadevice
whileDRBDisrunningontopofit.Thisalsoholdstruefordumpe2fs(8)andsimilarcommands.

addressAFaddr:port
AresourceneedsoneIPaddressperdevice,whichisusedtowaitforincomingconnectionsfrom
thepartnerdevicerespectivelytoreachthepartnerdevice.AFmustbeoneofipv4,ipv6,
ssocksorsdp(forcompatibilityreasonssciisanaliasforssocks).ItmaybeomitedforIPv4
addresses.TheactualIPv6addressthatfollowstheipv6keywordmustbeplacedinsidebrackets:
ipv6[fd01:2345:6789:abcd::1]:7800.

EachDRBDresourceneedsaTCPportwhichisusedtoconnecttothenode'spartnerdevice.Two
differentDRBDresourcesmaynotusethesameaddr:portcombinationonthesamenode.


on-io-errorhandler
istaken,ifthelowerleveldevicereportsio-errorstotheupperlayers.

handlermaybepass_on,call-local-io-errorordetach.

pass_on:Thenodedowngradesthediskstatustoinconsistent,markstheerroneousblockas
inconsistentinthebitmapandretriestheIOontheremotenode.

call-local-io-error:Callthehandlerscriptlocal-io-error.

detach:Thenodedropsitslowleveldevice,andcontinuesindisklessmode.

ping-inttime
IftheTCP/IPconnectionlinkingaDRBDdevicepairisidleformorethantimeseconds,DRBDwill
generateakeep-alivepackettocheckifitspartnerisstillalive.Thedefaultis10seconds,
theunitis1second.

ping-timeouttime
Thetimethepeerhastimetoanswertoakeep-alivepacket.Incasethepeer'sreplyisnot
receivedwithinthistimeperiod,itisconsideredasdead.Thedefaultvalueis500ms,the
defaultunitaretenthsofasecond.

become-primary-onnode-name
Setsonwhichnodethedeviceshouldbepromotedtoprimaryroleby
theinitscript.Thenode-namemighteitherbeahostnameorthe
keywordboth.Whenthisoptionisnotsetthedevicesstayin
secondaryroleonbothnodes.Usuallyonedelegatestherole
assignmenttoaclustermanager(e.g.heartbeat).
必須在common的startup中設置,若在資源中設置似乎會報錯

verify-alghash-alg
Duringonlineverification(asinitiatedbytheverifysub-command),
ratherthandoingabit-wisecomparison,DRBDappliesahash
functiontothecontentsofeveryblockbeingverified,andcompares
thathashwiththepeer.Thisoptiondefinesthehashalgorithm
beingusedforthatpurpose.Itcanbesettoanyofthekernel's
datadigestalgorithms.Inatypicalkernelconfigurationyoushould
haveatleastoneofmd5,sha1,andcrc32cavailable.Bydefault
thisisnotenabled;youmustsetthisoptionexplicitlyinorderto
beabletouseon-linedeviceverification.

cram-hmac-alg
YouneedtospecifytheHMACalgorithmtoenablepeerauthentication
atall.Youarestronglyencouragedtousepeerauthentication.The
HMACalgorithmwillbeusedforthechallengeresponse
authenticationofthepeer.Youmayspecifyanydigestalgorithm
thatisnamedin/proc/crypto.

shared-secret
Thesharedsecretusedinpeerauthentication.Maybeupto64
characters.Notethatpeerauthenticationisdisabledaslongasno
cram-hmac-alg(seeabove)isspecified.

resync-raterate
ToensureasmoothoperationoftheapplicationontopofDRBD,it
ispossibletolimitthebandwidthwhichmaybeusedbybackground
synchronizations.Thedefaultis250KB/sec,thedefaultunitis
KB/sec.OptionalsuffixesK,M,Gareallowed.

drbdadm常用選項和子命令
-d,--dry-run
-c,--config-file

attach
AttachesalocalbackingblockdevicetotheDRBDresource'sdevice.
detach
RemovesthebackingstoragedevicefromaDRBDresource'sdevice.
connect
Setsupthenetworkconfigurationoftheresource'sdevice.Ifthepeerdeviceisalready
configured,thetwoDRBDdeviceswillconnect.Iftherearemorethantwohostsectionsinthe
resourceyouneedtousethe--peeroptiontoselectthepeeryouwanttoconnectto.
disconnect
Removesthenetworkconfigurationfromtheresource.ThedevicewillthengointoStandAlone
state.
up
Isashortcutforattachandconnect.

down
Isashortcutfordisconnectanddetach.
syncer
Loadstheresynchronizationparametersintothedevice.
verify
Startsonlineverify.Duringonlineverify,dataonbothnodesiscomparedforequality.See
/proc/drbdforonlineverifyprogress.Ifout-of-syncblocksarefound,theyarenot
resynchronizedautomatically.Todothat,disconnectandconnecttheresourcewhenverification
hascompleted.
pause-sync
Temporarilysuspendanongoingresynchronizationbysettingthelocalpauseflag.Resynconly
progressesifneitherthelocalnortheremotepauseflagisset.Itmightbedesirableto
postponeDRBD'sresynchronizationuntilafteranyresynchronizationofthebackingstorage'sRAID
setup.
resume-sync
Unsetthelocalsyncpauseflag.
dstate
Showthecurrentstateofthebackingstoragedevices.(local/peer)

hidden-commands
Showsallcommandsundocumentedonpurpose.

常用命令:
drbdadmcreate-mdresource_name#初始化drbd資源
drbdadmverifyresource_name#啓動在線校驗
drbdsetup/dev/drbd0syncer-r100M#臨時設置re-synchronization的速度爲100M
drbdadmadjustresource_name#恢復drbd的re-synchronization的速度會配置文件中設置的值
cat/proc/drbd#查看drbd的狀態
drbdadmprimary[--force]resource_name#[強制]提升當前節點爲resource_name的主節點
drbdadmsecondary<resource_name>#用於在卸載drbd設備後將當前節點降級爲resource_name的從節點
drbdadmdump#Justparsetheconfigurationfileanddumpittostdout.可用於檢查語法
drbdadmdisconnect<resource_name>
drbdadmdetach<resource_name>


splitbrain腦裂
splitbrain實際上是指在某種情況下,造成drbd的兩個節點斷開連接,都以primary的身份來運行。當drbd某primary節點連接對方節點準備發送信息的時候如果發現對方也是primary狀態,

那麼會立刻自行斷開連接,並認定當前已經發生splitbrain了,這時候他會在系統日誌中記錄以下信息:“Split-Braindetected,droppingconnection!”當發生splitbrain之後,如果查看

連接狀態,其中至少會有一個是StandAlone狀態,另外一個可能也是StandAlone(如果是同時發現splitbrain狀態),也有可能是WFConnection的狀態。
如果我們在配置文件中配置了自動解決splitbrain(好像linbit不推薦這樣做),drbd會自行解決splitbrain問題,可通過如下策略進行配置。
Discardingmodificationsmadeonthe“younger”primary。在這種模式下,當網絡重新建立連接並且發現了裂腦,DRBD會丟棄最後切換到主節點上的主機所修改的數據。
Discardingmodificationsmadeonthe“older”primary.在這種模式下,當網絡重新建立連接並且發現了裂腦,DRBD丟棄首先切換到主節點上的主機後所修改的數據。
Discardingmodificationsontheprimarywithfewerchanges.在這種模式下,當網絡重新建立連接並且發現了裂腦,DRBD會比較兩臺主機之間修改的數據量,並丟棄修改數據量較少的主

機上的所有數據。
Gracefulrecoveryfromsplitbrainifonehosthashadnointermediatechanges.在這種模式下,如果其中一個主機在腦裂期間並沒有數據修改,DRBD會自動重新進行數據同步,並宣

布腦裂問題已解決。(這種情況幾乎不可能存在)
注意:自動裂腦自動修復能不能被接受取決於個人應用。考慮建立一個DRBD的例子庫。在“丟棄修改比較少的主節點的修改”興許對web應用好過數據庫應用。與此相反,財務的數據庫則是對

於任何修改的丟失都是不能容忍的,這就需要不管在什麼情況下都需要手工修復裂腦問題。因此需要在啓用裂腦自動修復前考慮你的應用情況。
如果沒有配置splitbrain自動解決方案,我們可以手動解決。首先我們必須要確定哪一邊應該作爲解決問題後的primary,一旦確定好這一點,那麼我們同時也就確定接受丟失在split

brain之後另外一個節點上面所做的所有數據變更了。當這些確定下來後,我們就可以通過以下操作來恢復了:
1、首先在確定要作爲secondary的節點上面切換成secondary並放棄該資源的數據:
drbdadmsecondaryresource_name
drbdadm—–discard-my-dataconnectresource_name
2、在要作爲primary的節點重新連接secondary(如果這個節點當前的連接狀態爲WFConnection的話,可以省略)
drbdadmconnectresource_name
當作完這些動作之後,從新的primary到secondary的re-synchnorisation會自動開始。

metadata
DRBD將數據的各種信息塊保存在一個專用的區域裏,這些metadata包括了
a,DRBD設備的大小
b,產生的標識
c,活動日誌
d,快速同步的位圖
metadata的存儲方式有內部和外部兩種方式,使用哪種配置都是在資源配置中定義的
內部metadata
內部metadata存放在同一塊硬盤或分區的最後的位置上
優點:metadata和數據是緊密聯繫在一起的,如果硬盤損壞,metadata同樣就沒有了,同樣在恢復的時候,metadata也會一起被恢復回來
缺點:metadata和數據在同一塊硬盤上,對於寫操作的吞吐量會帶來負面的影響,因爲應用程序的寫請求會觸發metadata的更新,這樣寫操作就會造成兩次額外的磁頭讀寫移動。
外部metadata
外部的metadata存放在和數據磁盤分開的獨立的塊設備上
優點:對於一些寫操作可以對一些潛在的行爲提供一些改進
缺點:metadata和數據不是聯繫在一起的,所以如果數據盤出現故障,在更換新盤的時候就需要認爲的干預操作來進行現有node對心硬盤的同步了
如果硬盤上有數據,並且硬盤或者分區不支持擴展,或者現有的文件系統不支持shrinking,那就必須使用外部metadata這種方式了。

disk在common段中是一個子section,定義的是關於drdb設備的一些參數,而在resource段中是一個配置參數,定義用於提供drbd設備的底層塊設備名稱


參考文檔:

http://www.drbd.org/docs/introduction/
http://czmmiao.iteye.com/blog/1773079
man5drbd.conf
mandrbdadm
DRBDUser'sGuide

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章