本文以兩台 CentOS 6 主機(marvin、sherry)示範如何從零建立一套 corosync + pacemaker 高可用集群,並逐步定義資源、監控、群組與各類約束。
1、節點名稱:集群每個節點的名稱都必須能互相解析。 /etc/hosts 中主機名的正反解析結果必須與 "uname -n" 的輸出一致。 2、時間必須同步,使用網路時間服務器校時。 3、非必須:各節點間能基於 ssh 金鑰認證通信。
[root@marvin heartbeat]# yum install corosync -y
[root@sherry heartbeat]# cd /etc/corosync/ [root@sherry corosync]# ls corosync.conf.example corosync.conf.example.udpu service.d uidgid.d [root@sherry corosync]# cp corosync.conf.example corosync.conf [root@sherry corosync]# vim corosync.conf
compatibility: whitetank # 是否相容 0.8 以前的版本
totem {
version: 2 # 通信協議
secauth: on # 安全認證功能;關閉的話他人知道多播位址即可加入,建議開啟
threads: 0 # 0 表示預設,認證時的並行執行緒數
interface {
ringnumber: 0 # 定義環號,避免心跳訊息循環發送;只有一張網卡就用 0
bindnetaddr: 192.168.1.0
mcastaddr: 225.122.111.111 # 224.0.1.0~238.255.255.255,建議用這組臨時位址
mcastport: 5405 # 多播連接埠
ttl: 1 # 只發一次,避免環路
}
}
logging {
fileline: off
to_stderr: no
to_logfile: yes
logfile: /var/log/cluster/corosync.log
to_syslog: no # 日誌開啟一項即可
debug: off
timestamp: no # 是否記錄時間戳
logger_subsys {
subsys: AMF
debug: off
}
}
amf {
mode: disabled # 與編程相關
}
[root@sherry corosync]# yum install pacemaker -y
注意:安裝後服務不會自動啟動,需要在 corosync.conf 中以 service 插件方式載入:
[root@sherry corosync]# vim corosync.conf
service {
ver: 1
name: pacemaker
}
aisexec {
user: root
group: root
}
# 生產環境請手敲金鑰 [root@sherry corosync]# mv /dev/random /dev/random.bak [root@sherry corosync]# mv /dev/urandom /dev/random [root@sherry corosync]# corosync-keygen [root@sherry corosync]# mv /dev/random /dev/urandom [root@sherry corosync]# mv /dev/random.bak /dev/random
金鑰 authkey 權限應為 400:
[root@sherry corosync]# ll total 24 -r-------- 1 root root 128 May 31 20:54 authkey -rw-r--r-- 1 root root 476 May 31 20:50 corosync.conf -rw-r--r-- 1 root root 2663 May 11 06:27 corosync.conf.example -rw-r--r-- 1 root root 1073 May 11 06:27 corosync.conf.example.udpu drwxr-xr-x 2 root root 4096 May 11 06:27 service.d drwxr-xr-x 2 root root 4096 May 11 06:27 uidgid.d
複製設定到另一節點:
[root@sherry corosync]# scp -P 6789 -p authkey corosync.conf root@marvin:/etc/corosync/
[root@sherry yum.repos.d]# cd /etc/yum.repos.d/ [root@sherry yum.repos.d]# wget https://download.opensuse.org/repositories/network:ha-clustering:Stable/CentOS_CentOS-6/network:ha-clustering:Stable.repo [root@sherry yum.repos.d]# yum install crmsh -y
[root@sherry corosync]# /etc/init.d/corosync start [root@sherry corosync]# /etc/init.d/pacemaker start
corosync 預設啟用 stonith,但當前集群沒有對應的 stonith 裝置,先停用:
crm(live)# configure crm(live)configure# property stonith-enabled=false crm(live)configure# verify crm(live)configure# commit
設定投票策略:
crm(live)configure# property no-quorum-policy=ignore crm(live)configure# verify crm(live)configure# commit
查看目前所有配置:
crm(live)configure# show
node marvin
node sherry
property cib-bootstrap-options: \
have-watchdog=false \
dc-version=1.1.14-8.el6-70404b0 \
cluster-infrastructure="classic openais (with plugin)" \
expected-quorum-votes=2 \
stonith-enabled=false
no-quorum-policy=ignore
先定義一個虛擬 IP:
crm(live)configure# primitive webip ocf:heartbeat:IPaddr params ip=192.168.1.199 crm(live)configure# verify crm(live)configure# commit
確認 IP 已掛上:
[root@marvin ~]# ip addr show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:0c:34:2c brd ff:ff:ff:ff:ff:ff
inet 192.168.1.220/24 brd 192.168.1.255 scope global eth1
inet 192.168.1.199/24 brd 192.168.1.255 scope global secondary eth1
crm(live)configure# primitive webstore ocf:heartbeat:Filesystem \
params device="sherry:/nfsshared/node1" directory="/mnt/nfs/node1" fstype='nfs' \
op monitor interval=20s timeout=40s \
op start timeout=60s \
op stop timeout=60s \
on-fail=restart
crm(live)configure# verify
crm(live)configure# primitive webserver lsb:nginx \
op monitor interval=30s timeout=15s \
on-fail=restart
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# show
node marvin
node sherry
primitive webip IPaddr \
params ip=192.168.1.199
primitive webserver lsb:nginx
property cib-bootstrap-options: ...
若要調整監控節奏,可直接編輯:
crm(live)configure# monitor webserver 30s:15s # 每 30 秒監控一次,15 秒逾時
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# show
primitive webserver lsb:nginx \
meta target-role=Stopped \
op monitor interval=30s timeout=15s
crm(live)# status
Last updated: Wed Jun 1 20:38:32 2016
Last change: Wed Jun 1 20:36:32 2016 by root via cibadmin on sherry
Stack: classic openais (with plugin)
Current DC: marvin (version 1.1.14-8.el6-70404b0) - partition with quorum
2 nodes and 2 resources configured, 2 expected votes
Online: [ marvin sherry ]
Full list of resources:
webip (ocf::heartbeat:IPaddr): Started marvin
webserver (lsb:nginx): Started sherry
停止資源:
crm(live)# resource crm(live)resource# stop webserver
清理資源狀態(清除 fail-count):
crm(live)resource# cleanup webserver Cleaning up webserver on marvin, removing fail-count-webserver Cleaning up webserver on sherry, removing fail-count-webserver * The configuration specifies that 'webserver' should remain stopped Waiting for 2 replies from the CRMd.. OK
先定義好資源,再加入群組:
crm(live)configure# group webservice webip webserver
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# show
primitive webip IPaddr \
params ip=192.168.1.199
primitive webserver lsb:nginx
group webservice webip webserver
刪除群組(底下的資源仍存在):
crm(live)resource# stop webservice crm(live)configure# delete webservice
# 節點離線(資源自動轉移) crm(live)# node crm(live)node# standby marvin # 節點上線 crm(live)node# online marvin # 節點清理(清除該節點上的資源狀態) crm(live)node# clearstate marvin
crm(live)configure# colocation webserver_and_webip inf: webserver webip crm(live)configure# verify crm(live)configure# commit crm(live)configure# show xml <rsc_colocation id="webserver_and_webip" score="INFINITY" rsc="webserver" with-rsc="webip"/> # webserver 跟著 webip 走
crm(live)configure# order webip-before-webserver mandatory: webip webserver crm(live)configure# verify crm(live)configure# commit crm(live)configure# show xml <rsc_order id="webip-before-webserver" kind="Mandatory" first="webip" then="webserver"/>
crm(live)configure# location webip_on_marvin webip 200: marvin crm(live)configure# verify crm(live)configure# commit
crm(live)# status
Online: [ marvin sherry ]
Full list of resources:
webip (ocf::heartbeat:IPaddr): Started marvin
webserver (lsb:nginx): Started marvin