高可用集群的方案主要作用就是利用多个节点主机提供相同的服务以实现缩短服务故障后可快速恢复服务,从而保证服务的持久服务。


wKioL1Y0tcXQLsOoAAGge3HXUFs229.jpg

 

高可用集群架构分三层:

一、Messaing/Infrastructure:称为基础设施层或消息层。我们称为message layer

二、ResourceAllocation:称为资源非配层。我们称为clusterresource manager,资源管理层,简称CRM

三、Resource :称为资源层。也叫作resource agent,资源代理层。

 

message layer

通过corosyncopenAIS来传递各node之间的心跳信息和事物信息。

 

CRM层:

DesignatedCoordinator,简称DC,是整个集群中用来决策事物的节点叫做DC。而DC是通过policy engine组件来完成决策事物的过程的。决策过程传递信息都要通过CRMcorosync层向其他节点来进行传递

Local ResourceManager,简称LRM,本地资源管理层,是CRM用来处理相应的resource agent的统一接口。

ClusterInformation Base,简称cib,是整个集群的信息资源库,存放整个集群的配置信息。所有的集群配置,(不论通过pcs在哪个node上进行配置),都会通过message layer(即corosync)层连接DC节点,然后配置生效的,之后DC将配置信息在通过messagelayer层同步给其他节点。

 

resouce agent层:

简称RAra实际上可以认为是各种服务的启动脚本。因此RA有不同风格的类型。一般分为5类。

       1serviceheartbeat legacy: /etc/ha.d/haresources.d/目录下的脚本,成为通用目的的服务

       2LSB: /etc/rc.d/init.d/目录下的脚本

       3OCFopen clusterframework 开放集群框架

                        provider:脚本提供者有:

                                   heartbeat

                                   pacemaker

                                   linbit

       4STONITH:专用于stonith设备的服务脚本ra

       5systemd:如centos7/etc/systemd/system/multi-user.wants

 

实例:使用corosync+pacemaker+crmsh实现httpd服务的高可用。

 

corosync+pacemaker 布置3节点高可用集群:messaging layer基于多播传递,基于认证传递

corosync默认的投票系统需要三个节点才能启动)

第一步:在三个主机节点上yum安装corosync+pacemaker,实现httpd服务的高可用,其中httpd服务采用nfs共享存储。

主机1lpw1 172.16.249.209 //集群node1,httpd-server,ansible主控机

主机2lpw2 172.16.249.73 //集群node2,httpd-server

主机3lpw3 172.16.249.208 //集群node3,httpd-server

主机4lpw4 172.16.249.236 //nfs-server,

 

要在corosynchost文件中设置认证的功能,需要为每一个node提供相同的秘钥;使用corosync-keygen自动生成秘钥,默认会放在/etc/corosync/authkey ,也可以使用-k选项指定秘钥存放位置。

[root@lpw1 corosync]# corosync-keygen -l

wKioL1Y0tdiyr7byAAHIEGuU5ss467.jpg

然后将此authkey文件复制到每一个node/etc/corosync/目录下

[root@lpw1 corosync]# scp -p ./authkeyroot@172.16.249.73:/etc/corosync/

[root@lpw1 corosync]# scp -p ./authkeyroot@172.16.249.208:/etc/corosync/

 

 

在第一个主机节点(lpw1)上修改corosync的配置

[root@lpw1 corosync]# cat corosync.conf |grep -v '^[[:space:]]*#'

totem {

       version:2

 

       crypto_cipher:aes128

       crypto_hash:sha1

       secauth:on

       interface{

              ringnumber: 0

              bindnetaddr: 172.16.0.0

              mcastaddr: 239.254.1.1

              mcastport: 5405

              ttl: 1

       }

}

#此处如果指定使用udpu协议,上面的配置不能使用mcastaddr,而要使用unicast或者broadcastudpu协议是传播单播的,但是也可基于广播传递,但是不能是组播

nodelist {

       node{

              ring0_addr:172.16.249.209

              nodeid:1

       }    

       node{

              ring0_addr:172.16.249.73

              nodieid:2

       }

       node{

              ring0_addr:172.16.249.208

              nodeid:3

       }

}

 

logging {

       fileline:off

       to_stderr:no

       to_logfile:yes

       logfile:/var/log/cluster/corosync.log

       to_syslog:no

       debug:off

       timestamp:off

       logger_subsys{

              subsys: QUORUM

              debug: off

       }

}

 

quorum {

       provider:corosync_votequorum

}

 

将此配置文件复制到其他两个主机节点

[root@lpw1 corosync]# scp -p./corosync.conf root@172.16.249.73:/etc/corosync/

[root@lpw1 corosync]# scp -p ./corosync.confroot@172.16.249.208:/etc/corosync/

 

在各个节点启动corosynce服务

[root@lpw1 corosync]# systemctl startcorosync.service

或者使用ansible启动

[root@lpw1 corosync]# ansible clu -mservice -a 'name=corosync.service state=started'

wKiom1Y0tcuRXnj0AAHeasI_vik302.jpg

可以在各节点验证:

wKioL1Y0tkqy5OU0AAFdksvM9rk246.jpgwKiom1Y0tjzT7KtIAAEW6EcNDcs245.jpgwKiom1Y0tkvAsxwZAAD6j8YvP2w141.jpg

可以使用corosync-cfgtool命令查看当前节点的信息

corosync-cfgtool [-i <interface ip>]-s] [-r] [-H] [service_name] [-k] [nodeid] [-a] [nodeid]

-s :显示当前节点的信息

-a :显示指定节点的ip地址

-k :根据node id来杀死某个节点

-R :重载corosync.conf配置文件

-H :关闭当前节点的corosync服务

wKiom1Y0tnazoqMiAAH0COcYtwY681.jpg

 

[root@lpw1corosync]#corosync-cmapctl

wKioL1Y0tszDEsbqAANNgbLbpQ8430.jpg

message layer层准备完毕

启动pacemaker服务

[root@lpw1 corosync]# vim/etc/sysconfig/pacemaker //此文件是pacemaker的环境配置文件,默认情况下不需要设置。

 PCMK_logfile=/var/log/pacemaker.log

可以在各个节点上使用systemctlstart pacemaker.service启动pacemaker服务,这里使用ansible启动

[root@lpw1 corosync]# ansible clu -mservice -a 'name=pacemaker.service state=started'

wKiom1Y0trjCUct5AAHqzzPQOis087.jpg

查看pacemaker的启动情况

 

wKiom1Y0tvCQUpf6AAMf3H00k8Q420.jpg

wKioL1Y0tynj4HfEAAMPy0uhuts481.jpg

wKiom1Y0tvCRa2zLAAM9s6tHJ20635.jpg

使用crm_mon命令可以监视pacemaker各节点的当前信息,该工具是由pacemaker自带的。

[root@lpw1 corosync]#  crm_mon

wKioL1Y0t0ygRvOtAAFmrWcoAa4587.jpg

至此messagelayer层和CRM层已经准备完毕。

 

[root@lpw4 ~]# mkdir -pv /www/htdocs

mkdir: 已创建目录 "/www"

mkdir: 已创建目录 "/www/htdocs"

[root@lpw4 ~]# vim /www/htdocs/index.html

 

[root@lpw4 ~]# vim /etc/exports

/www/htdocs/   172.16.0.0/16(rw)

启动rpcbindnfs服务

[root@lpw4 ~]# service rpcbind start

正在启动 rpcbind                                         [确定]

[root@lpw4 ~]# service nfs start

启动 NFS 服务:                                            [确定]

启动 NFS mountd                                          [确定]

启动 NFS 守护进程:                                        [确定]

[root@lpw4 ~]#

然后设置nfs开机启动:

[root@lpw4 ~]# chkconfig nfs on

确保三个几点主机都能使用挂在nfs-server的目录

wKioL1Y0t3_TCPEMAADG8PrGHhk552.jpg

wKiom1Y0t0bhbJMuAADIY8LfmW0190.jpg

wKioL1Y0t3_DyW1FAADGKNIjJ_Y100.jpg

在每个节点都测试挂载并能访问http服务

节点1

[root@lpw1 corosync]# mount -t nfs172.16.249.236:/www/htdocs /var/www/pma

[root@lpw1 corosync]# systemctl starthttpd.service

[root@lpw4 ~]# curl http://172.16.249.209

this is cluster heml

然后关闭httpd服务,但要保证开机启动

[root@lpw1 corosync]# umount /var/www/pma

[root@lpw1 corosync]# systemctl stophttpd.service

[root@lpw1 corosync]# systemctl enable  httpd.service

 

节点2

[root@lpw2 www]# mount -t nfs172.16.249.236:/www/htdocs /var/www/pma

[root@lpw4 ~]# curl http://172.16.249.73

this is cluster heml

[root@lpw4 ~]#

然后关闭httpd服务,但要保证开机启动

[root@lpw2 www]# umount /var/www/pma

[root@lpw2 www]# systemctl stophttpd.service

[root@lpw2 www]# systemctl enablehttpd.service

节点3

[root@lpw3 ~]# mount -t nfs172.16.249.236:/www/htdocs /var/www/pma

[root@lpw3 ~]# systemctl starthttpd.service

[root@lpw4 ~]# curl http://172.16.249.208

this is cluster heml

然后关闭httpd服务,但要保证开机启动

[root@lpw3 ~]#  umount /var/www/pma

[root@lpw3 ~]# systemctl stop httpd.service

[root@lpw3 ~]# systemctl enablehttpd.service

 

接下来配置资源:

此次配置根据资源约束来定义资源对各节点的依赖。

使用crmconfigur命令配置

crm(live)# configure

crm(live)configure# show

node 1: lpw1.com

node 2: lpw2.com

node 3: lpw3.com

property cib-bootstrap-options: \

       have-watchdog=false\

       dc-version=1.1.12-a14efad\

       cluster-infrastructure=corosync

 

配置集群属性:

确保关闭stonith功能

crm(live)# configure propertystonith-enabled=false

crm(live)# configure verify

crm(live)# configure commit

查看

wKiom1Y0t2TSXdivAAFKO0WKFoo186.jpg

在其他节点做校验

[root@lpw2 www]# crm_verify -L -V

[root@lpw3 ~]# crm_verify -L -V

接下来配置资源

定义原始资源:

crm(live)configure# primitive webipocf:heartbeat:IPaddr2 params ip="172.16.249.100" op monitorinterval=30s timeout=30s

crm(live)configure# verify

crm(live)configure# primitive webserbersystemd:httpd op monitor interval=30s timeout=30s

crm(live)configure# verify

crm(live)configure# primitive webstoreocf:heartbeat:Filesystem params device="172.16.249.236:/www/htdocs"directory="/var/www/pma" fstype="nfs" op start timeout=60sop stop timeout=60s op monitor interval=20s timeout=40s

crm(live)configure# verify

定义约束

定义排列约束:

crm(live)configure#colocation webserver_with_webstore_and_webip inf: webserver ( webip webstore )

crm(live)configure#verify

定义顺序约束:

crm(live)configure#order webstore_after_webip Mandatory: webip webstore

crm(live)configure#verify

crm(live)configure#order webserver_after_webstore Mandatory: webstore webserver

crm(live)configure#verify

crm(live)configure#commit

 

然后开始测试:

当前资源在node2

wKioL1Y0t7yTLuQ3AAIqKwcojCY009.jpg

停止node2,查看资源转移情况

crm(live)# node standby lpw2.com

crm(live)# status

此时资源转移到node3

wKiom1Y0t5-QVAEWAAJZ3zeciWs519.jpg

在另外的主机上测试httpd服务是否可用

[root@lpw4 htdocs]# curlhttp://172.16.249.100

lpw 3 this is cluster heml

[root@lpw4 htdocs]#

 

高可用集群配置如下:

crm(live)# configure

crm(live)configure# show

node 1: lpw1.com \

       attributesstandby=off

node 2: lpw2.com \

       attributesstandby=on

node 3: lpw3.com \

       attributesstandby=on

primitive webip IPaddr \

       paramsip=172.16.249.100 \

       opmonitor interval=20s timeout=20s \

       metatarget-role=Started

primitive webserver systemd:httpd \

       opmonitor interval=20s timeout=20s \

       metatarget-role=Started

primitive webstore Filesystem \

       paramsdevice="172.16.249.236:/www/htdocs"directory="/var/www/html" fstype=nfs \

       opstart timeout=60s interval=0 \

       opstop timeout=60s interval=0 \

       opmonitor timeout=40s interval=20s

colocationwebserver_with_webip_and_webstore inf: webserver ( webip webstore )

order webserver_after_webstore Mandatory:webstore webserver

order webstore_after_webip Mandatory: webipwebstore

property cib-bootstrap-options: \

       stonith-enabled=false\

       have-watchdog=false\

       dc-version=1.1.12-a14efad\

       cluster-infrastructure=corosync

 

特别注意:使用共享存储的nfs-server作为资源使用时,必须保证nfs-server共享出来的目录本身有可读或写权限,同时在定义/etc/exports文件时也要给响应的权限,这里一个难点就是nfs-server默认情况会对root用户实行权限压缩,而本实验环境是使用root身份来做的,因此在定义/etc/export文件共享目录的权限时要加上no_root_squash

 

 

可以定义位置约束来指明资源对某个节点的粘性:

crm(live)configure# locationwebserver_pref_node1 webip 100: lpw1.com

显示当前集群的资源

crm(live)# status

wKioL1Y0t_ygLr-QAAHwnrDTWXw139.jpg

现在资源在node1上,然后手动下线当前node1节点,然后在查看资源已经转移

crm(live)# node standby

crm(live)# status

wKiom1Y0t9zSzUlbAAIdD2oZPZg774.jpg

之后恢复当前node1在线,查看资源已经回到node1节点

crm(live)# node online

crm(live)# status

wKioL1Y0uC7DKc_cAAIZwXkyIxM139.jpg

 下面介绍几种高可用集群的解决方案:

  

高可用集群可用版本:

1、heartbeat v1 //单独的v1版,因为它本身就是完整的,有投票系统

2、heartbeat v2 //单独的v2版,也是完整的体系

3、heartbeat v3 + pacemaker //v3版的hearbeat中投票系统pacemaker被独立出来成文单独项目,因此两种必须同时使用,heartbeat v3不常用。

4、corosync + pacemaker //centos6使用的corosync v1版,corosync v2版需要自己编译,v1版的corosync没有投票系统,

5、cman + rgmanager  //cman的投票系统很强大,但是rgmanger功能有点弱

6、corosync + cman + pacemaker //corosync提供message layer层,cman提供投票系统,pacemaker提供CRM层

注意:centos6上的heartbeat v1和v2版没有rpm包

以上是centos6上可以实现的方案

7、corosync + pacemaker // centos7 可直接使用corosync v2


但是生产环境中实现高可用集群的最多的keepalived解决方案