操作系統的版本爲Centos7.7,做HA需要用到三個軟件包
軟件名 | 版本 |
Heartbeat | 3.0.6 |
Cluster Glue | 1.0.12 |
Resource Agents | 3.9.6 |
配置步驟
- 配置兩臺服務器的IP地址
- 配置兩臺服務器的計算機名和hosts文件
- 獲取三個rpm包並編譯安裝
- 僅安裝並啓動apache服務
- 配置Heartbeat並測試
IP地址配置
STEP1:server1地址配置
ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.5.101 netmask 255.255.255.0 broadcast 192.168.5.255
inet6 fe80::20c:29ff:fe8c:5445 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:8c:54:45 txqueuelen 1000 (Ethernet)
RX packets 3090 bytes 251941 (246.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4541 bytes 1524995 (1.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
STEP2:server2地址配置
ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.5.102 netmask 255.255.255.0 broadcast 192.168.5.255
inet6 fe80::20c:29ff:fe87:6fd7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:87:6f:d7 txqueuelen 1000 (Ethernet)
RX packets 2664 bytes 207443 (202.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4273 bytes 1487525 (1.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
修改hosts文件與計算機名
STEP1:server1計算機名配置
echo "server1" > /etc/hostname
STEP2:server1配置hosts文件
echo -e '192.168.5.101 server1\n192.168.5.102 server2' >> /etc/hosts
STEP3:server2計算機名配置
echo "server1" > /etc/hostname
STEP4:server2配置hosts文件
echo -e '192.168.5.101 server1\n192.168.5.102 server2' >> /etc/hosts
獲取RPM包並編譯安裝
STEP1:下載軟件包,如果下載有302錯誤就使用windows下載,完成後在上傳到Linux
wget -O ~/Heartbeat-3.0.6 https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz
wget -O ~/ClusterGlue-1.0.12 http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2
wget -O ~/ResourceAgents-3.9.6 http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/958e11be8686.tar.bz2
STEP2:安裝基礎環境
yum install -y gcc gcc-c++ autoconf automake \
libtool glib2-devel libxml2-devel bzip2 bzip2-devel \
e2fsprogs-devel libxslt-devel libtool-ltdl-devel asciidoc
STEP3:解壓軟件包,並將解壓出的目錄改爲方便看的名字
tar -xf Heartbeat-3-0-958e11be8686.bz2
tar -xf resource-agents-3.9.6.tar.gz
tar -xf Reusable-Cluster-Components-glue--0a7add1d9996.bz2
mv Heartbeat-3-0-958e11be8686/ Heartbeat
mv resource-agents-3.9.6/ resource-agents
mv Reusable-Cluster-Components-glue--0a7add1d9996/ Reusable-Cluster
rm -rf Reusable-Cluster-Components-glue--0a7add1d9996.bz2
rm -rf Heartbeat-3-0-958e11be8686.bz2
rm -rf resource-agents-3.9.6.tar.gz
STEP4:創建組並將用戶歸納
groupadd haclient
useradd -g haclient hacluster
STEP5:編譯安裝Reusabli-Cluster
- 進入家目錄下的Reusable-Cluster目錄
cd ~/Reusable-Cluster/
- 執行Reusable-Cluster目錄下的autogen.sh腳本
./autogen.sh
lib/stonith/Makefile.am:22: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
logd/Makefile.am:23: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
lrm/admin/Makefile.am:36: warning: '%'-style pattern rules are a GNU make extension
lrm/admin/Makefile.am:21: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
lrm/lrmd/Makefile.am:21: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
lrm/test/Makefile.am:23: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
replace/Makefile.am:20: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
autoconf
Now run ./configure
- 看到“Now run ./configure”後,執行configure腳本,後面的參數別忘了
./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
cluster-glue configuration:
Version = 1.0.12 (Build: unknown)
Features =
Prefix = /usr/local/heartbeat
Executables = /usr/local/heartbeat/sbin
Man pages = /usr/local/heartbeat/share/man
Libraries = /usr/local/heartbeat/lib
Header files = /usr/local/heartbeat/include
Arch-independent files = /usr/local/heartbeat/share
Documentation = /usr/local/heartbeat/share/doc/cluster-glue
State information = /usr/local/heartbeat/var
System configuration = /usr/local/heartbeat/etc
Use system LTDL = yes
HA group name = haclient
HA user name = hacluster
- 進行編譯並安裝
make && make install
- 使用echo命令查看執行結果,爲零正常
echo $?
0
STEP6:編譯安裝resource-agents
- 進入家目錄下的resource-agents目錄
cd ~/resource-agents/
- 執行Reusable-Cluster目錄下的autogen.sh腳本
./autogen.sh
autoreconf: Entering directory `.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal
autoreconf: configure.ac: tracing
autoreconf: configure.ac: not using Libtool
autoreconf: running: /usr/bin/autoconf
autoreconf: running: /usr/bin/autoheader
autoreconf: running: automake --add-missing --copy --no-force
configure.ac:102: installing './compile'
configure.ac:33: installing './config.guess'
configure.ac:33: installing './config.sub'
configure.ac:83: installing './install-sh'
configure.ac:83: installing './missing'
heartbeat/Makefile.am: installing './depcomp'
autoreconf: Leaving directory `.'
Now run ./configure and make
- 看到“Now run ./configure”後,執行configure腳本
./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
resource-agents configuration:
Version = UNKNOWN
Build Version = 02beac55c1da0ad99a5a19bd3b2333bcff7e916c
Features =
Prefix = /usr/local/heartbeat
Executables = /usr/local/heartbeat/sbin
Man pages = /usr/local/heartbeat/share/man
Libraries = /usr/local/heartbeat/lib
Header files = /usr/local/heartbeat/include
Arch-independent files = /usr/local/heartbeat/share
Documentation = /usr/local/heartbeat/share/doc/resource-agents
State information = /usr/local/heartbeat/var
System configuration = /usr/local/heartbeat/etc
HA_BIN directory prefix = /usr/local/heartbeat/libexec
RA state files = /usr/local/heartbeat/var/run/resource-agents
AIS Plugins =
- 編譯並安裝
make && make install
- 使用echo命令查看執行結果,爲零正常
echo $?
0
STEP7:編譯安裝Heartbeat
- 進入家目錄下的Heartbeat目錄
cd ~/Heartbeat/
- 執行Heartbeat目錄下的bootstrap腳本
./bootstrap
configure.ac:260: installing './compile'
configure.ac:15: installing './config.guess'
configure.ac:15: installing './config.sub'
configure.ac:44: installing './install-sh'
configure.ac:44: installing './missing'
buildtools/Makefile.am: installing './depcomp'
cts/Makefile.am:24: installing './py-compile'
autoconf
Now run ./configure.
- 導入環境變量(必須,否則在下一步執行configure腳本時會報錯)
export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"
- 執行configure腳本
./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
heartbeat configuration:
Version = "3.0.6"
Executables = "/usr/local/heartbeat/sbin"
Man pages = "/usr/local/heartbeat/share/man"
Libraries = "/usr/local/heartbeat/lib64"
Header files = "/usr/local/heartbeat/include"
Arch-independent files = "/usr/local/heartbeat/share"
Documentation files = "/usr/local/heartbeat/share/doc/heartbeat"
State information = "/usr/local/heartbeat/var"
System configuration = "/usr/local/heartbeat/etc"
Init (rc) scripts = "/etc/rc.d/init.d"
Init (rc) defaults = "/etc/sysconfig"
Use system LTDL = "yes"
HA group name = "haclient"
HA group id = "1000"
HA user name = "hacluster"
HA user user id = "1000"
Build dopd plugin = "yes"
Enable times kludge = "yes"
- 編譯並安裝
make && make install
- 使用echo命令查看執行結果
echo $?
0
STEP7:配置網卡支持文件
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/
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/
安裝Apache
STEP1:兩臺服務器安裝Apache
yum install -y httpd
STEP2:啓動apache並設置爲開機自啓動
systemctl start httpd
systemctl enable httpd
STEP3:server1配置
echo "This is server1" > /var/www/html/index.html
STEP4:server2配置
echo "This is server2" > /var/www/html/index.html
開始配置Heartbeat
STEP1:將三個配置文件的模版,複製到Heartbeat的主目錄下
cp ~/Heartbeat/doc/{authkeys,haresources,ha.cf} /usr/local/heartbeat/etc/ha.d/
STEP2:編輯ha.cf配置文件
debugfile /var/log/ha-debug #調試的日誌文件,一般測試建議開啓
logfile /var/log/ha-log #表示系統的的日誌文件所在路徑
logfacility local0 #利用系統的日誌系統打印日誌
keepalive 2 #心跳時間間隔爲2秒,既每兩秒發送一次廣播
deadtime 30 #在30秒內未收到對方的心跳信號,嘖立即切換服務
warntime 10 #如果10秒內備份機聯繫不上主機則寫入警告日誌
initdead 120 #在某些情況下,重啓網絡後需一定時間後才能工作,等待系統初始化
ucast ens33 192.168.5.102 #單播發送心跳報文,語法爲:ucast INTERFACENAME REMOTE_ADDRESS
udpport 694 #制定集羣節點間通信用的udp端口號
auto_failback on #當主節點恢復後,是否自動切換
node server1 #集羣中的計算機名,需要與uname -n的輸出一致
node server2 #同上
STEP3:編輯authkeys配置文件,第一行的auth後面跟使用算法序號,第三行的sha1後面跟自定義密碼
auth AUTH_NUMBER
#1 crc
2 sha1 AUTH_PASSWORD
#3 md5 Hello!
auth 2
#1 crc
2 sha1 abc.123
#3 md5 Hello!
STEP4:對authkeys文件賦予權限
chmod 600 authkeys
STEP5:編輯haresources配置文件,添加如下內容即可
server1 192.168.5.200 apache::/etc/httpd/conf/httpd.conf
語法爲
MASTER_SERVER Virtual_IP ServerName::mainconfigurePATH
STEP6:將這三個配置文件使用scp工具發送到server2即可
scp {authkeys,ha.cf,haresources} [email protected]:/usr/local/heartbeat/etc/ha.d/
STEP7:在Server2編輯/usr/local/heartbeat/etc/ha.d/ha.cf文件,做出如下修改
ucast ens33 192.168.5.102 -----> ucast ens33 192.168.5.101
啓動服務並測試
STEP1:啓動服務並設置開機自啓動
systemctl start heartbeat
systemctl enable heartbeat
STEP2:查看服務運行狀態,一定要看,因爲Heartbeat服務計算你做錯,啓動失敗,都不會報錯,一定要手動查看
systemctl status heartbeat
● heartbeat.service - Heartbeat High Availability Cluster Communication and Membership
Loaded: loaded (/usr/lib/systemd/system/heartbeat.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2019-11-10 06:39:12 EST; 2s ago
Main PID: 99213 (heartbeat)
CGroup: /system.slice/heartbeat.service
├─99213 heartbeat: master control process
├─99215 heartbeat: FIFO reader
├─99216 heartbeat: write: ucast ens33
└─99217 heartbeat: read: ucast ens33
STEP3:ping虛擬地址測試
ping 192.168.5.200
PING 192.168.5.200 (192.168.5.200) 56(84) bytes of data.
64 bytes from 192.168.5.200: icmp_seq=1 ttl=64 time=0.012 ms
^C
--- 192.168.5.200 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.012/0.012/0.012/0.000 ms
STEP4:使用curl測試apache集羣
curl 192.168.5.200
This is server1
STEP5:使用heartbeat自帶腳本切換節點
/usr/local/heartbeat/share/heartbeat/hb_standby
Going standby [all].
STEP6:在此使用curl測試
curl 192.168.5.200
This is server2