corosyc+pcmake+drbd雙web高可用方案

在對於業務不能中斷而訪問量不是很高的網站來說,用2臺服務器配置成一主一備的高可用的解決方案是比較常用的手段

本文基於corosync+pacemaker+drbd,來構建一套完整的雙LAMP高可用集羣系統


創建基礎環境

首先檢查內核是否支持DRBD,我的系統是centos6.2,6.2版本的內核默認是2.6.32-220.el6.i686,DRBD8.3版本需要至少2.6.33內核支持。所以需要安裝內核補丁。(我這裏centos6.2的內核安裝內核補丁出了點問題,在centos5.x系列上沒問題,所以實際上我是編譯安裝的)


DRBD各版本內核支持列表

sohu和163源沒有這個8.2的版本在centos官方源下載

http://mirror.centos.org/centos/5/extras/i386/


配置雙方hostname,並在hosts文件寫入,並建立雙方ssh無密碼通信

具體步驟這裏不再列出

hosts文件


Step1:DRBD

創建要同步的磁盤分區

查看磁盤分區情況

[root@ha1 drbd.d]# fdisk /dev/sdb -l


DRBD安裝包分爲內核部分和用戶空間管理的部分。這2個包都需要安裝

[root@ha1 drbd]# yum localinstall kmod-drbd83-8.3.8-1.el5.centos.i686.rpm drbd83-8.3.8-1.el5.centos.i386.rpm


(本人實際是以編譯的方式安裝,我的配置文件是在/usr/local/etc下面,下面配置文件的路徑以rpm包安裝爲例。centos6.2系統不知爲何安裝內核補丁有點問題)


在樣例配置文件基礎上進行配置

[root@ha1 drbd]# cp /usr/share/doc/drbd83-8.3.8/drbd.conf /etc/

編輯全局配置

[root@ha1 drbd]# cd /etc/drbd.d/

[root@ha1 drbd.d]# vi global_common.conf

global {

       usage-count no;

}

common {

       protocol C;

       handlers {

               pri-on-incon-degr "/usr/lib/drbd/notify-pri-on-incon-degr.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f";

               pri-lost-after-sb "/usr/lib/drbd/notify-pri-lost-after-sb.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f";

               local-io-error "/usr/lib/drbd/notify-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ; halt -f";

               fence-peer "/usr/lib/drbd/crm-fence-peer.sh";

               split-brain "/usr/lib/drbd/notify-split-brain.sh root";

               out-of-sync "/usr/lib/drbd/notify-out-of-sync.sh root";

               before-resync-target "/usr/lib/drbd/snapshot-resync-target-lvm.sh -p 15 -- -c 16k";

               after-resync-target /usr/lib/drbd/unsnapshot-resync-target-lvm.sh;

       }

       startup {

               wfc-timeout 120;

               degr-wfc-timeout 120;

       }

       disk {

               on-io-error detach;

               fencing resource-only;

       }

       net {

               cram-hmac-alg "sha1";

               shared-secret "lustlost";

       }

       syncer {

               rate 100M;

       }

}

定義資源配置

[root@ha1 drbd.d]# vi web.res

resource web {

       on ha1.lustlost.com {

       device  /dev/drbd0;

       disk    /dev/sdb1;

       address 172.16.93.148:7789;

       meta-disk       internal;

       }

       on ha2.lustlost.com {

       device  /dev/drbd0;

       disk    /dev/sdb1;

       address 172.16.93.149:7789;

       meta-disk       internal;      

       }

}

將配置文件cp一份到ha2

[root@ha1 drbd.d]# scp /etc/drbd.conf ha2:/etc

[root@ha1 drbd.d]# scp /etc/drbd.d/* ha2:/etc/drbd.d/

初始化磁盤

在ha1和ha2上執行

drbdadm create-md web

在兩個節點都啓動服務

[root@ha2 drbd.d]# service drbd status


啓動以後設置其中一個節點爲主節點,執行下面命令,然後數據會自動同步

[root@ha1 drbd.d]# drbdadm -- --overwrite-data-of-peer primary web

查看同步情況

[root@ha1 drbd.d]# drbd-overview


同步完成之後,在主節點上查看狀態


查看備節點的狀態


格式化drbd設備

[root@ha1 drbd.d]# mkfs.ext3 /dev/drbd0

掛載至目錄

[root@ha1 drbd.d]# mount /dev/drbd0 /web/


Step2:LAMP平臺

方便起見就直接yum安裝了

[root@ha1 ~]# yum install httpd mysql mysql-server php php-mysql –y

編輯apache的配置,目錄指向drbd掛載點的目錄

[root@ha1 ~]# vi /etc/httpd/conf/httpd.conf

編輯mysql的配置,數據目錄同樣指向drbd掛載點

[root@ha1 ~]# vi /etc/my.cnf


Drbd,Mysql和httpd將配製成集羣服務,配置mysql和php開機不啓動,由集羣服務控制開啓和關閉

[root@ha1 data]# chkconfig mysqld off

[root@ha1 data]# chkconfig httpd off

[root@ha1 data]# chkconfig drbd off

然後將ha2節點也配置成一樣


Step3:Corosync+pacemaker

安裝corosync和pacemaker

[root@ha1 ~]# yum install corosync pacemaker –y

編輯corosync全局配置

[root@ha1 ~]# cd /etc/corosync/

[root@ha1 corosync]# vi corosync.conf

配置如下

# Please read the corosync.conf.5 manual page

compatibility: whitetank

totem {

       version: 2

       secauth: off

       threads: 0

       interface {

               ringnumber: 0

               bindnetaddr: 172.16.93.148

               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 {

       ver:0

       name:pacemaker

}

aisexec {

       user:root

       group:root

}


生成密鑰文件

[root@ha2 corosync]# corosync-keygen

可能剛裝的系統或者開機比較短的系統,系統墒池沒有足夠多的隨機數,用此命令生成不了密鑰文件,可以使用find / 命令讓系統進行足夠隨機的IO一般就可以了

將配置文件拷貝一份到ha2

[root@ha1 corosync]# scp -p corosync.conf authkey ha2:/etc/corosync/

然後進入crm配置界面(話說crm真心好用。命令選項全部都能自動補齊,感覺就像在配置交換路由)

[root@ha1 corosync]# crm

crm(live)# configure

配置三個全局屬性

crm(live)configure# property no-quorum-policy=ignore 因爲是雙節點,所以忽略票數

crm(live)configure# property stonith-enabled=false   關閉stonith設備

crm(live)configure# rsc_defaults resource-stickiness=100 提高資源粘性,防止服務器故障恢復時資源轉移

然後規劃一下需要配置的資源

在雙LAMP節點中,有以下幾個資源

IP地址,mysql,httpd,drbd,掛載文件系統。這幾個資源都需要在同一個節點上,所以先定義一個組。而且這5個資源啓動的先後順序必須先搞清楚。

首先先定義這幾個資源

集羣虛擬IP

crm(live)configure#primitive webip ocf:heartbeat:IPaddr params ip=172.16.93.150 broadcast=255.255.255.0

數據庫

crm(live)configure# primitive webmysql lsb:mysqld

web服務器

crm(live)configure# primitive webserver lsb:httpd

drbd設備

crm(live)configure# primitive webdrbd ocf:heartbeat:drbd params drbd_resource=web

drbd主從定義

crm(live)configure# ms ms_drbd webdrbd meta master-mak="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"mount定義

crm(live)configure#primitive webfs ocf:heartbeat:Filesystem params device=/dev/drbd0 directory=/web fstype=ext3 op start timeout=60s op stop timeout=60s

指定約束,將所有的資源約束在一起

crm(live)configure# colocation myweb inf: webip webserver webmysql webfs ms_drbd:Master

定義次序

crm(live)configure# order webfs_after_drbd inf: ms_drbd:promote webfs:start #將drbd提升爲主的才掛載文件系統

crm(live)configure# order webmysql_after_webfs inf: webfs:start webmysql:start #掛載文件系統後才能啓動mysql

crm(live)configure# order webserver_after_webip inf: webip:start webserver:start #ip地址配置好以後才啓動apache

crm(live)configure# order webserver_after_webfs inf: webfs:start webserver:start

#文件系統掛載完成後才能啓動apache

查看一下全部定義好的資源


查看資源啓動情況

通過關閉ha1 的網卡,能夠成功的把所有資源成功轉移到ha2上

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章