Heartbeat 概述
Heartbeat主要由兩個部分組成:
心跳檢測部分和資源接管部分,心跳檢測部分主要是通過互相發送報文資訊來進行檢測對方狀態的,如果長時間未收到對方發送來的報文,則認為對方失效,那么就會開啟資源接管部分,接管對方存在的服務以及資源
Heartbeat環境搭建需要如下組件:
| 軟體包名稱 | 下載地址 |
|---|---|
| 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 |
測驗環境
| 主機 | IP地址 |
|---|---|
| node1 | 192.168.1.10 |
| node2 | 192.168.1.20 |
| node3 | 192.168.1.30 |
node1和node2 都是web服務器,node3為后端NFS服務器
搭建程序如下
所有節點執行以下命令
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# vi /etc/selinux/config
......
SELINUX=disabled
......
[root@localhost ~]# setenforce 0
[root@localhost ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.10 node1
192.168.1.20 node2
192.168.1.30 node3
以上執行完成以后,我們將每一個的節點的名稱進行修改
node3配置NFS服務器
[root@node3 ~]# yum install -y nfs-utils rpcbind //安裝nfs所需的軟體包
[root@node3 ~]# mkdir /share //創建共享目錄
[root@node3 ~]# echo "this is test" >>/share/index.html
[root@node3 ~]# vi /etc/exports //編輯檔案添加共享目錄的相關權限
/share *(rw) //賦予任何網段的主機可讀可寫權限
[root@node3 ~]# exportfs -r //執行這個生效
[root@node3 ~]# systemctl start nfs
[root@node3 ~]# systemctl enable nfs
[root@node3 ~]# systemctl start rpcbind
[root@node3 ~]# systemctl enable rpcbind
[root@node3 ~]# showmount -e //查看共享檔案
Export list for node3:
/share *
node1 和 node2配置(兩個配置一致,這里用node1來展示)
[root@node1 ~]# yum install -y nfs-utils httpd //安裝nfs軟體
[root@node1 ~]# showmount -e 192.168.1.30 //查看nfs服務器共享檔案的情況
Export list for 192.168.1.30:
/share *
[root@node1 ~]# mount -t nfs 192.168.1.30:/share /var/www/html/ //掛載測驗
[root@node1 ~]# df -h //查看掛載
.....
192.168.1.30:/share 17G 1.3G 16G 8% /var/www/html
卸載資源:后期這些資源通過heartbeat直接加載
[root@node1 ~]# umount /var/www/html/
這個時候node1 和 node2 都沒有啟動httpd服務器以及掛載,這一些操作,后面我們可以自動通過heartbeat來實作,不需要手動進行
node1 和 node2 安裝Heartbeat(兩個配置一致)
提前下載好我們需要的軟體包并且上傳到Linux服務器上
[root@node1 opt]# ls
0a7add1d9996.tar.bz2 958e11be8686.tar.bz2 resource-agents-3.9.6.tar.gz
[root@node1 opt]# yum install -y bzip2 bzip2-devel gcc gcc-c++ autoconf automake libtool e2fsprogs-devel glib2-devel libxml2 libxml2-devel libtool-ltdl-devel asciidoc libuuid-devel docbook //解決依賴關系
編譯安裝Cluster Glue
[root@node1 opt]# groupadd haclient //創建組
[root@node1 opt]# useradd -g haclient hacluster //創建用戶并加入到該組
[root@node1 opt]# tar -xf 0a7add1d9996.tar.bz2 //解壓
[root@node1 opt]# cd Reusable-Cluster-Components-glue--0a7add1d9996/
[root@node1 Reusable-Cluster-Components-glue--0a7add1d9996]# ./autogen.sh
[root@node1 Reusable-Cluster-Components-glue--0a7add1d9996]# ./configure --prefix=/usr/local/heartbeat/
[root@node1 Reusable-Cluster-Components-glue--0a7add1d9996]# make && make install
編譯安裝Resource Agents
[root@node1 opt]# tar -xf resource-agents-3.9.6.tar.gz
[root@node1 opt]# cd resource-agents-3.9.6
[root@node1 resource-agents-3.9.6]# ./autogen.sh
[root@node1 resource-agents-3.9.6]# ./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
[root@node1 resource-agents-3.9.6]# make && make install
編譯安裝Heartbeat
[root@node1 opt]# tar -xf 958e11be8686.tar.bz2
[root@node1 opt]# cd Heartbeat-3-0-958e11be8686/
[root@node1 Heartbeat-3-0-958e11be8686]# ./bootstrap
[root@node1 Heartbeat-3-0-958e11be8686]# export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"
[root@node1 Heartbeat-3-0-958e11be8686]# ./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
[root@node1 Heartbeat-3-0-958e11be8686]# make && make install
注node2配置到這里就可以了
node1生成相關檔案
生成組態檔
heartbeat主要由三個檔案組成
ha.cf: 主組態檔
haresource: 用來配置要讓Heartbeat托管的服務
authkey: 是用來指定Heartbeat的認證方式
[root@node1 opt]# cd Heartbeat-3-0-958e11be8686/
[root@node1 Heartbeat-3-0-958e11be8686]# cp doc/{ha.cf,haresources,authkeys} /usr/local/heartbeat/etc/ha.d/
[root@node1 ~]# chkconfig --add heartbeat
[root@node1 ~]# chkconfig heartbeat on //設定為開機自動啟動
[root@node1 ~]# chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys //權限必須為600否則啟動不起
[root@node1 ~]# mkdir -pv /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/
[root@node1 ~]# cp /usr/lib/ocf/lib/heartbeat/ocf-* /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/
[root@node1 ~]# ln -sv /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/ //不設定可能會報ping ucast 的錯誤
node1的配置(主要修改三大組態檔)
編輯 ha.cf 檔案
[root@node1 ~]# vi /usr/local/heartbeat/etc/ha.d/ha.cf
確保以下的代碼存在且無注釋
24 debugfile /var/log/ha-debug //調式資訊的存放地
29 logfile /var/log/ha-log //日志檔案存放地
34 logfacility local0
48 keepalive 2 //心跳檢測的時間為2秒,每2秒發送報文資訊
56 deadtime 30 //備用節點在30秒為收到主節點資訊,則啟動資源接管部分
61 warntime 10 //心跳延遲時間
71 initdead 60 //系統啟動或重啟后預留的忽略時間段,取值至少為deadtime的兩倍
76 udpport 694 //偵聽的埠號
121 ucast ens33 192.168.1.20 //采用ens33網卡單播與對應的IP地址進行通信
157 auto_failback on //定義當主節點恢復后,是否將服務自動切回
211 node node1 //節點的名稱 需和使用unmae -n 的一致
212 node node2 //節點的名稱
220 ping 192.168.1.1 //主要監測網路的狀態
253 respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail
259 apiauth ipfail gid=haclient uid=hacluster
編輯haresources資源檔案
[root@node1 ~]# vi /usr/local/heartbeat/etc/ha.d/haresources
node1 IPaddr::192.168.1.150/24/ens33 Filesystem::192.168.1.30:/share::/var/www/html::nfs httpd
node1:代表主機名稱,node2的這個檔案和這里一致
IPaddr::192.168.1.150/24/ens33 :系結VIP地址到指定的網卡上
Filesystem::192.168.1.30:/share::/var/www/html::nfs :指定掛載的路徑
httpd :表示啟動httpd這個腳本檔案(這個檔案主要用于啟動httpd服務器的)
httpd這個檔案一定要存在于這個目錄下才可:
/usr/local/heartbeat/etc/ha.d/resource.d
編輯authkeys心跳驗證檔案
[root@node1 ~]# vi /usr/local/heartbeat/etc/ha.d/authkeys
auth 1
1 md5 hello!
注:auth 后面的序號可以任意寫,不過換行了之后,最開始寫的序號需和auth后面的一致,然后就是采用的演算法了
演算法的分類:crc md5 sha1
演算法后面跟的就是密碼了
撰寫httpd啟動腳本
[root@node1 ~]# vi /usr/local/heartbeat/etc/ha.d/resource.d/httpd
#!/bin/bash
/bin/systemctl $1 httpd
[root@node1 ~]# chmod 755 !$ //賦予執行權限
chmod 755 /usr/local/heartbeat/etc/ha.d/resource.d/httpd
以上配置完成以后,我們將配置的檔案都發送node2,然后在再node2上進行修改即可
[root@node1 ~]# scp -rp /usr/local/heartbeat/etc/ha.d/* root@node2:/usr/local/heartbeat/etc/ha.d/
node2的配置
[root@node2 ~]# ln -sv /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/
[root@node2 ~]# chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys
[root@node2 ~]# vi /usr/local/heartbeat/etc/ha.d/ha.cf
121 ucast ens33 192.168.1.10 //修改為node1 的IP地址
node1 和node2 啟動heartbeat服務
在啟動之前先下載這個兩個軟體包:yum install -y net-tools psmisc
node1 啟動
[root@node1 ~]# /etc/init.d/heartbeat start
Starting heartbeat (via systemctl): [ OK ]
[root@node1 ~]# netstat -anlupt | grep 694
udp 0 0 0.0.0.0:694 0.0.0.0:* 108303/heartbeat: w
node2啟動
[root@node2 ~]# /etc/init.d/heartbeat start
Starting heartbeat (via systemctl): [ OK ]
[root@node2 ~]# netstat -anlupt |grep 694
udp 0 0 0.0.0.0:694 0.0.0.0:* 96290/heartbeat: wr
默認狀態下,node1 節點會自動生成VIP地址以及自動進行掛載
[root@node1 ~]# ifconfig
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.150 netmask 255.255.255.0 broadcast 192.168.1.255
ether 00:0c:29:d8:b0:c3 txqueuelen 1000 (Ethernet)
[root@node1 ~]# df -h
192.168.1.30:/share 17G 1.3G 16G 8% /var/www/html
[root@node1 ~]# ss -tan |grep 80
LISTEN 0 128 [::]:80 [::]:*
node2 查看什么也沒有
[root@node2 ~]# ifconfig
[root@node2 ~]# df -h
[root@node2 ~]# ss -tan | grep 80
瀏覽器訪問測驗
默認請求會交給node1去處理

故障檢驗
關掉node1節點的網卡,等待30秒去node2節點查看,然后瀏覽器訪問看能否訪問成功
[root@node1 ~]# ifdown ens33 //關掉網卡
[root@node2 ~]# ifconfig
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.150 netmask 255.255.255.0 broadcast 192.168.1.255
ether 00:0c:29:bd:6e:43 txqueuelen 1000 (Ethernet)
[root@node2 ~]# df -h
......
192.168.1.30:/share 17G 1.3G 16G 8% /var/www/html
[root@node2 ~]# ss -tan | grep 80
LISTEN 0 128 [::]:80 [::]:*
瀏覽器訪問網頁測驗

現在我們把node1的網卡啟動,看看主節點的資源會不會歸為
[root@node2 ~]# ifup ens33 //開啟網卡,稍微等待30秒左右
[root@node1 ~]# ip a //查看VIP地址是否生成
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:d8:b0:c3 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.1.150/24 brd 192.168.1.255 scope global secondary ens33:2
[root@node1 ~]# df -h //查看掛載情況
......
192.168.1.30:/share 17G 1.3G 16G 8% /var/www/html
[root@node1 ~]# netstat -anlupt | grep httpd //查看httpd服務器是否啟動
tcp6 0 0 :::80 :::* LISTEN 1525/httpd
使用curl來訪問測驗(在NFS服務器上進行)
[root@node3 ~]# curl 192.168.1.150
this is test
[root@node3 ~]# curl 192.168.1.150
this is test
整個實驗搭建完成,高可用性達到
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/225934.html
標籤:其他
下一篇:不講武德的LVM,耗子尾汁
