1. 程式人生 > 實用技巧 >Heartbeat原理及部署

Heartbeat原理及部署

  參考:https://blog.csdn.net/wzy0623/article/details/81188814

    http://www.mamicode.com/info-detail-1494631.html

    https://www.jianshu.com/p/12c4b3e43b65

  一,Heartbeat簡介

  通過Heartbeat,可以將資源(ip以及程式服務等資源)從一臺已經故障的計算機快速轉移到另一臺正常運轉的機器上繼續提供服務,一般稱之為高可用服務。官方網址:http://www.linux-ha.org/wiki/Heartbeat
通過修改Heartbeat的配置檔案,可以指定一臺Heartbeat伺服器作為主伺服器,另一臺自動成為熱備伺服器。在熱備伺服器上面配置Heartbeat守護程式來監聽來自主伺服器的心跳資訊。如果在規定時間內,無法監聽到心跳資訊,那麼就啟動故障轉移,取得主伺服器上的相關資源的所有權,接替主伺服器繼續不間斷的提供服務,從而達到資源以及服務高可用的目的。一般故障切換時間在5~20s之間。Heartbeat還支援主主模式,及兩臺伺服器互為主備。

另外,和Keepalived的服務一樣,Heartbeat高可用是伺服器級別的,不是服務級別的。它可以監測以下系統問題:
  伺服器宕機
  Heartbeat軟體故障
  心跳連線線故障
  服務故障不會導致切換,但可以通過服務宕機把Heartbeat服務停掉,從而主機實現自動切換。至少需要兩臺主機才可以實現高可用服務。兩臺Heartbeat服務之間可以通過以下方式進行通訊:
  穿行電纜,所謂的串列埠(首選,缺點是距離不能太遠)
  一根乙太網電纜兩網絡卡直連(常用方式)
  乙太網電纜,通過交換機等網路裝置連線。(交換機可能出現問題等)
Heartbeat通過ip地址接管和arp廣播進行故障轉移的。在主伺服器故障時,備用節點接管資源後,會立即強制更新所有客戶端本地的arp表,即清楚客戶端本地快取的故障伺服器的vip和mac地址的解析記錄,確保客戶端和新的主伺服器的對話。
真實ip,又被稱為管理ip,一般指配置在物理網絡卡上面的ip。在負載均衡高可用環境中,管理ip是不對外提供訪問服務的,僅作為管理伺服器使用,如SSH可以通過這個進行服務連線管理。VIP是虛擬ip,實際上就是eth0:X,x為0~255的任意數字,可以在一個網絡卡上面繫結多個別名。VIP當主伺服器故障時,可以自動漂移到備用伺服器。
  

  二,安裝Heartbeat

  環境檢視

  伺服器規劃

IP 用途 主機名
192.168.1.101 主伺服器 hdp3
192.168.1.102 備伺服器 hdp4

  初始配置

  禁用防火牆

systemctl disable firewalld

  關閉senlinux

  設定主機名並設定hosts檔案 

  兩臺主機名不一樣,hosts檔案一樣 

# cat /etc/hosts
192.168.1.101 hdp3
192.168.1.102 hdp4

  安裝依賴包

yum -y install glib2-devel libtool-ltdl-devl net-snmp-devel bzip2-devel ncurses-devel openssl-devel libtool libxml2 libxml2-devel gettext bison flex zlib-devel mailx which libxslt docbook-dtds docbook-style-xsl PyXML shadow-utils opensp autoconf automake bzip2 e2fsprogs-devel libxslt-devel libtool-ltdl-devel make asciidoc libuuid-devel asciidoc

  下載安裝包

  Heartbeat 3.0.6:http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/958e11be8686.tar.bz2
  Cluster Glue 1.0.12:http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2
  Resource Agents 3.9.6:https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz

  新增 haclient 組和 hacluster 賬戶,這個使用者主要用來配置respawn的。

groupadd haclient
useradd -g haclient -M -s /sbin/nologin hacluster

  安裝Cluster Glue 1.0.12

tar -xf 0a7add1d9996.tar.bz2 
cd Reusable-Cluster-Components-glue--0a7add1d9996/
./autogen.sh
./configure --prefix=/usr/local/heartbeat  LIBS='/lib64/libuuid.so.1'
make && make install

  安裝Resource Agents

tar -xf v3.9.6.tar.gz
cd resource-agents-3.9.6/
./autogen.sh
./configure --prefix=/usr/local/heartbeat  
make && make install

  安裝heartbeat-3-0-6

tar -xf 958e11be8686.tar.bz2
cd Heartbeat-3-0-958e11be8686/
#設定,否則安裝會報錯
export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"
#刪除配置檔案的最後一行
sed -i '/^#define HA_HBCONF_DIR /d' /usr/local/heartbeat/include/heartbeat/glue_config.h
./bootstrap 
./configure --prefix=/usr/local/heartbeat  LIBS='/lib64/libuuid.so.1'
make && make install

  拷貝配置檔案

cd /usr/local/heartbeat/share/doc/heartbeat
cp authkeys ha.cf haresources /usr/local/  heartbeat/etc/ha.d/

  設定配置檔案許可權及軟連線

cd /usr/local/heartbeat/etc/ha.d
chmod 600 authkeys
ln -svf /usr/local/heartbeat/lib64/heartbeat/plugins/RAExec/* /usr/local/heartbeat/lib/heartbeat/plugins/RAExec/
ln -svf /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/

  編輯配置檔案

  192.168.1.101上的ha.cf檔案內容如下

# cat /usr/local/heartbeat/etc/ha.d/ha.cf 
# 儲存除錯資訊檔案
debugfile /var/log/ha-debug    
# 日誌檔案
logfile /var/log/ha-log
# 表示使用系統日誌      
logfacility local0
# 心跳的時間間隔,單位秒        
keepalive 1            
# 超出該時間間隔未收到對方節點的心跳,則判定對方死亡
deadtime 30 
# 超出該時間間隔未收到對方節點的心跳,則發出警告記錄到日誌中           
warntime 10
# 在某系統上,系統啟動或重啟之後需要經過一段時間網路才能正常工作,該選項用於解決這種情況產生的時間間隔,取值至少為deadtime的2倍            
initdead 120 
# 設定廣播通訊使用的埠,694為預設使用的埠號             
udpport 694            
# 傳播心跳的廣播播網絡卡資訊
bcast eth0   
# 設定對方機器心跳檢測的IP
# 第二列為網絡卡名 第三列為心跳IP,生產中心跳需要與資料網路分開         
ucast eth0 192.168.1.102
# 在該選項設為on的情況下,一旦主節點恢復執行,則自動獲取資源並取代從節點    
auto_failback off       
# 配置主從的節點資訊
node hdp3             
node hdp4     
# 如果ping不通該地址,就認為當前斷網,需要轉移vip
ping 192.168.1.9         
# 指定與heartbeat一同啟動和關閉的程序,該程序被自動監視,遇到故障則重新啟動。最常用的程序是ipfail,該程序用於檢測和處理網路故障,需要配合ping語句指定的ping node來檢測網路連線。如果你的系統是64bit,請注意該檔案的路徑
respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail 
# 指定使用者和組
apiauth ipfail gid=haclient uid=hacluster

  注意:如果複製配置檔案可能導致無法啟動heartbeat,可能是因為字元編碼或者空格等原因,可以直接在原配置檔案修改

  192.168.1.102除了以下配置不一樣其他一致

# 設定對方機器心跳檢測的IP
# 第二列為網絡卡名 第三列為心跳IP,生產中心跳需要與資料網路分開         
ucast eth0 192.168.1.101

  編輯認證檔案authkeys

  開啟2認證,認證祕鑰為HI!生產中使用複雜祕鑰

# sed '/#/d' authkeys 
auth 2
2 sha1 HI!

  編輯資源配置檔案

  192.168.1.101配置檔案

#cat haresources 
hdp3 IPaddr::192.168.1.100/24/eth0

  解析:hdp3代表主機名,192.168.1.100為VIP 虛擬在網絡卡eth0上

  192.168.1.101配置檔案

# cat haresources 
hdp4 IPaddr::192.168.1.100/24/eth0

  啟動heartbeat

 systemctl start heartbeat

  主伺服器上檢視日誌

  備主機沒有虛擬VIP

  停止主伺服器的heartbeat

systemctl stop heartbeat

  主伺服器檢視ha日誌

  從伺服器檢視ha日誌

  再次啟動主伺服器的heartbeat

  檢視從伺服器日誌知道hdp3已經觸及active狀態

  從伺服器VIP還保留,除非從伺服器heartbeat停止主heartbeat應用正常則主會接收生成VIP

  三,使用heartbeat實現web高可用

  以上只實現的VIP漂移,下面通過heartbeat實現web高可用

  兩臺主機安裝apache

yum -y install httpd

  找到httpd啟動程式並負責到heartbeat管理目錄

# which  httpd
/usr/sbin/httpd
cp /usr/sbin/httpd /usr/local/heartbeat/etc/ha.d/resource.d/

  使用heartbeat需要滿足以下條件

1)httpd命令需要放在/etc/init.d/下或者/etc/ha.d/resource.d下

2)httpd命令需要具備可執行許可權(x)

3)httpd必須支援如下啟動方式

/etc/init.d/httpd

Usage: httpd {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}

  檢視httpd幫助可以知道httpd可以使用命令啟動,需要加引數-k

  

httpd -k start

  修改haresources

  兩臺伺服器配置一致

# cat haresources 
hdp3 IPaddr::192.168.1.100/24/eth0 httpd::-k

  重啟heartbeat檢視日誌

systemctl restart heartbeat

  主伺服器

  注意:使用heartbeat可以管理應用資源,應用無需使用systemctl enable設定成開機自啟動

  高可用測試,為了區分,在兩臺伺服器寫入不同的首頁

#主伺服器
echo hdp3>/var/www/html/index.html
#備伺服器
echo hdp4>/var/www/html/index.html

  停止主伺服器的heatbeat模擬中斷

  備伺服器檢視日誌

  web頁面使用VIP訪問變成備伺服器的首頁了

  生產中主備伺服器後端內容是一致的,主伺服器宕機對於使用者來說無感知