歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 利用 Heartbeat/DRBD 設置一個高可用性的群集

利用 Heartbeat/DRBD 設置一個高可用性的群集

日期:2017/2/27 15:55:11   编辑:Linux教程

先決條件

- 安裝最小化的 CentOS 5

- 確保這兩個節點能正確地解析名稱(通過 dns 或 /etc/hosts)

- yum update(例行的…… )

- yum install heartbeat drbd kmod-drbd(由 extras 軟件庫提供)

希望使用 drbd 8.2.6 而不是 8.0.13 的人們可以利用 drbd82 及 kmod-drbd82。請留意你不能混合使用不同版本。設置的語法亦有分別

現時情況:

  • node1.yourdomain.org 172.29.156.20/24,將要被復制的來源磁盤 /dev/sdb
  • node2.yourdomain.org 172.29.156.21/24,目的磁盤 /dev/sdb

設置 DRBD

我們會設置 DRBD 將 /dev/sdb 由一個節點復制到另一個節點(角色可以隨時對換)。drbd 的資源名稱是 "repdata"(你當然可以使用你喜歡的名稱)。下面是 /etc/drbd.conf 文件的內容:

#
# please have a a look at the example configuration file in
# /usr/share/doc/drbd/drbd.conf
#
global { usage-count no; }
resource repdata {
  protocol C;
  startup { wfc-timeout 0; degr-wfc-timeout     120; }
  disk { on-io-error detach; } # or panic, ...
  net {  cram-hmac-alg "sha1"; shared-secret "Cent0Sru!3z"; } # don't forget to choose a secret for auth !
  syncer { rate 10M; }
  on node1.yourdomain.org {
    device /dev/drbd0;
    disk /dev/sdb;
    address 172.29.156.20:7788;
    meta-disk internal;
  }
  on node2.yourdomain.org {
    device /dev/drbd0;
    disk /dev/sdb;
    address 172.29.156.21:7788;
    meta-disk internal;
  }
}

- 將這個配置文件(/etc/drbd.conf)復制到第二個節點

scp /etc/drbd.conf root@node2:/etc/

- 在引導 drbd 前先初始化磁盤上的中繼數據區(!兩個節點上都要!)

[root@node1 etc]# drbdadm create-md repdata
v08 Magic number not found
v07 Magic number not found
About to create a new drbd meta data block on /dev/sdb.
 . ==> This might destroy existing data! <==
Do you want to proceed? [need to type 'yes' to confirm] yes
Creating meta data... initialising activity log NOT initialized bitmap (256 KB) New drbd meta data block sucessfully created.

- 在兩個節點上引導 drbd(service drbd start)

[root@node1 etc]# service drbd start
Starting DRBD resources:    [ d0 n0 ]. ......
[root@node1 etc]# cat /proc/drbd
version: 8.0.4 (api:86/proto:86) SVN Revision: 2947 build by buildsvn@c5-i386-build, 2007-07-31 19:17:18
 . 0: cs:Connected st: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
   . resync: used:0/31 hits:0 misses:0 starving:0 dirty:0 changed:0 act_log: used:0/257 hits:0 misses:0 starving:0 dirty:0 changed:0
[root@node1 etc]# ssh root@node2 cat /proc/drbd  version: 8.0.4 (api:86/proto:86) SVN Revision: 2947 build by buildsvn@c5-i386-build, 2007-07-31 19:17:18
 . 0: cs:Connected st: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
   . resync: used:0/31 hits:0 misses:0 starving:0 dirty:0 changed:0 act_log: used:0/257 hits:0 misses:0 starving:0 dirty:0 changed:0

如你所見,這兩個節點都是次要的,這是正常的。我們須要決定現時那一個是主要節點(node1):這樣做便會開始兩個節點之間的首次「完整同步」:

[root@node1 etc]# drbdadm -- --overwrite-data-of-peer primary repdata
[root@node1 etc]# watch -n 1 cat /proc/drbd  version: 8.0.4 (api:86/proto:86) SVN Revision: 2947 build by buildsvn@c5-i386-build, 2007-07-31 19:17:18
 . 0: cs:SyncTarget st:Primary/Secondary ds:Inconsistent/Inconsistent C r---
  . ns:0 nr:68608 dw:68608 dr:0 al:0 bm:4 lo:0 pe:0 ua:0 ap:0
   . [>...................] sync'ed:  0.9% (8124/8191)M finish: 0:12:05 speed: 11,432 (11,432) K/sec resync: used:0/31 hits:4283 misses:5 starving:0 dirty:0 changed:5 act_log: used:0/257 hits:0 misses:0 starving:0 dirty:0 changed:0

- 現在我們可以將 /dev/drbd0 格式化,並在 node1 上掛載它:mkfs.ext3 /dev/drbd0 ; mkdir /repdata ; mount /dev/drbd0 /repdata

- 在 node1 上創建一些虛構的數據:

  • [root@node1 etc]# for i in {1..5};do dd if=/dev/zero of=/repdata/file$i bs=1M count=100;done

- 現在手動式地切換到第二個節點:

[root@node1 /]# umount /repdata ; drbdadm secondary repdata
[root@node2 /]# mkdir /repdata ; drbdadm primary repdata ; mount /dev/drbd0 /repdata
[root@node2 /]# ls /repdata/ file1  file2  file3  file4  file5  lost+found

非常好,數據已經被復制了……現在讓我們刪除/新增一些文件:

[root@node2 /]# rm /repdata/file2 ; dd if=/dev/zero of=/repdata/file6 bs=100M count=2

- 現在再返回第一個節點:

[root@node2 /]# umount /repdata/ ; drbdadm secondary repdata
[root@node1 /]# drbdadm primary repdata ; mount /dev/drbd0 /repdata
[root@node1 /]# ls /repdata/ file1  file3  file4  file5  file6  lost+found

很好…… drbd 可以正常運作了……讓我們確定它經常會被引導:chkconfig drbd on

設置 Heartbeat V2

讓我們設置一個簡單的 /etc/ha.d/ha.cf 文件:

keepalive 2
deadtime 30
warntime 10
initdead 120
bcast   eth0
node    node1.yourdomain.org
node    node2.yourdomain.org
crm yes

請亦創建 /etc/ha.d/authkeys(設置 600 的權限!!!):

auth 1
1 sha1 MySecret

在 node1 上引導 heartbeat 服務:

[root@node1 ha.d]# service heartbeat start
Starting High-Availability services: [OK]

檢查群集的狀況:

[root@node1 ha.d]# crm_mon 

現在復制 ha.cf 及 authkeys 到 node2,然後引導 heartbeat

[root@node1 ha.d]# scp /etc/ha.d/ha.cf /etc/ha.d/authkeys root@node2:/etc/ha.d/
[root@node2 ha.d]# service heartbeat start

用 crm_mon 來檢查群集:

=====
Last updated: Wed Sep 12 16:20:39 2007
Current DC: node1.centos.org (6cb712e4-4e4f-49bf-8200-4f15d6bd7385)
2 Nodes configured.
0 Resources configured.
=====
Node: node1.yourdomain.org (6cb712e4-4e4f-49bf-8200-4f15d6bd7385): online
Node: node2.yourdomain.org (f6112aae-8e2b-403f-ae93-e5fd4ac4d27e): online

有關圖像界面的備注: 你可以在一台 X 工作台上安裝 heartbeat-gui(yum install heartbeat-gui)來連接到群集,但你會需要在兩個節點上都更 hacluster 戶口的口令!(你亦可以用另一個戶口,但它必須被放在 haclient 群組內)

現 在我們會創建一個資源群組,當中藏有一個 IP 地址(172.29.156.200)、drbd 設備(名叫 repdata)、及掛載文件系統的動作(mount /dev/drbd0 /repdata)。注:使用一個群組比個別資源較為容易:它會依次序(ordered=true)在同一個節點上(collocated=true)引 導所有資源

下面是 /var/lib/heartbeat/crb/cib.xml 的內容:

 <cib generated="false" admin_epoch="0" epoch="25" num_updates="1" have_quorum="true" ignore_dtd="false" num_peers="0" cib-last-written="Sun Sep 16 19:47:18 2007" cib_feature_revision="1.3" ccm_transition="1">
   <configuration>
     <crm_config/>
     <nodes>
       <node id="6cb712e4-4e4f-49bf-8200-4f15d6bd7385" uname="node1.yourdomain.org" type="normal"/>
       <node id="f6112aae-8e2b-403f-ae93-e5fd4ac4d27e" uname="node2.yourdomain.org" type="normal"/>
     </nodes>
     <resources>
       <group id="My-DRBD-group" ordered="true" collocated="true">
         <primitive id="IP-Addr" class="ocf" type="IPaddr2" provider="heartbeat">
           <instance_attributes id="IP-Addr_instance_attrs">
             <attributes>
               <nvpair id="IP-Addr_target_role" name="target_role" value="started"/>
               <nvpair id="2e967596-73fe-444e-82ea-18f61f3848d7" name="ip" value="172.29.156.200"/>
             </attributes>
           </instance_attributes>
         </primitive>
         <instance_attributes id="My-DRBD-group_instance_attrs">
           <attributes>
             <nvpair id="My-DRBD-group_target_role" name="target_role" value="started"/>
           </attributes>
         </instance_attributes>
         <primitive id="DRBD_data" class="heartbeat" type="drbddisk" provider="heartbeat">
           <instance_attributes id="DRBD_data_instance_attrs">
             <attributes>
               <nvpair id="DRBD_data_target_role" name="target_role" value="started"/>
               <nvpair id="93d753a8-e69a-4ea5-a73d-ab0d0367f001" name="1" value="repdata"/>
             </attributes>
           </instance_attributes>
         </primitive>
         <primitive id="FS_repdata" class="ocf" type="Filesystem" provider="heartbeat">
           <instance_attributes id="FS_repdata_instance_attrs">
             <attributes>
               <nvpair id="FS_repdata_target_role" name="target_role" value="started"/>
               <nvpair id="96d659dd-0881-46df-86af-d2ec3854a73f" name="fstype" value="ext3"/>
               <nvpair id="8a150609-e5cb-4a75-99af-059ddbfbc635" name="device" value="/dev/drbd0"/>
               <nvpair id="de9706e8-7dfb-4505-b623-5f316b1920a3" name="directory" value="/repdata"/>
             </attributes>
           </instance_attributes>
         </primitive>
       </group>
     </resources>
     <constraints>
       <rsc_location id="runs_on_pref_node" rsc="My-DRBD-group">
         <rule id="prefered_runs_on_pref_node" score="100">
           <expression attribute="#uname" id="786ef2b1-4289-4570-8923-4c926025e8fd" operation="eq" value="node1.yourdomain.org"/>
         </rule>
       </rsc_location>
     </constraints>
   </configuration>
 </cib>

如你所見,我們創建了一個 rsc_location 的限制,好叫群集的所有資源都會缺省的節點上引導。

現 在你可以通過命令行(crm_resource)或圖像界面(更改位置限制值 —— 例如將 node1.yourdomain.org 轉為 node2.yourdomain.org,然後點擊 apply)來移動資源。你將會能夠看見所有資源因一個節點切換到另一個節點(位置、drbd 及掛載文件系統)

防火牆的考慮

你須要確定節點可以通過以下端口溝通:

DRBD: 7788
HEARTBEAT: 694
Copyright © Linux教程網 All Rights Reserved