在對于業務不能中斷而通路量不是很高的網站來說,用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各版本核心支援清單
<a target="_blank" href="http://blog.51cto.com/attachment/201306/073751841.jpg"></a>
sohu和163源沒有這個8.2的版本在centos官方源下載下傳
<a href="http://mirror.centos.org/centos/5/extras/i386/">http://mirror.centos.org/centos/5/extras/i386/</a>
配置雙方hostname,并在hosts檔案寫入,并建立雙方ssh無密碼通信
具體步驟這裡不再列出
hosts檔案
<a target="_blank" href="http://blog.51cto.com/attachment/201306/073751348.jpg"></a>
Step1:DRBD
建立要同步的磁盤分區
檢視磁盤分區情況
[root@ha1 drbd.d]# fdisk /dev/sdb -l
<a target="_blank" href="http://blog.51cto.com/attachment/201306/073752409.jpg"></a>
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 {
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
<a target="_blank" href="http://blog.51cto.com/attachment/201306/073752848.jpg"></a>
在兩個節點都啟動服務
[root@ha2 drbd.d]# service drbd status
<a target="_blank" href="http://blog.51cto.com/attachment/201306/073753500.jpg"></a>
啟動以後設定其中一個節點為主節點,執行下面指令,然後資料會自動同步
[root@ha1 drbd.d]# drbdadm -- --overwrite-data-of-peer primary web
檢視同步情況
[root@ha1 drbd.d]# drbd-overview
<a target="_blank" href="http://blog.51cto.com/attachment/201306/073753120.jpg"></a>
同步完成之後,在主節點上檢視狀态
<a target="_blank" href="http://blog.51cto.com/attachment/201306/073753560.jpg"></a>
檢視備節點的狀态
<a target="_blank" href="http://blog.51cto.com/attachment/201306/073754865.jpg"></a>
格式化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
<a target="_blank" href="http://blog.51cto.com/attachment/201306/073754953.jpg"></a>
編輯mysql的配置,資料目錄同樣指向drbd挂載點
[root@ha1 ~]# vi /etc/my.cnf
<a href="http://www.lustlost.com/wp-content/uploads/2012/11/image10.png"></a>
<a target="_blank" href="http://blog.51cto.com/attachment/201306/073754928.jpg"></a>
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
檢視一下全部定義好的資源
<a target="_blank" href="http://blog.51cto.com/attachment/201306/073756679.jpg"></a>
檢視資源啟動情況
通過關閉ha1 的網卡,能夠成功的把所有資源成功轉移到ha2上
本文轉自lustlost 51CTO部落格,原文連結:http://blog.51cto.com/lustlost/1177524,如需轉載請自行聯系原作者