1. 程式人生 > 實用技巧 >corosync+pacemaker構建HAweb叢集-----pcs資源管理器

corosync+pacemaker構建HAweb叢集-----pcs資源管理器

1.實驗環境

系統環境:centos6.4-i386

兩個節點:172.16.15.8主機名:stu_yxm8.mageduyxm8(別名)

172.16.15.9主機名:stu_yxm9.mageduyxm9(別名)

保證主機名稱和對應的IP地址解析服務可以正常工作:(yxm8、yxm9)

vim/etc/hosts

172.16.15.8stu_yxm8.mageduyxm8

172.16.15.9stu_yxm9.mageduyxm9

建立雙機互信:

yxm8:
# ssh-keygen -t rsa
# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
yxm9: # ssh-keygen -t rsa # ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

2.安裝軟體包、配置實驗環境

yxm8、yxm9上均執行
安裝corosync pacemaker pcs
yum -y install corosync pacemaker pcs
# cd /etc/corosync
# cp corosync.conf.example corosync.conf

編輯corosync.conf檔案,新增如下內容:在yxm8上執行

service {
ver:  0
name: pacemaker
# use_mgmtd: yes
}
aisexec {
user: root
group:  root
}

注意修改組播地址,強烈建議不要使用預設的組播地址。

設定網路地址:bindnetaddr:172.16.0.0

生成節點間通訊用到的密匙檔案:

#corosync-keygen (此處可以選擇安裝軟體生成大量隨機數)
將corosync和authkey複製至yxm9:
# scp -p corosync authkey  yxm9:/etc/corosync/
分別為兩個節點建立corosync生成的日誌檔案所在目錄:
# mkdir /var/log/cluster
# ssh yxm9  'mkdir /var/log/cluster'

啟動corosync服務:

#servicecorosynstart

檢視埠54045405是否已經開始監聽

#ss-uanlp

啟動yxm9上的corosync服務

#sshyxm9servicecorosyncstart

檢視叢集節點狀態

#pcs status
Last updated: Sat Oct 26 21:21:47 2013
Last change: Sat Oct 26 14:37:04 2013 via cibadmin on stu_yxm8.magedu
Stack: classic openais (with plugin)
Current DC: stu_yxm9.magedu - partition with quorum
Version: 1.1.8-7.el6-394e906
2 Nodes configured, 2 expected votes
0 Resources configured.
Online: [ stu_yxm8.magedu stu_yxm9.magedu ]
Full list of resources:

3.使用pcs資源管理器配置叢集屬性

禁用stonith:corosync預設啟用了stonith,而當前叢集並沒有相應的stonith裝置,因此此預設配置目前尚不可用,因此需要使用如下命令禁用stonith

# pcs property set stonith-enabled=false

檢視配資訊

#pcs config
...... ...... ......
Cluster Properties:
dc-version: 1.1.8-7.el6-394e906
cluster-infrastructure: classic openais (with plugin)
expected-quorum-votes: 2
stonith-enabled: false
...... ...... ......

4.為叢集新增資源

corosync支援heartbeatLSBocf等型別的資源代理,目前較為常用的型別為LSBOCF兩類,stonith類專為配置stonith裝置而用;

可以通過如下命令檢視當前集群系統所支援的型別:

# pcs resource standards
ocf
lsb
service
stonith
檢視資源代理的provider:
# pcs resource providers
heartbeat
pacemaker
redhat

為叢集配置一個IPVIP地址,用於實現提供高可用的web服務:

# pcs resource create WebIP ocf:heartbeat:IPaddr ip=172.16.15.188 cidr_netmask=16(掩碼) op monitor interval=30s

檢視資源是否已經啟用:

[[email protected]_yxm8 ~]# pcs status
Last updated: Sat Oct 26 21:53:31 2013
Last change: Sat Oct 26 21:28:58 2013 via cibadmin on stu_yxm8.magedu
Stack: classic openais (with plugin)
Current DC: stu_yxm9.magedu - partition with quorum
Version: 1.1.8-7.el6-394e906
2 Nodes configured, 2 expected votes
2 Resources configured.
Online: [ stu_yxm8.magedu stu_yxm9.magedu ]
Full list of resources:
webip  (ocf::heartbeat:IPaddr):    Started stu_yxm8.magedu

注意:此處同樣可以執行ifconfig命令檢視此地址是否已經在eth0的別名上生效。

通過如下的命令來修改忽略quorum(法定票數)不能滿足的叢集狀態檢查:

# pcs property set no-quorum-policy="ignore"

可以通過以下方式為資源指定預設黏性值:(防止其他節點重啟後,資源被搶奪而在此段時間內服務中斷)

# pcs resource rsc defaults resource-stickiness=100

5.配置web叢集

已經配置好的IP地址資源,將此叢集配置成為一個active/passive模型的webhttpd)服務叢集

首先在各節點啟動httpd服務,正常訪問後,停掉服務,使其開機不能自動啟動:

#service httpd stop
#chkconfig httpd off

httpd服務新增為叢集資源httpd新增為叢集資源有兩處資源代理可用:lsbocf:heartbeat在此使用lsb型別:

# pcs resource create webserver lsb:httpd
檢視配置檔案中生成的定義:
#pcs config
檢視資源啟動狀態:
#pcs status

測試web服務:

203340308.png

6.資源約束

資源約束:

pacemaker提供了三種資源約束方法:

<1>ResourceLocation(資源位置):定義資源可以、不可以或儘可能在哪些節點上執行;

<2>ResourceCollocation(資源排列):排列約束用以定義叢集資源可以或不可以在某個節點上同時執行;

<3>ResourceOrder(資源順序):順序約束定義叢集資源在節點上啟動的順序;

排列約束

對於兩個資源webipwebserver運行於不同節點的問題,可以通過以下命令來解決:

# pcs constraint colocation add webserver webip INFINITY(無窮大)

檢視狀態資訊,觀察兩個節點是否已經執行在同一個節點之上。

順序約束

要確保webserver在某節點啟動之前得先啟動webip可以使用如下命令實現:

# pcs constraint order webip then webserver

檢視定義結果:

#pcs constraint order
Ordering Constraints:
start webip then start webserver


位置約束:

由於HA叢集本身並不強制每個節點的效能相同或相近,因此若希望服務總能在某個效能較強的節點上執行,這可以通過位置約束來實現

eg:如果期望webserver傾向運行於stu_yxm8.magedu的分數為500,則可以使用類似如下命令實現:

語法:location add <id> <resource name> <node> <score>
# pcs constraint location add webserver_on_node1 webserver stu_yxm8.magedu 500

7.檢視最終配置

[[email protected]_yxm8 ~]# pcs config
Corosync Nodes:
Pacemaker Nodes:
 stu_yxm8.magedu stu_yxm9.magedu
Resources:
 Resource: webip (provider=heartbeat type=IPaddr class=ocf)
  Attributes: ip=172.16.15.188 clidr_netmask=16
  Operations: monitor interval=30s
 Resource: webserver (type=httpd class=lsb)
Location Constraints:
  Resource: webserver
    Enabled on: stu_yxm8.magedu (score:500)
Ordering Constraints:
  start webip then start webserver
Colocation Constraints:
  webserver with webip
Cluster Properties:
 dc-version: 1.1.8-7.el6-394e906
 cluster-infrastructure: classic openais (with plugin)
 expected-quorum-votes: 2
 stonith-enabled: false
 no-quorum-policy: ignore

停掉yxm8上的corosync服務

203446910.png

測試web服務

203531766.png

重新啟動yxm8上的corosync服務

203629560.png


轉載於:https://blog.51cto.com/dreamwolf/1323869