corosync+pacemaker做高可用web叢集

來源:互聯網
上載者:User

標籤:主機

實驗環境:

        兩台主機:  centos6.5+httpd2.4+php5.5組成web的基本環境,並且web頁面訪問正常,並且確保httpd24服務不會開機啟動。

                     node1.mylinux.com      10.204.80.79

                     node2.mylinux.com      10.204.80.80

我這裡啟用ansible來方便對兩個節點的管理,啟用一台主機做為管理節點,IP:10.204.80.71,在三台主機的hosts裡同都添加對應的名稱解析,管理節點對node1,node2兩個節點啟用ssh互信機制,在ansible的設定檔中把node1和node2加入webservers組。

 

安裝軟體:

        在兩台主機上安裝corosync和pacemaker,在管理主機上使用ansible命令的yum模組安裝,安裝過程會要有一段時間。

# ansible webservers -m yum  -a ‘name=corosync,pacemaker state=installed‘

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857765MXMO.png" width="635" height="334" />

這裡顯示corosync和pacemaker已經安裝過了,在這裡pacemaker是做為corosync的一個外掛程式來啟動並執行,在rhel7以後的版本中,pacemaker將做為一個獨立的守護進程來運行。

配置corosync:

安裝完corosync後會在/etc/corosync目錄下有個預設的corosync.config.example,改為corosync.config

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857765REYd.png" width="611" height="71" />

在corosync.config設定檔加添回pacemaker做為corosync的外掛程式。添加如下內容

service {
  ver:  0
  name: pacemaker
  # use_mgmtd: yes
}

aisexec {
  user: root
  group:  root
}

並設定此設定檔中bindnetaddr後面的ip地址為我主機所在的網路地址,節點的網段在10.204.80網段,設為10.204.80.0網段   bindnetaddr:10.204.80.0,設定檔如下

# Please read the corosync.conf.5 manual pagecompatibility: whitetanktotem {version: 2secauth: onthreads: 0interface {ringnumber: 0bindnetaddr: 10.204.80.0mcastaddr: 226.94.13.10mcastport: 5405ttl: 1}}logging {fileline: offto_stderr: noto_logfile: yesto_syslog: yeslogfile: /var/log/cluster/corosync.logdebug: offtimestamp: onlogger_subsys {subsys: AMFdebug: off}}amf {mode: disabled}service {      ver: 0      name: pacemaker       # use_mgmtd:yes}aisexec {     user: root     group: root}

這裡要注意的是bindnetaddr的地址只設本網的網段,否則有可能會報錯 corosync [TOTEM ] The network interface is down,以至節點心跳傳遞不過來。參考官方文檔,說得很明白。

bindnetaddr
              This  specifies  the  network  address  the  corosync  executive  should bind to.  For example, if the local interface is 192.168.5.92 with netmask
              255.255.255.0, set bindnetaddr to 192.168.5.0.   If  the  local  interface  is  192.168.5.92  with  netmask  255.255.255.192,  set  bindnetaddr  to
              192.168.5.64, and so forth.

產生節點間通訊時的認證密鑰檔案:

#corosync-kegen 按幾個斷行符號,就會產生authkey的密鑰檔案

把corosync.conf和authkey拷到node2結點的/etc/corosync/目錄下

配置介面:

    在rhel6.3以前常用的版本是crmsh, 是一個資源管理員的配置介面,在rhel6.4以後用的是pcs,這裡可以兩種都用,對比一下。因此crmsh在centos6.5的yum庫中沒有,所以要去下載安裝crmsh或自己編譯安裝crmsh。

:http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/x86_64/

需要下載的有三個檔案crmsh-2.1-1.6.x86_64.rpm , pssh-2.3.1-4.1.x86_64.rpm ,python-pssh-2.3.1-4.1.x86_64.rpm;因為crmsh需要依賴pssh,所以也需要下載

安裝配置介面:安裝crmssh除了下載的幾個檔案外,還有其他的python的依賴關係,所以用yum localinstall來安裝解決依賴關係。把這三個檔案放在/root目錄下,然後安裝

# yum --nogpgcheck localinstall  crmsh-2.1-1.6.x86_64.rpm  pssh-2.3.1-4.1.x86_64.rpm  python-pssh-2.3.1-4.1.x86_64.rpm

安裝完成,輸入crm 進入crm shell

   650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_14108577652VOe.png" width="158" height="44" />

在node1上啟動corosync

    650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857766pMd6.png" width="484" height="43" />

    在節點1上啟動節點2

     650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857766wm83.png" width="423" height="41" />

    啟動完成,使用命令 # crm status 查看節點狀態

     650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_14108577660ba6.png" width="496" height="179" />

    節點1和節點2 都已經正常啟動,並且叢集處於正常工作狀態。

     執行ps auxf 命令可以查看corosync啟動的各相關進程

      650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857767A3S5.png" width="613" height="103" />

 

配置叢集的工作屬性:

   corosync預設啟用了stonith,當前這個叢集沒有相應的stonith裝置,於是首先要禁用stonith

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857767LxYt.png" width="494" height="145" />

#crm configure show  顯示當前叢集的配置資訊,可以看到stonith已經禁用了

給叢集添加資源:

  corosync支援的資源代理有lsb和ocf等,lsb就是一些服務的啟動指令碼類的,如/etc/init.d/目錄下的一些指令碼。查看當前叢集系統所支援的類型:

#crm ra classes

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857768nkIP.png" width="271" height="89" />

stonith是專門為配置stonith裝置而用

查看某種類型下的所有資源代理的列表,可以用#crm ra list lsb | ocf (heartbeat | pacmemaker) | stonith 來查看。

650) this.width=650;" style="background-image: none; border-right- 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_14108577685YEC.png" width="774" height="138" />

一個web叢集所需要的資源有 vip,web服務,

我這兩個節點的vip為:10.204.80.88   web服務為 http24

crm的配置命令的文法:

primitive <rsc> [<class>:[<provider>:]]<type>

          [params attr_list]

          [operations id_spec]

            [op op_type [<attribute>=<value>...] ...]

op_type :: start | stop | monitor

primitive        主資源

                         格式  primitive  ID (資源名稱)  資源代理

                           參數  params

                                     meta  元屬性資訊

                                     utilization   使用資訊

                                     operations       操作資訊       操作類型  start |stop | monitor(監控,定義資源如果停止了是否會被發現,而且傳遞給叢集並且是否重新啟運它的)

 

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857768068C.png" width="601" height="187" />

添加一個IP=10.204.80.88為主資源

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857769JniJ.png" width="476" height="245" />

把httpd24資源做為web服務資源。檢測服務 verify,並提交commit

查看資源狀態

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857769hAEk.png" width="518" height="223" />

顯示有兩個節點,兩個資源設定,

因為沒有把資源定義為組,並且沒有定義約束,所以預設兩個資源分別運行在兩個節點上。

把這兩個資源定義到同一個組

在#crm 然後 輸入configure 進入配置模式,然後輸入 help group

可以查看協助文檔,有group命令的詳細說明和樣本

group文法

group <name> <rsc> [<rsc>...]
  [description=<description>]
  [meta attr_list]
  [params attr_list]

attr_list :: [$id=<id>] <attr>=<val> [<attr>=<val>...] | $id-ref=<id>

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857770l49M.png" width="493" height="296" />

把webip 和webserverf定義到同一個組webgroup裡面,然後查看叢集狀態

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857770dbLF.png" width="545" height="246" />

兩個資源都運行在同一個節點node1上面,訪問測試

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857771eiIk.png" width="618" height="195" />

把node1關機後,發現資源沒有轉移到node2節點上,查看叢集狀態#crm status

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_14108577717R21.png" width="520" height="193" />

發現在叢集狀態已經 是“WITHOUT quorum”已經失去法定票數,已經不是處於叢集狀態,但我這裡只有兩個節點,這樣是不合理的,可以以下命令忽略quorum不能滿足的叢集狀態檢查;

# crm configure property no-quorum-policy=ignore

查看叢集狀態

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857772Dbe5.png" width="535" height="260" />

叢集狀態已經在節點2上運行了

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857772M486.png" width="595" height="137" />

web頁面訪問正常

把node1節點啟動起來

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857772VLUu.png" width="525" height="243" />

node1和node2都線上,但資源並沒有轉移回到node1節點上,因為這裡並沒有定義資源粘性

定義資源的約束有三種:

location             位置約束 :定義資源可以、不可以或儘可能在哪些節點上運行

order                  次序約束:約束定義叢集資源在節點上的啟動順序

colocation         排列約束:排列約束用以定義叢集資源或以或不可以在某個節點上同時運行

我這裡定義webgroup預設運行在node1節點上

#crm  configure  prefer_node1  webserver 200:  node1.mylinux.com

這裡要注意的是: 後面跟節點的時候必須要有究竟間隔,否則會報錯,

查看設定檔

650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857773b0Pp.png" "536" height="293" />

順序約束,要定義強制要求先啟動webip 然後再啟動http服務,命令如下

#crm configure order  httpd-after-ip  mandatory: webip webserver

650) this.width=650;" style="background-image: none; border-right- 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201409/16/287547_1410857773AefU.png" width="488" height="21" />

 

corosync和pacemaker還有其他的功能強大功能,如果說健康狀態檢查,每隔多久檢查一下,等等,這裡先說到這

corosync+pacemaker做高可用web叢集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.