基於DRBD+corosync對MariaDB做高可用集羣

一、DRBD簡介

   DRBD的全稱爲:Distributed ReplicatedBlock Device(DRBD)分佈式塊設備複製,DRBD是由內核模塊和相關腳本而構成,用以構建高可用性的集羣。其實現方式是通過網絡來鏡像整個設備。你可以把它看作是一種網絡RAID。它允許用戶在遠程機器上建立一個本地塊設備的實時鏡像。

     1.下載rpm包

      目前適用CentOS 5的drbd版本主要有8.0、8.2、8.3三個版本,其對應的rpm包的名字分別爲drbd, drbd82和drbd83,對應的內核模塊的名字分別爲kmod-drbd, kmod-drbd82和kmod-drbd83。而適用於CentOS 6的版本爲8.4,其對應的rpm包爲drbd和drbd-kmdl,但在實際選用時,要切記兩點:drbd和drbd-kmdl的版本要對應;另一個是drbd-kmdl的版本要與當前系統的內容版本相對應。各版本的功能和配置等略有差異;我們實驗所用的平臺爲x86_64且系統爲CentOS 6.5,因此需要同時安裝內核模塊和管理工具。我們這裏選用最新的8.4的版本(drbd-8.4.3-33.el6.x86_64.rpm和drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm

2.準備工作

   兩個節點的節點的名稱和對應的ip地址解析服務應該能正常工作

3.安裝,由於drbd包沒有依賴關係,可以直接使用rpm安裝
  1. [root@node1 ~]# rpm -ivh drbd-8.4.3-33.el6.x86_64.rpm drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm
  2. [root@node2 ~]# rpm -ivh drbd-8.4.3-33.el6.x86_64.rpm drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm

重新啓動rsyslog服務

4.配置說明

   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的默認中進行繼承而無須定義。

5.配置文件/etc/drbd.d/global_common.conf

  1. global {
  2. usage-count no; //drbd官網用來統計drbd的使用數據的
  3. # minor-count dialog-refresh disable-ip-verification
  4. }
  5. common {  //提供共享配置
  6. handlers {  //處理器,在特定的環境下執行的命令
  7. # These are EXAMPLE handlers only.
  8. # They may have severe implications,
  9. # like hard resetting the node under certain circumstances.
  10. # Be careful when chosing your poison.
  11. 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"; //在主節點降級的情況下要執行的命令
  12. 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"; //在備節點接替主節點前,對主節點的操作
  13. local-io-error "/usr/lib/drbd/notify-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ; halt -f"; 當本地發送io錯誤時的操作
  14. # fence-peer "/usr/lib/drbd/crm-fence-peer.sh";
  15. # split-brain "/usr/lib/drbd/notify-split-brain.sh root";
  16. # out-of-sync "/usr/lib/drbd/notify-out-of-sync.sh root";
  17. # before-resync-target "/usr/lib/drbd/snapshot-resync-target-lvm.sh -p 15 -- -c 16k";
  18. # after-resync-target /usr/lib/drbd/unsnapshot-resync-target-lvm.sh;
  19. # after-resync-target /usr/lib/drbd/unsnapshot-resync-target-lvm.sh;
  20. }
  21. startup { //可以不配置,使用默認即可
  22. # wfc-timeout 等待對端上線的超時時間
  23. # degr-wfc-timeout 降級的超時時間
  24. #outdated-wfc-timeout 過期的等待超時時間
  25. #wait-after-sb 腦裂的等待時間
  26. }
  27. options { //可以不配置 使用默認即可
  28. # cpu-mask on-no-data-accessible
  29. }
  30. disk {
  31. # size max-bio-bvecs on-io-error fencing disk-barrier disk-flushes
  32. # disk-drain md-flushes resync-rate resync-after al-extents
  33. # c-plan-ahead c-delay-target c-fill-target c-max-rate
  34. # c-min-rate disk-timeout
  35. on-io-error 當發生io錯誤是,應該要做的操作,有三個選項,pass_on:降級當前節點;call-local-io-error:執行本機的io-error操作;detach:將磁盤拆掉
  36. }
  37. net {
  38. protocol C 協議版本
  39. cram-hmac-alg "sha1"
  40. shared-secret "kjasdbiu2178uwhbj"
  41. # protocol timeout max-epoch-size max-buffers unplug-watermark
  42. # connect-int ping-int sndbuf-size rcvbuf-size ko-count
  43. # allow-two-primaries cram-hmac-alg shared-secret after-sb-0pri
  44. # after-sb-1pri after-sb-2pri always-asbp rr-conflict
  45. # ping-timeout data-integrity-alg tcp-cork on-congestion
  46. syncer{
  47. rate 1000M 同步的速率
  48. }
  49. }

6.準備磁盤設備,雙方節點都需要準備,最好是能等同大小,編號最好也能一樣.

  1. [root@node2 ~]# fdisk /dev/sda
  2. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
  3. switch off the mode (command 'c') and change display units to
  4. sectors (command 'u').
  5. Command (m for help): n
  6. Command action
  7. e   extended
  8. p   primary partition (1-4)
  9. p
  10. Partition number (1-4): 3
  11. First cylinder (7859-15665, default 7859):
  12. Using default value 7859
  13. Last cylinder, +cylinders or +size{K,M,G} (7859-15665, default 15665): +10G
  14. Command (m for help): w
  15. The partition table has been altered!
  16. Calling ioctl() to re-read partition table.
  17. WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
  18. The kernel still uses the old table. The new table will be used at
  19. the next reboot or after you run partprobe(8) or kpartx(8)
  20. Syncing disks.
  21. [root@node2 ~]# kpartx -af /dev/sda
  22. device-mapper: reload ioctl on sda1 failed: Invalid argument
  23. create/reload failed on sda1
  24. device-mapper: reload ioctl on sda2 failed: Invalid argument
  25. create/reload failed on sda2
  26. device-mapper: reload ioctl on sda3 failed: Invalid argument
  27. create/reload failed on sda3
  28. [root@node2 ~]# partx -a /dev/sda
  29. BLKPG: Device or resource busy
  30. error adding partition 1
  31. BLKPG: Device or resource busy
  32. error adding partition 2
  33. BLKPG: Device or resource busy
  34. error adding partition 3

7.準備資源配置文件,如drbd.re

  1. resource drbd {
  2. on node1.zero1.com { 用on來標誌節點名稱
  3. device  /dev/drbd0;    指定drbd的編號
  4. disk    /dev/sda3;     磁盤分區編號
  5. address 192.168.1.200:7789;    監聽的套接字
  6. meta-disk internal; 對於原始數據的處理,此處爲保持在本磁盤
  7. }
  8. on node2.zero1.com {
  9. device  /dev/drbd0;
  10. disk    /dev/sda3;
  11. address 192.168.1.201:7789
  12. meta-disk internal
  13. }
  14. }

8.將數據複製到節點

  1. [root@node1 drbd.d]# scp * node2:/etc/drbd.d/

七、初始化測試

1.創建資源   

  1. [root@node1 drbd.d]# drbdadm create-md drbd
  2. Writing meta data...
  3. initializing activity log
  4. NOT initializing bitmap
  5. lk_bdev_save(/var/lib/drbd/drbd-minor-0.lkbd) failed: No such file or directory
  6. New drbd meta data block successfully created. //創建成功
  7. lk_bdev_save(/var/lib/drbd/drbd-minor-0.lkbd) failed: No such file or directory

2.啓動服務  

  1. [root@node2 ~]# service drbd start
  2. Starting DRBD resources: [
  3. create res: drbd
  4. prepare disk: drbd
  5. adjust disk: drbd
  6. adjust net: drbd
  7. ]

兩邊要同時啓動

   3.查看啓動狀態

  1. [root@node2 ~]# cat /proc/drbd
  2. version: 8.4.3 (api:1/proto:86-101)
  3. GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by gardner@, 2013-11-29 12:28:00
  4. 0: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r-----
  5. 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:10489084
  6. 也可以通過drbd-overview命令來查看
  7. [root@node2 ~]# drbd-overview
  8. 0:drbd/0 Connected Secondary/Secondary Inconsistent/Inconsistent C r-----

從上面的信息中可以看出此時兩個節點均處於Secondary狀態。

   4.提升節點

  1. [root@node1 drbd.d]# drbdadm primary --force drbd
  2. [root@node1 drbd.d]# drbd-overview
  3. 0:drbd/0 SyncSource Primary/Secondary UpToDate/Inconsistent C r-----
  4. [>....................] sync'ed:  4.8% (9760/10240)M

   可以看到正在同步數據,等數據同步完再查看

  1. [root@node1 ~]# cat /proc/drbd
  2. version: 8.4.3 (api:1/proto:86-101)
  3. GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by gardner@, 2013-11-29 12:28:00
  4. 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
  5. ns:10490880 nr:0 dw:0 dr:10496952 al:0 bm:641 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

   5.創建文件系統

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

  1. [root@node1 ~]# mke2fs -t ext4 /dev/drbd0
  2. [root@node1 ~]# mkdir /drbd
  3. [root@node1 ~]# mount /dev/drbd0 /drbd/

   6.切換Primary和Secondary節點

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

主節點

  1. [root@node1 ~]# cp /etc/fstab /drbd/
  2. [root@node1 ~]# umount /drbd/
  3. [root@node1 ~]# drbdadm secondary drbd

備節點

  1. [root@node2 ~]# drbdadm primary drbd
  2. [root@node2 ~]# mkdir /drbd
  3. [root@node2 ~]# mount /dev/drbd0 /drbd/
  4. [root@node2 ~]# ll /drbd/
  5. total 20
  6. -rw-r--r-- 1 root root   921 Apr 19 2014 fstab
  7. drwx------ 2 root root 16384 Apr 19 2014 lost+found

可以看到我們的數據在節點2上可以看到了

  1. [root@node2 ~]# cat /proc/drbd
  2. version: 8.4.3 (api:1/proto:86-101)
  3. GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by gardner@, 2013-11-29 12:28:00
  4. 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
  5. ns:24 nr:10788112 dw:10788136 dr:1029 al:2 bm:642 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

可以看到現在node2爲主節點

八、corosync的安裝與配置

      安裝corosync。

   2.crm的安裝

  1. [root@node1 ~]# yum install -y crmsh-1.2.6-4.el6.x86_64.rpm pssh-2.3.1-2.el6.x86_64.rpm
  2. [root@node2 ~]# yum install -y crmsh-1.2.6-4.el6.x86_64.rpm pssh-2.3.1-2.el6.x86_64.rpm

   3.定義drbd資源

  1. [root@node1 ~]# crm
  2. crm(live)# configure
  3. crm(live)configure# primitive mariadbdrbd ocf:linbit:drbd params drbd_resource=drbd op monitor role=Master interval=10s timeout=20s op monitor role=Slave interval=20s timeout=20s op start timeout=240s op stop timeout=120s
  4. 定於drbd的主從資源
  5. crm(live)configure# ms ms_mariadbdrbd mariadbdrbd meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true

   4.定義文件系統資源和約束關係

  1. crm(live)configure# primitive mariadbstore ocf:heartbeat:Filesystem params device="/dev/drbd0" directory="/mydata" fstype="ext4" op monitor interval=40s timeout=40s op start timeout=60s op stop timeout=60s
  2. crm(live)configure# colocation mariadbstore_with_ms_mariadbdrbd inf: mariadbstore ms_mariadbdrbd:Master
  3. crm(live)configure# order ms_mariadbrbd_before_mariadbstore mandatory: ms_mariadbdrbd:promote mariadbstore:start

   5.增加vip和MariaDB的資源及約束關係

  1. crm(live)configure# primitive madbvip ocf:heartbeat:IPaddr2 params ip="192.168.1.240" op monitor interval=20s timeout=20s on-fail=restart
  2. crm(live)configure# primitive maserver lsb:mysqld op monitor interval=20s timeout=20s on-fail=restart
  3. crm(live)configure# verify

定義約束關係

  1. crm(live)configure# colocation maserver_with_mariadbstore inf: maserver mariadbstore
  2. crm(live)configure# order mariadbstore_before_maserver mandatory: mariadbstore:start maserver:start
  3. crm(live)configure# verify
  4. crm(live)configure# colocation madbvip_with_maserver inf: madbvip maserver
  5. crm(live)configure# order madbvip_before_masever mandatory: madbvip maserver
  6. crm(live)configure# verify
  7. crm(live)configure# commit

   6.查看所有定義的資源

  1. node node1.zero1.com
  2. node node2.zero1.com
  3. primitive madbvip ocf:heartbeat:IPaddr2 \
  4. params ip="192.168.1.240" \
  5. op monitor interval="20s" timeout="20s" on-fail="restart"
  6. primitive mariadbdrbd ocf:linbit:drbd \
  7. params drbd_resource="drbd" \
  8. op monitor role="Master" interval="30s" timeout="20s" \
  9. op monitor role="Slave" interval="60s" timeout="20s" \
  10. op start timeout="240s" interval="0" \
  11. op stop interval="0s" timeout="100s"
  12. primitive mariadbstore ocf:heartbeat:Filesystem \
  13. params device="/dev/drbd0" directory="/mydata" fstype="ext4" \
  14. op monitor interval="40s" timeout="40s" \
  15. op start timeout="60s" interval="0" \
  16. op stop timeout="60s" interval="0"
  17. primitive maserver lsb:mysqld \
  18. op monitor interval="20s" timeout="20s" on-fail="restart"
  19. ms ms_mariadbdrbd mariadbdrbd \
  20. meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"
  21. colocation madbvip_with_maserver inf: madbvip maserver
  22. colocation mariadbstore_with_ms_mariadbdrbd inf: mariadbstore ms_mariadbdrbd:Master
  23. colocation maserver_with_mariadbstore inf: maserver mariadbstore
  24. order madbvip_before_masever inf: madbvip maserver
  25. order mariadbstore_before_maserver inf: mariadbstore:start maserver:start
  26. order ms_mariadbrbd_before_mairadbstore inf: ms_mariadbdrbd:promote mariadbstore:start
  27. property $id="cib-bootstrap-options" \
  28. dc-version="1.1.10-14.el6_5.2-368c726" \
  29. cluster-infrastructure="classic openais (with plugin)" \
  30. expected-quorum-votes="2" \
  31. stonith-enabled="false" \
  32. no-quorum-policy="ignore"

九、測試

   1.查看運行狀態

  1. [root@node1 ~]# crm status
  2. Last updated: Wed Apr 23 16:24:11 2014
  3. Last change: Wed Apr 23 16:21:50 2014 via cibadmin on node1.zero1.com
  4. Stack: classic openais (with plugin)
  5. Current DC: node1.zero1.com - partition with quorum
  6. Version: 1.1.10-14.el6_5.2-368c726
  7. 2 Nodes configured, 2 expected votes
  8. 5 Resources configured
  9. Online: [ node1.zero1.com node2.zero1.com ]
  10. Master/Slave Set: ms_mariadbdrbd [mariadbdrbd]
  11. Masters: [ node1.zero1.com ]
  12. Slaves: [ node2.zero1.com ]
  13. mariadbstore   (ocf::heartbeat:Filesystem):    Started node1.zero1.com
  14. madbvip    (ocf::heartbeat:IPaddr2):   Started node1.zero1.com
  15. maserver   (lsb:mysqld):   Started node1.zero1.com

   2.手動切換節點

  1. [root@node1 ~]# crm node standby node1.zero1.com
  2. [root@node1 ~]# crm status
  3. Last updated: Wed Apr 23 16:26:05 2014
  4. Last change: Wed Apr 23 16:25:34 2014 via crm_attribute on node1.zero1.com
  5. Stack: classic openais (with plugin)
  6. Current DC: node1.zero1.com - partition with quorum
  7. Version: 1.1.10-14.el6_5.2-368c726
  8. 2 Nodes configured, 2 expected votes
  9. 5 Resources configured
  10. Node node1.zero1.com: standby
  11. Online: [ node2.zero1.com ]
  12. Master/Slave Set: ms_mariadbdrbd [mariadbdrbd]
  13. Masters: [ node2.zero1.com ]
  14. Stopped: [ node1.zero1.com ]
  15. mariadbstore   (ocf::heartbeat:Filesystem):    Started node2.zero1.com
  16. madbvip    (ocf::heartbeat:IPaddr2):   Started node2.zero1.com
  17. maserver   (lsb:mysqld):   Started node2.zero1.com

這樣所有的資源都在node2上運行了。

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