您的位置:首页 > 运维架构 > 网站架构

基于corosync+pacemaker实现主从高可用集群

2012-12-01 15:01 513 查看
本实验由两个节点组成高可用主从集群,在实际中不常用,目的是通过实验来加深对corosync的认识和理解。

环境设置:

node1:node1.magedu.com
ip 172.16.14.10
node2:node2.magedu.com
ip 172.16.14.11
VIP 172.16.14.2


一:准备工作

1、DNS域名解析

用uname -n的结果来识别对方的名字

使node1,node2的uname -n分别为:
node1.magedu.com
node2.magedu.com

node1:
# sed -i 's@\(HOSTNAME=\).*@\1node1.magedu.com@g'  /etc/sysconfig/network
# hostname node1.magedu.com

node2:
# sed -i 's@\(HOSTNAME=\).*@\1node2.magedu.com@g' /etc/sysconfig/network
# hostname node2.magedu.comm

在/etc/hosts添加:
172.16.14.10 node1.magedu.com node1
172.16.14.11 node2.magedu.com node2


2、node1,node2可以远程通信
在node1上:
ssh-keygen -t rsa -P ''
ssh-copy-id -i ~/.ssh/id_rsa root@node2

命令测试:
ssh node2 'ifconfig'
然后再实现node2对node1的通信

3、两个节点时间同步
[root@node2 ~]# date 112822022012;ssh node1 'date 112822022012'
Fri Nov 28 22:02:00 CST 2012
Fri Nov 28 22:02:00 CST 2012

4、在node1,node2上安装httpd

# yum -y install httpd
建立测试页面,为了便于区分,将页面结果设置为不同内容
node1上:
编辑/var/www/html/index.html写入:
<h1>node1</h1>

node2:
编辑/var/www/html/index.html写入:
<h1>node2</h1>


测试完成后;将两个节点的httpd服务关闭,并使之开机不能自动启动
service httpd stop
chkconfig httpd off

关闭selinux
setenforce 0


二:安装配置corosync

1、安装以下rpm包:

cluster-glue
cluster-glue-libs
heartbeat
openaislib
resource-agentsyyu
corosync
heartbeat-libs
pacemaker
corosynclib
libesmtp
pacemaker-libs


下载至本地/root后安装:
# yum -y --nogpgcheck localinstall *.rpm


2、配置corosync(以下命令在node1.magedu.com上执行)
# cd /etc/corosync
# cp corosync.conf.example corosync.conf

# vim corosync.conf
作如下修改:
version: 2          ##版本
secauth: on
threads: 1          ##线程数
bindnetaddr: 172.16.0.0    ##绑定的网段
mcastaddr: 226.94.14.14   ##使用的组播地址
to_syslog: no   ##是否将日志发送到日志文件系统

//添加这个service可以实现pacemaker随corosync的启动而自动启动
service {
ver:    0
name:   pacemaker
}

//定义ais的辅助运行用户和组的
aisexec {
name:   root
group:  root
}


3、生成节点间通信时用到的认证密钥文件:

# corosync-keygen
将corosync和authkey复制至node2:
# scp -p corosync.conf  authkey  node2:/etc/corosync/

分别为两个节点创建corosync生成的日志所在的目录:
# mkdir /var/log/cluster
# ssh node2  'mkdir /var/log/cluste


4、启动corosync
# service corosync start
#tail -50 /var/log/messages      ##查看日志信息

# ssh node2 -- /etc/init.d/corosync start   ##启动node2


5、查看状态:
[root@node1 corosync]# crm
crm(live)# status
Current DC: node1.magedu.com - partition with quorum
Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f
2 Nodes configured, 2 expected votes
0 Resources configured.
============
Online: [ node1.magedu.com node2.magedu.com ]


三:crm用法--crm是一个交互式的shell接口

[root@node1 ~]# crm
crm(live)# help

This is the CRM command line interface program.

Available commands:         ##crm使用的子命令

cib              manage shadow CIBs                    ##集群信息库
resource         resources management                  ##资源管理
configure        CRM cluster configuration             ##集群配置
node             nodes management                      ##节点管理
options          user preferences
ra               resource agents information center    ##资源代理
status           show cluster status                   ##显示集群状态信息
quit,bye,exit    exit the program                      ##退出程序
help             show help                             ##帮助信息
end,cd,up        go back one level                     ##返回上一层

最常用的有:
1、集群的配置用configure:

crm(live)# configure                    ##进入资源配置命令
crm(live)configure# help
primitive        define a resource      ##定义资源
group            define a group         ##定义组
location         a location preference  ##位置约束
colocation       colocate resources     ##排列约束
order            order resources        ##顺序约束
show             display CIB objects    ##显示信息
edit             edit CIB objects       ##进入编辑模式
delete           delete CIB objects     ##删除cib对象
commit           commit the changes to the CIB    ##提交信息
verify           verify the CIB with crm_verify  ##检查语法错误

2、资源代理RA的信息:
crm(live)configure# cd
crm(live)# ra
crm(live)ra# help
Available commands:
classes          list classes and providers             ##资源代理的类别
list             list RA for a class (and provider)     ##显示每个类别的所有RA
meta             show meta data for a RA                ##资源代理的元数据
providers        show providers for a RA and a class    ##资源代理的提供者
quit             exit the program
help             show help
end              go back one level

crm(live)ra# classes                ##显示四种资源代理类别
heartbeat                           ##heartbaet V1版本
lsb                                 ## linux标准库
ocf / heartbeat pacemaker           ##Open cluster Framework 开放集群架构
stonith                             ##专为配置stonith设备而用

crm(live)ra# list lsb               ##用list可以查看每个类别的
crm(live)ra# list ocf
AoEtarget           AudibleAlarm        CTDB
ClusterMon          Delay               Dummy

crm(live)ra# list ocf heartbeat      ##查看ocf下的子类别
AoEtarget           AudibleAlarm        CTDB
ClusterMon          Delay               Dummy
EvmsSCC             Evmsd               Filesystem
ICP                 IPaddr              IPaddr2
//只显示部分;IPaddr是ocf类别下heartbeat子类别的资源代理;用于IP的代理;下面我们将用到

crm(live)ra# meta ocf:heartbeat:IPaddr        ##用meta查看某个具体资源代理的属性
##使用方式  类别:子类别:资源代理

Parameters (* denotes required, [] the default):   ##*表示必选的,[]表示默认值

ip* (string): IPv4 address                    ##使用的参数;*表示必选的
The IPv4 address to be configured in dotted quad notation, for example
"192.168.1.1".
...............
Operations' defaults (advisory minimum):       ##默认的操作

start         timeout=20s                  ##启动超时时间
stop          timeout=20s                  ##停止超时时间
monitor       interval=5s timeout=20s      ##每隔5s监视一次,20s是超时时间

配置1个全局属性:
crm
configure
property stonith-enabled=false       ##是否启用stonish设备
verify      ##检查语法
commit      ##提交

接下来开始配置资源了:

定义资源用primitive,help一下:
crm(live)configure# help primitive
Usage:
...............
primitive  <rsc>  [<class>:[<provider>:]]<type>
资源        RA类别:提供者:哪个类型的RA
[params attr_list]                        ##params是关键字,指定参数
[meta attr_list]                          ##元数据属性
[utilization attr_list]
[operations id_spec]                      ##操作
[op op_type [<attribute>=<value>...] ...]   ##指定额外的属性;如超时时长


四:集群资源配置:

1、下面来配置一个web IP的集群资源:

crm(live)configure# primitive WebIP ocf:heartbeat:IPaddr  params ip="172.16.14.2"
crm(live)configure# verify
crm(live)configure# commit

crm(live)configure# show
node node1.magedu.com
node node2.magedu.com
primitive WebIP ocf:heartbeat:IPaddr \
params ip="172.16.14.2"
property $id="cib-bootstrap-options" \
dc-version="1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f" \
cluster-infrastructure="openais" \
expected-quorum-votes="2" \
stonith-enabled="false"

crm(live)configure# cd
crm(live)# status                     ##状态查看
============
Last updated: Sat Dec  1 11:12:19 2012
Stack: openais
Current DC: node1.magedu.com - partition with quorum
Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f
2 Nodes configured, 2 expected votes
1 Resources configured.
============

Online: [ node1.magedu.com node2.magedu.com ]

WebIP     (ocf::heartbeat:IPaddr):     Started node1.magedu.com
//发现WebIP运行在node1上
//ifconfig查看;172.16.14.2在node1上
[root@node1 ~]# ifconfig
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:76:E9:56
inet addr:172.16.14.2  Bcast:172.16.255.255  Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
Interrupt:67 Base address:0x2000


2、添加WebServe资源:
crm(live)configure# primitive WebServer lsb:httpd
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# show
node node1.magedu.com
node node2.magedu.com
primitive WebIP ocf:heartbeat:IPaddr \
params ip="172.16.14.2"
primitive WebServer lsb:httpd           ##显示WebServe
property $id="cib-bootstrap-options" \
dc-version="1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f" \
cluster-infrastructure="openais" \
expected-quorum-votes="2" \
stonith-enabled="false"

crm(live)configure# cd
crm(live)# status
WebIP     (ocf::heartbeat:IPaddr):     Started node1.magedu.com
WebServer     (lsb:httpd):     Started node2.magedu.com
//发现WebServer在node2上;默认资源是负载均衡模式的。多个
资源运行在多个节点上的,我们要想把资源运作在一个节点上;
可以通过定义组来实现:
下面将WebIP,WebServer定义在一个Webservice组中;
crm(live)configure# group Webservice WebIP WebServer

crm(live)configure# cd
crm(live)# status
Resource Group: Webservice             ##发现Webservice
WebIP     (ocf::heartbeat:IPaddr):     Started node1.magedu.com
WebServer     (lsb:httpd):     Started node1.magedu.com
//有上面得知,定义组后WebIP, WebServer都运行在node1上了
//通过访问172.16.14.2,只显示node1的页面内容;

把node1为standby:
[root@node1 ~]# crm node standby
[root@node1 ~]# crm status
============
Last updated: Sat Dec  1 11:46:27 2012
Stack: openais
Current DC: node1.magedu.com - partition with quorum
Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f
2 Nodes configured, 2 expected votes
1 Resources configured.
============

Node node1.magedu.com: standby          ##node1已为standby
Online: [ node2.magedu.com ]

Resource Group: Webservice
WebIP     (ocf::heartbeat:IPaddr):     Started node2.magedu.com
WebServer     (lsb:httpd):     Started node2.magedu.com
//说明资源都转移到node2上面了
//此时页面也显示为node2的测试内容了
这样就完成了角色转换了

下面让node2为standby:node1为online:
[root@node1 ~]# crm node online
[root@node1 ~]# crm status
============
Last updated: Sat Dec  1 12:12:56 2012
Stack: openais
Current DC: node1.magedu.com - partition with quorum    ##没有达到法定票数
Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f
2 Nodes configured, 2 expected votes
1 Resources configured.
============

Node node2.magedu.com: standby
Online: [ node1.magedu.com ]

Resource Group: Webservice
WebIP     (ocf::heartbeat:IPaddr):     Started node1.magedu.com
WebServer     (lsb:httpd):     Started node1.magedu.com

//对于两个节点的集群,不具备法定票数也允许运行;有以下措施:
crm(live)configure# property no-quorum-policy=ignore    ##不具备法定票数忽略
crm(live)configure# verify
crm(live)configure# commit


3、下面通过约束关系来管理资源;这需要删除前面定义的组:

需先停止资源运行,再删除组

crm(live)resource# stop Webservice     ##停止资源运行
crm(live)resource# cd
crm(live)# configure
crm(live)configure# delete Webservice
提交查看结果;
WebIP,WebServer又以独立资源运行起来了

1)、定义排序约束;(两个资源必须在一起或不在一起)

crm(live)configure# colocation WebServer_with_WebIP inf:
WebServer WebIP
提交后查看:
crm(live)configure# show xml
<rsc_colocation id="WebServer_with_WebIP" rsc="WebServer"
score="INFINITY" with-rsc="WebIP"/>
//说明这两个资源必须在一起

crm(live)# status
Online: [ node1.magedu.com node2.magedu.com ]

WebIP     (ocf::heartbeat:IPaddr):     Started node1.magedu.com
WebServer     (lsb:httpd):     Started node1.magedu.com
// 两个资源在一个节点运行
//通过浏览去访问为node1的页面

让节点1为standby:
crm(live)# node standby
crm(live)# status
WebIP     (ocf::heartbeat:IPaddr):     Started node2.magedu.com
WebServer     (lsb:httpd):     Started node2.magedu.com
//资源转移到node2上

2)、用顺序约束可以让资源按一前一后顺序启动:

crm(live)configure# order WebServer_after_webIP mandatory: WebIP
WebServer
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# show xml
<rsc_order first="WebIP" id="WebServer_after_webIP"
score="INFINITY" then="WebServer"/>

//先启动WebIP;再启动WebServe

3)、用位置约束定义一个资源停留在一个节点上的倾向性有多大
crm(live)configure# location WebIP_on_node1 WebIP 500:
node1.magedu.com
//WebIP停留在node1的倾向值为500;
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# show

crm(live)# node standby        ##节点1standby
crm(live)# status
Node node1.magedu.com: standby
Online: [ node2.magedu.com ]
WebIP     (ocf::heartbeat:IPaddr):     Started node2.magedu.com
WebServer     (lsb:httpd):     Started node2.magedu.com
//资源转移到node2上

crm(live)# node online    ##node1为online;发现资源回到node1上


资源配置的结果:
crm(live)configure# show
node node1.magedu.com \
attributes standby="off"
node node2.magedu.com \
attributes standby="off"
primitive WebIP ocf:heartbeat:IPaddr \
params ip="172.16.14.2"
primitive WebServer lsb:httpd
location WebIP_on_node1 WebIP 500: node1.magedu.com
colocation WebServer_with_WebIP inf: WebServer WebIP
order WebServer_after_webIP inf: WebIP WebServer
property $id="cib-bootstrap-options" \
dc-version="1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f" \
cluster-infrastructure="openais" \
expected-quorum-votes="2" \
stonith-enabled="false" \
no-quorum-policy="ignore"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息