分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

Centos 6.7高可用web集群corosync+pacemaker实现方案

发布时间:2023-09-06 01:35责任编辑:沈小雨关键词:暂无标签
实验环境:

1、本实验共有两个测试节点:172.16.0.12 , 172.16.0.13

2、集群服务为apache的httpd服务

3、提供web服务的VIP为 172.16.0.99

4、系统为centos 6.7

5、本网络内有一台时间同步服务器,172.16.0.111



实验前准备:

1、各节点主机名互相解析

# vim /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

172.16.0.12 node1

172.16.0.13 node2


# vim /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

172.16.0.12 node1

172.16.0.13 node2

2、修改两个节点的主机名

node1:

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

node2:

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

3、设定两个节点基于密钥进行通信

node1:

# ssh-keygen -t rsa

# ssh-copy-id -i ~/.ssh/id_rsa.pub root@node2

node2:

# ssh-keygen -t rsa

# ssh-copy-id -i ~/.ssh/id_rsa.pub root@node1


4、同步两个节点的时间

node1:

#ntpdate 172.16.0.111

10 Jan 11:13:03 ntpdate[2195]: step time server 172.16.0.111 offset -28799.600512 sec

node2:

# ntpdate 172.16.0.111

10 Jan 11:14:20 ntpdate[5681]: step time server 172.16.0.111 offset 200348.267958 sec

同步后测试:

# date; ssh node2 'date'

Wed Jan 10 11:15:54 CST 2018

Wed Jan 10 11:15:54 CST 2018

实验过程

1、安装corosync和pacemaker:

node1:

# yum -y install corosync pacemaker

node2:

# yum -y install corosync pacemaker



2、配置corosync (以下命令在node1上执行)

#cd /etc/corosync

#cp corosync.conf.example corosync.conf

#vim corosync.conf

compatibility: whitetank #这个表示是否兼容0.8之前的版本

totem { #图腾,这是用来定义集群中各节点中是怎么通信的以及参数

version: 2 #图腾的协议版本,它是种协议,协议是有版本的,它是用于各节点互相通信的协议,这是定义版本的

secauth: on #表示安全认证功能是否启用的,此处我们开启

threads: 0 #实现认证时的并行线程数,0表示默认配置

interface { # 指定在哪个接口上发心跳信息的,它是个子模块

? ringnumber: 0 #环数目

bindnetaddr: 172.16.0.0 # 绑定的网络地址,此处改为节点主机所处的网段

mcastaddr: 239.255.1.1 #多播地址,一对多通信,239.255.x.x都可以

mcastport: 5405 # 多播端口

ttl: 1 # 表示只向外播一次

}

}

logging { # 跟日志相关

fileline: off

to_stderr: no # 表示是否需要发送到错误输出

to_logfile: yes #是不是送给日志文件

to_syslog: no #是不是送给系统日志

logfile: /var/log/cluster/corosync.log #日志文件路径

debug: off #是否启动调试

timestamp: on #日志是否需要记录时间戳

logger_subsys { #日志的子系统

subsys: AMF

debug: off

}

}

amf { # 跟编程接口相关的

mode: disabled

}

# 注:以下红色内容为我们手动添加的内容

service { #定义一个服务来启动pacemaker

ver: 0 #定义版本

name: pacemaker #这个表示启动corosync时会自动启动pacemaker

# use_mgmtd: yes

}

aisexec { #表示启动ais的功能时以哪个用户的身份去运行的

user: root

group: root #其实这个块定义不定义都可以,corosync默认就是以root身份去运行的

}


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

# corosync-keygen

将corosync.conf和authkey复制至node2:

# scp -p corosync.conf authkey node2:/etc/corosync/

注:生成密钥文件需要确保熵池中有足够的随机数来使用,可以去ftp服务器或者互联网上下载一个大文件生成大量的随机数。


4、启动corosync服务:

# service corosync start;ssh node2 'service corosync start'

# ss -tunl | grep 5405

udp UNCONN 0 0 172.16.0.12:5405 *:*

udp UNCONN 0 0 239.255.1.1:5405 *:*

此时多播地址已经监听在5405端口上,说明没有问题


查看corosync引擎是否正常启动:

# grep -e "Corosync Cluster Engine" -e "configuration file" /var/log/cluster/corosync.log

Jan 10 14:38:25 corosync [MAIN ] Corosync Cluster Engine ('1.4.7'): started and ready to provide service.

Jan 10 14:38:25 corosync [MAIN ] Successfully read main configuration file '/etc/corosync/corosync.conf'.


查看初始化成员节点通知是否正常发出:

# grep TOTEM /var/log/cluster/corosync.log

Jan 10 14:38:25 corosync [TOTEM ] Initializing transport (UDP/IP Multicast).

Jan 10 14:38:25 corosync [TOTEM ] Initializing transmit/receive security: libtomcrypt SOBER128/SHA1HMAC (mode 0).

Jan 10 14:38:25 corosync [TOTEM ] The network interface [172.16.0.12] is now up.

Jan 10 14:38:25 corosync [TOTEM ] A processor joined or left the membership and a new membership was formed.

Jan 10 14:43:45 corosync [TOTEM ] A processor joined or left the membership and a new membership was formed.

检查启动过程中是否有错误产生。下面的错误信息表示packmaker不久之后将不再作为corosync的插件运行,因此,建议使用cman作为集群基础架构服务;此处可安全忽略。

# grep ERROR: /var/log/cluster/corosync.log | grep -v unpack_resources

Jan 10 16:11:33 corosync [pcmk ] ERROR: process_ais_conf: You have configured a cluster using the Pacemaker plugin for Corosync. The plugin is not supported in this environment and will be removed very soon.

Jan 10 16:11:33 corosync [pcmk ] ERROR: process_ais_conf: Please see Chapter 8 of 'Clusters from Scratch' (http://www.clusterlabs.org/doc) for details on using Pacemaker with CMAN

Jan 10 16:11:34 corosync [pcmk ] ERROR: pcmk_wait_dispatch: Child process mgmtd exited (pid=5150, rc=100)


查看pacemaker是否正常启动:

[root@node1 cluster]# grep pcmk_startup /var/log/cluster/corosync.log

Jan 10 16:11:33 corosync [pcmk ] info: pcmk_startup: CRM: Initialized

Jan 10 16:11:33 corosync [pcmk ] Logging: Initialized pcmk_startup

Jan 10 16:11:33 corosync [pcmk ] info: pcmk_startup: Maximum core file size is: 18446744073709551615

Jan 10 16:11:33 corosync [pcmk ] info: pcmk_startup: Service: 9

Jan 10 16:11:33 corosync [pcmk ] info: pcmk_startup: Local hostname: node1


注:以上的错误检查在node2上也要执行一遍,要确定没有问题再向下操作

6、安装crmsh

RHEL自6.4起不再提供集群的命令行配置工具crmsh,转而使用pcs;如果你习惯了使用crm命令,可下载相关的程序包自行安装即可。crmsh依赖于pssh,因此需要一并下载。

#cd /etc/yum.repos.d/

#wget http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/network:ha-clustering:Stable.repo

#yum -y install crmsh

注:此处需要下载suse的yum源

查看集群节点启动状态

# crm status

Stack: classic openais (with plugin)

Current DC: node1 (version 1.1.15-5.el6-e174ec8) - partition with quorum

Last updated: Wed Jan 10 16:36:52 2018Last change: Wed Jan 10 16:22:45 2018 by hacluster via crmd on node1

, 2 expected votes

2 nodes and 0 resources configured


Online: [ node1 node2 ]


No resources

7、配置集群的工作属性,禁用stonith

corosync默认启用了stonith,而当前集群并没有相应的stonith设备,因此此默认配置目前尚不可用,这可以通过如下命令验正:

# crm_verify -L -V

error: unpack_resources:Resource start-up disabled since no STONITH resources have been defined

error: unpack_resources:Either configure some or disable STONITH with the stonith-enabled option

error: unpack_resources:NOTE: Clusters with shared data need STONITH to ensure data integrity

Errors found during check: config not valid

我们里可以通过如下命令先禁用stonith:

# crm configure property stonith-enabled=false

使用如下命令查看当前的配置信息:

#crm configure show

node node1

node node2

property cib-bootstrap-options: \

have-watchdog=false \

dc-version=1.1.15-5.el6-e174ec8 \

cluster-infrastructure="classic openais (with plugin)" \

expected-quorum-votes=2 \

stonith-enabled=false

从中可以看出stonith已经被禁用。

8、为集群定义资源

#crm

#crm(live)# configure

crm(live)configure# primitive webip ocf:heartbeat:IPaddr2 params ip=172.16.0.99

crm(live)# status

Stack: classic openais (with plugin)

Current DC: node1 (version 1.1.15-5.el6-e174ec8) - partition with quorum

Last updated: Wed Jan 10 19:34:35 2018Last change: Wed Jan 10 19:30:48 2018 by root via cibadmin on node1

, 2 expected votes

2 nodes and 1 resource configured


Online: [ node1 node2 ]


Full list of resources:


webip(ocf::heartbeat:IPaddr2):Started node1

此时可以看出webip已经启动在node1上面了

在node1上运行:

# service corosync stop

#crm status

Stack: classic openais (with plugin)

Current DC: node2 (version 1.1.15-5.el6-e174ec8) - partition WITHOUT quorum

Last updated: Wed Jan 10 21:15:49 2018Last change: Wed Jan 10 21:14:51 2018 by root via cibadmin on node1

, 2 expected votes

3 nodes and 1 resource configured


Online: [ node2 ]

OFFLINE: [ node1 online ]


Full list of resources:


webip(ocf::heartbeat:IPaddr2):Stopped

此时我们发现,webip资源并没有转移到node2上,这是因为此时的集群状态为"WITHOUT quorum",即已经失去了quorum,此时集群服务本身已经不满足正常运行的条件,这对于只有两节点的集群来讲是不合理的。因此,我们可以通过如下的命令来修改忽略quorum不能满足的集群状态检查:

# crm configure property no-quorum-policy=ignore

片刻之后,集群就会在目前仍在运行中的节点node2上启动此资源了,如下所示:

Stack: classic openais (with plugin)

Current DC: node2 (version 1.1.15-5.el6-e174ec8) - partition WITHOUT quorum

Last updated: Wed Jan 10 21:19:18 2018Last change: Wed Jan 10 21:19:13 2018 by root via cibadmin on node2

, 2 expected votes

3 nodes and 1 resource configured


Online: [ node2 ]


Full list of resources:


webip(ocf::heartbeat:IPaddr2):Started node2

好了,验正完成后,我们正常启动node1

# service corosync start

正常启动node1.magedu.com后,集群资源WebIP很可能会重新从node2.magedu.com转移回node1.magedu.com。资源的这种在节点间每一次的来回流动都会造成那段时间内其无法正常被访问,所以,我们有时候需要在资源因为节点故障转移到其它节点后,即便原来的节点恢复正常也禁止资源再次流转回来。这可以通过定义资源的黏性(stickiness)来实现。在创建资源时或在创建资源后,都可以指定指定资源黏性。

资源黏性值范围及其作用:

0:这是默认选项。资源放置在系统中的最适合位置。这意味着当负载能力“较好”或较差的节点变得可用时才转移资源。此选项的作用基本等同于自动故障回复,只是资源可能会转移到非之前活动的节点上;

大于0:资源更愿意留在当前位置,但是如果有更合适的节点可用时会移动。值越高表示资源越愿意留在当前位置;

小于0:资源更愿意移离当前位置。绝对值越高表示资源越愿意离开当前位置;

INFINITY:如果不是因节点不适合运行资源(节点关机、节点待机、达到migration-threshold 或配置更改)而强制资源转移,资源总是留在当前位置。此选项的作用几乎等同于完全禁用自动故障回复;

-INFINITY:资源总是移离当前位置;

我们这里可以通过以下方式为资源指定默认黏性值:

# crm configure rsc_defaults resource-stickiness=100


9、结合上面已经配置好的IP地址资源,将此集群配置成为一个active/passive模型的web(httpd)服务集群

为了将此集群启用为web(httpd)服务器集群,我们得先在各节点上安装httpd,并配置其能在本地各自提供一个测试页面。

Node1:

# yum -y install httpd

# echo "<h1>Node1</h1>" > /var/www/html/index.html

Node2:

# yum -y install httpd

# echo "<h1>Node2</h1>" > /var/www/html/index.html

而后在各节点手动启动httpd服务,并确认其可以正常提供服务。接着使用下面的命令停止httpd服务,并确保其不会自动启动(在两个节点各执行一遍):

# /etc/init.d/httpd stop

# chkconfig httpd off

接下来我们将此httpd服务添加为集群资源。将httpd添加为集群资源有两处资源代理可用:lsb和ocf:heartbeat,为了简单起见,我们这里使用lsb类型:


首先可以使用如下命令查看lsb类型的httpd资源的语法格式:

# crm ra info lsb:httpd

start and stop Apache HTTP Server (lsb:httpd)


The Apache HTTP Server is an efficient and extensible \

server implementing the current HTTP standards.


Operations' defaults (advisory minimum):


start timeout=15

stop timeout=15

status timeout=15

restart timeout=15

force-reload timeout=15

monitor timeout=15 interval=15

接下来新建资源WebServer

# crm configure primitive WebServer lsb:httpd

接下来查看集群生成的配置

node node1 \

attributes standby=off

node node2 \

attributes standby=off

node online

primitive WebServer lsb:httpd

primitive webip IPaddr2 \

params ip=172.16.0.99

property cib-bootstrap-options: \

have-watchdog=false \

dc-version=1.1.15-5.el6-e174ec8 \

cluster-infrastructure="classic openais (with plugin)" \

expected-quorum-votes=2 \

stonith-enabled=false \

last-lrm-refresh=1515587693 \

no-quorum-policy=ignore

rsc_defaults rsc-options: \

resource-stickiness=100

查看资源启用状态

Stack: classic openais (with plugin)

Current DC: node2 (version 1.1.15-5.el6-e174ec8) - partition with quorum

Last updated: Wed Jan 10 21:48:18 2018Last change: Wed Jan 10 21:46:27 2018 by root via cibadmin on node1

, 2 expected votes

3 nodes and 2 resources configured


Online: [ node1 node2 ]


Full list of resources:


webip(ocf::heartbeat:IPaddr2):Started node2

WebServer(lsb:httpd):Started node1

因此,对于前述的webip和WebServer可能会运行于不同节点的问题,可以通过以下命令来解决:

# crm configure colocation websserver-with-webip INFINITY: WebServer WebIP

#crm status

如下的状态信息显示,两个资源已然运行于同一个节点

Stack: classic openais (with plugin)

Current DC: node2 (version 1.1.15-5.el6-e174ec8) - partition with quorum

Last updated: Wed Jan 10 21:52:39 2018Last change: Wed Jan 10 21:52:06 2018 by root via cibadmin on node2

, 2 expected votes

3 nodes and 2 resources configured


Online: [ node1 node2 ]

OFFLINE: [ online ]


Full list of resources:


webip(ocf::heartbeat:IPaddr2):Started node2

WebServer(lsb:httpd):Started node2

10、接着,我们还得确保WebServer在某节点启动之前得先启动webip,这可以使用如下命令实现:

# crm configure order webserver-after-webip mandatory: webip WebServer

此外,由于HA集群本身并不强制每个节点的性能相同或相近,所以,某些时候我们可能希望在正常时服务总能在某个性能较强的节点上运行,这可以通过位置约束来实现:

#location webserver_on_node1 WebServer 50: node1

这条命令实现了将WebSite约束在node1上,且指定其分数为50;


最终的配置结果如下所示:

node node1 \

attributes standby=off

node node2 \

attributes standby=off

node online

primitive WebServer lsb:httpd

primitive webip IPaddr2 \

params ip=172.16.0.99

order webserver-after-webip Mandatory: webip WebServer

colocation webserver-with-webip inf: WebServer webip

location webserver_on_node1 WebServer 50: node1

property cib-bootstrap-options: \

have-watchdog=false \

dc-version=1.1.15-5.el6-e174ec8 \

cluster-infrastructure="classic openais (with plugin)" \

expected-quorum-votes=2 \

stonith-enabled=false \

last-lrm-refresh=1515587693 \

no-quorum-policy=ignore

rsc_defaults rsc-options: \

resource-stickiness=100


补充知识:

多播地址(multicast address)即组播地址,是一组主机的标示符,它已经加入到一个多播组中。在以太网中,多播地址是一个48位的标示符,命名了一组应该在这个网络中应用接收到一个分组的站点。在IPv4中,它历史上被叫做D类地址,一种类型的IP地址,它的范围从224.0.0.0到239.255.255.255,或,等同的,在224.0.0.0/4。在IPv6,多播地址都有前缀ff00::/8。


多播是第一个字节的最低位为1的所有地址,例如01-12-0f-00-00-02。广播地址是全1的48位地址,也属于多播地址。但是广播又是多播中的特例,就像是正方形属于长方形,但是正方形有长方形没有的特点。


好了,今天就写到这里,下一篇可能会补充一些crmsh的使用,这点东西,在有文档参考的情况下,竟然写了一上午,真是累啊,不过学习的乐趣确也在其中。


Centos 6.7高可用web集群corosync+pacemaker实现方案

原文地址:http://blog.51cto.com/12523910/2059622

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved