1. 程式人生 > >使用heartbeat+monit實現主備雙熱備份系統

使用heartbeat+monit實現主備雙熱備份系統

-name ogr nod lena tor 安裝 popu 虛擬 ssi

技術分享技術分享


一、使用背景

項目須要實現主備雙熱自己主動切換的功能,保證系統7*24小時不間斷執行;現已有兩臺雙網卡的IBM的server,為了不再添加成本採購獨立外部存儲設備和雙機熱備軟件。採用了linux下開源的HA軟件進行部署,即heartbeat+monit方式。

1、使用heartbeat來進行心跳監測和資源接管,心跳監測能夠通過網絡鏈路和串口進行,此處使用網絡鏈路。並且支持 冗 余鏈路,它們之間相互發送報文來告訴對方自己當前的狀態,假設在指定的時間內未收到對方發送的報文。那麽就覺得對方失效,這時需啟動資源接管模塊來接管運 行在對方主機上的資源或者服務。

2、使用monit相應用服務進程監控、重新啟動。Monit是一款功能很豐富的進程、文件、文件夾和設備的監測軟件,用於Unix平臺。 它能夠自己主動修復那些已經停止運作的程序,特使適合處理那些因為多種原因導致的軟件錯誤。

3、改進點:因為沒有共享存儲設備來存儲數據和應用服務,須要採用DRBD的方式進行兩臺server間的數據同步,也就是數據鏡像。

因時間緊迫沒有時間研究DRBD這樣的鏡像技術。興許考慮加進來。

4、項目的應用程序分三個獨立的進程:數據採集進程、數據處理進程、數據通信服務進程。將這三個進程做成linux的服務方式進行管理,即:servicemyprocess start這樣的方式

3、系統拓撲圖及表述(如上圖)

兩個server中的eth0網卡用來外部通信,eth1網卡用來心跳檢測。

OSubunt12.04

VIP 虛擬IP192.168.134

主節點:

Eth0IP(外網):192.168.1.132

Eth1IP(內網):192.168.2.2

備節點:

Eth0IP(外網):192.168.1.133

Eth1IP(內網):192.168.2.3

二、安裝monit

1、由於heartbeat僅僅負責心跳和兩臺server通信的功能,可是在自己的服務進程掛掉時,不能實現自己主動重新啟動。

由於對monit比較熟悉,便採用了monit來監控服務進程,和heartbeat結合使用。

事實上也能夠採用其它方式。如mon等。

Sudo apt-getinstall monit

2、編輯monit啟動腳本。位置:/etc/init.d/monit,加入自己的進程(紅色字體部分)其作用是在heartbeat在進行切換的時候,能夠停止當前server上的應用服務。待切換完畢後啟動備用server上的應用服務程序(由於沒有做軟鏡像也沒有共享存儲,應用服務和數據在兩臺server上各有一份。在我的使用場景中能夠這樣做)

………

stop)

log_daemon_msg "Stopping $DESC""$NAME"

if start-stop-daemon --retry TERM/5/KILL/5--oknodo --stop --quiet \

--pidfile $PID --exec$DAEMON

then

log_end_msg 0

else

log_end_msg 1

fi

#################################

#此處加入要監控的服務進程

service DataCollection stop

service DataProcss stop

service RsServer stop

################################

;;

reload)

…………….

3、編輯/etc/monit/monitrc配置文件,網上這方面的資料比較多能夠參考。在當中增加自己須要監控的進程。最後將編輯好的monitrc文件權限改動為700

………………

#須要監控的進程配置

#########Start checkDataCollection##########################

check process DataCollection with pidfile/tmp/kd_data_collection_filename.pid

start program ="/etc/init.d/DataCollection start"

stop program ="/etc/init.d/DataCollection stop"

#########End checkDataCollection##########################

#########Start checkDataProcss##########################

check process DataProcss with pidfile/tmp/kd_data_process_filename.pid

start program ="/etc/init.d/DataProcss start"

stop program ="/etc/init.d/DataProcss stop"

#########End checkDataProcss##########################

#########Start checkRsServer##########################

check process RsServer with pidfile/tmp/kd_data_server_filename.pid

start program = "/etc/init.d/RsServerstart"

stop program = "/etc/init.d/RsServerstop"

#########End checkRsServer##########################

################Startcheck heartbeat######################

check process heartbeat with pidfile/var/run/heartbeat.pid

start program ="/etc/init.d/heartbeat start"

stop program = "/etc/init.d/heartbeatstop"

################Endcheck heartbeat##############################

###############################################################################

## Includes

###############################################################################

##

## It is possibleto include additional configuration parts from other files or

## directories.

#

include /etc/monit/conf.d/*

#.

………………

安裝好的monit通過web方法管理進程截圖:

技術分享

三、安裝heartbeat

Sudo apt-get install heartbeat

編輯以下三個文件:

ha.cf 基本的配置文件。大部分配置信息在該文件裏

haresources 資源配置文件

authkeys 權限配置】

1、 ha.cf配置

logfile /var/log/ha-log

logfacility local0

keepalive 2

deadtime 30

warntime 10

initdead 120

udpport 694

bcast eth1 #Linux

auto_failback on

node ubuntuA

node ubuntuB

ping 192.168.1.1

2、 haresources

…………

ubuntuA192.168.1.134 monit

#node-nameresource1 resource2 ... resourceN

…………

3、 authkeys

auth 1

1 crc

#2 sha1 HI!

#3 md5 Hello!

將上面的三個配置文件分別復制到主、備server上的/etc/ha.d文件夾下,並將authkeys權限改為600

在主、備服server上分別啟動heartbeat,能夠在通過tail -f /var/log/ha-log查看日誌。進行測試









使用heartbeat+monit實現主備雙熱備份系統