[教學][研究] 用DRBD+Heartbeat架設高可用性(HA)的NFS Server (CentOS 5.4 x86)
2010/02/24
Lu
DRDB全名為Distributed Replicated Block Device (分散式複製區塊裝置),是由PhilippReisner and LarsEllenberg 開發維護。
它可以讓你把本地磁碟內容同步到另一台主機上,把它加上心跳(heartbeat,HB)功能,就可以構建Linux下的高可用性(HA, High Availability)叢集(Cluster)環境。
本地主機(主要節點,Primary)與遠端主機(次要節點,Secondary)的資料可以保證即時同步,當本地系統出現故障時,遠端主機上還會保留有一份相同的資料,可以繼續使用。
如果加上Heartbeat(心跳)功能,就可以建立Linux下的高可用性(HA, High Availability)叢集(Cluster)環境。
參考資料
http://www.drbd.org/docs/install/ 一、環境
系統架構如下
代碼:
+———————+
| Client |
|192.168.128.x |
+———————+
|
|
+———————————+
| 192.168.128.100 |
| (/dev/drbd0) |
+———————————+
| |
| |
+————————+ +—————————+
| 本地硬碟 | | 遠端主機硬碟 |
|192.168.128.101 | | 192.168.128.102 |
| (/dev/sdb1) | | (/dev/sdb1) |
+————————+ +—————————+
虛擬IP(VIP) 192.168.128.100對外提供服務
主要伺服器(Primary) eth0: 192.168.128.101 eth1: 10.0.0.101(心跳檢測)
備份伺服器(Secondary) eth0: 192.168.128.102 eth1: 10.0.0.102(心跳檢測)
測試機 eth0: 192.168.128.xxx
(兩台的 eth1 非必須,只要在設定 ha.cf 檔案時把 eth1 改為 eth0 即可;
測試機請在同網段,因為稍後在 NFS 中設定了只有 192.168.128.0 網段才能存取)
小弟測試時使用 Windows XP + VMware Workstation 7.01 建立 3 台 VM
各別安裝 CentOS 5.4 x86 (final)
除了 OS 安裝時建立的硬碟,關機後,VM上再增加一個 1GB SCSI HD (/dev/sdb)
然後把 /dev/sdb 硬碟整個分割為 /dev/sdb1,把兩台的/dev/sdb1分割做成/dev/drbd0。
DRBD其實可以備份到多台主機,但是這裡只拿兩台測試。
(1) 設定主機名稱(必須)
在兩台主機上
代碼:
vim /etc/hosts
各自增加下面內容
代碼:
192.168.128.101 centos1.lu.idv.tw centos1
192.168.128.102 centos2.lu.idv.tw centos2
或修改第一台後,執行下面命令拷貝到第二台上
代碼:
scp /etc/hosts 192.168.128.102:/etc/
要讓電腦主機名稱立刻生效,請在主要主機上執行
代碼:
hostname centos1.lu.idv.tw
請在備份主機上執行
代碼:
hostname centos2.lu.idv.tw
要讓電腦重新啟動後,主機名稱設定生效,請在主要主機上執行
代碼:
vim /etc/sysconfig/network
把
代碼:
HOSTNAME=localhost.localdomain
改為
代碼:
HOSTNAME=centos1.lu.idv.tw
請在備份主機上執行把
代碼:
HOSTNAME=localhost.localdomain
改為
代碼:
HOSTNAME=centos2.lu.idv.tw
(2) 建立 DRBD 即時備份的硬碟分割
這兩個分割大小必須相同,不要格式化也不要mount。
我們用 fdisk 對 /dev/sdb 硬碟做分割,整顆分割做成一個分割 /dev/sdb1。
代碼:
[root@centos1 ~]# fdisk /dev/sdb
Command (m for help): n
Command action
e extended
p Primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-130, default 130): 130
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 65 522112 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
-------------------------------------------------------------------------------------------------
二、套件安裝
為了方便,建議一次安裝所有需要的套件,請在兩台主機執行下面命令,安裝套件
代碼:
yum -y install drbd83 kmod-drbd83 portmap nfs-utils heartbeat
yum -y install heartbeat
說明:
drbd83 kmod-drbd83 是 DRDB 8.3.x 版所需要
portmap portmap nfs-utils 是 NFS 所需要
heartbeat 是 HeartBeat (HA) 所需要,用yum安裝heartbeat有點問題,第一次只會安裝一部分,所以必須再執行一次安裝。
-------------------------------------------------------------------------------------------------
三、NFS的安裝與設定
代碼:
yum -y install portmap nfs-utils
chkconfig portmap on
chkconfig nfs off
service portmap start
service nfs start
nfs不需要啟動,也不需要設定成開機自動執行,Heartbeat會自動啟動和停止NFS。
不過我們還是先測試一下NFS可以執行 (portmap必須先啟動,NFS才能執行)。
在兩台主機分別建立共享目錄 d
代碼:
mkdir /d
編輯要NFS分享的內容
代碼:
vim /etc/exports
內容如下(假設讓 192.168.128.0/24 上的電腦有 rw 權限)
代碼:
/d 192.168.128.0/255.255.255.0(rw,no_root_squash,no_all_squash,sync)
-------------------------------------------------------------------------------------------------
四、DRBD的安裝和設定
(1)安裝DRBD
先用yum檢查是否有套件可以直接安裝(如果前面安裝過,可跳過)
代碼:
[root@localhost ~]# yum list | grep drbd
drbd.i386 8.0.16-5.el5.centos extras
drbd82.i386 8.2.6-1.el5.centos extras
drbd83.i386 8.3.2-6.el5_3 extras
kmod-drbd.i686 8.0.16-5.el5_3 extras
kmod-drbd-PAE.i686 8.0.16-5.el5_3 extras
kmod-drbd-xen.i686 8.0.16-5.el5_3 extras
kmod-drbd82.i686 8.2.6-2 extras
kmod-drbd82-PAE.i686 8.2.6-2 extras
kmod-drbd82-xen.i686 8.2.6-2 extras
kmod-drbd83.i686 8.3.2-6.el5_3 extras
kmod-drbd83-PAE.i686 8.3.2-6.el5_3 extras
kmod-drbd83-xen.i686 8.3.2-6.el5_3 extras
發現有 8.0.x、8.2.x和8.3.x三個版本可以安裝,小弟安裝最新版本。
[root@centos1 ~]# yum -y install drbd83 kmod-drbd83
安裝後驗證一下 DRBD 的模組是否啟動,沒啟動則用modprobe啟動它(兩台都要做)。
(2) 掛載 DRBD 模組
代碼:
[root@centos1 ~]# lsmod | grep drbd
[root@centos1 ~]# modprobe drbd
[root@centos1 ~]# lsmod | grep drbd
drbd 220440 3
然後在 /etc/rc.local 加上
代碼:
modprobe drbd
,讓每次電腦執行時自動啟動它。
(3) 建立 /dev/drbd0 節點
在兩台主機上都建立硬體設備drbd
代碼:
[root@centos1 ~]# mknod /dev/drbd0 b 147 0
mknod: `/dev/drbd0': File exists
(有可能在安裝 DRBD 時自動建立了)
如果有多個drbd分割需要建立,可依次執行
mknod /dev/drbd1 b 147 1
mknod /dev/drbd2 b 147 2
...
(4) 編輯 drbd.conf 設定檔
編輯 DRBD 的設定檔 /etc/drbd.conf
( 執行 find / -name drbd.conf -print 可以找到參考設定檔案)
代碼:
[root@centos1 /]# vim /etc/drbd.conf
內容如下 (同步的傳輸率請自行根據環境頻寬修改,注意別吃光了頻寬,建議總頻寬的30%)
代碼:
global {
usage-count yes;
}
common {
syncer { rate 30M; }
}
resource r0 {
protocol C;
startup {
wfc-timeout 0;
degr-wfc-timeout 120;
}
disk {
on-io-error detach;
# no-disk-flushes;
# no-md-flushes
# size 1G;
}
net {
}
on centos1.lu.idv.tw {
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.128.101:7789;
meta-disk internal;
}
on centos2.lu.idv.tw {
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.128.102:7789;
meta-disk internal;
}
}
另外一台主機上設定相同,或直接拷貝過去
代碼:
scp /etc/drbd.conf 192.168.128.102:/etc/
(5) 建立資源r0
建立資源r0 (兩台都要做)
代碼:
[root@centos1 /]# drbdadm create-md r0
--== Thank you for participating in the global usage survey ==--
The server's response is:
s
you are the 8235th user to install this version
Writing meta data...
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created.
success
(6) 設定防火牆
設定防火牆在開機後自動啟動
代碼:
chkconfig iptables on
修改防火牆設定檔案
代碼:
vim /etc/sysconfig/iptables
在適當位置增加下面規則
代碼:
-A RH-Firewall-1-INPUT -s 192.168.128.102 -p tcp --dport 7789 -j ACCEPT
重新啟動防火牆載入規則
代碼:
service iptables start
或直接下命令增加規則 (立刻生效,但重新啟動後會消失)
在 192.168.128.101 執行
代碼:
iptables -A RH-Firewall-1-INPUT -s 192.168.128.102 -p tcp --dport 7789 -j ACCEPT
在 192.168.128.102 執行
代碼:
iptables -A RH-Firewall-1-INPUT -s 192.168.128.101 -p tcp --dport 7789 -j ACCEPT
或在兩台都執行
代碼:
iptables -A RH-Firewall-1-INPUT -p tcp --dport 7789 -j ACCEPT
執行下面命令將設定存到 /etc/sysconfig/iptables,讓防火牆重新啟動後生效
代碼:
iptables-save
(7) 啟動 DRBD 服務
設定drbd開機自動啟動.
代碼:
[root@localhost ~]# chkconfig drbd on
立刻啟動 DRBD 服務
代碼:
[root@localhost ~]# service drbd start
Starting DRBD resources: [ d(r0) s(r0) n(r0) ].
檢查目前狀態
代碼:
[root@centos1 ~]# cat /proc/drbd
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by
[email protected], 2009-08-29 14:02:24
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:b oos:1044124
Secondary/Secondary表示兩台目前都是Secondary狀態 (因為DRDB不知道哪台是Primary主機)
ds是磁碟狀態,Inconsistent表示『不一致』
另外也可以用下面命令檢查狀態
代碼:
service drbd status
或
drbdadm state r0
或
drbdadm role r0
(8) 設定哪台為 Primary 主機(只在主要節點上操作)
第一次設定主要節點時用 drbdadm 命令會失敗,需要先用 drbdsetup 來做,以後就可以直接用drbdadm了。
代碼:
[root@centos1 /]# drbdsetup /dev/drbd0 primary -o
[root@centos1 /]# drbdadm primary r0
檢查狀態
代碼:
[root@localhost ~]# cat /proc/drbd
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by
[email protected], 2009-08-29 14:02:24
0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r----
ns:150224 nr:0 dw:0 dr:150528 al:0 bm:9 lo:0 pe:19 ua:10 ap:0 ep:1 wo:b oos:894492
[=>..................] sync'ed: 14.6% (894492/1044124)K
finish: 0:00:23 speed: 37,408 (37,408) K/sec
Secondary/Secondary 已經變成 Primary/Secondary 了。
目前Primary主機磁碟狀態是UpToDate (已經更新),Secondary主機磁碟狀態是Inconsistent(不一致)。
[=>..................] 表示目前同步到 14.6%,同步完成後應該看到下面狀態。
代碼:
[root@localhost ~]# cat /proc/drbd
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by
[email protected], 2009-08-29 14:02:24
0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----
ns:1044124 nr:0 dw:0 dr:1044124 al:0 bm:64 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
(9) DRBD 的使用
現在可以把主要主機上的DRBD設備掛載到一個目錄上進行使用,
次要主機的DRBD設備無法被掛載,因為它是用來接收主要主機資料的,只能備份用(連讀取也不行)。
所有的讀寫功能只有在Primary主機可以操作,當Primary故障,
需把Secondary主機提升為Primary,才可以讀寫DRBD設備。
在centos1主伺服器上執行mkfs.ext3做格式化
代碼:
[root@centos1 ~]# mkfs.ext3 /dev/drbd0
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
130560 inodes, 261031 blocks
13051 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
16320 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
並用mount指令掛載
代碼:
[root@centos1 mnt]# mount /dev/drbd0 /d
檢查掛載情況
代碼:
[root@centos1 ~]# mount
/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hdc on /media/CentOS_5.4_Final type iso9660 (ro,noexec,nosuid,nodev,uid=0)
/dev/drbd0 on /d type ext3 (rw)
由最後一行 /dev/drbd0 on /d type ext3 (rw) 可知道設備掛載到 /d 目錄了
-------------------------------------------------------------------------------------------------
五、DRBD的相關測試
(1) Primary 當掉
把 Primary 主機的 eth0 網路卡停用,假設是 Primary 主機故障。
[root@centos1 /]# ifdown eth0
檢查 Primary 主機狀態 (有4種方法可以選擇,任選一種即可)
代碼:
[root@centos1 ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by
[email protected], 2009-08-29 14:02:24
m:res cs ro ds p mounted fstype
0:r0 StandAlone Primary/Unknown UpToDate/DUnknown r---- ext3
[root@centos1 ~]# cat /proc/drbd
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by
[email protected], 2009-08-29 14:02:24
0: cs:StandAlone ro:Primary/Unknown ds:UpToDate/DUnknown r----
ns:1077332 nr:0 dw:33208 dr:1044249 al:13 bm:64 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
[root@centos1 ~]# drbdadm state r0
'drbdadm state' is deprecated, use 'drbdadm role' instead.
Primary/Unknown
[root@centos1 ~]# drbdadm role r0
Primary/Unknown
發現變成 Primary/Unknown,Primary主機找不到 Secondary 主機。
檢查 Secondary 主機狀態:
代碼:
[root@centos2 ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by
[email protected], 2009-08-29 14:02:24
m:res cs ro ds p mounted fstype
0:r0 WFConnection Secondary/Unknown UpToDate/DUnknown C
[root@centos2 ~]# cat /proc/drbd
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by
[email protected], 2009-08-29 14:02:24
0: cs:WFConnection ro:Secondary/Unknown ds:UpToDate/DUnknown C r----
ns:0 nr:1077332 dw:1077332 dr:0 al:0 bm:64 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
[root@centos2 ~]# drbdadm state r0
'drbdadm state' is deprecated, use 'drbdadm role' instead.
Secondary/Unknown
[root@centos2 ~]# drbdadm role r0
Secondary/Unknown
在 Secondary 主機上也找不到 Primary 主機了。
假設 Primary 主機復原,啟動網路卡
代碼:
[root@centos1 /]# ifup eth0
檢查狀態
代碼:
[root@centos1 ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by
[email protected], 2009-08-29 14:02:24
m:res cs ro ds p mounted fstype
0:r0 StandAlone Primary/Unknown UpToDate/DUnknown r---- ext3
[root@centos1 ~]# cat /proc/drbd
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by
[email protected], 2009-08-29 14:02:24
0: cs:StandAlone ro:Primary/Unknown ds:UpToDate/DUnknown r----
ns:1077332 nr:0 dw:33208 dr:1044249 al:13 bm:64 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
[root@centos1 ~]# drbd state r0
-bash: drbd: command not found
[root@centos1 ~]# drbdadm state r0
'drbdadm state' is deprecated, use 'drbdadm role' instead.
Primary/Unknown
[root@centos1 ~]# drbdadm role r0
Primary/Unknown
還是顯示未連接狀態。
執行連線指令,檢察狀態:
代碼:
[root@centos1 ~]# drbdadm connect r0
[root@centos1 ~]# cat /proc/drbd
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by
[email protected], 2009-08-29 14:02:24
0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----
ns:0 nr:0 dw:33208 dr:1044249 al:13 bm:64 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
發現重新連上了。
(2) 同步測試與 Primary 和 Secondary 主機交換:
在 /d 目錄隨邊編輯個檔案,隨便寫點內容:
代碼:
[root@centos1 ~]# vim /d/test.txt
如果要交換 Primary 和 Secondary 主機,請在 Primary 上卸載設備,然後設定為 Secondary:
[root@centos1 /]# umount /d
[root@centos1 /]# drbdadm secondary r0
[root@centos1 ~]# cat /proc/drbd
version: 8.3.2 (api:88/proto:86-90)
GIT-hash: dd7985327f146f33b86d4bff5ca8c94234ce840e build by
[email protected], 2009-08-29 14:02:24
0: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r----
ns:4 nr:0 dw:33212 dr:1044249 al:13 bm:74 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
現在兩台主機都是Secondary。
在centos2主機上,將它升級為Primary,然後 mount 掛載起來:
代碼:
[root@centos2 /]# drbdadm primary r0
[root@centos2 ~]# drbdadm role r0
Primary/Secondary
[root@centos2 ~]# mount /dev/drbd0 /d
[root@centos2 ~]# mount
/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hdc on /media/CentOS_5.4_Final type iso9660 (ro,noexec,nosuid,nodev,uid=0)
/dev/drbd0 on /d type ext3 (rw)
現在 centos2 成為 Primary 主機了。
檢查檔案是否有同步拷貝過來
[root@centos2 ~]# cat /d/test.txt
(3) Split-Brain腦裂測試
這是指兩台都變成 Primary 時的處理解決測試。
先把 Primary 的 eth0 做 ifdown,然後把 Secondary 主機提升為 Primary,並且 mount;
然後 Primary 的 eth0 啟用,發現沒有自動恢復主從關係。
解決方法是先在 Secondary 上放棄資料,連上資源 r0:
代碼:
[root@centos2 ~]# drbdadm -- --discard-my-data connect r0
然後在 Primary 重新連上 r0
代碼:
[root@centos1 ~]# drbdadm connect r0
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
HeartBeat
DRBD 的 Primary 當掉時,必須手動把 Secondary 切換成 Primary。
如果系統再安裝 HeartBeat 做 HA (高可用性, High Availability),則可以達到熱備援 (Hot Stand-by),
自動把 Secondary 啟動為 Primary 主機,並且自動掛載分享磁碟。
一、安裝
請先安裝 Heartbeat (要安裝兩次,如果前面安裝了則可跳過)
代碼:
yum -y install heartbeat
yum -y install heartbeat
-------------------------------------------------------------------------------------------------
二、設定
Heartbeat 的相關的設定檔案有 3 個
代碼:
/etc/ha.d/ha.cf
/etc/ha.d/haresources
/etc/ha.d/authkeys
用 find 指令或許可以找到參考用的設定檔案說明文件,例如
代碼:
fina / -name ha.cf -print
(1) 編輯 HA 相關功能檔 ha.cf
代碼:
[root@centos1 /]# vim /etc/ha.d/ha.cf
內容如下
代碼:
#debugfile /var/log/ha-debug.log
logfile /var/log/ha-log
logfacility local0
keepalive 2 #設定心跳(監測)時間時間為2秒
warntime 5
deadtime 20
initdead 120
#hopfudge 1
udpport 694 #使用 UDP Port 694 進行心跳監測
auto_failback off #如果主要伺服器故障後修復完成,另一台伺服器是否該放棄資源
#baud 19200
bcast eth1 #使用eth0做心跳監測 (注意:如果電腦只有 eth0, 請改成 eth0)
#ucast eth0 192.168.128.101
#watchdog /dev/watchdog
#crm on
node centos1.lu.idv.tw #節點1,必須要與 hostname 或 uname -n 顯示的主機名稱相符合才可以,此處也可以寫為 centos1
node centos2.lu.idv.tw #節點2,此處也可以寫為 centos2
ping 192.168.128.2 #使用 ping Gateway 當成檢查主要主機是否仍在運作 (心跳是否正常)
#ping 192.168.128.101 192.168.128.102 192.168.128.100
#deadping 5
#ping_group lbgroup 10.0.0.101 10.0.0.102
#ping_group lbgroup 192.168.128.101 192.168.128.102
#設定和 heartbeat 一起啟動、關閉的進程
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
(2) 編輯雙機相互驗證檔案 authkeys
代碼:
[root@centos1 ha.d]# vim /etc/ha.d/authkeys
內容如下
代碼:
auth 1
1 crc
給予相關權限
代碼:
[root@centos1 ha.d]# chmod 600 /etc/ha.d/authkeys
(3) 設定 HA 資源檔 haresources
代碼:
[root@centos1 ha.d]# vim /etc/ha.d/haresources
內容如下
代碼:
centos1.lu.idv.tw IPaddr::192.168.128.100/22/eth0 drbddisk::r0 Filesystem::/dev/drbd0::/d::ext3 nfs
haresources裡指定的資源可以是多個,啟動時按照從左到右的順序,關閉時則按照從右到左的順序。
代碼:
[root@centos1 ha.d]# vim /etc/ha.d/resource.d/killnfsd
內容如下
代碼:
killall -9 nfsd; /etc/init.d/nfs restart; exit 0
設定相關權限
代碼:
[root@centos1 ha.d]# chmod 755 /etc/ha.d/resource.d/killnfsd
把所有設定拷貝到 另外一台主機
代碼:
[root@centos1 ha.d]# scp -r /etc/ha.d/
[email protected]:/etc/
-------------------------------------------------------------------------------------------------
三、啟動
依序在兩台主機啟動 Heartbeat
代碼:
[root@centos1 ~]# service heartbeat start
或
[root@centos1 ~]# /etc/init.d/heartbeat start
[root@centos2 ~]# /etc/init.d/heartbeat start
-------------------------------------------------------------------------------------------------
四、測試
在測試機上安裝 NFS,把 192.168.128.100 (VIP) 的 /d 目錄掛載到自己電腦的 /mnt/test 目錄
代碼:
[root@test ~]# yum -y install portmap nfs-utils
[root@test ~]# service iptables stop
[root@test ~]# service portmap start
[root@test ~]# service nfs start
[root@localhost ~]# showmount -e 192.168.128.100
Export list for 192.168.128.100:
/d 192.168.128.0/255.255.255.0
[root@test ~]# mkdir /mnt/test
[root@test ~]# mount 192.168.128.100:/d /mnt/test
[root@test ~]# mount
/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hdc on /media/CentOS_5.4_Final type iso9660 (ro,noexec,nosuid,nodev,uid=0)
nfsd on /proc/fs/nfsd type nfsd (rw)
192.168.128.100:/d on /mnt/test type nfs (rw,addr=192.168.128.100)
將主要節點的heartbeat服務停止,測試次要主機是否主動接管
代碼:
[root@centos1 ~]# service heartbeat stop
在 Secondary 主機上檢查
代碼:
[root@centos2 ~]# mount
/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hdc on /media/CentOS_5.4_Final type iso9660 (ro,noexec,nosuid,nodev,uid=0)
nfsd on /proc/fs/nfsd type nfsd (rw)
/dev/drbd0 on /d type ext3 (rw)
在最後一行發現已經主動掛載起來了。
(完)