基礎:node
磁盤鏡像技術DRBDlinux
DRBD(Distributed Replicated Block Device)是由內核模塊和相關腳本構成,用以構建高可用性的集羣。其實現方式是經過網絡來鏡像整個設備。它容許用戶在遠程機器上創建一個本地塊設備的實時鏡像。也能夠把它當作一個網絡RAID1.centos
工做原理:api
DRBD負責接收數據,把數據寫到本地磁盤,而後發送給另外一個主機。另外一個主機再將數據存到本身的磁盤中。目前,DRBD每次只容許對一個節點進行讀寫訪問,這對於一般的故障切換高可用性集羣已經夠了。服務器
DRBD協議:網絡
A:數據一旦寫入磁盤併發送到網絡中就認爲完成了寫入操做併發
B:收到接受確認就認爲完成了寫入操做app
C:收到寫入確認就完成了寫入操做分佈式
目前運用最多的就是C協議ide
DRBD的三個進程:
drbd0——worker:主進程
drbd0——asender:primary上drbd0的數據發送進程
drbd0——receiver;secondary上drbd0的數據接受進程
配置DRBD前須要注意幾點:
mount drbd設備之前必須把設備切換到primary狀態
兩個節點中,同一時刻只能有一臺處於primary狀態,另外一臺處於secondary狀態,處於secondary狀態的服務器上不能加載drbd設備。主備服務器同步的兩個分區大小最好相同,這樣不至於浪費磁盤空間,由於drbd鏡像至關於網絡raid1.
測試環境:
– 兩個節點 (node1:192.168.1.110 node2:192.168.1.111 hosts作綁定 )
- 兩塊硬盤 (node1與node2節點各自添加相同大小的一塊硬盤)
– 保持兩個節點時間同步
– Selinux 關閉 防火牆7788端口打開(或者關閉防火牆)
開始安裝:
安裝第三方源
rpm -Uvh http://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm yum makecache yum install *gcc* make* #源碼包安裝是的依賴
安裝DRBD(yum):
[root@node1 ~]# yum -y install drbd83-utils kmod-drbd83 [root@node2 ~]# yum -y install drbd83-utils kmod-drbd83 node1與node2執行"yum install kernel-devel kernel kernel-headers flex " ,不然會報錯:FATAL: Module drbd not found. 安裝後reboot
手動加入drbd模塊:
# depmod
說明:在編譯並準備好一個Linux內核加載模塊後,modprobe前必須先執行命令depmod,此命令會生成新的modules.dep。
/sbin/modprobe drbd
lsmod |grep drbd
安裝DRBD(源碼):
[root@node1 ~]#wget http://oss.linbit.com/drbd/8.3/drbd-8.3.16.tar.gz && tar -zxvf drbd-8.3.16.tar.gz #drbd共有兩部分組成:內核模塊和用戶空間的管理工具。 [root@node1 ~]#cd drbd-8.3.16 [root@node1 drbd-8.3.16]#./configure --prefix=/usr/local/drbd --with-km #"--with-km"啓用drbd內核模塊(這裏可使用./configure --help查詢) [root@node1 drbd-8.3.16]#make KDIR=/usr/src/kernels/2.6.32-358.el6.x86_64/ #KDIR指的是本身的內核路徑 [root@node1 drbd-8.3.16]#make install [root@node1 drbd-8.3.16]#mkdir -p /usr/local/drbd/var/run/drbd #建立drbd運行所需目錄 [root@node1 drbd-8.3.16]#cp /usr/local/drbd/etc/rc.d/init.d/drbd /etc/rc.d/init.d/ #添加啓動腳本 [root@node1 drbd-8.3.16]#chkconfig --add drbd && chkconfig drbd on #設置開機啓動 安裝drbd模塊: [root@node1 drbd-8.3.16]#cd drbd [root@node1 drbd-8.3.16]#make clean [root@node1 drbd-8.3.16]#make KDIR=/usr/src/kernels/2.6.32-358.el6.x86_64/ [root@node1 drbd-8.3.16]#cp drbd.ko /lib/modules/`uname -r`/kernel/lib/ [root@node1 drbd-8.3.16]# depmod [root@node1 drbd-8.3.16]#/sbin/modprobe drbd [root@node1 drbd-8.3.16]#lsmod |grep drbd
DRBD分區:
在兩個節點爲drbd單獨分區
[root@node1 ~]# fdisk -cu /dev/sdb [root@node2 ~]# fdisk -cu /dev/sdb
以下:
[root@node1 yum.repos.d]# fdisk -cu /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x2a0f1472. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) Command (m for help): p Disk /dev/sdb: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders, total 4194304 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x2a0f1472 Device Boot Start End Blocks Id System Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First sector (2048-4194303, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303): Using default value 4194303 Command (m for help): w The partition table has been altered!
mkfs.ext4 /dev/sdb1
建立分佈式的複製的塊設備文件:
[root@node1 ~]# vi /etc/drbd.d/clusterdb.res resource clusterdb { startup { wfc-timeout 30; outdated-wfc-timeout 20; degr-wfc-timeout 30; } net { cram-hmac-alg sha1; shared-secret sync_disk; } syncer { rate 10M; al-extents 257; on-no-data-accessible io-error; } on node1 { device /dev/drbd0; disk /dev/sdb1; address 192.168.1.110:7788; flexible-meta-disk internal; } on node2 { device /dev/drbd0; disk /dev/sdb1; address 192.168.1.111:7788; meta-disk internal; } }
確認在兩個節點作了hosts解析
/etc/hosts 192.168.1.110 node1 node1.example.com 192.168.1.111 node2 node2.example.com
時間同步(crond加入定時執行)
crontab -e 5 * * * * root ntpdate time-nw.nist.gov :wq crontab -l 5 * * * * root ntpdate time-nw.nist.gov service crond restart
拷貝DRBD配置文件至node2節點:
[root@node1 ~]# scp /etc/drbd.d/clusterdb.res node2:/etc/drbd.d/clusterdb.res
初始化兩個節點的DRBD元數據:
[root@node1 ~]# drbdadm create-md clusterdb [root@node2 ~]# drbdadm create-md clusterdb You want me to create a v08 style flexible-size internal meta data block. There appears to be a v08 flexible-size internal meta data block already in place on /dev/sdb1 at byte offset 2146430976 Do you really want to overwrite the existing v08 meta-data? [need to type 'yes' to confirm] yes Writing meta data... initializing activity log NOT initialized bitmap New drbd meta data block successfully created.
報錯:
md_offset 2146758656 al_offset 2146725888 bm_offset 2146660352 Found ext3 filesystem 2096448 kB data area apparently used 2096348 kB left usable by current configuration Device size would be truncated, which would corrupt data and result in 'access beyond end of device' errors. You need to either * use external meta data (recommended) * shrink that filesystem first * zero out the device (destroy the filesystem) Operation refused. Command 'drbdmeta 0 v08 /dev/sdb1 internal create-md' terminated with exit code 40 drbdadm create-md clusterdb: exited with code 40
執行:dd if=/dev/zero of=/dev/sdb1 bs=1M count=1 而後從新初始化可解決報錯.
在兩個節點開啓DRBD進程:
[root@node1 ~]# service drbd start [root@node2 ~]# service drbd start
運行drbdadm設置node1爲主節點命令:
[root@node1 ~]# drbdadm -- --overwrite-data-of-peer primary all 注: 也能夠在要設置爲Primary的節點上使用以下命令來設置主節點: drbdsetup primary all 或 [root@node1 ~]# drbdsetup /dev/drbd0 primary -o 然後再次查看狀態,能夠發現數據同步過程已經開始:
檢查磁盤初始同步是否完成,確認你是否在主節點上:
[root@node1 yum.repos.d]# cat /proc/drbd version: 8.3.16 (api:88/proto:86-97) GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build32R6, 2013-09-27 15:59:12 0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r----- ns:78848 nr:0 dw:0 dr:79520 al:0 bm:4 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:2017180 [>....................] sync'ed: 4.0% (2017180/2096028)K finish: 0:02:58 speed: 11,264 (11,264) K/sec ns:1081628 nr:0 dw:33260 dr:1048752 al:14 bm:64 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0]
注:"/proc/drbd"中顯示了drbd當前的狀態.第一行的st表示兩臺主機的狀態,都是」備機」狀態.ds是磁盤狀態.
在分佈式複製的塊設備上建立文件系統:
[root@node1 yum.repos.d]# /sbin/mkfs.ext4 /dev/drbd0 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 width=0 blocks 131072 inodes, 524007 blocks 26200 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=536870912 16 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 26 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
在你的主節點掛載DRBD分區:
mkdir /data mount /dev/drbd0 /data [root@node1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_unixmencentos65-lv_root 19G 3.6G 15G 20% / tmpfs 1.2G 44M 1.2G 4% /dev/shm /dev/sda1 485M 80M 380M 18% /boot /dev/drbd0 2.0G 36M 1.9G 2% /data
建立測試文件:
[root@node1 ~]#dd if=/dev/zero of=/data/tempfile1.tmp bs=104857600 count=2 #建立200M的測試文件 [root@node1 data]# ll total 204816 drwx------ 2 root root 16384 Jan 25 00:14 lost+found -rw-r--r-- 1 root root 209715200 Jan 25 00:15 tempfile1.tmp
關閉主/設置輔節點:
(從主節點卸載data目錄,同時將主節點降爲輔助節點)
(設置輔助節點爲主節點,掛載data目錄)
[root@node1 ~]#umount /data #卸載drbd設備
[root@node1 ~]#drbdadm secondary clusterdb #關閉node1的主節點,設置爲輔節點
[root@node1 data]# cat /proc/drbd version: 8.3.16 (api:88/proto:86-97) GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37 0: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r----- ns:774292 nr:8 dw:476096 dr:300622 al:123 bm:23 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
[root@node2 ~]#drbdadm -- --overwrite-data-of-peer primary all #設置node2爲主節點,關閉輔節點
[root@node2 ~]#mount /dev/drbd0 /data #掛載drbd0到data目錄
[root@node2 data]# cat /proc/drbd version: 8.3.16 (api:88/proto:86-97) GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----- ns:32 nr:774292 dw:774324 dr:2030 al:2 bm:23 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
查看data目錄:
[root@node2 data]# ll total 204816 drwx------ 2 root root 16384 Jan 25 02:24 lost+found -rw-r--r-- 1 root root 209715200 Jan 25 02:25 tempfile1.tmp
注意:
修改主節點爲輔節點時必定要卸載掛載的目錄,不然會報錯!
翻譯自: http://www.unixmen.com/configure-drbd-centos-6-5/
可參考: http://geekpeek.net/drbd-how-to-configure-drbd-on-centos/
DRBD腦裂處理:http://www.tuicool.com/articles/Zv22i2j
DRBD介紹、工做原理及腦裂故障處理:http://bruce007.blog.51cto.com/7748327/1330959
DRBD腦裂處理:http://itindex.net/detail/50197-drbd
源碼下載地址:http://oss.linbit.com/drbd/