天天看點

linux高可用叢集heartbeat實作http的高可用

    linux高可用叢集的種類非常多。比方常見的heartbeat,corosync,rhcs,keepalived,這些叢集軟體的出現為我們的業務生産環境提供了高可用的保證。本文将簡介一下用heartbeat的v2版本号來處理一個簡單的http高可用叢集的搭建。

    在實作http高可用叢集之前,首先至少須要2台主機。而且須要做3點主要的準備工作:

    1.設定節點名稱,并且叢集中的全部節點都能通過節點名稱去解析叢集中的全部主機。為了叢集服務的高可用性,這裡選擇使用/etc/hosts。并且要保證uname -n的值必須和hostname中的值一緻。

    2.使用ssh讓雙機互信。

    3.時間同步。

    在安裝heartbeat軟體之前。首先把上面的基本工作完畢。這裡採用2台主機(192.168.1.201,192.168.1.202)來做我們的高可用叢集服務。

    1.首先登陸192.168.1.201改動hostname=test1.qiguo.com,在/etc/sysconfig/network中改動HOSTNAME=test1.qiguo.com保證server下次啟動的時候主機名不變。在/etc/hosts中加入192.168.1.201 test1.qiguo.com test1; 192.168.1.20 test2.qiguo.com test2兩行,再在192.168.1.202的這台主機上運作相同的操作。  

    2.在192.168.1.201中運作ssh-keygen -t rsa,然後使用ssh-copy-id -i root@test2實作ssh互信。這個步驟在兩個機器上都要運作。

    3.在2台server上使用時間同步指令ntpdate 133.100.11.8(可用的ntpserverip位址)

    上面三步完畢以後,就能夠開始安裝heartbeat了。能夠去epel下載下傳heartbeat的安裝包,預設須要下heartbeat-2.1.4-11.el5.i386,heartbeat-gui-2.1.4-11.el5.i386。heartbeat-pils-2.1.4-11.el5.i386,heartbeat-stonith-2.1.4-11.el5.i386 這4個軟體包。可是這4個軟體包依賴于其他兩個軟體包perl-MailTools-1.77-1.el5.noarch,libnet-1.1.6-7.el5.i386。是以首先得把這2個軟體包給裝出來。

使用rpm

-ivh perl-MailTools-1.77-1.el5.noarch的時候會報依賴關系的錯誤,是以用yum --nogpgcheck localinstall perl-MailTools-1.77-1.el5.noarch來安裝。

然後用相同的方式來一起安裝剩餘的幾個包。

注意:這些軟體要在2個server上都要安裝

    安裝完畢heartbeat中。heartbeat預設的配置檔案在/etc/ha.d中。

ha.d中的rc.d都是資源管理相關的腳本,而resource.d中都是資源代理腳本,服務腳本在/etc/ha.d/heartbeat中。

預設裝起後的heartbeat沒有配置檔案,隻是能夠從/usr/share/doc/heartbeat-2.1.4/中把ha.cf,authkeys和haresources三個檔案放在/etc/ha.d中。

這3個配置檔案的作用是:

    authkeys:密鑰檔案,這個檔案的權限必須為600,否則不能啟動heartbeat服務

    ha.cf:heartbeat服務自身的配置檔案

    haresources:資源代理配置檔案

    以下僅僅須要對這3個檔案做下配置就能夠實作我們的http高可用叢集了。

先來看authkeys檔案:

    #auth 1   提供密鑰的認證方式

    #1 crc   循環備援校驗碼認證

    #2 sha1 HI!   sha1算法認證

    #3 md5 Hello!   md5認證

    這裡最好採用sha或者md5認證。crc的性能偏低。假設使用md5認證的配置檔案例如以下: 

    auth 1 # 1代碼使用以下以1開頭的行來作為密鑰認證的條件    

    1 md5 9adc3f50d9bb9e9c795fce0a839aa766

    生成md5的方式僅僅須要在shell指令行中,輸入echo "qiguo" | md5sum就可以

    第二個配置檔案ha.cf裡面的内容非常多。簡介例如以下:

#debugfile   /var/log/ha-debug #是否啟用debug的日志
    logfile   /var/log/ha-log  #日志檔案的存放位置
    #logfacility   local0  #日志的設施,假設啟用了logfile,就不要啟動這個選項
    keepalive   2   #每隔多少時間進行心跳檢測一次
    #deadtime   30   #server經過多少時間後,還沒有檢測到其存在,就覺得其已經掉線
    #warntime   10   #警告時長
    #initdead   120  #一個叢集起來多久。第二個叢集還沒啟動。則覺得叢集不成功
    #udpport    694  #監聽的端口
    #baud     19200  #串行線的發送速率
    bcast    eth0   #以廣播的方式發送心跳檢測(這裡我們使用廣播的方式,直接啟動bcast eth0就可以,這樣的方式在區域網路中機子多的情況下,非常耗費資源)
    #mcast    eth0 255.0.0.1 694 1 0  #以多點傳播的方式發送心跳檢測
    #ucast    eth0 192.168.1.2 #以單點傳播的方式發送心跳檢測
    #auto_failback on  #主節點挂了以後,又恢複了,是否從新跳轉到主節點上,on表示從新跳轉。
    #stonith baytech /etc/ha.d/conf/stonith.baytech  #定義stonith,怎麼隔絕不線上的節點
    #node ken3  #叢集内的節點名稱,每個節點須要使用一個node,而且值必須與uname -n的值同樣
    node test1.qiguo.com
    node test2.qiguo.com
    #ping 10.10.10.254  #指定ping的位址
    ping 192.168.1.1    #網管位址      

    第三個配置檔案haresources檔案是叢集資源配置檔案。

上面提供了非常多的配置例子,拿當中一個的例子配置檔案來說明: #node1  10.0.0.170 Filesystem::/dev/sda1::/data1::ext2

    node1就是主節點的名稱,10.0.0.170就是vip,Filesystem是資源代理(資源代理能夠從/etc/ha.d/resource.d和/etc/init.d/從查找,"::"代表該資源代理的參數)。這裡我們做http高可用,是以配置例如以下:

    test1.qiguo.com IPaddr::192.168.1.210/24/eth0 httpd就可以

    上述三個配置檔案成功後。就把他們拷貝到192.168.1.202這個主機上。複制完畢以後,分别在兩台主機上裝上httpd服務。

裝上的httpd服務一定不能讓他們開機自己主動啟動。

假設所有配置成功以後,能夠關閉httpd服務開始啟動heartbeat服務了。

heartbeat[4825]: 2014/05/11_23:54:35 info: Version 2 support: false
heartbeat[4825]: 2014/05/11_23:54:35 WARN: Logging daemon is disabled --enabling logging daemon is recommended
heartbeat[4825]: 2014/05/11_23:54:35 info: **************************
heartbeat[4825]: 2014/05/11_23:54:35 info: Configuration validated. Starting heartbeat 2.1.4
heartbeat[4826]: 2014/05/11_23:54:35 info: heartbeat: version 2.1.4
heartbeat[4826]: 2014/05/11_23:54:35 info: Heartbeat generation: 1399811242
heartbeat[4826]: 2014/05/11_23:54:35 info: glib: UDP Broadcast heartbeat started on port 694 (694) interface eth0
heartbeat[4826]: 2014/05/11_23:54:35 info: glib: UDP Broadcast heartbeat closed on port 694 interface eth0 - Status: 1
heartbeat[4826]: 2014/05/11_23:54:35 info: glib: ping heartbeat started.
heartbeat[4826]: 2014/05/11_23:54:35 info: G_main_add_TriggerHandler: Added signal manual handler
heartbeat[4826]: 2014/05/11_23:54:35 info: G_main_add_TriggerHandler: Added signal manual handler
heartbeat[4826]: 2014/05/11_23:54:35 info: G_main_add_SignalHandler: Added signal handler for signal 17
heartbeat[4826]: 2014/05/11_23:54:35 info: Local status now set to: 'up'
heartbeat[4826]: 2014/05/11_23:54:36 info: Link test1.qiguo.com:eth0 up.
heartbeat[4826]: 2014/05/11_23:54:36 info: Link 192.168.1.1:192.168.1.1 up.
heartbeat[4826]: 2014/05/11_23:54:36 info: Status update for node 192.168.1.1: status ping
heartbeat[4826]: 2014/05/11_23:54:41 info: Link test2.qiguo.com:eth0 up.
heartbeat[4826]: 2014/05/11_23:54:41 info: Status update for node test2.qiguo.com: status up
harc[4835]:     2014/05/11_23:54:41 info: Running /etc/ha.d/rc.d/status status
heartbeat[4826]: 2014/05/11_23:54:42 info: Comm_now_up(): updating status to active
heartbeat[4826]: 2014/05/11_23:54:42 info: Local status now set to: 'active'
heartbeat[4826]: 2014/05/11_23:54:42 info: Status update for node test2.qiguo.com: status active
harc[4853]:     2014/05/11_23:54:42 info: Running /etc/ha.d/rc.d/status status
heartbeat[4826]: 2014/05/11_23:54:53 info: remote resource transition completed.
heartbeat[4826]: 2014/05/11_23:54:53 info: remote resource transition completed.
heartbeat[4826]: 2014/05/11_23:54:53 info: Initial resource acquisition complete (T_RESOURCES(us))
IPaddr[4907]:   2014/05/11_23:54:53 INFO:  Resource is stopped
heartbeat[4871]: 2014/05/11_23:54:53 info: Local Resource acquisition completed.
harc[4957]:     2014/05/11_23:54:53 info: Running /etc/ha.d/rc.d/ip-request-resp ip-request-resp
ip-request-resp[4957]:  2014/05/11_23:54:53 received ip-request-resp IPaddr::192.168.1.210/24/eth0 OK yes
ResourceManager[4976]:  2014/05/11_23:54:53 info: Acquiring resource group: test1.qiguo.com IPaddr::192.168.1.210/24/eth0 httpd
IPaddr[5002]:   2014/05/11_23:54:53 INFO:  Resource is stopped
ResourceManager[4976]:  2014/05/11_23:54:53 info: Running /etc/ha.d/resource.d/IPaddr 192.168.1.210/24/eth0 start
IPaddr[5097]:   2014/05/11_23:54:53 INFO: Using calculated netmask for 192.168.1.210: 255.255.255.0
IPaddr[5097]:   2014/05/11_23:54:53 INFO: eval ifconfig eth0:0 192.168.1.210 netmask 255.255.255.0 broadcast 192.168.1.255
IPaddr[5068]:   2014/05/11_23:54:53 INFO:  Success
ResourceManager[4976]:  2014/05/11_23:54:53 info: Running /etc/init.d/httpd  start      

   觀察日志,能夠發現高可用的http叢集已經啟動起來了。

如今人為的在test1這台機子上運作shutdown -h now後觀察日志的變化。(也能夠使用heartbeat自帶的hb_standby腳本來切換。預設在/usr/lib/heartbeat檔案夾下)   

heartbeat[11796]: 2014/05/11_20:56:46 info: Received shutdown notice from 'test1.qiguo.com'.
heartbeat[11796]: 2014/05/11_20:56:46 info: Resources being acquired from test1.qiguo.com.
heartbeat[11862]: 2014/05/11_20:56:46 info: acquire local HA resources (standby).
heartbeat[11863]: 2014/05/11_20:56:46 info: No local resources [/usr/share/heartbeat/ResourceManager listkeys test2.qiguo.com] to acquire.
heartbeat[11862]: 2014/05/11_20:56:46 info: local HA resource acquisition completed (standby).
heartbeat[11796]: 2014/05/11_20:56:46 info: Standby resource acquisition done [all].
harc[11888]:    2014/05/11_20:56:46 info: Running /etc/ha.d/rc.d/status status
mach_down[11903]:       2014/05/11_20:56:46 info: Taking over resource group IPaddr::192.168.1.210/24/eth0
ResourceManager[11928]: 2014/05/11_20:56:46 info: Acquiring resource group: test1.qiguo.com IPaddr::192.168.1.210/24/eth0 httpd
IPaddr[11954]:  2014/05/11_20:56:46 INFO:  Resource is stopped
ResourceManager[11928]: 2014/05/11_20:56:46 info: Running /etc/ha.d/resource.d/IPaddr 192.168.1.210/24/eth0 start
IPaddr[12049]:  2014/05/11_20:56:46 INFO: Using calculated netmask for 192.168.1.210: 255.255.255.0
IPaddr[12049]:  2014/05/11_20:56:46 INFO: eval ifconfig eth0:0 192.168.1.210 netmask 255.255.255.0 broadcast 192.168.1.255
IPaddr[12020]:  2014/05/11_20:56:46 INFO:  Success
ResourceManager[11928]: 2014/05/11_20:56:46 info: Running /etc/init.d/httpd  start
mach_down[11903]:       2014/05/11_20:56:46 info: /usr/share/heartbeat/mach_down: nice_failback: foreign resources acquired
mach_down[11903]:       2014/05/11_20:56:46 info: mach_down takeover complete for node test1.qiguo.com.
heartbeat[11796]: 2014/05/11_20:56:46 info: mach_down takeover complete.      

   打開備server上的日志,注意觀察,在備server上,已經将資源所有拿了過去,如今繼續訪問192.168.1.210能夠看到顯示的就是test2這台主機上的内容。當test1從新上線以後,因為我們上面設定了auto_failback的值為on,是以會再次把資源拿回來,這裡就不再放日志檔案了。到這裡一個簡單的高可用httpd服務就已經建立起來了。 

   因為非常多情況下httpd高可用服務還會用到共享檔案的服務,是以有時候須要共享檔案系統。僅僅須要在haresources中多定義一個檔案系統的資源。

繼續閱讀