1、前言
HA即(high available)高可用,又被叫作雙機熱備,用於關鍵性業務。簡單理解就是,有2臺機器 A 和 B,正常是 A 提供服務,B 待命閒置,當 A 宕機或服務宕掉,會切換至B機器繼續提供服務。常見的實現高可用的開源軟件有 heartbeat 和 keepalivedhtml
2、準備實驗環境
服務器A:
主機名:master
操做系統:CentOS6.5 64位
eth0網卡地址:192.168.1..3node
服務器B:
主機名:slave
操做系統:CentOS6.5 64位
eth0網卡地址:192.168.1.4linux
虛擬VIP:
VIP:192.168.1.2web
3、設置主機名
A服務器
[root@localhost ~]# hostname master
[root@localhost ~]# vim /etc/sysconfig/network
HOSTNAME=mastervim
B服務器
[root@localhost ~]# hostname slave
[root@localhost ~]# vim /etc/sysconfig/network
HOSTNAME=slave服務器
4、關閉防火牆和Selinux(2臺節點都要操做)
[root@localhost ~]# setenforce 0
[root@localhost ~]# service iptables stop
[root@localhost ~]# chkconfig iptables off網絡
5、配置hosts文件(2臺節點都操做)
[root@localhost ~]# vim /etc/hosts
192.168.1.3 master
192.168.1.4 slavessh
6、設置ssh互信(2臺節點都操做)
[root@master ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
[root@master ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.4
測試
[root@master ~]# ssh 192.168.1.4 'ifconfig'ide
7、安裝epel擴展源 (2臺都操做)
[root@master ~]yum install -y epel-release
8、安裝heartbeat (2臺都操做)
[root@master ~]# yum -y install heartbeat httpd
[root@slave ~]# yum -y install heartbeat httpd測試
8、配置時間同步(2臺都操做)
[root@master ~]# date -s "2019-02-27「
2019年 02月 27日 星期三 00:00:00 CST
[root@master ~]# date -s "13:50:30"
9、配置httpd
[root@master ~]# echo "Master" > /var/www/html/index.html
[root@master ~]# service httpd start
[root@master ~]# service httpd stop
[root@master ~]# chkconfig httpd off
10、配置Heartbeat
一、拷貝三個配置文件
[root@master ~]# cd /etc/ha.d/
[root@master ha.d]# cp -r /usr/share/doc/heartbeat-3.0.4/{authkeys,ha.cf,haresources} ./
二、修改authkeys
[root@master ha.d]# vim authkeys
auth 1
1 md5 5t2dhlk6sdrf8dshnm3
更改authkeys的權限
[root@master ha.d]# chmod 600 authkeys
三、編輯haresources文件
[root@master ha.d]# vim haresources
master IPaddr::192.168.1.2/24/eth0:0 httpd
四、配置ha.cf文件
[root@master ha.d]# vim ha.cf
bcast eth0
node master
node slave
配置說明
debugfile /var/log/ha-debug:該文件保存heartbeat的調試信息。
logfile /var/log/ha-log:heartbeat的日誌文件。
keepalive 2:心跳的時間間隔,默認時間單位爲秒s。
deadtime 30:超出該時間間隔未收到對方節點的心跳,則認爲對方已經死亡。
warntime 10:超出該時間間隔未收到對方節點的心跳,則發出警告並記錄到日誌中。
initdead 60:在某系統上,系統啓動或重啓以後須要通過一段時間網絡才能正常工做,該選項用於解決這種狀況產生的時間間隔,取值至少爲deadtime的2倍。
udpport 694:設置廣播通訊使用的端口,694爲默認使用的端口號。
bcast eth0 :經過eth0網卡進行向外廣播
11、把主節點上的三個配置文件拷貝到從節點
[root@master ~]#cd /etc/ha.d
[root@master ha.d]#scp authkeys ha.cf haresources slave:/etc/ha.d
12、啓動heartbeat服務
[root@master ha.d]# service heartbeat start
httpd的80端口已經打開
網頁訪問VIP地址
十3、切換至備機
必須遠程啓動備機的heartbeat
[root@master heartbeat]# ssh slave 'service heartbeat start'
[root@master ha.d]# cd /usr/share/heartbeat/
[root@master heartbeat]# ./hb_standby
若是想切換至主機則
[root@slave ~]# cd /usr/share/heartbeat
[root@slave heartbeat]# ./hb_standby
準備一臺NFS服務器
操做系統:CentOS7.5 64位
eth0網卡地址:192.168.1.5
主機名:localhost
[root@localhost ~]#mkdir /web/htdocs -pv
[root@localhost ~]#vim /etc/exports
/web/htdocs 192.168.1.0/24(ro)
[root@localhost ~]#yum -y install nfs* --skip-broken
[root@localhost ~]#systemctl start nfs
[root@localhost ~]#cd /web/htdocs/
[[root@localhost htdocs]#vim index.html
NFS server
[root@localhost ~]#systemctl start rpcbind
[root@localhost ~]#systemctl stop firewalld
[root@localhost ~]#setenforce 0
服務器(2臺都操做)
[root@master ~]# mount 192.168.1.5:/web/htdocs /mnt
[root@master ~]# ls /mnt/
index.html
[root@master ~]# cat /mnt/index.html
NFS server
[root@master ~]# umount /mnt/
[root@master ~]# cd /etc/ha.d/
[root@master ha.d]# vim haresources
master 192.168.1.2/24/eth0:0 Filesystem::192.168.1.5:/web/htdocs::/var/www/html::nfs httpd
[root@master ha.d]# scp haresources slave:/etc/ha.d/
[root@master ha.d]# service heartbeat start
[root@master ha.d]# ssh slave 'service heartbeat start'
驗證是否能夠訪問
設置master爲備機
[root@master ha.d]# cd /usr/share/heartbeat/
[root@master heartbeat]# ./hb_standby
查看slave服務器
[root@slave ~]# mount
顯示掛載成功