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

初接触Linux,基于corosync+pacemaker实现web集群高可用

2014-04-24 20:48 639 查看
今天给大家带来的是web集群的实现
一高可用集群
高可用  High Availability
高可用是为了实现服务器在线时间能够接近于百分之百能最大量减少因服务器错误所带来的N多问题。公式如下
可用性=最大在线时间/最大在线时间+服务器维修离线时间
如果你的系统在此计算公式内得到的可用性小于95%那就说明你的服务构架需要优化了
高可用构架
高可用一般可分为三层构架如下图




1Messaging Layer

信息层心跳信息传输层它是运行在每一个主机上的一个进程

今天要讲的corosync就是运行在这一层的

2CRMCluster Resources Manager

集群资源管理器依赖于各自底层的心跳信息层。之所以有这一层是因为那些非ha_aware软件本身不具备集群高可用能力才借助于CRM来实现的而如果一个应用程序自己能够利用底层心跳信息传递层的功能完成集群事务决策的软件就叫ha_aware。

在这层中其实还有一层叫做LRMLocal Resource Manager本地资源管理层这一层是真正去把CRM层的决策去落实的层次就好比CRM层是公司的董事长LRM就是总经理CRM负责整个公司的远景规划及战略实施然后分配给总经理LRM去实施总经理再分摊给下面的各小弟RA去完成这些在上图中也可以直观的表现出来

pacemaker就是属于这一层的而pacemaker的配置接口的crmsuse所以我们安装的时候需要安装crmsh

3RAResource Agent

资源代理就是能够接收CRM的调度用于实现在节点上对某一个资源完成管理的工具通常是一些脚本

1heartbeat legacy

heartbeat的传统类型监听在udp的694端口上

2LSB,linux standard base

那些在/etc/rc.d/init.d/*的脚本就是属于LSB的

3OCFOpen Cluster Framework

开放集群架构那些提供资源代理脚本的组织叫providerpacemaker就是其中的一个provider

4STONITH

shoot the other node in the head这个RA类型主要是做节点隔离的专为配置stonith设备耐用。

使用STONITH主要目的就是为了避免由于网络原因节点之间不能完全通信比如分为了两部分左边3台右边2台左边的3台能收到各自的心跳信息右边的2台也可以收到各自的心跳信息就是左边部分和右边部分收不到各自的心跳信息因此它们都各自以为对方故障了就会各自重新推选出一台做为DCDesignated Coordinator从而出现了两个集群这就导致了资源争用如果恰好双方都往其共享存储写数据很可能就会导致文件系统崩溃这种现象就叫做集群分裂brain-split。
为了避免集群分裂就出现了法定票数quorum票数>半数票数的集群成为满足法定票数这一说法就是在集群通信故障时为了避免资源抢占应该让一方放弃成为集群具体应该哪方放弃呢这就是投票的结果了只有具有法定票数的一方才有资格做为集群相反的一方就应该退出集群但它放弃后并不代表服务停止所以应该让其释放资源关闭电源。stonith设备就是在这里用的要让退出集群的设备彻底失效电源交换机就是这个原理了

而如果一个集群只有两个节点的话这又是一种特殊的集群万一出现集群分裂后它们双方可能都不会具有法定票数那结果可想而知资源不会转移导致整个资源都故障了因为没有仲裁设备

说这么多就是为了说明下面两个比较重要的概念

①、corosync默认启用了stonith功能而我们要配置的集群并没有stonith设备因此在配置集群的全局属性时要对其禁用

②、当一个集群没有法定票数时资源是不会正常转移的当一个节点出现故障时资源不会正常的转移到正常的节点上就会导致所有的资源都故障了。所以应该定义法定票数不足时做忽略而不是停止所有资源
二.安装于配置
安装前
先确定两台机器IP解析名和主机名和 uname -n 命令结果一致

需要编辑/etc/hosts和/etc/sysconfig/network两个文件
然后建立双机互信传输文件不需要输入密码,双方都需要执行下列的命令



两台机器一定要时间同步最好找个时间服务器都执行以下命令
[root@dota01]# ntpdate 172.16.0.1
22 Apr 19:52:05 ntpdate[2694]: adjust time server 172.16.0.1 offset -0.000291 sec
[root@dota01]# date
Tue Apr 22 19:52:10 CST 2014
安装corosync和pacemaker

yum -y install corosync pacemaker
修改corosync的配置文件

[root@dota01]# cd /etc/corosync/  移动到配置目录
[root@dota01]# ls
authkey  corosync.conf.example  corosync.conf.example.udpu  service.d  uidgid.d 这目录下有现成的配置文件改名即可
[root@dota01]# mv corosync.conf.example corosync.conf
修改配置文件增加service段和aisexec段
totem {
version: 2
secauth: off
threads: 0
interface {
ringnumber: 0
bindnetaddr: 172.16.0.0      #绑定网络地址
mcastaddr: 230.100.100.7     #心跳信息传递的组播地址
mcastport: 5405
ttl: 1
}
}
logging {
fileline: off
to_stderr: no
to_logfile: yes
to_syslog: yes
logfile: /var/log/cluster/corosync.log   /corosync的日志文件
debug: off
timestamp: on
logger_subsys {
subsys: AMF
debug: off
}
}
amf {
mode: disabled
}
service {
ver: 0
name: pacemaker            #定义corosync在启动时自动启动pacemaker
}
aisexec {                          #表示启动corosync的ais功能以哪个用户的身份运行 这个可以省略默认就是root用户执行
user: root
group: root
}
生成密钥文件

[root@dota01]# corosync-keygen
Corosync Cluster Engine Authentication key generator.
Gathering 1024 bits for key from /dev/random.
Press keys on your keyboard to generate entropy.
Writing corosync key to /etc/corosync/authkey.
[root@dota01]# ls
authkey  corosync.conf  corosync.conf.example  corosync.conf.example.udpu  service.d  uidgid.d
[root@dota01]# ll
total 24
-r-------- 1 root root  128 Apr 22 20:03 authkey
-rw-r--r-- 1 root root  522 Apr 18 20:09 corosync.conf
-rw-r--r-- 1 root root  445 Nov 22 18:37 corosync.conf.example
-rw-r--r-- 1 root root 1084 Nov 22 18:37 corosync.conf.example.udpu
drwxr-xr-x 2 root root 4096 Nov 22 18:37 service.d
drwxr-xr-x 2 root root 4096 Nov 22 18:37 uidgid.d
[root@dota01]# authkey就是生成的密钥文件并且保证权限是600或者400
传送密钥文件和配置文件至另外一台主机上
[root@dota01]# scp corosync.conf authkey dota02:/etc/corosync/
corosync.conf                                                                                                                        100%  522     0.5KB/s   00:00
authkey                                                                                                                              100%  128     0.1KB/s   00:00
[root@dota01]#
自RH6.4之后偶就不再提供pacemaker的命令行工具我们还要单独安装命令行工具crmsh可是这个工具又以来pssh所以一并安装了
yum -y install pssh-2.3.1-2.el6.x86_64.rpm crmsh-1.2.6-4.el6.x86_64.rpm
两个节点都需要启动corosync
[root@dota01]# service corosync start
Starting Corosync Cluster Engine (corosync):               [  OK  ]
[root@dota01]#
查看集群状态
[root@dota01]# crm status
Last updated: Tue Apr 22 20:26:25 2014
Last change: Tue Apr 22 20:26:20 2014 via crmd on dota01.dota.com
Stack: classic openais (with plugin)
Current DC: dota02.dota.com - partition WITHOUT quorum
Version: 1.1.10-14.el6-368c726
8 Nodes configured, 9 expected votes
2 Resources configured
Online: [ dota01.dota.com dota02.dota.com ] 这是我的两个节点
OFFLINE: [ dota01 localhost.localdomain node1 node1.sun.com node2 node2.sun.com ]
dota01ip   (ocf::heartbeat:IPaddr):    Started dota02.dota.com
dota01server   (lsb:httpd):    Started dota01.dota.com
从上图可以了解到我的两个节点都同时在线而且dota02是DC还具有法定票数
接下来我们要给两个节点定一下资源配置
当然接下来的操作都是在crm命令下的操作所以熟悉crm必不可少
当你在命令行输入crm就是进入crm模式在模式下按两下Tab键就可显示当前目录下所有可执行的命令当然如果还是看不懂那也可以执行help来查看帮助信息
[root@dota01]# crm
crm(live)# 进入crm模式并且按两下Tab键补全
?           cd          configure   exit        history     options     ra          site        up
bye         cib         end         help        node        quit        resource    status
crm(live)# help  查看每个命令的意思
This is crm shell, a Pacemaker command line interface.
Available commands:
cib              manage shadow CIBs
resource         resources management
configure        CRM cluster configuration
node             nodes management
options          user preferences
history          CRM cluster history
site             Geo-cluster support
ra               resource agents information center
status           show cluster status
help,?           show help (help topics for list of topics)
end,cd,up        go back one level
quit,bye,exit    exit the program
crm(live)# configure
crm(live)configure#
?                  colocation         exit               load               op_defaults        refresh            save               user
bye                commit             fencing_topology   location           order              rename             schema             verify
cd                 default-timeouts   filter             master             primitive          role               show               xml
cib                delete             graph              modgroup           property           rsc_defaults       simulate
cibstatus          edit               group              monitor            ptest              rsc_template       template
clone              end                help               ms                 quit               rsc_ticket         up
collocation        erase              history            node               ra                 rsctest            upgrade
crm(live)configure# order help 还可以用这种方式查看命令使用格式
usage: order <id> {kind|<score>}: <rsc>[:<action>] <rsc>[:<action>] ...
[symmetrical=<
定义全局配置测试并提交
crm(live)# configure  property  stonith-enabled=false 禁止stonith设备正常情况下不应该禁止但是我们这里不考虑stonith设备
crm(live)# configure  property  no-quorum-policy=ignore 忽略法定投票因为节点就两个不可能超过半数票
crm(live)#
crm(live)# configure  必须在configure目录下执行
crm(live)configure# verify
crm(live)configure# commit
INFO: apparently there is nothing to commit
INFO: try changing something first
crm(live)configure#
资源类型

上面提到过了添加资源是在crm中定义的但是真正去干活的却是做RA的那些小弟们因此在添加资源之前必须要知道当前集群所支持的RA类型。corosync支持heartbeatLSB和ocf等类型的资源代理目前较为常用的类型为LSB和OCF两类stonith类专为配置stonith设备而用。
查看当前集群所支持的RA类型列表
crm(live)# ra 在资源列表目录下
crm(live)ra# classes    查看资源的类型
lsb
ocf / heartbeat pacemaker
service
stonith
crm(live)ra#
查看ocf类型下的资源代表
crm(live)ra# list ocf
CTDB           ClusterMon     Dummy          Filesystem     HealthCPU      HealthSMART    IPaddr         IPaddr2        IPsrcaddr      LVM            MailTo
Route          SendArp        Squid          Stateful       SysInfo        SystemHealth   VirtualDomain  Xinetd         apache         conntrackd     controld
dhcpd          ethmonitor     exportfs       mysql          mysql-proxy    named          nfsserver      nginx          pgsql          ping           pingd
postfix        remote         rsyncd         rsyslog        slapd          symlink        tomcat
crm(live)ra# list ocf heartbeat
CTDB           Dummy          Filesystem     IPaddr         IPaddr2        IPsrcaddr      LVM            MailTo         Route          SendArp        Squid
VirtualDomain  Xinetd         apache         conntrackd     dhcpd          ethmonitor     exportfs       mysql          mysql-proxy    named          nfsserver
nginx          pgsql          postfix        rsyncd         rsyslog        slapd          symlink        tomcat
crm(live)ra#
为集群添加资源
ip资源名是dota01ip类型是ocfheartbeatIPaddr IP是172.16.9.147 启动监控程序 检测到失败后的方式为重启服务

crm(live)# configure
crm(live)configure# primitive dota01ip ocf:heartbeat:IPaddr params ip=172.16.9.147 op monitor interval=30s timeout=20s on-fail=restart
crm(live)configure# verify     #校验
crm(live)configure# commit
查看下IP是否生效
[root@dota01]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:6e:d8:ff brd ff:ff:ff:ff:ff:ff
inet 172.16.9.21/16 brd 172.16.255.255 scope global eth0
inet 172.16.9.147/16 brd 172.16.255.255 scope global secondary eth0
inet6 fe80::20c:29ff:fe6e:d8ff/64 scope link
valid_lft forever preferred_lft forever
用浏览器查看服务是否正常




ok了然后让都dota01停掉看看dota02能听正常提供服务这是实现高可用最重要的一步。
crm(live)# node standby 让当前节点自动成为备用节点
crm(live)# node standby
crm(live)# status
Last updated: Thu Apr 24 20:26:41 2014
Last change: Thu Apr 24 20:26:38 2014 via crm_attribute on dota01.dota.com
Stack: classic openais (with plugin)
Current DC: dota01.dota.com - partition WITHOUT quorum
Version: 1.1.10-14.el6-368c726
4 Nodes configured, 9 expected votes
2 Resources configured
Node dota01.dota.com: standby
Online: [ dota02.dota.com ]
OFFLINE: [ dota01 localhost.localdomain ]
webip  (ocf::heartbeat:IPaddr):    Started dota02.dota.com
webserver  (lsb:httpd):    Started dota02.dota.com
成功了。

资源约束
1位置约束Location
定义资源更倾向运行在哪一个节点上数值越大倾向性超高

inf无穷大 n -n -inf负无穷但凡有可能就不会运行在这个节点上
2排列约束Order
资源运行在同一节点的倾向性

inf -inf就代表那种老死不相往来的状态

3顺序约束Colocation
定义资源的启动与关闭次序

就如我们定义的三个资源他们的启动顺序应该是webip,webstore,webserver那就给它们定义一个顺序约束吧
crm(live)configure# order webip_webstore_webserver mandatory: webip webstore webserver
crm(live)configure# verify
crm(live)configure# commit
定义完成

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: