標籤:主機
安裝環境:Centos 6.4, httpd2.4,mysql5.5,heartbeat v2
提供兩台機器node1和node2,在/etc/hosts檔案中添加名稱解析,並且主機名稱要與節點名稱要相同,即uname -n的名稱要和hosts定義的名稱必須一樣。
# IP HOSTNAME ALIAS
10.204.80.79 node1.mylinux.com node1
10.204.80.80 node2.mylinux.com node2
這兩台伺服器之間建立ssh互信
#ssh-kegen –t rsa –p”” 產生id_rsa.pub和id_rsa 公開金鑰和私密金鑰檔案
#ssh-copy-id –i .ssh/id_rsa.pub [email protected] 把公開金鑰檔案拷到node2上,在node2中同樣操作,然後把公開金鑰檔案拷到node1上,這樣,兩台主機之間就可以不通過密碼進行互訪。
安裝httpd,並提供訪問頁面,為了顯示效果,可以在兩台機器上的網頁檔案上委任標記顯示區別,我用的是web頁面是phpinfo 加標識來識別,基礎環境就已經安裝好了。
安裝heartbeat v2版本
因為heartbeat v2版本已經不支援在centos6版本上的rpm包,所以不能直接用yum安裝,需要用src源碼安裝包編譯後才能安裝。我這裡用的是已經編譯好了的rpm包,安裝過程要解決依賴關係,
yum -y install perl-MailTools net-snmp-libs pygtk2-libglade libnetsnmp net-snmp-libs
然後通過rpm –ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-gui-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm
650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832458nK1X.png" width="747" height="105" />
安裝heartbeat v2.14版本完成
heartbeat v2的設定檔放的位置在 /etc/ha.d/目錄下,有三個設定檔,authkeys ha.cf haresource,安裝完成後預設是沒有這三個檔案的,可以手動建立,也可以在/usr/share/doc/heartbeat-2.1.4/ 目錄下把這三個檔案的樣本檔案拷到/etc/ha.d/ 目錄下。
設定檔的作用:
authkeys : 認證檔案,裡面有啟用哪種加密方式及加密金鑰,crc ,sha1 和md5三種認證機制。
650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832459uhvq.png" width="313" height="102" />
如果要啟用哪種認證方式,則在 auth 後面選擇對應的資料,然後加指定驗證密鑰,密碼是用openssl隨機產生的一串16位字元
haresource :這是由heartbeat v1版本中內建的資源管理配製檔案,定義依靠和資源情況 。
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832459OPgR.png" width="344" height="33" />
這點定義的時資源節點預設依靠在node1節點上,vip:10.204.80.89,預設綁定在eth0上,定義的服務為httpd24
ha.cf : 主設定檔, 定義節點,日誌,傳輸心跳的方式等。
logfile /var/log/ha-log
node node1.mylinux.com
node node2.mylinux.com
mcast eth0 225.4.0.4 694 1 0
ping 10.204.80.3
配置就已經完成,啟動heartbeat
在node1上啟動heartbeat ,提示heartbeat啟動成功,但資源是停止的。
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832459Y4ZO.png" width="369" height="95" />
查看開機記錄
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832460P8HL.png" width="562" height="151" />
查看啟用連接埠
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832460RmtS.png" width="562" height="71" />
組播連接埠694已經啟動
在當前節點上啟動node2
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832461xIhn.png" width="430" height="90" />
查看資源
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832461lcpU.png" width="430" height="200" />
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832462tEuZ.png" width="562" height="126" />
vip和httpd都已經啟動。web頁面也正常顯示
650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832462kuqw.png" width="599" height="233" />
在node1上停止heartbeat服務
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_14098324638UOz.png" width="313" height="76" />
然後在node2上查看資源
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832463UIFa.png" width="562" height="249" />
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832464tjKp.png" width="562" height="119" />
Vip和httpd都已經成功轉移到node2上,web訪問頁面也顯示是node2上的頁面
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832464ajK8.png" width="621" height="213" />
在node1上重新啟動heartbeat
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832465mNL9.png" width="559" height="332" />
資源又重新回到node1上了,這是因為在haresource定義 資源的時候是預設在Node1上。heartbeat配置高可用web服務成功
配置基於nfs共用儲存的mysql高可用叢集
提供兩台機器mysql1和mysql2,在/etc/hosts檔案中添加名稱解析,並且主機名稱要與節點名稱要相同,即uname -n的名稱要和hosts定義的名稱必須一樣。
# IP HOSTNAME ALIAS
10.204.80.86 mysql1.mylinux.com mysql1
10.204.80.85 mysql2.mylinux.com mysql2
nfs伺服器 10.204.80.71
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832465lGew.png" width="301" height="55" />
mysql1和mysql2建立互信關係,並且在三台伺服器上添加指定同樣uid和gid的mysql帳號
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832465vQdE.png" width="376" height="98" />
在mysql1和mysql2兩台機器上掛載nfs,掛載目錄為/mysqldata,並建立/mysqldata/data目錄為mysql的資料目錄
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832466dg0o.png" width="562" height="161" />
用二進位格式安裝mysql5.5.38,並指定mysql的資料目錄為/mysqldata/data
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832466SrFK.png" width="643" height="22" />、
在mysql1節點上啟動mysql服務成功,
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832467h3lh.png" width="562" height="263" />
然後在mysql1節點上停止mysql服務,然後在mysql2節點上同樣啟動mysql服務,注意在/etc/my.cnf檔案中一定要添加 datadir = /mysqldata/data 這一條設定檔,
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832467i5M0.png" width="260" height="275" />
在mysql2結點上啟動mysql服務成功
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832468glVZ.png" width="307" height="59" />
叢集環境已經搭好,安裝heartbeat v2版本過程參照上面web高可用叢集過程。啟用圖型介面配置資源
在/etc/ha.d/ha.cf主配置中寫入以下配置
node mysql1.mylinux.com
node mysql2.mylinux.com
mcast eth0 225.5.0.4 694 1 0
ping 10.204.80.3
crm on # 啟用圖型介面配置資源
在/etc/ha.d/authkeys 啟用驗證功能
2 sha1 7cd37bc0d0fbc374
輸入命令 crm_mon,可以查看兩個節點的狀態資訊
650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832468bmqW.png" width="527" height="199" />
兩個節點都已經線上,但沒有定義資源
在命令列輸入650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832468Lsuw.png" width="192" height="25" />啟用圖型介面
650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_14098324690AZ1.png" width="376" height="314" />
安裝heartbeat以後會自動建立hacluster帳號,對這個帳號添加密碼,啟動heartbeat服務就可以登陸進去配置資源。
650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832470iFG2.png" width="452" height="377" />
顯示,兩個節點都已經正常線上狀態。就可以添加資源
因為啟動mysql服務必須要有三個資源:VIP NFS以及mysql服務,所以把這三個資源定義到同一個資源群組中。
添加資源vip,ip地址,綁定在哪個網卡的別名上,指定netmask
650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832470FE9t.png" width="434" height="344" />
同樣添加filesystem資源nfs和mysqld服務,然後把資源啟動
650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832471bzwe.png" width="472" height="394" />
定義約束:
因為前面定義的資源都在同一個組裡面,所以這些資源必須運行在同一個節點上,於是要定義的約束是位置約束,預設資源依靠在節點mysql1上。順序約束,必須先掛載nfs資源然後才能啟用mysql服務,必須要先停止mysql服務,然後才能停止nfs服務,定義屬效能
位置約束: 定義在mysql1上,也就是如果mysql1節點掛了會自動轉移到節點mysql2,當mysql1節點重新線上後,資源又會自動轉移到mysql1節點上來。
650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_140983247297Du.png" width="443" height="370" />
順序約束:定義資源啟動的先後順序
650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832472FAtm.png" width="428" height="293" />
劃紅圈的部份就已經說明了資源啟動的循序關聯性,至此叢集已經定義完成。
把mysql1節點狀態改為Standby,顯示轉換過程
650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832473KnV3.png" width="590" height="492" />
650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832474Yg8R.png" width="583" height="486" />
資源轉移完成,當把mysql1節點重新上線,資源又會自動轉移 回去。
650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/4/287547_1409832474MG6B.png" width="579" height="483" />
heartbeat v2配置高可用web叢集和基於nfs搭建MySQL高可用叢集