【本文環境】VirtualBox 4.0.4,CentOS 5.5 x64
Heartbeat[1]是Linux下最常用的HA方案之一,主要為瞭解決叢集前置伺服器的單點失效問題。Heartbeat顧名思義可以在兩台電腦間通過心跳訊號來互相探測對方是否正常工作,因此,這兩台電腦間需要有相應的物理介質串連起來。在這裡,我並沒有使用額外的網線(CAT-5雙絞線[2])和網卡來實現,而是採用了串口線來建立心跳串連。
這樣,在VirtualBox虛擬環境中,虛擬機器間還需要使用一個虛擬串口互相串連,這還需要一個軟體的配合:Named Pipe TCP Proxy。 1,在本機上通過Named Pipe TCP Proxy可以建立一個具名管道,這樣運行於本機上的虛擬機器就可以將虛擬機器串口的I/O重新導向到這個管道上,如果兩個虛擬機器都定向到這個管道的時候,就相當於兩個虛擬機器通過一根串口線串連到了一起。
圖 1 在Named Pipe TCP Proxy中建立管道串連
這樣,在建立Linux虛擬機器的時候,兩個虛擬機器就都使用“//./pipe/vbox”作為虛擬串口,唯一的區別是首先啟動的虛擬機器需要勾選“建立通道”選項, 2。
圖 2 Linux虛擬機器的串口設定
分別啟動兩個Linux虛擬機器,首先驗證一下兩台虛擬機器是否虛擬地通過串口串連到了一起。為了便於區分,我這裡用這兩台虛擬機器的機器名分別代表它們:wuyfcentos1(IP:192.168.2.101)和wuyfcentos2(IP:192.168.2.102)。我們可以分別在兩台虛擬機器上開啟終端,在wuyfcentos1上執行命令:
[root@wuyfcentos1 ~]# cat
這樣,輸入到這個虛擬機器COM1串口的資料都會在控制台中列印出來,如果我們在wuyfcentos2虛擬機器上執行echo命令:
[root@wuyfcentos2 ~]# echo 'test serial connection' > /dev/ttyS0
在wuyfcentos1上可以看到這段文本:
[root@wuyfcentos1 ~]# cat
test serial connection
這樣表明,從wuyfcentos1串口發送的資料可以被wuyfcentos2串口接收到,兩台虛擬機器相當於通過串口線串連在了一起。
下面,就要使用Linux Heartbeat,在CentOS上可以通過yum的安裝:
[root@wuyfcentos1 ~]# yum install heartbeat
Linux Heartbeat通過設定檔配置,設定檔均位於/etc/ha.d/目錄下,一個Heartbeat叢集節點需要3個設定檔:“ha.cf”、”authkeys”、“haresources”,這都需要手動建立,讓我們分別來看這3個設定檔的內容:
“ha.cf”:
serial /dev/ttyS0 #使用串口,串口為/dev/ttyS0
baud 19200 #通訊使用的傳輸速率
auto_failback on
autojoin none
warntime 5
deadtime 15
initdead 60
keepalive 1
node wuyfcentos1.localdomain #節點1
node wuyfcentos2.localdomain #節點2
這個檔案詳細怎麼配置可以參考:
http://www.linux-ha.org/doc/users-guide/_creating_an_initial_heartbeat_configuration.html#_the_literal_ha_cf_literal_file
http://www.linux-ha.org/ha.cf
“authkeys”:
auth 1
1 crc
注意,這個檔案必須是600許可權,所以建立檔案後需要運行命令:
[root@wuyfcentos1 ~]# chmod 600 authkeys
這個檔案詳細怎麼配置可以參考:
http://www.linux-ha.org/doc/users-guide/_the_literal_authkeys_literal_file.html
http://www.linux-ha.org/authkeys
“haresources”:
wuyfcentos1.localdomain 192.168.2.100 httpd
這是wuyfcentos1這個虛擬機器的配置,表明Heartbeat使用的叢集IP為“192.168.2.100”,並且,Heartbeat管理的服務是httpd,因為我們希望將兩台伺服器的Apache進行一個互備。
最後我們還需要把Heartbeat註冊為系統服務:
[root@wuyfcentos1 ~]# chkconfig --list heartbeat
[root@wuyfcentos1 ~]# chkconfig heartbeat on
修改完Heartbeat的設定檔後,通過reload命令重新載入:
[root@wuyfcentos1 ~]# service heartbeat reload
我在wuyfcentos1和wuyfcentos2下的/var/www/html下放置了不同的index.html檔案,如果我直接存取Heartbeat叢集IP:http://192.168.2.100,看到的頁面是wuyfcentos2上的頁面:
這時,強行殺死wuyfcentos2虛擬機器進程,重新整理頁面馬上變成了wuyfcentos1上的頁面:
再把wuyfcentos2虛擬機器啟動起來後,重新整理頁面也會再切回到wuyfcentos2的頁面,因為wuyfcentos1將資源重新交還到wuyfcentos2手中,這個時候在後台日誌中可以看到這樣的資訊:
Mar 24 11:23:46 wuyfcentos1 heartbeat: [19364]: info: Heartbeat restart on node wuyfcentos2.localdomain
Mar 24 11:23:46 wuyfcentos1 heartbeat: [19364]: info: Link wuyfcentos2.localdomain:/dev/ttyS0 up.
Mar 24 11:23:46 wuyfcentos1 heartbeat: [19364]: info: Status update for node wuyfcentos2.localdomain: status up
Mar 24 11:23:46 wuyfcentos1 harc[20082]: [20088]: info: Running /etc/ha.d/rc.d/status status
Mar 24 11:23:48 wuyfcentos1 heartbeat: [19364]: info: Status update for node wuyfcentos2.localdomain: status active
Mar 24 11:23:48 wuyfcentos1 harc[20094]: [20100]: info: Running /etc/ha.d/rc.d/status status
Mar 24 11:23:48 wuyfcentos1 heartbeat: [19364]: info: remote resource transition completed.
Mar 24 11:23:48 wuyfcentos1 heartbeat: [19364]: info: wuyfcentos1.localdomain wants to go standby [foreign]
Mar 24 11:23:49 wuyfcentos1 heartbeat: [19364]: info: standby: wuyfcentos2.localdomain can take ourforeign resources
Mar 24 11:23:49 wuyfcentos1 heartbeat: [20106]: info: give up foreign HA resources (standby).
Mar 24 11:23:49 wuyfcentos1 heartbeat: [20106]: info: foreign HA resource release completed (standby).
Mar 24 11:23:49 wuyfcentos1 heartbeat: [19364]: info: Local standby process completed [foreign].
[1] http://www.linux-ha.org/wiki/Heartbeat
[2] http://zh.wikipedia.org/zh/CAT-5