在对于业务不能中断而访问量不是很高的网站来说,用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各版本内核支持列表
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上
本文出自 “lustlost-迷失在欲望之中” 博客,请务必保留此出处
抱最大的希望,为最大的努力,做最坏的打算