歡迎您光臨本站 註冊首頁

drbd安裝使用指南

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

drbd安裝使用指南

一:概述:

DRBD 是由內核模塊和相關腳本而構成,用以構建高可用性的集群。其實現方式是通過網路來鏡像整個設備。您可以把它看作是一種網路RAID。 Drbd 負責接收數據,把數據寫到本地磁碟,然後發送給另一個主機。另一個主機再將數據存到自己的磁碟中。   

源碼下載http://oss.linbit.com/drbd/0.7/drbd-0.7.19.tar.gz

核心參考文檔:http://www.drbd.org/drbd-howto.html



二:主要實現

假設有兩台機器nannan:192.168.0.136 需要鏡像的硬碟:/dev/hdc3

IXDBA.NET社區論壇

  root:192.168.0.139 需要鏡像的硬碟:/dev/hdc2

主伺服器為192.168.0.136 簡稱為136

備份伺服器為192.168.0.139 簡稱為139

平常對數據讀寫都在136上實現,當136 down掉后可以啟動139,實現數據的熱備份。

真正的熱切換需要才用HA 來實現。



三:下載安裝

安裝環境:Red Hat Enterprise Linux AS release 4,內核版本:2.6.9-22.EL

確認內核源碼存在,可到http://oss.linbit.com/drbd/去載.

下載源碼注意:當前最新的drbd-8.0pre3,配置文件無法正常配置,出現一大堆錯誤,所以下載以前的穩定版本。tar源碼解包后

運行:

A、make KDIR=/usr/src/linux   /*內核所在的位置*/

/*如果你沒有更改內核可以直接運行make,軟體會直接到/lib/module裡邊去尋找系統環境,如果是新的內核需要對內核進行編譯安裝,

否則make時候會錯誤中斷掉*/

B、make install

安裝完主要生成命令:drbdsetup ,drbdadmin

和配置文件:/etc/drbd.conf ,啟動文件,/etc/init.d/drbd

模塊文件:drbd.ko(在編譯好的安裝包目錄下的drbd下可以找到)

所有命令和配置文件都可以在源碼包編譯成功的目錄下面找到。

./scripts/drbd.conf是最原始的配置文件,當/etc/drbd.conf被破壞,可以直接拷貝覆蓋掉。

C、創建硬體設備drbd

mknod /dev/drbd0 b 147 0

mknod /dev/drbd1 b 147 1

mknod /dev/drbd2 b 147 2

或者用shell來建立多個:

#for i in $(seq 0 15) ; do mknod /dev/drbd$i b 147 $i ; done

D、DRBD 協議說明

    A 數據一旦寫入磁碟併發送到網路中就認為完成了寫入操作。

    B 收到接收確認就認為完成了寫入操作。

    C 收到寫入確認就認為完成了寫入操作。

    您還可以選擇其它參數來將數據傳輸給磁碟和網路選項。更多詳情,請參見drbdsetup 手冊頁。

四:配置drbd

修改/etc/drbd.conf

主要修改了:機器名和設備名ip地址

==================================================

  on web1{

    device     /dev/drbd0;

    disk       /dev/hdc3;

    address    192.168.0.136:7788;

    meta-disk  internal;

# meta-disk is either 'internal' or '/dev/ice/name '

    #

    # You can use a single block device to store meta-data

    # of multiple DRBD's.

    # E.g. use meta-disk /dev/hde6; and meta-disk /dev/hde6;

    # for two different resources. In this case the meta-disk

    # would need to be at least 256 MB in size.

    #

    # 'internal' means, that the last 128 MB of the lower device

    # are used to store the meta-data.

    # You must not give an index with 'internal'.

  }

on web2 {

    device    /dev/drbd0;

    disk      /dev/hdc2;

    address   192.168.0.139:7788;

    meta-disk internal;

  }

==================================================

下面是整個drbd.conf的配置文件:

注意:配置的版本是drbd-0.7.19.tar.gz,不同版本無法兼容。修改的地方就以上幾個地方和註釋,還有把除了resource r0 外的其他配置塊如resource r1等全部去掉。

也就是說

/****

on root {

    device    /dev/drbd0;

    disk      /dev/hdc2;

    address   192.168.0.139:7788;

    meta-disk internal;

  }

}

****/

後面的所有內容全部去掉,



下面是一個完整的配置好的drbd.conf

#

# drbd.conf example

#

skip {

  As you can see, you can also comment chunks of text

  with a 'skip{ skipped text }' section.

  This comes in handy, if you just want to comment out

  some 'resource <some name> {...}' section:

  just precede it with 'skip'.

  The basic format of option assignment is

  <option name><linear whitespace><value>;

  

  It should be obvious from the examples below,

  but if you really care to know the details:

  

  <option name> :=

        valid options in the respective scope

  <value>  := <num>|<string>|<choice>|...

              depending on the set of allowed values

              for the respective option.

  <num>    := +, sometimes with an optional suffix of K,M,G

  <string> := (<name>|\"([^\"\\\n]*|\\.)*\")+

  <name>   := +

}

# global {

    # use this if you want to define more resources later

    # without reloading the module.

    # by default we load the module with exactly as many devices

    # as configured mentioned in this file.

    #

    # minor-count 5;

    # The user dialog counts and displays the seconds it waited so

    # far. You might want to disable this if you have the console

    # of your server connected to a serial terminal server with

    # limited logging capacity.

    # The Dialog will print the count each 'dialog-refresh' seconds,

    # set it to 0 to disable redrawing completely. [ default = 1 ]

    #

    # dialog-refresh 5; # 5 seconds

    # You might disable one of drbdadm's sanity check.

    # disable-ip-verification;

# }

#

# this need not be r#, you may use phony resource names,

# like "resource web" or "resource mail", too

#

resource r0 {

  protocol C;

# what should be done in case the cluster starts up in

  # degraded mode, but knows it has inconsistent data.

  incon-degr-cmd "echo '!DRBD! pri on incon-degr' | wall ; sleep 60 ; halt -f";

  startup {

    degr-wfc-timeout 120;    # 2 minutes.

  }

  disk {

  }

  net {

  }

  syncer {

    rate 10M;

    group 1;

    al-extents 257;

  }

  on web1{

    device     /dev/drbd0;

    disk       /dev/hdc3;

    address    192.168.0.136:7788;

    meta-disk  internal;

  }

  on web2 {

    device    /dev/drbd0;

    disk      /dev/hdc2;

    address   192.168.0.139:7788;

    meta-disk internal;

  }

}

=================================================



五、啟動drbd

先確認兩台要鏡像的機器是否正常,之間的網路是否通暢,需要載入的硬碟是否處於umount狀態。

A、 drbd採用的是模塊控制的方式

所以先要載入drbd.ko 模塊

在136伺服器執行:

#insmod drbd.ko 或者modprobe drbd

drbd.ko可以在編譯好的源碼包里找到。

判斷是否載入成功可以使用lsmod來查看:

#lsmod

Module     size    Used by

drbd     143088   -

有的話表示載入模塊成功

#drbdadm up all

啟動drbd服務,使他掛在後台狀態下運行.

可以使用命令netstat -an查看

有啟動埠7788,同時也監聽對方的7788埠,來實現數據交換。

netstat -ant的輸出結果里有一行:

#netstat -ant

tcp        0      0 192.168.0.136:7789          0.0.0.0:*                   LISTEN



B:在139伺服器執行

#modprobe drbd

#/etc/rc.d/init.d/drbd start

netstat -atn的輸出結果,說明兩台虛擬機的drbd服務已經連接上了:

#netstat -ant

tcp        0      0 192.168.0.136:7789          192.168.0.139:32845         ESTABLISHED

tcp        0      0 192.168.0.136:32770         192.168.0.139:7789          ESTABLISHED

六:設置許可權

drbd的基本服務都起來了,現在需要對主的伺服器也就使192.168.0.136這台伺服器進行配置,讓他能夠對drbd0設備進行讀寫。

在136機器上運行

#drbdadm -- --do-what-I-say  primary all   #設置136伺服器為同步主目錄,也就是同步以136的這個分區為準.

注意命令格式需要一樣,沒有任何提示的話表示基本上成功了

#sfdisk -s

可以看見有一個硬體設備:/dev/drbd0

此時,兩台設備之間就建立起一個鏡像,您可以查看/proc/drbd 進行核實。

# cat /proc/drbd

如果原來硬碟沒有文件系統的話,現在您可以在設備/dev/drbd0上創建一個文件系統,然後把它載入到136上。

#mkfs.ext3 -j /dev/drbd0

#mkdir /mnt/gaojf

# mount /dev/drbd  /mnt/gaojf

現在/dev/drbd0就等於你伺服器上面的一個硬體設備,你可以對他進行任何的讀寫操作。

七:drbd測試:

1:在136主伺服器上:

#drbdadm primary all

#touch /mnt/gaojf/gaojf

完后,在執行

#umount /mnt/gaojf

#drbdadm secondary all

2:接著到139備用伺服器上執行

#drbdadm primary all

#mkdir -p /mnt/gaojf

#mount /dev/drbd0 /mnt/gaojf

#ls -l /mnt/gaojf/gaojf

gaojf

Ok,沒問題,可以看到數據在136伺服器寫入,在139馬上可以看到.



幾點注意的地方:

1. mount drbd設備以前必須把設備切換到primary狀態。

2. 兩個節點中,同一時刻只能有一台處於primary狀態,另一台處於secondary狀態。

3. 處於secondary狀態的伺服器上不能載入drbd設備。

4. 主備伺服器同步的兩個分區大小最好相同,這樣不至於浪費磁碟空間,因為drbd磁碟鏡像相當於網路raid 1.

5: 在配置過程中,如果出現下面錯誤

Missing argument 'meta_data_dev'

USAGE:

disk lower_dev meta_data_dev meta_data_index [{--size|-d} 0 ... 8587575296]

     [{--on-io-error|-e} {pass_on|call-local-io-error|detach}]

     [{--fencing|-f} {dont-care|resource-only|resource-and-stonith}] [{--use-bmbv|-b}]

那麼就還要初始化meta-data area

drbdadm create-md r0

更詳細的參考:http://www.ixdba.net/article/5c/125.html
《解決方案》

螞蟻。我用821測試過了。這玩意兒目前來說不太可靠啊!
《解決方案》

確實有一定問題,對於關鍵的,大型的應用的確不太適合,一般的應用還是可以的。
《解決方案》

也許這個方法能解決你們說的不太可靠的問題。:)
在這種方法中我遇到的一個問題就是當集群中主節點down機或拔掉網線的時候,從可以順利接管主伺服器,實現高可用,但這個時候如果你往現在的主(原來的從)的drbd磁碟上寫入數據,那麼在另一台機器重新啟動或插上網線的時候,就會發生「split brain」 ,這個時候drbd的數據就不是同步的了,想要同步就必須手工恢復。很奇怪的是如果沒有mount drbd就很少發生這種情況。這就好像一個半自動的高可用,我們需要經常去監視他是否斷掉了,那怕是重啟一台機器都經常會發生「split brain」,我看了drbd的文檔,裡面有很多策略應該可以避免我上面的情況,可惜我的實驗全部失敗了。希望有成功的指點我一下。
我的e文不太好,我這段幫助貼上來,e文好的自己看吧。
       -A, --after-sb-0pri asb-0p-policy
              possible policies are:

              disconnect
                     No automatic resynchronisation, simply disconnect.

              discard-younger-primary
                     Auto sync from the node that was primary before the split-  brain situation occurred.

              discard-older-primary
                     Auto sync from the node that became primary as second during the split-brain situation.

              discard-zero-changes
                     In case one node did not write anything since the split brain became evident, sync from the node that wrote something to the node that did
                     not write anything. In case none wrote anything this policy uses a random decission to perform a "resync" of 0 blocks. In case  both  have
                     written something this policy disconnects the nodes.

              discard-least-changes
                     Auto sync from the node that touched more blocks during the split brain situation.

              discard-node-NODENAME
                     Auto sync to the named node.
       -B, --after-sb-1pri asb-1p-policy
              possible policies are:

              disconnect
                     No automatic resynchronisation, simply disconnect.

              consensus
                     Discard the version of the secondary if the outcome of the after-sb-0pri algorithm would also destroy the current secondary』s data. Other-
                     wise disconnect.

              discard-secondary
                     Discard the secondary』s version.

              call-pri-lost-after-sb
                     Always honour the outcome of the after-sb-0pri
                      algorithm. In case it decides the current secondary has the right data, call the pri-lost-after-sb on the current primary.

              violently-as0p
                     Always honour the outcome of the after-sb-0pri
                      algorithm. In case it decides the current secondary has the right data, accept a possible instantaneous change of the primary』s data.

       -C, --after-sb-2pri asb-2p-policy
              possible policies are:

              disconnect
                     No automatic resynchronisation, simply disconnect.

              call-pri-lost-after-sb
                     Always honour the outcome of the after-sb-0pri
                      algorithm. In case it decides the current secondary has the right data, call the pri-lost-after-sb on the current primary.

              violently-as0p
                     Always honour the outcome of the after-sb-0pri
                      algorithm. In case it decides the current secondary has the right data, accept a possible instantaneous change of the primary』s data.

我理解的意思就是 split brain, after - sb (不知道你會不會和我的理解一樣……)
這裡我解決的辦法是用一個腳本來檢測 split brain ,並自動判斷同步。
shell.1 發到啟動組裡面
#!/bin/sh
PATH=$PATH:/sbin:/usr/sbin:/usr/local/bin
[ -f /proc/drbd ] || exit 1
if ( grep 'Secondary/Unknown' /proc/drbd );then
        drbdadm disconnect all
        drbdadm -- --discard-my-data connect all
        (sleep 1;echo 'drbd';sleep 2;echo 'drbd';sleep 3)|telnet 192.16.1.22
fi
shell.2 由heartbeat執行。
#! /bin/bash
#
# chkconfig: 345 15 88
# description: Linux High availability services .

# Source function library.
. /etc/init.d/functions
[ ! -f /etc/sysconfig/network ] && exit 1
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

# if the ip configuration utility isn't around we can't function.
[ -x /sbin/ip ] || exit 1;[ -f /proc/drbd ] || exit 1
DRBDSTATE=$(drbdadm state all)
while $(grep -E "SyncSource.*Inconsistent" /proc/drbd >/dev/null 2<&-)
   do
        sleep 10
   done

start () {
        sleep 5
        ip addr add 192.16.1.20/24 brd 192.16.3.255 dev bond0
        /etc/init.d/portmap start
        drbdadm primary all
        mount -t ext3 -o rw /dev/drbd0 /mnt/disk0
        mount -t ext3 -o rw /dev/drbd1 /mnt/disk1
        /etc/init.d/nfs start
        /etc/init.d/nfslock start
        exportfs -avr
        return $RETVAL
}
stop () {
        ip addr del 192.16.1.20/24 brd 192.16.1.255 dev bond0
        /etc/init.d/nfs stop
        /etc/init.d/nfslock stop
        umount /mnt/disk0
        umount /mnt/disk1
        drbdadm secondary all
        /etc/init.d/portmap stop
        if ( grep 'Secondary/Unknown' /proc/drbd );then
        exec /etc/rc.d/my-shell.sh;fi
        return $RETVAL
}

# See how we were called.
case "$1" in
  start|stop)
        $1
        ;;
  restart|reload)
        /etc/init.d/$0 stop
        /etc/init.d/$0 start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload}"
        exit 1
esac

exit 0
添加用戶 drbd
passwd
drbd:x:105:105:DRBD:/home/drbd:/sbin/drbdsh
drbdsh文件
#!/bin/sh
# Variables and Function definition
PATH=$PATH:/sbin:/usr/sbin:/usr/local/bin

#Program Main
[ -f /proc/drbd ] || exit 1
TEMP=$(drbdadm state all)
D_STATE=(${TEMP//\// })
if ( echo ${D_STATE[@]}|grep Primary >/dev/null 2<&- ) && \
   ( echo ${D_STATE[@]}|grep Unknown >/dev/null 2<&- );then
        drbdadm connect all
        else exit 1
fi
exit 0
啟動 telnet
在 hosts.deny 里添加 in.telnet :ALL :ALL EXCEPT 192.16.1.22
這樣就可以保證每次啟動后數據的同步了。
下面是我的部分配置文件:
drbd.conf
global { usage-count yes; }
common { syncer { rate 10M; } }
resource r0 {
        protocol C;
        handlers { pri-on-incon-degr "halt -f"; }
        disk { on-io-error detach; }
        net {
                cram-hmac-alg sha1;
                shared-secret "800hr_disk_0";
        }
        on test01 {
                device          /dev/drbd0;
                disk            /dev/sda6;
                address         192.16.1.21:7789;
                meta-disk       internal;
        }
        on test02 {
                device          /dev/drbd0;
                disk            /dev/sda6;
                address         192.16.1.22:7789;
                meta-disk       internal;
        }
}

[ 本帖最後由 xinyv 於 2008-1-9 09:03 編輯 ]
《解決方案》

我目前也沒弄明白怎麼保證數據完整性的。呵呵!所以還是等有了商業應用看看別人用的效果吧!

[ 本帖最後由 molecar 於 2008-1-9 12:12 編輯 ]
《解決方案》

net {
        after-sb-0pri discard-older-primary;
        after-sb-1pri call-pri-lost-after-sb;
        after-sb-2pri call-pri-lost-after-sb;
}
在drbd.conf中配置如上參數,也許能解決各位所遇到的split brain問題
《解決方案》

split brain是很正常的現象
主節點當機后,輔節點成為primary,並且有數據的改動
然後主節點重啟后,默認肯定是primary,但是又由於數據的不一致性,於是兩個primary就開始split brain了。
《解決方案》

推薦一下,這個是螞蟻原創的。可否考慮設置為精華
《解決方案》

DRBD

剛好我手上有一個菲律賓的博彩項目。資料庫採用的是MYSQL 5.0.41.用戶要求在不增加投資情況下提供高可用性。我初步測試了HEARTBEAT+DRBD+NFS+MYSQL+MON這個組合。(HEARTBEAT 2.0.8;DRBD 8.2.4,Red Hat Enterprise Linux AS release 4,內核版本:2.6.9-34.EL

計劃滿足以下要求:
1)  service heartbeat stop
2)  kiallall heartbeat
3)  reboot
4)  拔掉生產網路網線
5)  拔掉主伺服器的電源線
在以上情況下,集群可以自動進行集群ip和mysql切換,同時保證數據完整性。
我參照mysql公司提供的mysql+drbd集群文檔進行了配置,同時參考了你的設置。
結論是
service heartbeat stop;kiallall heartbeat;拔掉生產網路網線
以上情況是滿足我的要求的。
但是當拔掉主伺服器的電源線后,備份伺服器heartbeat不能啟動相應資源。
錯誤信息:return code 20 from /etc/ha.d/resource.d/drbddisk
我分析DRBD需要兩個節點cs處於connected狀態才能設置primary狀態,進而掛載/dev/drbd0設備。當拔掉主伺服器的電源線后,cs肯定不是connected狀態,因此不能將備份伺服器drbd設置為primary狀態。從而造成heartbeat服務不能啟動相應資源。
按照我的測試,heartbeat+mysql是可以滿足以上五個要求的(測試時未考慮數據完整性。)
我的drbd.conf
on web1{

    device     /dev/drbd0;

    disk       /dev/drbd0;

    address    10.10.10.1:7788;(使用的是心跳網線)

    meta-disk  internal;

  }
net {
        after-sb-0pri discard-older-primary;
        after-sb-1pri call-pri-lost-after-sb;
        after-sb-2pri call-pri-lost-after-sb;
}
ha.cf
使用ipfail,dopd同步進程
Haresources
Local139 192.168.30.249 drbddisk::r0 filesystem::.dev/drbd0::/opt/mysql/data portmap nfslock nfs mysqld
在我這裡拔掉生產網路網線的數據完整性已經可以保證了。數據沒有問題。
你們在配置drbd+和heartbeat過程中能滿足我的全部用戶要求嗎?
《解決方案》

最好能讓mysql server帶數據,同是正在寫入或讀取數據的時候宕機!然後再檢驗數據:mrgreen:

[火星人 ] drbd安裝使用指南已經有945次圍觀

http://coctec.com/docs/service/show-post-7539.html