一、相關概念:
補充 {
what is high Availability?
A=MTBF/(MTBF+MTTR)
MTBF(mean time betweenfailures平均無故障時間)
MTTR(mean time to repair平均修複時間)
two ways improve availability?
increase MTBF to very large values
reduce MTTR to very low values
hardware failure causes?
design failure(rare);randomfailure(rare);infant mortality(high rate of failure)使用不當;wear out(high rate of failure)用壞了
increase hardware MTBF?
use better components;preemptivelyreplace hardware prior to wear out在用壞前提前替換掉
software failure causes?
implementation defects(very common)任一軟體代碼超過三行都有缺陷;typicaly measured in defects per KLOC
increase software MTBF?
experienced engineering team;peer reviewof all code;simple design;compact code foot print;static and runtimeanalysis tools such as valgrind,lint,high compiler warning levels,coverity,lcov;testcoverage of the software
}
openais(redhat5.X的RHCS,作為核心中資訊通信的API,借助于CMAN完成message layer,不同版本有不同的代稱,例如whitetank版本wilson版本等,使用rgmanager完成資源管理,openais是接口,一大堆的API)
corosync是openais發展到wilson版本後衍生出來的開放性叢集引擎
openais在9.0版本後分成兩個項目,一部分是corosync(隻保留了叢集引擎),另一部分是openais(保留了AIS規範和corosync的一部分子功能);有時需要corosync提供AIS,這時就要安裝openais插件
AIS(application interface standard,應用接口規範,是用來定義API的開放性規範的集合,這些應用程式作為中間件為應用服務提供一種開放的、高移植性的程式接口,在實作高可用過程中是亟需的)
SAF(service availability forum,服務可用性論壇是一開放性論壇,它開發并釋出這些免費規範,使用AIS規範的API,可以減少應用程式的複雜性、縮短應用程式的開發時間,這些規範的主要目的就是為了提高中間元件可移植性和應用程式的高可用性)
SAF AIS(是一個開放性工程,在不斷更新,而openais是基于SAF标準的叢集架構的應用程式接口規範,openais提供一種叢集模式(叢集架構、叢集成員管理、通信方式、叢集監測等),能為叢集軟體工具提供滿足AIS标準的叢集接口,但沒有叢集資源管理的功能)
pacemaker(比crm特性更豐富,支援全CLI配置,也支援GUI管理工具)
Hawk(Web GUI,圖形界面資源管理工具,SLES(suse linux enterprise server)也使用corosync+pacemaker)
LCMC(linux cluster management console,GUI)
Conga(RHCS,基于web的管理界面,luci是控制端,ricci是被控制端要安裝在每一個node上,配置ricci成為luci的管理對象,并配置好yum源,後續的pacemaker、corosync都會自動安裝)
keepalived(vrrp,virtual route redundency protocol,僅支援2個node,在雙主模型下keepalived比較理想,若多于2個node隻能使用heartbeat、corosync這套元件)
注:HA叢集中節點數最好大于兩個(大于兩個,為奇數個,就不使用ping node或仲裁磁盤)
二、操作(環境redhat5,2.6.18,32bit,兩個node,同上篇):
cluster-glue-1.0.6-1.6.el5.i386.rpm(讓叢集中各元件結合起來協同工作)
cluster-glue-libs-1.0.6-1.6.el5.i386.rpm
corosync-1.2.7-1.1.el5.i386.rpm
corosynclib-1.2.7-1.1.el5.i386.rpm
heartbeat-3.0.3-2.3.el5.i386.rpm(pacemaker預設依賴heartbeat,安裝上不啟用即可)
heartbeat-libs-3.0.3-2.3.el5.i386.rpm
pacemaker-1.0.12-1.el5.centos.i386.rpm
pacemaker-libs-1.0.12-1.el5.centos.i386.rpm
resource-agents-1.0.3-2.6.el5.i386.rpm
其它軟體包介紹:
ldirectord-1.0.3-2.6.el5.i386.rpm(專用于ipvs,作為ipvs的補充,可幫助生成ipvs規則及後端RS的health_check,靈活地應用在高可用叢集中,與corosync和heartbeat都能結合工作,用于heartbeat的包為heartbeat-ldirectord-2.1.4-11.el5.i386.rpm)
openais-debuginfo-1.1.3-1.6.el5.i386.rpm(不做開發可不安裝,以插件方式工作在corosync)
openaislib-1.1.3-1.6.el5.i386.rpm
準備環境(參照叢集第二篇heartbeatV2文章)有:時間同步、主機名、ssh雙機互信、本地yum源
注:redhat5系統使用corosync1.2.7(1系列的),redhat6使用2系列的
node1-side:
将以上rpm包放至/root目錄下
#cd
#scp ./* node2:/root
#for I in {1..2};do ssh node$I ‘yum -y --nogpgcheck localinstall /root/*.rpm’;done
#for I in {1..2};do ssh node$I ‘yum -y install httpd’;done
#rpm -ql corosync
/etc/corosync/corosync.conf.sample
/etc/init.d/corosync
/usr/sbin/corosync
/usr/sbin/corosync-keygen(生成密鑰,它是從/dev/random(随機數來自熵池,磁盤中斷的時間間隔和擊鍵的時間間隔)下擷取随機數,如果不夠會等待,提示我們狂敲鍵盤讓其自動生成)
注:heartbeat中的authkeys(密鑰手動生成,且明文傳輸,要改權限為600)
#cd /etc/corosync
#cp corosync.conf.example corosync.conf
#man corosync.conf
totem { }(圖騰,每個node同其它node通信通過totem定義)
logging { }
event { }
#vim /etc/corosync/corosync.conf
totem {
version: 2(配置檔案的版本号,唯一合法的版本号為2,不是說我們改動過就是第一版之類的,是被corosync所支援的版本,各版本間文法不同)
secauth: on(安全認證,為安全要開啟,用HMAC/SHA1加密,會消耗系統資源)
threads: 2(開啟多個線程執行加密或多點傳播,若secauth為off這項就沒用,若為on此項才有意義)
interface {
ringnumber: 0(每個接口所定義的環,若僅有一個node不定義此項,若有多個node,每個node有兩塊網卡(備援),定義多個node上的eth0是一個環,eth1是一環,若有多個node沒定義此項,消息在網絡中會形成環路一起傳遞下去)
bindnetaddr: 192.168.1.0(綁定的網絡位址,若一個node上有兩塊網卡,在哪個網絡上傳遞資訊,在哪個網絡進行多點傳播,這項定義的是網絡,不能寫IP,是IP所在的網絡位址)
mcastaddr: 226.94.1.1(D類IP,僅能使用224.0.2.0~238.255.255.255)
mcastport: 5405
}
logging {
fileline: off(定義不同的日志間列印分隔符)
to_stderr: no(發到标準錯誤輸出,也就是螢幕)
to_logfile: yes(使用指定檔案記錄日志)
to_syslog: no(用系統自帶的syslog記錄/var/log/messages,隻開一項即可)
logfile: /var/log/cluster/corosync.log(此目錄不存在,記得在開啟服務前要建立此目錄)
debug: off(出錯時打開此項調試)
timestamp: on(記錄時間戳,能定位錯誤,若開啟此項每記錄一次日志都要擷取一次目前系統時間(一次系統調用要傳回使用者空間,核心通過硬體擷取時間),多記錄一個條目會使檔案體積變大)
logger_subsys {(子系統資訊)
subsys: AMF
debug: off
}
amf {
mode: disabled(沒安裝openais,未啟用)
service {(添加此段,啟動corosync時,将pacemaker也啟動)
ver: 0
name: pacemaker
#use_mgmtd: yes
aisexec {(運作openais所指定使用者群組,由于未安裝openais,此段忽略)
user: root
group: root
}
#corosync-keygen(會生成authkey檔案,權限400,非文本檔案)
#file /etc/corosync/authkey
/etc/corosync/authkey: data
#ll !$
-r-------- 1 root root 128 Jul 10 02:28/etc/corosync/authkey
#scp -p authkey corosync.conf node2:/etc/corosync/
#for I in {1..2};do ssh node$I ‘mkdir /var/log/cluster/’;done
#service corosync start
#ssh node2 ‘service corosync start’
#grep -e "Corosync Cluster Engine" -e "configuration file" /var/log/cluster/corosync.log(檢視corosync引擎是否正常啟動)
#grep TOTEM /var/log/cluster/corosync.log(檢視初始化成員節點通知是否正常發出)
#grep ERROR: /var/log/cluster/corosync.log(檢視啟動過程是否有錯誤,此實驗沒有STONITH裝置,會報錯,執行#crm configure property stonith-enabled=false即可)
#grep pcmk_startup /var/log/cluster/corosync.log(檢視pacemaker是否啟動)
#crm_mon
注:沒有配置ping node很有可能叢集分裂
#crm(取代了heartbeatV2中的crm_sh,比crm_sh強大的多,進入後help,可管理resource、node、ra,可檢視node和resource的status,支援cd指令,支援指令補全等)
注:crm指令在任一node運作都行,它會自動連接配接DC進而使配置儲存
crm(live)#ra
crm(live)ra# list lsb
crm(live)ra# list ocf
crm(live)ra# classes
heartbeat
lsb
ocf / heartbeat pacemaker
stonith
crm(live)ra# meta [ocf:[heartbeat:]IPaddr
crm(live)ra# meta ocf:heartbeat:Filesystem(或meta Filesystem)
crm(live)ra# providers IPaddr
crm(live)ra# providers Filesystem
crm(live)ra# providers Dummy
heartbeat pacemaker
crm有兩種模式:
互動模式(下配置,執行commit指令後才生效);
批處理模式(下配置立即生效,例如#crm configure property stonith-enabled=false)
crm(live)# configure
crm(live)configure# property stonith-enabled=false(互動模式)
crm(live)configure# property no-quorum-policy=ignore
crm(live)configure# verify(檢查配置檔案是否正确)
crm(live)configure# commit(在互動模式下,要送出)
crm(live)configure# show [xml](display CIBobjects)
node node1.magedu.com
node node2.magedu.com
property$id="cib-bootstrap-options" \
dc-version="1.0.12-unknown"\
cluster-infrastructure="openais"\
expected-quorum-votes="2"\
stonith-enabled="false"
no-quorum-policy="ignore"
crm(live)configure# show xml(打開/var/lib/heartbeat/crm/cib.xml檔案)
crm(live)configure# primitive webipocf:heartbeat:IPaddr params ip=192.168.41.222 nic=eth0 cidr_netmask=24 iflabel=eth0:0(打開另一終端,crm(live)configure# help primitive,crm(live)ra# meta ocf:heartbeat:IPaddr,參考文法配置正确)
crm(live)configure# primitive webstore ocf:heartbeat:Filesystem params device=192.168.41.128:/web/htdocs directory=/var/www/html fstype=nfs op start timeout=60 op stop timeout=60(這裡的start timeout和stop timeout必須要定義,因為預設值是20秒,小于建議的60秒,op後指定的這些選項隻能比建議的長)
crm(live)configure# primitive httpd lsb:httpd
crm(live)configure# group webservice webip webstore httpd
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# show
crm(live)configure# edit(可直接編輯配置,是在vim下配置)
crm(live)configure# cd
crm(live)#exit
[root@node1 ~]#
[root@node1 ~]# crm
crm(live)# node
crm(live)node# help
crm(live)node# show
crm(live)node# list
crm(live)node# standby(或#crm node standby)
crm(live)node# online(或#crm node online)
crm(live)node# clearstate node1.magedu.com
crm(live)# resource
crm(live)resource# help
crm(live)resource# stop webservice
crm(live)resource# start webservice
crm(live)resource# restart webservice
crm(live)resource# list
Resource Group: webservice
webip (ocf::heartbeat:IPaddr)Started
webstore (ocf::heartbeat:Filesystem)Started
httpd (lsb:httpd) Started
crm(live)resource# status
crm(live)resource# cleanup webservice
Cleaning up webip on node1.magedu.com
Cleaning up webip on node2.magedu.com
Cleaning up webstore on node1.magedu.com
Cleaning up webstore on node2.magedu.com
Cleaning up httpd on node1.magedu.com
Cleaning up httpd on node2.magedu.com
Waiting for 7 replies from the CRMd.......
[root@node1 ~]# crm_mon(在以上node和resource上操作時,觀察監控的變化)
以上是通過組将各資源綁在一起,以下是通過constraint将資源綁在一起
crm(live)resource# cd
crm(live)configure# delete webservice
[root@node1 ~]# crm_mon(将組删除後可看到,三個資源是分散地運作在不同的node上)
============
Last updated: Thu Nov 5 23:54:54 2015
Stack: openais
Current DC: node2.magedu.com - partitionwith quorum
Version: 1.0.12-unknown
2 Nodes configured, 2 expected votes
3 Resources configured.
Online: [ node1.magedu.com node2.magedu.com]
httpd (lsb:httpd): Started node1.magedu.com
webstore (ocf::heartbeat:Filesystem): Startednode2.magedu.com
webip (ocf::heartbeat:IPaddr): Started node1.magedu.com
crm(live)configure# help colocation
crm(live)configure# colocation httpd_with_webstore inf: httpd webstore
crm(live)configure# colocation webstore_with_webip inf: webstore webip
crm(live)configure# order webstore_before_httpd mandatory: httpd webstore
crm(live)configure# order webip_before_httpd mandatory: httpd webip
crm(live)configure# exit
bye
[root@node1 ~]# crm node standby(将目前node備用,并在另一終端打開#crm_mon)
……
httpd (lsb:httpd): Startednode2.magedu.com
webstore (ocf::heartbeat:Filesystem): Started node2.magedu.com
webip (ocf::heartbeat:IPaddr): Started node2.magedu.com
[root@node1 ~]# crm node online
httpd (lsb:httpd): Startednode1.magedu.com
webstore (ocf::heartbeat:Filesystem): Started node1.magedu.com
webip (ocf::heartbeat:IPaddr): Started node1.magedu.com
crm(live)configure# location webip_on_node1webip rule 100: #uname eq node1.magedu.com(位置限制)
crm(live)configure# rsc_defaults resource-stickiness=200(資源粘性)
注:位置限制和資源粘性結合起來決定資源到底在哪個node運作,這兩項都設定後,資源粘性(定義資源樂意運作在目前node),位置限制(傾向性,定義資源樂意運作在哪個node),若在node1上#crm node standby,資源轉移至node2,而當node1重新online後資源不會切換(若目前node1故障不能工作,資源同樣轉移到node2)(如果按例子中設定僅定義位置限制,則在node1上standby後,會切至node2,當node1重新online後,資源又切至node1)
[root@node1 ~]# crm_mon(目前運作在node1)
httpd (lsb:httpd): Startednode1.magedu.com
[root@node1 ~]# service corosync stop(将node1服務停止)
Signaling Corosync Cluster Engine(corosync) to terminate: [ OK ]
Waiting for corosync services tounload:.. [ OK ]
[root@node2 ~]# crm_mon(資源運作在node2上)
Online: [ node2.magedu.com ]
OFFLINE: [ node1.magedu.com ]
本文轉自 chaijowin 51CTO部落格,原文連結:http://blog.51cto.com/jowin/1719779,如需轉載請自行聯系原作者