corosync+pacemaker使用crmsh構建高可用叢集
一、叢集簡介
引自suse官方關於corosync的高可用叢集的框架圖:
由圖,我們可以看到,suse官方將叢集的Architecture Layers分成四層。最低層Messaging/Infrastructure Layer提供了HeartBeat節點間傳遞心跳資訊,即為心跳層。第二層Membership Layer層為叢集事務決策層,決定了哪些節點作為叢集節點,並傳遞給叢集內所有節點,如果叢集中成員擁有的法定票數不大於半數,該怎麼作出決策等,通俗點講,就是投票系統,同時,還提供了構建成員關係的檢視。第三層Resource Allocation Layer為資源分配層,包含了crm,cib;crm作為資源分配層的核心元件,包括了Local Resource Manager、Transition Engine、Policy Engine三大元件,這三大元件都是在crm的基礎之上得以實現的,同時,每個節點的crm還維持本節點的cib。在crm中選出一個節點作為DC(Designated Coordinator),DC負責維持主cib,所以所有cib的修改都由DC來實現,而後DC同步給其他節點,一個叢集只有一個DC。cib是一個在記憶體中xml格式書寫的儲存著叢集配置各條目的配置資訊(叢集狀態,各節點,各資源,約束關係),可以使用gui修改,也可以使用crm_sh命令列修改。Transition Engine和Policy Engine,這兩個元件只有DC擁有,PE是當叢集狀態改變時,根據配置檔案中的約束資訊,節點粘性計算要轉移到的節點一應狀態資訊,並寫入cib,TE根據PE作出的指示進行轉移資源等操作。DC將改變的狀態資訊傳至各crm,各節點的crm將要作出的改變傳給lrm作出相應的更改,lrm有start|stop|monitor三種狀態資訊,在接受crm指令作出更改後,將狀態資訊傳回crm。說到lrm了,下面我們來看看第四層Resources Layer層,即資源代理層,負責根據lrm傳來的指令,找到對應的執行指令碼執行,資源代理層有lsb格式,ocf格式的指令碼,ocf相對於lsb,除了能接收更多引數外,還能提供監控功能。
HA Cluster的工作模型
A/P:兩節點叢集,active,passive:工作於主備模型
HA Services通常只有一個;HA resources可能會有多個
A/A:兩節點叢集,active/active;工作於雙主模型
N-M:N個節點,M個服務;通常N>M
N-N:N個節點,N個服務
HA Cluster的組合方式:
heartbeat v2(v1 crm)
heartbeat v2(v2 crm)
corosync + pacemaker
cman + rgmanager
二、corosync + pacemaker
corosync + pacemaker框架圖(引自馬哥文件)
Pacemaker Stack棧由眾多元件組成,其主要元件為corosync + pacemaker。corosync能實現底層傳遞心跳資訊,但不提供資源管理器,因此,由工作於corosync之上的pacemaker提供資源管理的功能,並藉助Resource Agents和Cluster Glue為不具備高可用的服務與資源提供高可用服務。
框架圖的箭頭描述了編譯安裝過程中的依賴關係,首先安裝corosync和cluster glue,其次安裝Resource Agents,之後再安裝pacemaker,接著安裝distibuted lock manager(分散式鎖管理器),最後安裝cLVM2,GFS2,OCFS2(非必須)。cLVM2,GFS2,OCFS2提供叢集檔案系統。
colosync
OpenAIS:Application Interface Standard 開放應用介面標準
提供了一種叢集模式,包含叢集框架,叢集成員管理,通訊方式,叢集監測,但沒有叢集資源管理。因其為開源的,所以每種分支包含的元件不同。其主要分支有:picacho,witetank,wilson。colosync是由wilson把關於OpenAIS的功能獨立出來形成的專案,實際上colosync是一個叢集管理引擎,只是OpenAIS的一個子元件。從此OpenAIS分裂為兩個專案,corosync和wilson。
pacemaker
(引自pacemaker官方站點http://clusterlabs.org/wiki/Main_Page)
上圖描述了corosync + pacemaker的四種工作模型。
配置前準備工作
等同HA叢集部署前的準備工作 ----> 看這裡
實現web高可用 ----> 看這裡
heartbeatV3編譯安裝 ----> 看這裡
MYSQL高可用 ----> 看這裡
ipvs+ldirectord實現ipvs高可用----> 看這裡
三、corosync + pacemaker部署
配置情況:
系統版本:CentOS6.7 x86_64
corosync.x86_64 0:1.4.7-5.el6
pacemaker.x86_64 0:1.1.14-8.el6_8.2
node1:192.168.0.15
node2:192.168.0.16
web server VIP:192.168.0.25
node1:
[[email protected] ~]# yum install corosync pacemaker
[[email protected] ~]# cd /etc/corosync/
[[email protected] corosync]# cp corosync.conf.example corosync.conf
[[email protected] corosync]# vim corosync.conf
##totem定義叢集內各節點間是如何通訊的,totem本是一種協議,專用於corosync專用於各節點間的協議,協議是有版本的
totem {
##版本號
version: 2
##安全認證on|off,使用corosync-keygen命令生成金鑰
secauth: on
##用於安全認證開啟的執行緒,0為不基於執行緒模式工作
threads: 0
##介面設定
interface {
##環數量,如果一個主機有多塊網絡卡,避免心跳資訊迴流
ringnumber: 0
##多播監聽的網路地址(本機網段為192.168.0.0/24)
bindnetaddr: 192.168.0.0
##多播地址,可設定為239.255.x.x
mcastaddr: 239.165.17.17
##多播地址監聽的埠
mcastport: 5405
##生存時間為1
ttl: 1
}
}
##定義日誌資訊
logging {
##是否記錄fileline
fileline: off
##是否將日誌發往標準錯誤輸出(螢幕)
to_stderr: no
##是否記錄在日誌檔案中
to_logfile: yes
##日誌檔案目錄
logfile: /var/log/cluster/corosync.log
##是否將日誌發往系統日誌
to_syslog: no
##除錯
debug: off
##是否開啟時間戳
timestamp: on
##是否包含AMF元件的日誌資訊
logger_subsys {
subsys: AMF
debug: off
}
}
pacemaker與corosync結合有兩種方式,一是作為corosync的外掛,二是pacemaker作為單獨的服務。不過一般在CentOS6的系統上一般是將pacemaker做為corosync的外掛,用corosync啟動pacemaker服務。因此,需要在corosync的配置檔案中新增關於pacemaker的內容。
service {
ver: 0
name: pacemaker
##是否讓pacemaker獨立啟動mgmtd程序(可省略)
use_mgmtd: yes
}
##指明執行外掛的使用者與組(可省略)
aisexec{
user: root
group: root
}
驗證網絡卡是否支援多播,如沒有MULTICAST,手動開啟
[[email protected] corosync]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:de:67:fa brd ff:ff:ff:ff:ff:ff
建立安全認證檔案,如/dev/random中的隨機數不足1024,敲擊鍵盤使其增加值1024
[[email protected] corosync]# corosync-keygen
Corosync Cluster Engine Authentication key generator.
Gathering 1024 bits for key from /dev/random.
Press keys on your keyboard to generate entropy.
Writing corosync key to /etc/corosync/authkey.
node2:
[[email protected] ~]# yum install corosync pacemaker
將node1的配置檔案複製到node2
[[email protected] corosync]# scp -p authkey corosync.conf node2:/etc/corosync/
authkey 100% 128 0.1KB/s 00:00
corosync.conf 100% 2757 2.7KB/s 00:00
啟動服務
[[email protected] corosync]# service corosync start; ssh node2 'service corosync start'
檢視corosync引擎是否正常啟動:
[[email protected] corosync]# grep -e "Corosync Cluster Engine" -e "configuration file" /var/log/cluster/corosync.log
Nov 21 12:36:25 corosync [MAIN ] Corosync Cluster Engine ('1.4.7'): started and ready to provide service.
Nov 21 12:36:25 corosync [MAIN ] Successfully read main configuration file '/etc/corosync/corosync.conf'
檢視初始化成員節點通知是否正常發出:
[[email protected] corosync]# grep TOTEM /var/log/cluster/corosync.log
Nov 21 12:36:26 corosync [TOTEM ] Initializing transport (UDP/IP Multicast).
Nov 21 12:36:26 corosync [TOTEM ] Initializing transmit/receive security: libtomcrypt SOBER128/SHA1HMAC (mode 0).
檢查啟動過程中是否有錯誤產生:
[[email protected] corosync]# grep ERROR: /var/log/cluster/corosync.log | grep -v unpack_resources
##下面的錯誤資訊表示packmaker不久之後將不再作為corosync的外掛執行,因此,建議使用cman作為叢集基礎架構服務;此處可安全忽略。
Nov 21 12:36:26 corosync [pcmk ] ERROR: process_ais_conf: You have configured a cluster using the Pacemaker plugin for Corosync. The plugin is not supported in this environment and will be removed very soon.
Nov 21 12:36:26 corosync [pcmk ] ERROR: process_ais_conf: Please see Chapter 8 of 'Clusters from Scratch' (http://www.clusterlabs.org/doc) for details on using Pacemaker with CMAN
Nov 21 12:36:28 corosync [pcmk ] ERROR: pcmk_wait_dispatch: Child process mgmtd exited (pid=1859, rc=100)
檢視pacemaker是否正常啟動:
[[email protected] corosync]# grep pcmk_startup /var/log/cluster/corosync.log
Nov 21 12:36:26 corosync [pcmk ] info: pcmk_startup: CRM: Initialized
Nov 21 12:36:26 corosync [pcmk ] Logging: Initialized pcmk_startup
Nov 21 12:36:26 corosync [pcmk ] info: pcmk_startup: Maximum core file size is: 18446744073709551615
Nov 21 12:36:26 corosync [pcmk ] info: pcmk_startup: Service: 9
Nov 21 12:36:26 corosync [pcmk ] info: pcmk_startup: Local hostname: node1
四、安裝crmsh
官方下載地址:https://github.com/ClusterLabs/crmsh
發行版本rpm包:http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/
[[email protected] ~]# vim /etc/yum.repos.d/CentOS-Base.repo
[network_ha-clustering_Stable]
name=Stable High Availability/Clustering packages (CentOS_CentOS-6)
type=rpm-md
baseurl=http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/
gpgcheck=1
gpgkey=http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6//repodata/repomd.xml.key
enabled=1
#配置好yum源後可以直接yum了,注意crmsh和pssh都要裝上
[[email protected] ~]# yum install crmsh
[[email protected] ~]# yum install pssh*
#檢視crm的狀態
[[email protected] corosync]# crm status
Last updated: Mon Nov 21 14:50:27 2016 Last change: Mon Nov 21 14:50:26 2016 by hacluster via crmd on node1
Stack: classic openais (with plugin)
Current DC: node1 (version 1.1.14-8.el6_8.2-70404b0) - partition with quorum
2 nodes and 0 resources configured, 2 expected votes
Online: [ node1 node2 ]
五、crmsh命令簡介
1、檢視配置資訊
crm(live)# configure
crm(live)configure# show
node node1
node node2
property cib-bootstrap-options: \
dc-version=1.1.14-8.el6_8.2-70404b0 \
cluster-infrastructure="classic openais (with plugin)" \
expected-quorum-votes=2
2、因為無stonith裝置,所以禁用stonith,stonith的值為布林型別,true和false
crm(live)configure# property stonith-enabled=false
3、提交配置
crm(live)configure# commit
4、介紹node子命令
crm(live)configure# cd .. #返回上一級
crm(live)# node
crm(live)node# help
attribute Manage attributes
clearstate Clear node state #清理當前node的狀態資訊
delete Delete node #刪除節點
fence Fence node
maintenance Put node into maintenance mode
online Set node online #將當前節點重新上線,standby轉為online
ready Put node into ready mode
server Show node hostname or server address
show Show node #顯示當前所有節點
standby Put node into standby #將當前節點轉為備用
status Show nodes' status as XML
status-attr Manage status attributes
utilization Manage utilization attributes
cd Navigate the level structure
help Show help (help topics for list of topics)
ls List levels and commands #指明級別和子命令列出當前可用節點
quit Exit the interactive shell
up Go back to previous level
5、介紹resource子命令
crm(live)# resource
crm(live)resource# help
ban Ban a resource from a node #禁止資源在一個節點
cleanup Cleanup resource status #清理資源狀態
constraints Show constraints affecting a resource #顯示影響資源的約束
demote Demote a master-slave resource #降級主從資源
failcount Manage failcounts #管理員錯誤狀態統計資料
locate Show the location of resources #顯示資源的位置
maintenance Enable/disable per-resource maintenance mode #啟用/禁用每個資源維護模式
manage Put a resource into managed mode #資源定義為可被管理狀態
meta Manage a meta attribute #管理meta 屬性
move Move a resource to another node #將資源移動到另一個節點
operations Show active resource operations #顯示活動的資源的操作
param Manage a parameter of a resource #管理資源引數
promote Promote a master-slave resource
refresh Refresh CIB from the LRM status
reprobe Probe for resources not started by the CRM
restart Restart resources #重啟一個資源
scores Display resource scores #檢視資源
secret Manage sensitive parameters
start Start resources #開啟一個資源
status Show status of resources #資源當前狀態
stop Stop resources #停止一個資源
trace Start RA
unmanage Put a resource into unmanaged mode #資源定義為不可被管理狀態
untrace Stop RA tracing
utilization Manage a utilization attribute
6、介紹資源代理RA子命令
crm(live)# ra
crm(live)ra# help
classes List classes and providers #列出資源代理型別
info Show meta data for a RA #顯示資源代理的幫助資訊
list List RA for a class (and provider) #列出資源代理型別中所擁有的資源代理
providers Show providers for a RA and a class
validate Validate parameters for RA
7、介紹configure子命令
node define a cluster node #定義一個叢集節點
primitive define a resource #定義資源
monitor add monitor operation to a primitive #對一個資源新增監控選項(如超時時間,啟動失敗後的操作)
group define a group #定義一個組型別(包含一個或多個資源,這些資源可通過“組”這個資源統一進行排程)
clone define a clone #定義一個克隆型別(可以在同一個叢集內的多個節點執行多份克隆)
ms define a master-slave resource #定義一個主從型別(叢集內的節點只能有一個執行主資源,其它從的做備用)
rsc_template define a resource template #定義一個資源模板
location a location preference #定義位置約束優先順序(預設運行於那一個節點(如果位置約束的值相同,預設傾向性那一個高,就在那一個節點上執行))
colocation colocate resources #排列約束資源(多個資源在一起的可能性)
order order resources #順序約束,定義資源在同一個節點上啟動時的先後順序
rsc_ticket resources ticket dependency
property set a cluster property #設定叢集屬性
rsc_defaults set resource defaults #設定資源預設屬性(粘性)
fencing_topology node fencing order #隔離節點順序
role define role access rights #定義角色的訪問許可權
user define user access rights #定義用使用者訪問許可權
op_defaults set resource operations defaults #設定資源預設選項
schema set or display current CIB RNG schema
show display CIB objects #顯示叢集資訊庫對
edit edit CIB objects #編輯叢集資訊庫物件(vim模式下編輯)
filter filter CIB objects #過濾CIB物件
delete delete CIB objects #刪除CIB物件
default-timeouts set timeouts for operations to minimums from the meta-data
rename rename a CIB object #重新命名CIB物件
modgroup modify group #改變資源組
refresh refresh from CIB #重新讀取CIB資訊
erase erase the CIB #清除CIB資訊
ptest show cluster actions if changes were committed
rsctest test resources as currently configured
cib CIB shadow management
cibstatus CIB status management and editing
template edit and import a configuration from a template
commit commit the changes to the CIB #將更改後的資訊提交寫入CIB
verify verify the CIB with crm_verify #CIB語法驗證
upgrade upgrade the CIB to version 1.0
save save the CIB to a file #將當前CIB匯出到一個檔案中(匯出的檔案存於切換crm 之前的目錄)
load import the CIB from a file #從檔案內容載入CIB
六、使用crmsh配置pacemaker
配置兩節點的corosync/pacemaker叢集,設定兩個全域性屬性
stonith-enable=false
no-quorum-policy=ignore
crm(live)configure# property stonith-enabled=false
crm(live)configure# property no-quorum-policy=ignore
crm(live)configure# commit
配置web高可用叢集
crm(live)# cd configure
crm(live)configure# primitive webip ocf:heartbeat:IPaddr params ip=192.168.0.25 nic=eth0 cidr_netmask=24
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# primitive webserver lsb:httpd
crm(live)configure# verify
crm(live)configure# commit
檢視資源執行
[[email protected] ~]# crm status
Last updated: Mon Nov 21 17:11:54 2016 Last change: Mon Nov 21 17:11:08 2016 by root via cibadmin on node1
Stack: classic openais (with plugin)
Current DC: node2 (version 1.1.14-8.el6_8.2-70404b0) - partition with quorum
2 nodes and 2 resources configured, 2 expected votes
Online: [ node1 node2 ]
Full list of resources:
webip (ocf::heartbeat:IPaddr): Started node1
webserver (lsb:httpd): Started node2
高可用叢集預設為資源平均分配,因此我們要通過組或者約束使資源在同一個節點
crm(live)configure# group webservice webip webserver
crm(live)configure# verify
crm(live)configure# commit
檢視資源執行
[[email protected] ~]# crm status
Last updated: Mon Nov 21 17:16:23 2016 Last change: Mon Nov 21 17:16:08 2016 by root via cibadmin on node1
Stack: classic openais (with plugin)
Current DC: node2 (version 1.1.14-8.el6_8.2-70404b0) - partition with quorum
2 nodes and 2 resources configured, 2 expected votes
Online: [ node1 node2 ]
Full list of resources:
Resource Group: webservice
webip (ocf::heartbeat:IPaddr): Started node1
webserver (lsb:httpd): Started node1
測試:
通過排列約束定義資源
crm(live)configure# delete webservice
crm(live)configure# commit
crm(live)configure# colocation webserver_with_webip inf: webserver webip
crm(live)configure# verify
crm(live)configure# commit
可以看到,ip與server又在一起了
[[email protected] ~]# crm status
Last updated: Mon Nov 21 17:35:02 2016 Last change: Mon Nov 21 17:34:43 2016 by root via cibadmin on node1
Stack: classic openais (with plugin)
Current DC: node2 (version 1.1.14-8.el6_8.2-70404b0) - partition with quorum
2 nodes and 2 resources configured, 2 expected votes
Online: [ node1 node2 ]
Full list of resources:
webip (ocf::heartbeat:IPaddr): Started node1
webserver (lsb:httpd): Started node1
通過順序約束定義資源啟動順序
crm(live)configure# order webip_before_webserver Mandatory: webip webserver
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# show
node node1
node node2
primitive webip IPaddr \
params ip=192.168.0.25 nic=eth0 cidr_netmask=24
primitive webserver lsb:httpd
order webip_before_webserver Mandatory: webip webserver
colocation webserver_with_webip inf: webserver webip
property cib-bootstrap-options: \
dc-version=1.1.14-8.el6_8.2-70404b0 \
cluster-infrastructure="classic openais (with plugin)" \
expected-quorum-votes=2 \
stonith-enabled=false \
no-quorum-policy=ignore
通過位置約束定義資源傾向性
crm(live)configure# location webip_on_node2 webip rule 50: #uname eq node2
crm(live)configure# show
node node1
node node2
primitive webip IPaddr \
params ip=192.168.0.25 nic=eth0 cidr_netmask=24
primitive webserver lsb:httpd
order webip_before_webserver Mandatory: webip webserver
location webip_on_node2 webip \
rule 50: #uname eq node2
colocation webserver_with_webip inf: webserver webip
property cib-bootstrap-options: \
dc-version=1.1.14-8.el6_8.2-70404b0 \
cluster-infrastructure="classic openais (with plugin)" \
expected-quorum-votes=2 \
stonith-enabled=false \
no-quorum-policy=ignore
crm(live)configure# verify
crm(live)configure# commit
可以看到資源都轉移到node2上了
[[email protected] ~]# crm status
Last updated: Mon Nov 21 17:49:08 2016 Last change: Mon Nov 21 17:47:58 2016 by root via cibadmin on node1
Stack: classic openais (with plugin)
Current DC: node2 (version 1.1.14-8.el6_8.2-70404b0) - partition with quorum
2 nodes and 2 resources configured, 2 expected votes
Online: [ node1 node2 ]
Full list of resources:
webip (ocf::heartbeat:IPaddr): Started node2
webserver (lsb:httpd): Started node2
此外,可以在全域性配置中定義當前資源對當前節點的粘性
crm(live)configure# property default-resource-stickiness=50
crm(live)configure# verify
crm(live)configure# commit
注意:資源粘性預設大於資源的傾向性
七、配置叢集對資源的監控
因為corosync+pacemaker叢集預設對節點高可用,但是對於節點上資源的執行狀態無法監控,因此,我們要配置叢集對於資源的監控,在資源因意外情況下,無法提供服務時,對資源提供高可用。
crm(live)configure# primitive webip ocf:heartbeat:IPaddr params ip=192.168.0.25 nic=eth0 cidr_netmask=24 op monitor interval=10s timeout=20s
crm(live)configure# verify
crm(live)configure# commit
注意:timeout時間不得少於20s,否則會提示
crm(live)configure# verify
WARNING: webip: specified timeout 10s for monitor is smaller than the advised 20s
crm(live)configure# primitive webserver lsb:httpd op monitor interval=10s timeout=20s
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# group webservice webip webserver
crm(live)configure# verify
crm(live)configure# commit
此時,資源運行於node1
[[email protected] ~]# crm status
Last updated: Mon Nov 21 18:09:43 2016 Last change: Mon Nov 21 18:09:23 2016 by root via cibadmin on node1
Stack: classic openais (with plugin)
Current DC: node2 (version 1.1.14-8.el6_8.2-70404b0) - partition with quorum
2 nodes and 2 resources configured, 2 expected votes
Online: [ node1 node2 ]
Full list of resources:
Resource Group: webservice
webip (ocf::heartbeat:IPaddr): Started node1
webserver (lsb:httpd): Started node1
此時,在node1上手動結束httpd,測試監控
[[email protected] ~]# killall httpd
[[email protected] ~]# ps aux | grep httpd
root 5567 0.0 0.1 103304 888 pts/1 S+ 18:13 0:00 grep httpd
[[email protected] ~]# ps aux | grep httpd
root 5637 0.0 0.7 175304 3760 ? Ss 18:13 0:00 /usr/sbin/httpd
apache 5639 0.0 0.4 175304 2432 ? S 18:13 0:00 /usr/sbin/httpd
apache 5640 0.0 0.4 175304 2432 ? S 18:13 0:00 /usr/sbin/httpd
apache 5641 0.0 0.5 175304 2448 ? S 18:13 0:00 /usr/sbin/httpd
apache 5642 0.0 0.4 175304 2432 ? S 18:13 0:00 /usr/sbin/httpd
apache 5643 0.0 0.4 175304 2432 ? S 18:13 0:00 /usr/sbin/httpd
apache 5644 0.0 0.4 175304 2432 ? S 18:13 0:00 /usr/sbin/httpd
apache 5645 0.0 0.4 175304 2432 ? S 18:13 0:00 /usr/sbin/httpd
apache 5646 0.0 0.4 175304 2432 ? S 18:13 0:00 /usr/sbin/httpd
root 5654 0.0 0.1 103304 884 pts/1 S+ 18:13 0:00 grep httpd
可以看到,httpd又被啟動了