前提:
1)本配置共有兩個測試節點,分別node1.magedu.com和node2.magedu.com,相的IP地址分別爲172.16.100.67和172.16.100.68;
2)集羣服務爲nginx服務;
3)提供web服務的地址爲172.16.100.53,即vip;
4)系統爲CentOS 7.3 x86_64
1、準備工作
爲了配置一臺Linux主機成爲HA的節點,通常需要做出如下的準備工作:
1) 所有節點的主機名稱和對應的IP地址解析服務可以正常工作,且每個節點的主機名稱需要跟”uname -n“命令的結果保持一致;因此,需要保證兩個節點上的/etc/hosts文件均爲下面的內容:
172.16.100.67 node1.magedu.com node1
172.16.100.68 node2.magedu.com node2
爲了使得重新啓動系統後仍能保持如上的主機名稱,還分別需要在各節點執行類似如下的命令:
Node1:
hostnamectl set-hostname node1.magedu.com
hostname node1.magedu.com
Node2:
hostnamectl set-hostname node2.magedu.com
hostname node2.magedu.com
2) 設定兩個節點可以基於密鑰進行ssh通信,這可以通過類似如下的命令實現:
Node1:
ssh-keygen -t rsa -P ''
ssh-copy-id -i ~/.ssh/id_rsa.pub root@node2.magedu.com
Node2:
ssh-keygen -t rsa -P ''
ssh-copy-id -i ~/.ssh/id_rsa.pub root@node1.magedu.com
3) 多節點時間同步
2、安裝並啓動集羣
2.1 安裝並啓動pcsd
Node1 AND Node2:
yum install -y pacemaker pcs psmisc policycoreutils-python
systemctl start pcsd.service
systemctl enable pcsd.service
echo 'magedu.com' | passwd --stdin hacluster
2.2 配置corosync
Node1 OR Node2:
pcs cluster auth node1.magedu.com node2.magedu.com
Username: hacluster
Password:
node1.magedu.com: Authorized
node2.magedu.com: Authorized
pcs cluster setup --name mycluster node1.magedu.com node2.magedu.com
Shutting down pacemaker/corosync services...
Redirecting to /bin/systemctl stop pacemaker.service
Redirecting to /bin/systemctl stop corosync.service
Killing any remaining services...
Removing all cluster configuration files...
node1.magedu.com: Succeeded
node2.magedu.com: Succeeded
2.3 啓動集羣
Nod1 OR Node2:
```
node1.magedu.com: Starting Cluster...
node2.magedu.com: Starting Cluster...
上面的命令相當於在各節點分別執行如下命令:
systemctl start corosync.service
systemctl start pacemaker.service
2.4 檢查集羣啓動狀態
檢查各節點通信狀態(顯示爲no faults即爲OK):
corosync-cfgtool -s
Printing ring status.
Local node ID 1
RING ID 0
id = 172.16.100.67
status = ring 0 active with no faults
檢查集羣成員關係及Quorum API:
corosync-cmapctl | grep members
runtime.totem.pg.mrp.srp.members.1.config_version (u64) = 0
runtime.totem.pg.mrp.srp.members.1.ip (str) = r(0) ip(172.16.100.67)
runtime.totem.pg.mrp.srp.members.1.join_count (u32) = 1
runtime.totem.pg.mrp.srp.members.1.status (str) = joined
runtime.totem.pg.mrp.srp.members.2.config_version (u64) = 0
runtime.totem.pg.mrp.srp.members.2.ip (str) = r(0) ip(172.16.100.68)
runtime.totem.pg.mrp.srp.members.2.join_count (u32) = 1
runtime.totem.pg.mrp.srp.members.2.status (str) = joined
pcs status corosync
Membership information
Nodeid Votes Name
1 1 node1.magedu.com (local)
2 1 node2.magedu.com
檢查pacemaker的啓動狀況:
~]# ps axf
…… ……
4777 ? Ss 0:00 /usr/sbin/pacemakerd -f
4778 ? Ss 0:00 _ /usr/libexec/pacemaker/cib
4779 ? Ss 0:00 _ /usr/libexec/pacemaker/stonithd
4780 ? Ss 0:00 _ /usr/libexec/pacemaker/lrmd
4781 ? Ss 0:00 _ /usr/libexec/pacemaker/attrd
4782 ? Ss 0:00 _ /usr/libexec/pacemaker/pengine
4783 ? Ss 0:00 _ /usr/libexec/pacemaker/crmd
最後查看pacemaker集羣狀態:
~]# pcs status
Cluster name: mycluster
WARNING: no stonith devices and stonith-enabled is not false
Last updated: Fri Oct 16 16:06:00 2015
Last change: Fri Oct 16 15:59:29 2015
Stack: corosync
Current DC: node2.magedu.com (2) - partition with quorum
Version: 1.1.12-a14efad
2 Nodes configured
0 Resources configured
Online: [ node1.magedu.com node2.magedu.com ]
Full list of resources:
PCSD Status:
node1.magedu.com: Online
node2.magedu.com: Online
Daemon Status:
corosync: active/disabled
pacemaker: active/disabled
pcsd: active/disabled
其中的WARNING信息是因爲當前集羣系統開啓了stonith-enabled屬性但卻沒有配置stonith設備所致。使用crm_verify命令也可檢查出此錯誤。
~]# crm_verify -L -V
error: unpack_resources: Resource start-up disabled since no STONITH resources have been defined
error: unpack_resources: Either configure some or disable STONITH with the stonith-enabled option
error: unpack_resources: NOTE: Clusters with shared data need STONITH to ensure data integrity
Errors found during check: config not valid
可以使用如下命令禁用集羣的stonith特性,而後再次檢查將不再出現錯誤信息。
~]# pcs property set stonith-enabled=false
~]# crm_verify -L -V
3、pcs命令簡介
pcs命令可以使用pacemaker集羣的全生命週期管理,每一種管理功能均通過相應的子命令實現。
cluster:Configure cluster options and nodes
resource:Manage cluster resources
stonith:Configure fence devices
constraint:Set resource constraints
property:Set pacemaker properties
acl:Set pacemaker access control lists
status:View cluster status
config:View and manage cluster configuration
4、配置集羣屬性
corosync有許多全局配置屬性,例如前面修改的stonith-enabled即爲此類屬性之一。pcs的property子命令可用於顯示或設置集羣的各屬性。下面的命令可以獲取其詳細使用幫助。
~]# pcs property –help
其相關的管理命令有:
list|show [ | –all | –defaults]
set [–force] [–node ] =[]
unset [–node ]