CentOS7 Heartbeat

本文轉載於,https://www.linuxidc.com/Linux/2019-02/157123.htm

heartbeat項目是 Linux-HA 工程的一個組成部分,它實現了一個高可用集羣系統。心跳服務和集羣通信是高可用集羣的兩個關鍵組件,在 Heartbeat 項目裏,由 Heartbeat 模塊實現了這兩個功能。

一、環境說明

#uname -r
3.10.0-862.9.1.el7.x86_64
#cat /etc/RedHat-release 
CentOS Linux release 7.5.1804 (Core)

雙節點互備集羣:

節點1:node1 node1.pjy.com 192.168.146.141
節點2:node2 node2.pjy.com 192.168.146.150
VIP地址:192.168.146.222

二、前提條件準備

在每個節點上做以下配置:

  • 1.關閉firewalld
  • 2.關閉selinux
  • 3.同步時間
  • 4.配置主機名
  • 5.配置主機間ssh互信,免密鑰認證

以下配置是在節點1(141)主機上的配置,節點2(150)省略:

systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i s#SELINUX=enforcing#SELINUX=disabled# /etc/selinux/config
ntpdate 10.0.0.100
hostname node1.pjy.com
echo "node1.pjy.com" >> /etc/hostname
ssh-keygen -q -t rsa -N '' -f ~/.ssh/id_rsa
ssh-copy-id [email protected]
echo -e "192.168.146.141 node1.pjy.com node1\n192.168.146.150 node2.pjy.com node2" >> /etc/hosts

三、開始安裝

1.安裝基礎環境包:

yum install gcc gcc-c++ autoconf automake libtool glib2-devel libxml2-devel bzip2 bzip2-devel e2fsprogs-devel libxslt-devel libtool-ltdl-devel asciidoc

2.創建用戶和組

groupadd haclient
useradd -g haclient hacluster

3.下載軟件包:Reusable-Components-glue、resource-agents、heartbeat

http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/958e11be8686.tar.bz2
http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2
https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz

4.安裝glue

tar xf 0a7add1d9996.tar.bz2
cd Reusable-Cluster-Components-glue--0a7add1d9996/
./autogen.sh
./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
make && make install
echo $?
cd ..

5.安裝Resource Agents

tar xf v3.9.6.tar.gz
cd resource-agents-3.9.6/
./autogen.sh 
./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
make && make install
echo $?
cd ..

6.安裝HeartBeat

tar xf 958e11be8686.tar.bz2
cd Heartbeat-3-0-958e11be8686/
./bootstrap
export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"
./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
make && make install
echo $?

7.配置網卡支持插件文件

mkdir -pv /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/
cp /usr/lib/ocf/lib/heartbeat/ocf-* /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/

#注意:一般啓動時會報錯因爲 ping和ucast這些配置都需要插件支持 需要將lib64下面的插件軟連接到lib目錄 纔不會拋出異常

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/

#以上在節點1上安裝完成,在節點2上執行以上同樣的步驟,此處省略...

#下面開始配置

四、配置heartbeat

#拷貝三個模版配置文件到 /usr/local/heartbeat/etc/ha.d 目錄下

cp doc/{ha.cf,haresources,authkeys} /usr/local/heartbeat/etc/ha.d/

1.配置ha.cf配置文件

#該配置文件用於配置 心跳的核心配置

vim /usr/local/heartbeat/etc/ha.d/ha.cf

debugfile /var/log/ha-debug  #表示調試的日誌文件 一般測試建議開啓
logfile /var/log/ha-log  #表示系統的的日誌文件路徑
logfacility    local0  #表示使用系統日誌與上面只能開啓一個
keepalive 2  #主備之間的心跳間隔時間單位:s
deadtime 30  #表示如果連接對方30s還無法連接,表示節點死亡需要考慮vip轉移
warntime 10  #表示10s時間未收到心跳時發出警告日誌
initdead 120  #有時機器啓動後需要一段時間網卡才能正常工作 需要預留一定的時間後,再開始判斷心跳檢測
udpport 694  #多播的udp端口
#baud  19200  #串行端口的波特率
#serial /dev/ttyS0      # Linux  #串口的接口名
#serial /dev/cuaa0      # FreeBSD
#serial /dev/cuad0      # FreeBSD 6.x
#serial /dev/cua/a      # Solaris
#bcast  eth0            # Linux #傳播心跳的廣播網卡信息
#bcast  eth1 eth2      # Linux
#bcast  le0            # Solaris
#bcast  le1 le2        # Solaris
#mcast eth0 225.0.0.1 694 1 0  #多播傳送心跳的網卡 多播組 端口 躍點數 是否迴環內傳送
ucast ens33 192.168.146.150 #設置單播心跳,設置對方的ip地址,此處使用單播
auto_failback on  #表示如果主機停止後,從機接管設置爲on當主機從新啓動後,主機立即接管vip off從機不會釋放vip給主機
node    node1.pjy.com  #配置主從的節點信息,要與uname -n保持一致
node    node2.pjy.com
#############################################
#使用ping模式 有時當主機掛掉或者heartbeat掛掉後vip纔會轉移  有時出現某個進程掛掉 切換需要使用腳本 
#ping模式用於測試 如果網卡ping不同 某個主機 就認爲當前斷網 需要轉移vip 
#respawn root    /usr/local/heartbeat/libexec/heartbeat/ipfail 表示當ping不通時 自動調用 ipfail這個腳本
#apiauth ipfail gid=haclient uid=hacluster 表示有權限操作ipfail腳本的組和用戶
############################################
ping 192.168.146.2
#ping組的所有主機
#ping_group group1 10.10.10.254 10.10.10.253
#respawn userid /path/name/to/run
#指定與heartbeat一同啓動和關閉的進程,該進程被自動監視,遇到故障則重新啓動。最常用的進程是ipfail,該進程用於檢測和處理網絡故障,需要配合ping語句指定的ping node來檢測網絡連接。如果你的系統是64bit,請注意該文件的路徑。
#respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail
#apiauth ipfail gid=haclient uid=hacluster

2.配置authkeys配置文件

#該文件表示發送心跳時 機器用於驗證的key的hash算法,節點之間必須配置成一致的密碼

vim /usr/local/heartbeat/etc/ha.d/authkeys

auth 2 #表示使用id爲2的驗證 下邊需要定義一個2的驗證算法
2 sha1 1a2b3c #ID 2的驗證加密爲shal,並添加密碼

#更改權限爲600

chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys

3.配置haresources配置文件

#該文件表示資源的管理,如果是主機,當主機啓動後自動加載該文件中配置的所有啓動資源,資源腳本默認在haresources同級目錄下的resource.d目錄下

vim /usr/local/heartbeat/etc/ha.d/haresources
#指定節點主機名,和VIP地址,以雙冒號分隔資源,此處以apache爲例進行配置
node1.pjy.com 192.168.146.222 apache::/etc/httpd/conf/httpd.conf

4.節點2上準備配置文件

#拷貝三個配置好的文件到節點2上,只需修改ha.cf配置文件中的單播地址爲對方地址即可(ucast ens33 192.168.146.141)。

scp authkeys ha.cf haresources root@node2:/usr/local/heartbeat/etc/ha.d/

5.安裝httpd資源服務

#在每個節點上安裝httpd服務並測試

yum install httpd
echo "<h1>node1.pjy.com</h1>" >>/var/www/html/index.html
systemctl start httpd
curl 192.168.146.141

#測試httpd服務正常後關閉httpd服務並關閉自啓動

systemctl stop httpd
systemctl disable httpd

6.啓動服務

#啓動每個節點上heartbeat服務

systemctl enable heartbeat
systemctl start heartbeat
ssh node2 'systemctl start heartbeat'

7.測試結果

 curl http://192.168.146.222
<h1>node1.pjy.com</h1>
#使用heartbeat自帶腳本切換主備節點
#/usr/local/heartbeat/share/heartbeat/hb_standby
Going standby [all].
curl http://192.168.146.222
<h1>node2.pjy.com</h1>

8.配置NFS共享存儲

#在建立NFS的服務器上建立NFS存儲:

yum install nfs-utils rpcbind
systemctl start rpcbind
systemctl start nfs
cat /etc/exports  #建立NFS共享存儲
/data 192.168.146.0/24(ro)
mkdir /data  #建立測試文件
echo '<h1>nfs server</h1>' > /data/index.html

#然後修改haresources配置文件添加自動掛載nfs資源:

node1.pjy.com IPaddr::192.168.146.222/24/ens33 Filesystem::192.168.146.151:/data::/var/www/html::nfs::ro apache::/etc/httpd/conf/httpd.conf

#重啓heartbeat服務加載配置

systemctl restart heartbeat
ssh node2 'systemctl restart heartbeat'

#測試是否加載資源:

ip addr
netstat -lntup
mount

#資源轉移測試:

/usr/local/heartbeat/share/heartbeat/hb_standby

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章