實(shí)驗(yàn)簡介
本實(shí)驗(yàn)采用centos6為基礎(chǔ)操作系統(tǒng),通過heartbeat v1 版本來實(shí)現(xiàn)httpd服務(wù)的高可用。
實(shí)驗(yàn)架構(gòu)
- active:192.168.99.41 node3.magedu.com
- passive:192.168.99.24 node4.magedu.com
- Floting IP:192.168.99.200
配置HA集群的前提
- 節(jié)點(diǎn)間時間必須同步
ntpdate
[root@node3 ~]# ntpdate 0.centos.pool.ntp.org
[root@node4 ~]# ntpdate 0.centos.pool.ntp.org
- 節(jié)點(diǎn)間需要通過主機(jī)名互信,必須解析IP地址
- 建議通過hosts文件來實(shí)現(xiàn)
- 通信中使用的名字與節(jié)點(diǎn)的名字必須保持一致:
uname -n命令或hoatname
# hostname
node3.magedu.com
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.0.1 server.magelinux.com server
192.168.99.41 node3.magedu.com node3
192.168.99.42 node4.magedu.com node4
考慮仲裁設(shè)備是否會被用到
因?yàn)槲覀儾捎脙晒?jié)點(diǎn)必須使用仲裁設(shè)備,此處我們采用ping node來實(shí)現(xiàn)仲裁建立各節(jié)點(diǎn)之間的root用戶能夠基于密鑰認(rèn)證
[root@node3 ~]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
9a:52:eb:68:6a:a1:80:fa:c6:ec:db:4a:48:e7:0e:f4 root@node3.magedu.com
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| |
|.o . . S|
|=.= . + |
|+=.E. + |
|o.B..+ |
| =**o . |
+-----------------+
[root@node3 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@node4.magedu.com
[root@node3 ~]# date ;ssh node4.magedu.com 'date'
Mon Feb 6 10:21:35 CST 2017
Mon Feb 6 10:21:35 CST 2017
至此準(zhǔn)備工作完成我們應(yīng)該開始正式的安裝部署了
解決依賴關(guān)系與安裝heartbeat程序包
#注意需要配置好yum倉庫和epel倉庫
[root@node3 ~]# yum install -y net-snmp-libs libnet PyXML
[root@node3 ~]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:heartbeat-pils ########################################### [ 33%]
2:heartbeat-stonith ########################################### [ 67%]
3:heartbeat ########################################### [100%]
#這里的rpm包使我們自己配置安裝的
復(fù)制配置文件并修改相應(yīng)的權(quán)限
[root@node3 ~]# cp /usr/share/doc/heartbeat-2.1.4/{ha.cf,haresources,authkeys} /etc/ha.d/
[root@node3 ~]# chmod 600 /etc/ha.d/authkeys #必須修改為600,否則服務(wù)有可能無法啟動
[root@node3 ~]# ll /etc/ha.d/
total 48
-rw------- 1 root root 645 Feb 6 10:43 authkeys
-rw-r--r-- 1 root root 10539 Feb 6 10:43 ha.cf
-rwxr-xr-x 1 root root 745 Sep 10 2013 harc
-rw-r--r-- 1 root root 5905 Feb 6 10:43 haresources
drwxr-xr-x 2 root root 4096 Feb 6 10:36 rc.d
-rw-r--r-- 1 root root 692 Sep 10 2013 README.config
drwxr-xr-x 2 root root 4096 Feb 6 10:36 resource.d
-rw-r--r-- 1 root root 7864 Sep 10 2013 shellfuncs
- 主配置配置
vim /etc/ha.d/ha.cf
定義heartbeat守護(hù)進(jìn)程的工作方式和屬性
logfile /var/log/ha-log
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport 694
mcast eth0 225.12.0.1 694 1 0
auto_failback on
node node3.magedu.com
node node4.magedu.com
ping 192.168.99.1
compression bz2
compression_threshold 2
- 集群資源配置resources
通過此配置文件來配置資源
[root@node3 ~]# vim /etc/ha.d/haresources
node3.magedu.com 192.168.99.200/24/eth0/192.168.99.255 httpd
- 密鑰認(rèn)證文件配置authkeys
通過此來設(shè)置集群內(nèi)多個節(jié)點(diǎn)的認(rèn)證
[root@node3 ~]# openssl rand -base64 16
duPgfWr4O5SgHlUbNgCNtA==
[root@node3 ~]# vim /etc/ha.d/authkeys
auth 2
#1 crc
2 sha1 duPgfWr4O5SgHlUbNgCNtA
以上配置完成后我們將其復(fù)制到另外一臺節(jié)點(diǎn)上,快速數(shù)顯heatbeat的安裝配置
[root@node3 ~]# scp -r heartbeat2/ node4.magedu.com:/root
[root@node4 ~]# rpm -ivh heartbeat2/heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat2/heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat2/heartbeat-stonith-2.1.4-12.el6.x86_64.rpm
[root@node3 ~]# scp -p /etc/ha.d/{ha.cf,haresources,authkeys} node4.magedu.com:/etc/ha.d/
安裝httpd服務(wù),并禁止開機(jī)自啟
# yum install -y httpd
提供不同的頁面
[root@node3 ~]# echo "<h1>node3.magedu.com</h1>" > /var/www/html/index.html
[root@node4 ~]# echo "<h1>node4.magedu.com</h1>" > /var/www/html/index.html
啟動服務(wù)
[root@node3 ~]# service httpd start
Starting httpd: [ OK ]
[root@node3 ~]# curl 192.168.99.41
<h1>node3.magedu.com</h1>
[root@node3 ~]# service httpd stop
Stopping httpd: [ OK ]
[root@node3 ~]# chkconfig httpd off
[root@node3 ~]# service heartbeat start
logd is already running
Starting High-Availability services:
2017/02/06_13:46:17 INFO: Resource is stopped
Done.
[root@node3 ~]# service httpd start
Starting httpd: [ OK ]
[root@node3 ~]# curl 192.168.99.41
<h1>node3.magedu.com</h1>
[root@node3 ~]# service httpd stop
Stopping httpd: [ OK ]
[root@node3 ~]# chkconfig httpd off
[root@node4 ~]# service heartbeat start
logd is already running
Starting High-Availability services:
2017/02/06_13:46:17 INFO: Resource is stopped
Done.
注意:需要兩臺機(jī)器同時啟動heartbeat才能看到效果
效果檢驗(yàn)
1.兩臺主機(jī)的heartbeat服務(wù)都啟動,我們用curl請求會產(chǎn)生以下效果:
[root@client ~]# curl 192.168.99.200
<h1>node3.magedu.com</h1>
此時因?yàn)橹鞴?jié)點(diǎn)的為node3.magedu.com,所以我們訪問到的是node3.magedu.com主機(jī)提供的頁面
2.關(guān)閉主節(jié)點(diǎn)上的
heartbeat服務(wù),我們會發(fā)現(xiàn)haresources配置的文件全都跑到備用節(jié)點(diǎn)上了
[root@node3 ~]# service heartbeat stop
Stopping High-Availability services:
Done.
[root@node3 ~]#
[root@client ~]# curl 192.168.99.200
<h1>node4.magedu.com</h1>
[root@client ~]#
通過上述示例我們達(dá)到了雙機(jī)主/備用,資源快速切換的效果
3.我們重新讓主節(jié)點(diǎn)上線,它會將資源搶回來,這是我們在
ha.cf文件中配置的
[root@node3 ~]# service heartbeat start
[root@client ~]# curl 192.168.99.200
<h1>node3.magedu.com</h1>
- tip:溫馨小提示
在
/usr/lib64/heartbeat/目錄下有一些小程序,我們可以用來對heartbeat高可用集群進(jìn)行手動實(shí)現(xiàn)hb_standby ,hb_takeover,ha_propagate...
借用共享存儲來實(shí)現(xiàn)資源轉(zhuǎn)移
準(zhǔn)備第三個節(jié)點(diǎn)用來充當(dāng)NFS共享服務(wù)器,此處作者用centos7主機(jī)來充當(dāng)nfs服務(wù)。
[root@nfs ~]# mkdir /web/htdocs -pv
mkdir: created directory ‘/web’
mkdir: created directory ‘/web/htdocs’
[root@nfs ~]# echo "<h1>Page from NFS</h1>" > /web/htdocs/index.html
[root@nfs ~]# vim /etc/exports
[root@nfs ~]# systemctl start nfs
分別掛載測試是否成功
#節(jié)點(diǎn)1:node3.magedu.com
[root@node3 ~]# mount -t nfs 192.168.99.241:/web/htdocs /var/www/html/
[root@node3 ~]# service httpd start
Starting httpd: [ OK ]
[root@node3 ~]# curl node3.magedu.com
<h1>Page from NFS</h1>
[root@node3 ~]# service httpd stop
[root@node3 ~]# umount /var/www/html/
# 節(jié)點(diǎn)2:node4.magedu.com
[root@node4 ~]# mount -t nfs 192.168.99.241:/web/htdocs /var/www/html/
[root@node4 ~]# service httpd start
[root@node4 ~]# curl 192.168.99.42
<h1>Page from NFS</h1>
[root@node4 ~]# service httpd stop
[root@node4 ~]# umount /var/www/html/
至此,nfs服務(wù)已經(jīng)準(zhǔn)備ok。我們可以配置1heartbeat來進(jìn)行掛載使用測試了
- 1.編輯harources添加nfs資源
[root@node3 ~]# vim /etc/ha.d/haresources
node3.magedu.com 192.168.99.200/24/eth0/192.168.99.255 Filesystem::192.168.99.241:/web/htdocs::/var/www/html::nfs httpd
[root@node4 ~]# vim /etc/ha.d/haresources
node4.magedu.com 192.168.99.200/24/eth0/192.168.99.255 Filesystem::192.168.99.241:/web/htdocs::/var/www/html::nfs httpd
- 2.啟動服務(wù)
[root@node3 ~]# service heartbeat start;ssh node4.magedu.com 'service heartbeat start'
- 3.驗(yàn)證結(jié)果:
# 文件已經(jīng)掛載到node3.magedu.com這個節(jié)點(diǎn)上去了
[root@node3 ~]# mount
/dev/mapper/vg0-root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg0-usr on /usr type ext4 (rw)
/dev/mapper/vg0-var on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.99.241:/web/htdocs on /var/www/html type nfs (rw,vers=4,addr=192.168.99.241,clientaddr=192.168.99.41)
#請求weeb服務(wù)正常是nfs提供的頁面
[root@client ~]# curl 192.168.99.200
<h1>Page from NFS</h1>
# 停止node3.magedu.com節(jié)點(diǎn),資源將轉(zhuǎn)移到node4.magedu.com上,且用戶不會猶豫任何察覺
[root@node3 ~]# /usr/lib64/heartbeat/hb_standby
#nfs掛載到了node4節(jié)點(diǎn)上
[root@node4 ~]# mount
/dev/mapper/vg0-root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg0-usr on /usr type ext4 (rw)
/dev/mapper/vg0-var on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.99.241:/web/htdocs on /var/www/html type nfs (rw,vers=4,addr=192.168.99.241,clientaddr=192.168.99.42)
#web服務(wù)仍然能訪問
[root@client ~]# curl 192.168.99.200
<h1>Page from NFS</h1>