Corosync+pacemaker實現集群的高可用


一、Corosync和pacemaker的了解:

Corosync是集群管理套件的一部分,他在傳遞信息的時候可以通過一個簡單的配置文件來定義信息傳遞的方式和協議等。也就是說,corosync是Messaging Layer集群信息層軟件,需要pacemaker資源管理器,才能構成一個完整的高可用集群。它也是運行於心跳層的開源軟件。(是集群框架引擎程序)

 Pacemaker也就是Cluster Resource Manager(簡稱CRM),,是一個集群資源管理器。它利用群集基礎構建(corosync或heartbeat)提供的消息和成員管理能力來探測並從節點或資源級別的故障中恢復,以實現群集服務的最大可用性。

Pacemaker和corosync,前者用於資源的轉移,后者用於心跳的檢測。結合起來使用,實現對高可用架構的自動管理。心跳檢測使用來檢測服務器是否還在提供服務,若出現服務器異常,就認為它掛掉了,此時pacemaker將會對資源進行轉移。

Crmsh是pacemaker的命令行工具。

二、高可用集群的配置(實現環境:關閉selinux和火牆)

主要實現的是,在server1和server4上安裝corosync和pacemaker,當一個節點服務器出問題,則另一個節點服務器進行服務。

1)在server1和server4中安裝pacemaker和corosync

--->  yum  install pacemaker  corosync  -y

2)在server1中配置corosync的配置文件,完成后復制到server4中

--->  cp /etc/corosync/corosync.conf.example /etc/corosync/corosync.conf

         --->  vim  /etc/corosync/corosync.conf          #修改內容如下

  # Please read the corosync.conf.5 manual page 

  compatibility: whitetank

  totem {
          version: 2          # 表示配置文件的版本
          secauth: off        # 表示集群認證關閉
          threads: 0          # 並發開啟的線程數
          interface {         # 接口信息
                  ringnumber: 0
                  bindnetaddr: 172.25.2.0              # 集群工作的網段
                  mcastaddr: 226.94.1.1                # 多播地址
                  mcastport: 5405                      # 多播端口號(注意,在多個人使用時,修改自己的端口號或多播地址,防止結點添加在自己的集群中)
                  ttl: 1
          }
  }
  logging {       # 配置日志存儲部分
          fileline: off
          to_stderr: no
          to_logfile: yes
          to_syslog: yes
          logfile: /var/log/cluster/corosync.log
          debug: off

          timestamp: on
          logger_subsys {
                  subsys: AMF
                  debug: off
          }
  }

  amf {
          mode: disabled
  }
  service {       # 啟動corosync之后啟動pacemaker;若var為1的時候,需要手動啟動pacemaker
          name: pacemaker
          ver: 0 
  }

 

 

   --->  scp  /etc/corosync/corosync.conf  server4:/etc/corosync/corosync.conf

   3)在server1和server4中安裝crm軟件(軟件包需要在網上下載)

   --->  yum  install   -y  crmsh-1.2.6.0.rc2.2.1.x86_64.rpm  pssh-2.3.1-4.1.x86_64.rpm

     4)開啟服務 (server1和server4均需要啟動)   

   --->  /etc/init.d/corosync  start     # 當corosync啟動后,pacemaker也會啟動

   5)檢測crm(出現結點1和結點4,代表結點添加成功)

 

  

   6)兩個節點均crm_verify -VL 校驗(會出現如下提示信息)

   --->  crm_verify  -LV      

  

   # 出現錯誤時,我們可以設置STONITH的值,消除錯誤(Stonith 即shoot the other node in the head使Heartbeat軟件包的一部分, 該組件允許系統自動地復位一個失敗的服務器使用連接到一個健康的服務器的遙遠電源設備。 Stonith設備是一種能夠自動關閉電源來響應軟件命令的設備 )

  

     

   7)我們在server4中添加策略(ip和apache),在server1中進行監控

   # 首先添加vip (完成之后,可以在server1的監控中看到)

  

  

   # 添加參數,當結點server1壞掉的時候,server4會自動接管(也相當於忽略結點的檢測)

  

   在server1中執行 /etc/init.d/corosync  stop后,在server4中監控結果為:

  

   # 添加spache服務在集群中(警告:默認時間和自己設置的監控時間不同,忽略,它會以默認時間為准);但此時cpahce和vip不在同一主機上,需建立資源組,把vip和apache服務添加進去;(注意,添加進去的服務不需要手動啟動,pacamaker會幫我們啟動)

     

  

   此時,server4中監控的情況為

  

   7)此時,apache的高可用已經配置完成,我們可以進行測試。當運行的節點有問題時,會http會自動切換到另一個節點上。

  

 

   # 修改node1為standy(停止節點node1),此時瀏覽器中顯示的server4

 

  

三、含fence設備的高可用實現

   在server1和server4中查看,是否有key文件。

   1)在真機中安裝軟件

   --->  yum  search  fence-cirtd        #查找相關軟件

   --->  yum  install -y yum install  -y fence-virtd-libvirt.x86_64  fence-virtd-multicast.x86_64 fence-virtd.x86_64

   --->  mkdir  /etc/cluster             # 創建密碼生成目錄

   --->  cd  /etc/cluster 

   --->  fence_virtd  -c                 # 生成配置文件       
  Module search path [/usr/lib64/fence-virt]: 回車

  Available backends:
      libvirt 0.1
  Available listeners:
      multicast 1.2
 
  Listener modules are responsible for accepting requests
  from fencing clients.

  Listener module [multicast]: 回車(默認為多播

  The multicast listener module is designed for use environments
  where the guests and hosts may communicate over a network using
  multicast.

  The multicast address is the address that a client will use to
  send fencing requests to fence_virtd.
 
  Multicast IP Address [225.0.0.12]: 回車(多播ip)

  Using ipv4 as family.

  Multicast IP Port [1229]: 回車(多播接口)

  Setting a preferred interface causes fence_virtd to listen only
  on that interface.  Normally, it listens on all interfaces.
  In environments where the virtual machines are using the host
  machine as a gateway, this *must* be set (typically to virbr0).
  Set to 'none' for no interface.

  Interface [br0]: 回車(此處為自己真機的ip設備名稱)

  The key file is the shared key information which is used to
  authenticate fencing requests.  The contents of this file must
  be distributed to each physical host and virtual machine within
  a cluster.

  Key File [/etc/cluster/fence_xvm.key]: 回車(密鑰位置,沒有目錄自己建立)

  Backend modules are responsible for routing requests to
  the appropriate hypervisor or management layer.

  Backend module [libvirt]:

  Configuration complete.

  === Begin Configuration ===
  fence_virtd {
      listener = "multicast";
      backend = "libvirt";
      module_path = "/usr/lib64/fence-virt";
  }

  listeners {
      multicast {
          key_file = "/etc/cluster/fence_xvm.key";
          address = "225.0.0.12";
          interface = "br0";
          family = "ipv4";
          port = "1229";
      }

  }

  backends {
      libvirt {
          uri = "qemu:///system";
      }
  }

  === End Configuration ===
  Replace /etc/fence_virt.conf with the above [y/N]? y(對配置進行覆蓋保存)
   ---> dd  if=/dev/urandom  of=fence_xvm.key  bs=128  count=1         # 隨機截取密碼文件的大小

   --->  systemctl  start fence-virtd.service                          # 啟動服務(要關閉火牆)

   2)將真機中生成的密鑰發送給server1和server4

   --->  scp  /etc/cluster/fence_xvm.key  server1:/etc/cluster         # 若在server1或4中沒有cluster目錄。創建即可

   --->  scp  /etc/cluster/fence_xvm.key  server4:/etc/cluster 

   3)在server1和server4中執行stonith_admin  -l,查看是否有fence代理:fence_xvm。如果沒有我們需要安裝fence-virt-0.2.3-15.el6.x86_64,  

   --->  yum install  fence-virt-0.2.3-15.el6.x86_64   -y  

   4)在corosync的策略中添加fence(當節點有問題時,可以自啟動) 

   crm(live)configure# primitive vmfence stonith:fence_xvm  params pamk_host_map="server1:vm1;server4:vm4"  op  monitor interval=1min

  

   4)在另一端監控情況如下:

  

   5)測試,此時當我們對虛擬機server4使用命令--->  echo c> /proc/sysrq-trigger 的時候,它會自動重啟,並添加在可用節點里面。

   注意:當添加vmfence時,出現 vmfence_start_0 on server1 'unknown error' (1)錯誤的時候,我們可以進行如下操作:(對之前的數據進行清除)

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM