天天看點

corosync+pacemaker高可用叢集

    一、簡介

    高可用叢集,是指以減少服務中斷(如因伺服器當機等引起的服務中斷)時間為目的的伺服器叢集技術。簡單的說,叢集就是一組計算機,它們作為一個整體向使用者提供一組網絡資源。這些單個的計算機系統就是叢集的節點。

高可用叢集的出現是為了減少由計算機硬體和軟體易錯性所帶來的損失。它通過保護使用者的業務程式對外不間斷提供的服務,把因軟體/硬體/人為造成的故障對業務的影響降低到最小程度。如果某個節點失效,它的備援節點将在幾秒鐘的時間内接管它的職責。是以,對于使用者而言,叢集永遠不會停機。高可用叢集軟體的主要作用就是實作故障檢查和業務切換的自動化。

    高可用叢集架構圖:

<a href="http://s3.51cto.com/wyfs02/M01/8A/20/wKioL1godUugWjL6AAJNWK6hA94432.png" target="_blank"></a>

     架構整體說明:

    随着網際網路技術的迅速發展很多大中小型公司已經離不開網際網路辦公及其提供服務,例如淘寶、美團等網站當機幾個小時損失是緻命的,很多網站對其高可用性越來越強,這就意味着運維人員需做到從硬體和軟體兩方面保證伺服器的平均無故障時間減小,才能提高其可用性。corosync是一個叢集管理套件的部分,它在傳遞資訊的時候可以通過一個簡單的配置來定義資訊傳遞的方式和協定等,能實作資源間的高可用。目前,corosync功能和特性已經非常完善了,是以pacmaker獨立出來之後通常都将pacmaker和corosync結合來使用,corosync并沒有通用的資料總管,是以要借助pacmaker來實作,pacemaker是作為corosync的插件來使用的,是以隻需要在corosync配置檔案中啟動pacemaker插件即可;但是真正啟動corosync并且配置它需要指令行接口進行調用,沒配置pcs那麼這裡我們隻能使用crm工具來對其進行資源的管理。

    架構詳細說明圖:

    corosync叢集常見的組合方式及配置接口:

    heartbeat v1 + hasource   

    heartbeat v2 + crm   

    heartbeat v3 + pacemaker + crmsh(corosync v1版本時沒有投票系統,corosync v2有投票系統,當系統發生網絡分區、腦裂時則将會将所有的資源轉移至可用的其他主機之上) 

    corosync v1 + pacemaker   corosync v2 + pacemaker  

    cman +rgmanager corosync v1 + cman + pacemaker  

    CRM:叢集資源管理

    資源類型:

    primitive:基本資源,主資源,僅能運作一個節點

    group:組,将組成一個高可用服務所需要的所有資源集合在一起

    clone:克隆,同一資源科出現多份,可運作在多個節點

    multi-state(master/slave):多狀态類型,是克隆資源的特殊表現,副本間可存在主從的關系

    RA:資源代理

    資源代理類别: 

    LSB:位于/etc/rc.d/init.d/*,支援start,stop,restart,reload,status,force-reload

注意:使用LSB資源類型的不能開機自動運作 

    OCF(open cluster framework):/usr/lib/ocf/resource.d/provider/,類似于LSB的腳本,但僅支援start,stop,status,monitor,meta-data  

    STONITH:調用stonith裝置的功能,systemd:unite ifle,/usr/lib/systemd/system/ 

注意:必須設定enable,設定為開機自動啟動

    資源限制方式: 

    位置限制:定義資源對節點的傾向性

    排序限制:定義資源彼此能否運作在同一個節點的傾向性

    順序限制:多個資源啟動順序的依賴關系

    HA叢集常用的工作模型:

 A/P:兩節點,active/passive,工作于主備模型

    A/A:兩節點,active/active,工作于主主模型

    N-M:N&gt;M,N個節點,M個服務,假設每個節點運作一個服務,活動節點數為N,備用節點數為N-M

    在叢集分裂(split-brain)時需要使用到資源隔離,有兩種隔離級别:

    STONITH:節點級别的隔離,通過斷開一個節點的電源或者重新啟動節點

    fencing:資源級别的隔離,類似通過向交換機發出隔離信号,特意讓資料無法通過此接口

    當叢集分裂,即分裂後的一個叢集的法定票數小于總票數一半時采取對資源的控制政策

   二、corosync安裝及其配置

    安裝:

    要求:1.基于主機名之間進行互相解析    2.各個節點之間時間需同步

    安裝:yum  -y install pacemaker (CentOS7)

    corosync配置詳解:corosync的主要配置分為totem、logging、quorum、nodelist配置段    

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

<code>totem {    </code><code>#定義各個節點之間的通信方式、加密與否,</code>

<code>version: 2 </code><code>#指明totem的版本資訊</code>

<code>cluster_name:myclusters    </code><code>#定義叢集名稱</code>

<code>crypto_cipher: aes128      </code><code>#定義加密算法</code>

<code>crypto_hash: sha1          </code><code>#定義hash算法</code>

<code>interface {   </code><code>#心跳及其事務傳遞接口</code>

<code>ringnumber: 0   </code><code>#環接口号</code>

<code>bindnetaddr: 10.1.0.0  </code><code>#綁定的網段</code>

<code>mcastaddr: 239.25.102.1 </code><code>#多點傳播位址</code>

<code>mcastport: 5405        </code><code>#多點傳播端口</code>

<code>ttl: 1                </code><code>#生存時間,防止發生環路</code>

<code>}</code>

<code>logging {    </code><code>#日志子產品</code>

<code>fileline: off</code>

<code>to_stderr: no    </code><code>#标準錯誤輸出</code>

<code>to_logfile: </code><code>yes</code>

<code>logfile: </code><code>/var/log/cluster/corosync</code><code>.log </code><code>#日志存放路徑</code>

<code>to_syslog: no</code>

<code>debug: off</code>

<code>timestamp: on</code>

<code>logger_subsys {</code>

<code>subsys: QUORUM</code>

<code>quorum {    </code><code>#投票系統</code>

<code>provider: corosync_votequorum    </code><code>#開啟投票機制</code>

<code>nodelist {    </code><code>#節點清單</code>

<code>node {</code>

<code>ring0_addr: 10.1.10.65 </code><code>#節點IP位址</code>

<code>nodeid: 1              </code><code>#節點編号</code>

<code>ring0_addr: 10.1.10.66 </code><code>#節點清單</code>

<code>nodeid: 2              </code><code>#節點編号</code>

    完成上訴配合後需生成密碼:corosync-kegen -l 

    将上訴配置檔案和秘鑰檔案拷貝至另一台cluster即可。

    啟動服務:

    systemctl start corosync 

    systemctl start pacemaker  

    安裝crmsh接口工具來管理叢集資源及其配置:yum -y install crmsh-2.1.4-1.1.x86_64.rpm pssh-2.3.1-4.2.x86_64.rpm python-pssh-2.3.1-4.2.x86_64.rpm

    三、corosync+pacemaker+nfs實作高可用案例        

    此實驗需将另一台伺服器啟動nfs服務并挂載至兩節點上配置同樣的頁面檔案  

<code>node 167840321: node1.alren.com \ </code><code>#預設設定,此在corosync配置檔案中定義</code>

<code>  </code><code>attributes standby=on</code>

<code>node 167840322: node2.alren.com \</code>

<code>  </code><code>attributes standby=off</code>

<code>primitive webip IPaddr \  </code><code>#定義webip</code>

<code>  </code><code>params ip=10.1.10.80 \</code>

<code>  </code><code>meta target-role=Started</code>

<code>primitive webserver systemd:httpd    </code><code>#定義webserver</code>

<code>primitive webstore Filesystem \      </code><code>#定義webstore  </code>

<code>  </code><code>params device=</code><code>"10.1.10.67:/www/html"</code> <code>directory=</code><code>"/var/www/html"</code> <code>fstype=nfs \</code>

<code>  </code><code>op</code> <code>start timeout=60s interval=0 \    </code><code>#附加選項為逾時時間、時間間隔</code>

<code>  </code><code>op</code> <code>stop timeout=60s interval=0</code>

<code>group webservice webip webstore webserver </code><code>#将webip、webserver、webstore加入到webservice組 </code>

<code>location cli-prefer-webservice webservice role=Started inf: node1.alren.com</code>

<code>property cib-bootstrap-options: \</code>

<code>  </code><code>have-watchdog=</code><code>false</code> <code>\</code>

<code>  </code><code>dc</code><code>-version=1.1.13-10.el7-44eb2dd \</code>

<code>  </code><code>cluster-infrastructure=corosync \</code>

<code>  </code><code>cluster-name=myclusters \</code>

<code>  </code><code>stonith-enabled=</code><code>false</code> <code>\</code>

<code>  </code><code>symmetric-cluster=</code><code>true</code>

<code># vim: set filetype=pcmk:</code>

    實驗測試圖:              

    上訴配置可用排列限制和順序限制實作并且能實作節點之間的粘性和啟動順序

<code>crm(live)configure</code><code># show</code>

<code>node 167840321: node1.alren.com \</code>

<code>attributes standby=on</code>

<code>attributes standby=off</code>

<code>primitive webip IPaddr \</code>

<code>params ip=10.1.10.80 \</code>

<code>meta target-role=Started</code>

<code>primitive webserver systemd:httpd</code>

<code>primitive webstore Filesystem \</code>

<code>params device=</code><code>"10.1.10.67:/www/html"</code> <code>directory=</code><code>"/var/www/html"</code> <code>fstype=nfs \</code>

<code>op</code> <code>start timeout=60s interval=0 \</code>

<code>op</code> <code>stop timeout=60s interval=0</code>

<code>colocation webip_webserver_with_webstore inf: webip webserver webstore </code><code>#設定排列限制</code>

<code>order webip_before_webstore_before_webserver Mandatory: webip webstore webserver </code><code>#設定順序限制,此時啟動順序為:webip,webstore,webserver</code>

<code>have-watchdog=</code><code>false</code> <code>\</code>

<code>dc</code><code>-version=1.1.13-10.el7-44eb2dd \</code>

<code>cluster-infrastructure=corosync \</code>

<code>cluster-name=myclusters \</code>

<code>stonith-enabled=</code><code>false</code> <code>\</code>

<code>symmetric-cluster=</code><code>true</code> <code>\</code>

<code>default-resource-stickiness=200</code>

定義資源監控配置如下:當httpd服務停止時,将自動重新開機httpd,如重新開機失敗則将資源轉移至可用的節點

<code>primitive webserver systemd:httpd \</code>

<code>op</code> <code>start timeout=15s interval=0 \    </code><code>#定義資源啟動間隔及其逾時時間</code>

<code>op</code> <code>stop timeout=15s interval=0 \     </code><code>#定義資源停止時間間隔及其逾時時間</code>

<code>op</code> <code>monitor interval=15s timeout=15s  </code><code>#定義資源監控的時間間隔及其逾時時間</code>

<code>colocation webip_webserver_with_webstore inf: webstore webip webserver</code>

<code>order webip_before_webstore_before_webserver Mandatory: webip webstore webserver</code>

<code>default-resource-stickiness=200 \</code>

<code>no-quorum-policy=ignore \</code>

<code>last-lrm-refresh=1479180221</code>

    總結:綜合上訴的配置總體感覺corosync+pacemaker的方式實作高可用比lvs略微複雜,corosync同樣可實作對RS的健康狀态檢測,可借助ldirectory實作自動生成ipvs規則。

本文轉自chengong1013 51CTO部落格,原文連結:http://blog.51cto.com/purify/1872962,如需轉載請自行聯系原作者

繼續閱讀