1. 程式人生 > >Centos 6.7高可用web集群corosync+pacemaker實現方案

Centos 6.7高可用web集群corosync+pacemaker實現方案

應用 pen security ready 錯誤信息 standard suse 資源 好的

實驗環境:

1、本實驗共有兩個測試節點:172.16.0.12 , 172.16.0.13

2、集群服務為apache的httpd服務

3、提供web服務的VIP為 172.16.0.99

4、系統為centos 6.7

5、本網絡內有一臺時間同步服務器,172.16.0.111



實驗前準備:

1、各節點主機名互相解析

# vim /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

172.16.0.12 node1

172.16.0.13 node2


# vim /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

172.16.0.12 node1

172.16.0.13 node2

2、修改兩個節點的主機名

node1:

# sed -i 's@\(HOSTNAME=\).*@\1node1@g' /etc/sysconfig/network

node2:

# sed -i 's@\(HOSTNAME=\).*@\1node1@g' /etc/sysconfig/network

3、設定兩個節點基於密鑰進行通信

node1:

# ssh-keygen -t rsa

# ssh-copy-id -i ~/.ssh/id_rsa.pub root@node2

node2:

# ssh-keygen -t rsa

# ssh-copy-id -i ~/.ssh/id_rsa.pub root@node1


4、同步兩個節點的時間

node1:

#ntpdate 172.16.0.111

10 Jan 11:13:03 ntpdate[2195]: step time server 172.16.0.111 offset -28799.600512 sec

node2:

# ntpdate 172.16.0.111

10 Jan 11:14:20 ntpdate[5681]: step time server 172.16.0.111 offset 200348.267958 sec

同步後測試:

# date; ssh node2 'date'

Wed Jan 10 11:15:54 CST 2018

Wed Jan 10 11:15:54 CST 2018

實驗過程

1、安裝corosync和pacemaker:

node1:

# yum -y install corosync pacemaker

node2:

# yum -y install corosync pacemaker



2、配置corosync (以下命令在node1上執行)

#cd /etc/corosync

#cp corosync.conf.example corosync.conf

#vim corosync.conf

compatibility: whitetank #這個表示是否兼容0.8之前的版本

totem { #圖騰,這是用來定義集群中各節點中是怎麽通信的以及參數

version: 2 #圖騰的協議版本,它是種協議,協議是有版本的,它是用於各節點互相通信的協議,這是定義版本的

secauth: on #表示安全認證功能是否啟用的,此處我們開啟

threads: 0 #實現認證時的並行線程數,0表示默認配置

interface { # 指定在哪個接口上發心跳信息的,它是個子模塊

? ringnumber: 0 #環數目

bindnetaddr: 172.16.0.0 # 綁定的網絡地址,此處改為節點主機所處的網段

mcastaddr: 239.255.1.1 #多播地址,一對多通信,239.255.x.x都可以

mcastport: 5405 # 多播端口

ttl: 1 # 表示只向外播一次

}

}

logging { # 跟日誌相關

fileline: off

to_stderr: no # 表示是否需要發送到錯誤輸出

to_logfile: yes #是不是送給日誌文件

to_syslog: no #是不是送給系統日誌

logfile: /var/log/cluster/corosync.log #日誌文件路徑

debug: off #是否啟動調試

timestamp: on #日誌是否需要記錄時間戳

logger_subsys { #日誌的子系統

subsys: AMF

debug: off

}

}

amf { # 跟編程接口相關的

mode: disabled

}

# 註:以下紅色內容為我們手動添加的內容

service { #定義一個服務來啟動pacemaker

ver: 0 #定義版本

name: pacemaker #這個表示啟動corosync時會自動啟動pacemaker

# use_mgmtd: yes

}

aisexec { #表示啟動ais的功能時以哪個用戶的身份去運行的

user: root

group: root #其實這個塊定義不定義都可以,corosync默認就是以root身份去運行的

}


3、生成節點間通信用到的密鑰文件:

# corosync-keygen

將corosync.conf和authkey復制至node2:

# scp -p corosync.conf authkey node2:/etc/corosync/

註:生成密鑰文件需要確保熵池中有足夠的隨機數來使用,可以去ftp服務器或者互聯網上下載一個大文件生成大量的隨機數。


4、啟動corosync服務:

# service corosync start;ssh node2 'service corosync start'

# ss -tunl | grep 5405

udp UNCONN 0 0 172.16.0.12:5405 *:*

udp UNCONN 0 0 239.255.1.1:5405 *:*

此時多播地址已經監聽在5405端口上,說明沒有問題


查看corosync引擎是否正常啟動:

# grep -e "Corosync Cluster Engine" -e "configuration file" /var/log/cluster/corosync.log

Jan 10 14:38:25 corosync [MAIN ] Corosync Cluster Engine ('1.4.7'): started and ready to provide service.

Jan 10 14:38:25 corosync [MAIN ] Successfully read main configuration file '/etc/corosync/corosync.conf'.


查看初始化成員節點通知是否正常發出:

# grep TOTEM /var/log/cluster/corosync.log

Jan 10 14:38:25 corosync [TOTEM ] Initializing transport (UDP/IP Multicast).

Jan 10 14:38:25 corosync [TOTEM ] Initializing transmit/receive security: libtomcrypt SOBER128/SHA1HMAC (mode 0).

Jan 10 14:38:25 corosync [TOTEM ] The network interface [172.16.0.12] is now up.

Jan 10 14:38:25 corosync [TOTEM ] A processor joined or left the membership and a new membership was formed.

Jan 10 14:43:45 corosync [TOTEM ] A processor joined or left the membership and a new membership was formed.

檢查啟動過程中是否有錯誤產生。下面的錯誤信息表示packmaker不久之後將不再作為corosync的插件運行,因此,建議使用cman作為集群基礎架構服務;此處可安全忽略。

# grep ERROR: /var/log/cluster/corosync.log | grep -v unpack_resources

Jan 10 16:11:33 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.

Jan 10 16:11:33 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

Jan 10 16:11:34 corosync [pcmk ] ERROR: pcmk_wait_dispatch: Child process mgmtd exited (pid=5150, rc=100)


查看pacemaker是否正常啟動:

[root@node1 cluster]# grep pcmk_startup /var/log/cluster/corosync.log

Jan 10 16:11:33 corosync [pcmk ] info: pcmk_startup: CRM: Initialized

Jan 10 16:11:33 corosync [pcmk ] Logging: Initialized pcmk_startup

Jan 10 16:11:33 corosync [pcmk ] info: pcmk_startup: Maximum core file size is: 18446744073709551615

Jan 10 16:11:33 corosync [pcmk ] info: pcmk_startup: Service: 9

Jan 10 16:11:33 corosync [pcmk ] info: pcmk_startup: Local hostname: node1


註:以上的錯誤檢查在node2上也要執行一遍,要確定沒有問題再向下操作

6、安裝crmsh

RHEL自6.4起不再提供集群的命令行配置工具crmsh,轉而使用pcs;如果你習慣了使用crm命令,可下載相關的程序包自行安裝即可。crmsh依賴於pssh,因此需要一並下載。

#cd /etc/yum.repos.d/

#wget http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/network:ha-clustering:Stable.repo

#yum -y install crmsh

註:此處需要下載suse的yum源

查看集群節點啟動狀態

# crm status

Stack: classic openais (with plugin)

Current DC: node1 (version 1.1.15-5.el6-e174ec8) - partition with quorum

Last updated: Wed Jan 10 16:36:52 2018 Last change: Wed Jan 10 16:22:45 2018 by hacluster via crmd on node1

, 2 expected votes

2 nodes and 0 resources configured


Online: [ node1 node2 ]


No resources

7、配置集群的工作屬性,禁用stonith

corosync默認啟用了stonith,而當前集群並沒有相應的stonith設備,因此此默認配置目前尚不可用,這可以通過如下命令驗正:

# 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:

# crm configure property stonith-enabled=false

使用如下命令查看當前的配置信息:

#crm configure show

node node1

node node2

property cib-bootstrap-options: \

have-watchdog=false \

dc-version=1.1.15-5.el6-e174ec8 \

cluster-infrastructure="classic openais (with plugin)" \

expected-quorum-votes=2 \

stonith-enabled=false

從中可以看出stonith已經被禁用。

8、為集群定義資源

#crm

#crm(live)# configure

crm(live)configure# primitive webip ocf:heartbeat:IPaddr2 params ip=172.16.0.99

crm(live)# status

Stack: classic openais (with plugin)

Current DC: node1 (version 1.1.15-5.el6-e174ec8) - partition with quorum

Last updated: Wed Jan 10 19:34:35 2018 Last change: Wed Jan 10 19:30:48 2018 by root via cibadmin on node1

, 2 expected votes

2 nodes and 1 resource configured


Online: [ node1 node2 ]


Full list of resources:


webip (ocf::heartbeat:IPaddr2): Started node1

此時可以看出webip已經啟動在node1上面了

在node1上運行:

# service corosync stop

#crm status

Stack: classic openais (with plugin)

Current DC: node2 (version 1.1.15-5.el6-e174ec8) - partition WITHOUT quorum

Last updated: Wed Jan 10 21:15:49 2018 Last change: Wed Jan 10 21:14:51 2018 by root via cibadmin on node1

, 2 expected votes

3 nodes and 1 resource configured


Online: [ node2 ]

OFFLINE: [ node1 online ]


Full list of resources:


webip (ocf::heartbeat:IPaddr2): Stopped

此時我們發現,webip資源並沒有轉移到node2上,這是因為此時的集群狀態為"WITHOUT quorum",即已經失去了quorum,此時集群服務本身已經不滿足正常運行的條件,這對於只有兩節點的集群來講是不合理的。因此,我們可以通過如下的命令來修改忽略quorum不能滿足的集群狀態檢查:

# crm configure property no-quorum-policy=ignore

片刻之後,集群就會在目前仍在運行中的節點node2上啟動此資源了,如下所示:

Stack: classic openais (with plugin)

Current DC: node2 (version 1.1.15-5.el6-e174ec8) - partition WITHOUT quorum

Last updated: Wed Jan 10 21:19:18 2018 Last change: Wed Jan 10 21:19:13 2018 by root via cibadmin on node2

, 2 expected votes

3 nodes and 1 resource configured


Online: [ node2 ]


Full list of resources:


webip (ocf::heartbeat:IPaddr2): Started node2

好了,驗正完成後,我們正常啟動node1

# service corosync start

正常啟動node1.magedu.com後,集群資源WebIP很可能會重新從node2.magedu.com轉移回node1.magedu.com。資源的這種在節點間每一次的來回流動都會造成那段時間內其無法正常被訪問,所以,我們有時候需要在資源因為節點故障轉移到其它節點後,即便原來的節點恢復正常也禁止資源再次流轉回來。這可以通過定義資源的黏性(stickiness)來實現。在創建資源時或在創建資源後,都可以指定指定資源黏性。

資源黏性值範圍及其作用:

0:這是默認選項。資源放置在系統中的最適合位置。這意味著當負載能力“較好”或較差的節點變得可用時才轉移資源。此選項的作用基本等同於自動故障回復,只是資源可能會轉移到非之前活動的節點上;

大於0:資源更願意留在當前位置,但是如果有更合適的節點可用時會移動。值越高表示資源越願意留在當前位置;

小於0:資源更願意移離當前位置。絕對值越高表示資源越願意離開當前位置;

INFINITY:如果不是因節點不適合運行資源(節點關機、節點待機、達到migration-threshold 或配置更改)而強制資源轉移,資源總是留在當前位置。此選項的作用幾乎等同於完全禁用自動故障回復;

-INFINITY:資源總是移離當前位置;

我們這裏可以通過以下方式為資源指定默認黏性值:

# crm configure rsc_defaults resource-stickiness=100


9、結合上面已經配置好的IP地址資源,將此集群配置成為一個active/passive模型的web(httpd)服務集群

為了將此集群啟用為web(httpd)服務器集群,我們得先在各節點上安裝httpd,並配置其能在本地各自提供一個測試頁面

Node1:

# yum -y install httpd

# echo "<h1>Node1</h1>" > /var/www/html/index.html

Node2:

# yum -y install httpd

# echo "<h1>Node2</h1>" > /var/www/html/index.html

而後在各節點手動啟動httpd服務,並確認其可以正常提供服務。接著使用下面的命令停止httpd服務,並確保其不會自動啟動(在兩個節點各執行一遍):

# /etc/init.d/httpd stop

# chkconfig httpd off

接下來我們將此httpd服務添加為集群資源。將httpd添加為集群資源有兩處資源代理可用:lsb和ocf:heartbeat,為了簡單起見,我們這裏使用lsb類型:


首先可以使用如下命令查看lsb類型的httpd資源的語法格式:

# crm ra info lsb:httpd

start and stop Apache HTTP Server (lsb:httpd)


The Apache HTTP Server is an efficient and extensible \

server implementing the current HTTP standards.


Operations' defaults (advisory minimum):


start timeout=15

stop timeout=15

status timeout=15

restart timeout=15

force-reload timeout=15

monitor timeout=15 interval=15

接下來新建資源WebServer

# crm configure primitive WebServer lsb:httpd

接下來查看集群生成的配置

node node1 \

attributes standby=off

node node2 \

attributes standby=off

node online

primitive WebServer lsb:httpd

primitive webip IPaddr2 \

params ip=172.16.0.99

property cib-bootstrap-options: \

have-watchdog=false \

dc-version=1.1.15-5.el6-e174ec8 \

cluster-infrastructure="classic openais (with plugin)" \

expected-quorum-votes=2 \

stonith-enabled=false \

last-lrm-refresh=1515587693 \

no-quorum-policy=ignore

rsc_defaults rsc-options: \

resource-stickiness=100

查看資源啟用狀態

Stack: classic openais (with plugin)

Current DC: node2 (version 1.1.15-5.el6-e174ec8) - partition with quorum

Last updated: Wed Jan 10 21:48:18 2018 Last change: Wed Jan 10 21:46:27 2018 by root via cibadmin on node1

, 2 expected votes

3 nodes and 2 resources configured


Online: [ node1 node2 ]


Full list of resources:


webip (ocf::heartbeat:IPaddr2): Started node2

WebServer (lsb:httpd): Started node1

因此,對於前述的webip和WebServer可能會運行於不同節點的問題,可以通過以下命令來解決:

# crm configure colocation websserver-with-webip INFINITY: WebServer WebIP

#crm status

如下的狀態信息顯示,兩個資源已然運行於同一個節點

Stack: classic openais (with plugin)

Current DC: node2 (version 1.1.15-5.el6-e174ec8) - partition with quorum

Last updated: Wed Jan 10 21:52:39 2018 Last change: Wed Jan 10 21:52:06 2018 by root via cibadmin on node2

, 2 expected votes

3 nodes and 2 resources configured


Online: [ node1 node2 ]

OFFLINE: [ online ]


Full list of resources:


webip (ocf::heartbeat:IPaddr2): Started node2

WebServer (lsb:httpd): Started node2

10、接著,我們還得確保WebServer在某節點啟動之前得先啟動webip,這可以使用如下命令實現:

# crm configure order webserver-after-webip mandatory: webip WebServer

此外,由於HA集群本身並不強制每個節點的性能相同或相近,所以,某些時候我們可能希望在正常時服務總能在某個性能較強的節點上運行,這可以通過位置約束來實現:

# location webserver_on_node1 WebServer 50: node1

這條命令實現了將WebSite約束在node1上,且指定其分數為50;


最終的配置結果如下所示:

node node1 \

attributes standby=off

node node2 \

attributes standby=off

node online

primitive WebServer lsb:httpd

primitive webip IPaddr2 \

params ip=172.16.0.99

order webserver-after-webip Mandatory: webip WebServer

colocation webserver-with-webip inf: WebServer webip

location webserver_on_node1 WebServer 50: node1

property cib-bootstrap-options: \

have-watchdog=false \

dc-version=1.1.15-5.el6-e174ec8 \

cluster-infrastructure="classic openais (with plugin)" \

expected-quorum-votes=2 \

stonith-enabled=false \

last-lrm-refresh=1515587693 \

no-quorum-policy=ignore

rsc_defaults rsc-options: \

resource-stickiness=100


補充知識:

多播地址(multicast address)即組播地址,是一組主機的標示符,它已經加入到一個多播組中。在以太網中,多播地址是一個48位的標示符,命名了一組應該在這個網絡中應用接收到一個分組的站點。在IPv4中,它歷史上被叫做D類地址,一種類型的IP地址,它的範圍從224.0.0.0到239.255.255.255,或,等同的,在224.0.0.0/4。在IPv6,多播地址都有前綴ff00::/8。


多播是第一個字節的最低位為1的所有地址,例如01-12-0f-00-00-02。廣播地址是全1的48位地址,也屬於多播地址。但是廣播又是多播中的特例,就像是正方形屬於長方形,但是正方形有長方形沒有的特點。


好了,今天就寫到這裏,下一篇可能會補充一些crmsh的使用,這點東西,在有文檔參考的情況下,竟然寫了一上午,真是累啊,不過學習的樂趣確也在其中。


Centos 6.7高可用web集群corosync+pacemaker實現方案