HA高可用集羣之drbd詳解

一 DRBD

drbd(Distributed Replicated Block Device,分佈式複製塊設備),實現用網絡來鏡像的備份整個設備(一般是存儲設備)2033581_1376720329Miiy


drbd接受到數據後,把數據寫到本地的磁盤中,同時再發送一份到另外一臺主機,另外一臺主機把數據再存入本地的磁盤內。

在整個傳輸的過程中,根據數據到達的地方,可以將drdb的複製模式分成3種:

  • 協議A:異步複製協議。本地寫成功後立即返回,數據放在發送buffer中,可能丟失。

  • 協議B:內存同步(半同步)複製協議。本地寫成功並將數據發送到對方後立即返回。

  • 協議C:同步複製協議。本地和對方寫成功確認後返回

drbd用在高可用集羣中,爲共享存儲設備提供冗餘,比如:爲nfs,smaba,還有mysql的數據目錄都可以做分佈式存儲來提高冗餘或者高可用。

drbd的資源規則:

drbd一般情況下,只支持一主一從,這已經足夠滿足服務的冗餘了、

主節點:可讀寫

從節點:只能被動接受主機傳來的數據信息,不能讀寫,連掛載都不被允許。

drbd做爲分佈式複製塊設備,通常都可以用哪些硬件充當呢:

1、硬盤,或者硬盤中的一個分區

2、RAID設備

3、LVM邏輯卷

drbd有兩種實現方法:

1、直接將drdb打補丁進內核中

2、通過用戶空間軟件實現內核級別的功能(如:netfilter的實現是用用戶空間iptables命令實現的)

二 配置drbd概述

DRBD 配置工具

  • drbdadm:高級管理工具,管理/etc/drbd.conf,向drbdsetup和drbdmeta發送指令。

  • drbdsetup:配置裝載進kernel的DRBD模塊

  • drbdmeta:管理META數據結構

DRBD的主配置文件爲/etc/drbd.conf;爲了管理的便捷性,目前通常會將些配置文件分成多個部分,且都保存至/etc/drbd.d目錄中,主配置文件中僅使用"include"指令將這些配置文件片斷整合起來。通常,/etc/drbd.d目錄中的配置文件爲global_common.conf和所有以.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資源,每個資源通常定義在一個單獨的位於/etc/drbd.d目錄中的以.res結尾的文件中。資源在定義時必須爲其命名,名字可以由非空白的ASCII字符組成。每一個資源段的定義中至少要包含兩個host子段,以定義此資源關聯至的節點,其它參數均可以從common段或drbd的默認中進行繼承而無須定義。

DRBD 資源

  • Resource name:可以是除了空白字符的任意的ACSII碼字符

  • DRBD device:在雙方節點上,此DRBD設備的設備文件;一般爲/dev/drbdN,其主設備號147

  • Disk configuration:在雙方節點上,各自提供的存儲設備

  • Nerwork configuration:雙方數據同步時所使用的網絡屬性

來說說整體是怎麼配置的,給個配置步驟,就能看起來邏輯上清晰一點:

1、安裝drdb

        drbd-8.4.3-33.el6.x86_64         drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64

        安裝drbd時,要看自己的內核版本,一定要安裝和自己內核一樣版本的軟件包。

2、配置資源文件

        需要在/etc/drbd.d/global_common.conf中做修改

        將自己建立的磁盤設備加入資源中,命名以 .res結尾(例如:mystore.res)

   當然在配置資源的時候,你應該提前創建好設備。

3、將drbd加入到系統服務chkconfig --add drbd  (如果做HA高可用,就不需要開機自動啓動)

4、初始化資源組drbdadm create-md resource_name  (例如:drbdadm create-md mystore)

5、啓動服務 service drbd start  (配置完測試,啓動服務,drbd也作爲一個服務運行的)

6、設置primary主機,並同步數據

     默認情況運行服務後,兩個節點都是處於secondary狀態,我們需要手動去設置一個節點成爲primary狀態

     在要設置爲Primary的節點上執行如下命令:drbdsetup /dev/drbd0 primary –o ,也可以在要設置爲Primary的節點上使用如下命令來設置主節點: drbdadm -- --overwrite-data-of-peer primary web

7、分區、格式化/dev/drbd*

8、一個節點進行掛載

9、查看狀態

三 配置過程

1、安裝drbd

[root@station139 ~]# ls
anaconda-ks.cfg               drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm
drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm –> 這個包安裝和kernel版本不同的話會出現出現的錯誤:
[root@node2 ~]# service drbd start
Starting DRBD resources: Can not load the drbd module.
 [root@station139 ~]# rpm -ivh --nodeps drbd-kmdl-2.6.32-358.el6-8.4.3-33.el6.x86_64.rpm 
warning: drbd-kmdl-2.6.32-358.el6-8.4.3-33.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 66534c2b: NOKEY
Preparing...                ########################################### [100%]
   1:drbd-kmdl-2.6.32-358.el########################################### [100%]
[root@station139 ~]# rpm -ivh drbd-8.4.3-33.el6.x86_64.rpm 
warning: drbd-8.4.3-33.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 66534c2b: NOKEY
Preparing...                ########################################### [100%]
   1:drbd                   ########################################### [100%]
[root@station139 ~]# rpm -q drbd
drbd-8.4.3-33.el6.x86_64
在配置配置文件之前,我們來爲我們準備實驗的兩臺主機分別創建磁盤文件;
[root@station139 ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to         switch off the mode (command 'c') and change display units to         sectors (command 'u').

Command (m for help): p

Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000902d4

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        7859    62914560   8e  Linux LVM
/dev/sda3            7859        9164    10489446   8e  Linux LVM

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e                    因爲只剩下一個主分區了可以建立了,所以我們把最後一個主分區用來做擴展,在擴展分區上創建分區
Selected partition 4
First cylinder (9165-15665, default 9165): 
Using defaultvalue 9165
Last cylinder, +cylinders or +size{K,M,G} (9165-15665, default 15665): 
Using defaultvalue 15665

Command (m for help): n
First cylinder (9165-15665, default 9165): 
Using defaultvalue 9165
Last cylinder, +cylinders or +size{K,M,G} (9165-15665, default 15665): +5G

Command (m for help): P

Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000902d4

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        7859    62914560   8e  Linux LVM
/dev/sda3            7859        9164    10489446   8e  Linux LVM
/dev/sda4            9165       15665    52219282+   5  Extended
/dev/sda5            9165        9818     5253223+  83  Linux     創建了一個5G的磁盤空間

Command (m for help): W
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
      讓系統識別我們創建的分區:
error adding partition 3
[root@station139 ~]# kpartx -af /dev/sda
device-mapper: reload ioctl on sda1 failed: Invalid argument
create/reload failed on sda1
device-mapper: reload ioctl on sda2 failed: Invalid argument
create/reload failed on sda2
device-mapper: reload ioctl on sda3 failed: Invalid argument
create/reload failed on sda3
device-mapper: reload ioctl on sda4 failed: Invalid argument
create/reload failed on sda4
device-mapper: reload ioctl on sda5 failed: Invalid argument
create/reload failed on sda5
[root@station139 ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
在另外一臺主機(node2)上也應該創建一個同本機大小相同的磁盤存儲設備。

2、配置配置文件

[root@node1 ~]# cat /etc/drbd.d/global_common.conf   

global {   

    usage-count no; #讓linbit公司收集目前drbd的使用情況,yes爲參加,我們這裏不參加設置爲no   

    # minor-count dialog-refresh disable-ip-verification   

}

common {  

    handlers {   

        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 {  

        # 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   

        on-io-error detach; #同步錯誤的做法是分離   

    }

    net {  

        # 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  
加入如下兩行:實現節點之間會話加密,設置算法,和加密的key 
注意: 最後要加分號;
        cram-hmac-alg "sha1"; #設置加密算法sha1   

        shared-secret "mydrbdlab"; #設置加密key   

    }   

           syncer {      需要加入的內容
                       rate 1000M;
                       }

       
}

 
配置資源
[root@station139 ~]# vim /etc/drbd.d/mystore.res

resource mystore {   資源名mystore應該和配置文件定義的名字是相同的。 
        on node1.corosync.com{     主機名,如果是本機的話,要和“uname –n”結果相同,hosts文件中也應該有對應的IP解析。如果是在網絡中的話需要/etc/hosts文件中要有主機名和IP地址對應的解析。
        device  /dev/drbd0;          drbd0 爲drdb的第一個設備
        disk    /dev/sda5;       所在本地的那個磁盤,也就是磁盤位置   
        address 172.16.249.207:7789;    對應的IP地址和端口號
        meta-disk       internal;    表示資源是在本地
}
        on node2.corosync.com{    這個是另外一臺主機,也做同樣的設置
        device  /dev/drbd0;
        disk    /dev/sda3;
        address 172.16.249.208:7789;  這個地方配置另外這臺主機的IP地址和端口。drbd服務默認端口是 7789;
        meta-disk       internal;
}
}
將配置文件同步到node2
如上的配置文件,是兩個主機中的配置應該是相同的,所以可以直接放這兩個配置文件複製過去,直接放在另外一個主機的對應的配置文件目錄下。
[root@station139 ~]# scp /etc/drbd.d/* node2.corosync.com:/etc/drbd.d/
The authenticity of host 'node2.corosync.com (172.16.249.208)' can't be established.RSA key fingerprint is c7:65:26:ff:5e:11:b9:de:a4:7a:20:9d:a2:28:e4:66.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added 'node2.corosync.com,172.16.249.208' (RSA) to the list of known [email protected]'s password: 
global_common.conf                                                           100% 1886     1.8KB/s   00:00    
mystore.res                                                                  100%  247     0.2KB/s   00:00
3、將drbd加入到系統服務chkconfig --add drbd  (如果做HA高可用,就不需要開機自動啓動)
因爲我們的目的是做高可用集羣的,在做高可用集羣的時候,務必要關閉開機自動啓動,所以我們就不走這條常規的道路了。
4、初始化資源組drbdadm create-md resource_name  (例如:drbdadm create-md mystore)
node1與node2上初始化資源
 
node1上的初始化
[root@node1 ~]# drbdadm create-md mystore  初始化資源組命令
Writing meta data...
initializing activity log
NOT initializing bitmap
New drbd meta data block successfully created.   初始化成功
node2上的初始化
[root@node2 ~]# drbdadm create-md mystore
  --==  Thank you for participating in the global usage survey  ==--
The server's response is:

you are the 15309th user to install this version
Writing meta data...
initializing activity log
NOT initializing bitmap
New drbd meta data block successfully created.
 
5、啓動服務 service drbd start  (配置完測試,啓動服務,drbd也作爲一個服務運行的)
 node1:
[root@node1 ~]# rpm -ivh drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm 
warning: drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 66534c2b: NOKEY
Preparing...                ########################################### [100%]
   1:drbd-kmdl-2.6.32-431.el########################################### [100%]
[root@node1 ~]# service drbd start
Starting DRBD resources: [
     create res: mystore
   prepare disk: mystore
    adjust disk: mystore
     adjust net: mystore
]
..........
***************************************************************
 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 'mystore'; 0 sec -> wait forever) To abort waiting enter 'yes' [ 236]:
node2:
[root@node2 ~]# service drbd start
Starting DRBD resources: [
     create res: mystore
   prepare disk: mystore
    adjust disk: mystore
     adjust net: mystore
]
查看下drbd運行狀態:
[root@node1 ~]# watch -n 1 `cat /proc/drbd`
GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by gardner@, 2013-11-29 12:28:00 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:5252056'

 ds:Inconsistent/Inconsistent  兩個都處於非一致狀態
ro:Secondary/Secondary  此時還不知道誰是主誰是從

6、設置primary主機,並同步數據

   從上面的信息中可以看出此時兩個節點均處於Secondary狀態。於是,我們接下來需要將其中一個節點設置爲Primary。在要設置爲Primary的節點上執行如下命令:# drbdadm primary --force resource(資源名)

注: 也可以在要設置爲Primary的節點上使用如下命令來設置主節點:
    # drbdadm -- --overwrite-data-of-peer primary web

打算讓哪個節點做爲primary就在哪個節點上執行如下命令:

[root@node1 ~]# drbdadm primary --force mystore



再次查看狀態,可以發現數據同步過程開始了:

[root@node1 ~]# drbd-overview
  0:mystore/0  SyncSource Primary/Secondary UpToDate/Inconsistent C r---n- 
    [======>.............] sync'ed: 35.4% (3316/5128)M
 
我們再用watch –n 1 `cat /proc/drbd`來查看下有沒有認出主從:
line 0: `version: 8.4.3 (api:1/proto:86-101) GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by gardner@, 2013-11-29 
12:28:00 0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r---n- ns:1427464
 nr:0 dw:0 dr:1429152 al:0 bm:87 lo:3 pe:3 ua:4 ap:0 ep:1 wo:f oos:3826648 [====>...............] sync'ed: 27.3% (3736/5128)M finish: 0:02:34 speed: 24,788 (24,156) K/sec'
ro:Primary/Secondary  已經有主從了 
ds:UpToDate/Inconsistent  而且有一方已經開始呈現更新狀態
更新完成後, drbd-overview 查看:
[root@node1 ~]# drbd-overview
  0:mystore/0  Connected Primary/Secondary UpToDate/UpToDate C r----- 

顯示已經分出主從,並且已經更新完了 UpToDate/UpToDate

對主Primary/Secondary模型的drbd服務來講,在某個時刻只能有一個節點爲Primary,因此,要切換兩個節點的角色,只能在先將原有的Primary節點設置爲Secondary後,才能原來的Secondary節點設置爲Primary:

node1:    drbdadm secondary mystore

node2:    drbdadm primary  mystore

 
7、分區、格式化/dev/drbd*

文件系統的掛載只能在Primary節點進行,因此,也只有在設置了主節點後才能對drbd設備進行格式化

[root@node1 ~]# mke2fs -t ext4 /dev/drbd0   格式化成ext4   對/dev/drbd0 此刻就不是對/dev/sda#了
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe  blocks
328656 inodes, 1313014 blocks
65650 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1346371584
41 block groups
32768 blocks per group, 32768 fragments per group
8016 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

創建目錄,掛載/dev/drbd0 來看看能不能能不能掛載上這個drdb
[root@node1 ~]# mkdir /mydata
[root@node1 ~]# mount /dev/drbd0 /mydata/
[root@node1 ~]# cd /mydata/
[root@node1 mydata]# ls
lost+found
我們拷貝文件進去,並做修改,然後取消掉掛載,讓本機降級成secondary,然後讓另外一臺主機上升成primary,來看下數據有沒有同步過去
node1:
[root@node1 mydata]# cp /etc/fstab .
[root@node1 mydata]# vim fstab    刪除剩下兩行、然後講本機降級成secondary,讓另外一臺主機升級成primary,然後在另外一臺被提升成primary的主機上查看

/dev/cdrom              /media/cdrom            iso9660 defaults        0 0
/dev/myvg/mydata        /nfs                    ext4    defaults        0 0
[root@node1 ~]# umount /dev/drbd0       卸載掛載
[root@node1 ~]# drbdadm secondary mystore   降級爲secondary
node2:
[root@node2 ~]# drbdadm primary mystore   升級爲primary
[root@node2 ~]# mkdir /mydata              創建目錄
[root@node2 ~]# mount /dev/drbd0 /mydata/    掛載
[root@node2 ~]# cd /mydata/
[root@node2 mydata]# ls
fstab  lost+found
[root@node2 mydata]# cat fstab    可以看到文件已經變成我們改變之後的了
/dev/cdrom        /media/cdrom        iso9660    defaults    0 0
/dev/myvg/mydata    /nfs            ext4    defaults    0 0
 
本文就是實現了  drbd的工作模式
drdb有時候可能需要自動的去實現角色切換、下一篇來講解 drbd+corosync 實現自動切換
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章