主要内容
使用corosync和pacemaker实现高可用
corosync在高可用中处于消息发送层,用于检测节点间通讯是否正常,而pacemaker则用于管理集群资源。通常在使用corosync和pacemaker的时候,我们都会使用统一的工具对它们进行管理,例如旧式的crmsh和新式的pcs。
使用crmsh或者pcs管理的好处是我们不必面向配置文件,而是直接通过命令行的方式管理集群节点,减少编辑配置文件造成的错误。另一个好处是降低学习成本,我们可以不必学习corosync和pacemaker的相关配置命令,只需要学习crmsh或者pcs如何使用。
虽然我们可以不去学习corosync和pacemaker如何配置,但是它们的工作原理还是必须知道的。
安装corosync和pacemaker
前面说过,我们选择使用第三方管理工具配置corosync和pacemaker,因此需要把这个管理工具也安装上。
这里我们选择了pcs
。
1 2 |
yum install -y corosync pacemaker pcs |
配置集群
软件包安装好后,它们各自拥有一个守护进程,分别是corosync、pacemaker和pcsd。默认情况下它们没有被启动,但是我们不必把所有3个进程都启动,只需要启动pcsd即可,然后通过pcsd来管理其他两个进程。
通常,pcs命令只需要在其中一个节点运行即可。pcs命令通过pcsd进程和其他节点进行通讯。
以下情况如果没有特别说明,则pcs命令只运行于某个节点上
- 启动pcsd服务
以下命令需要在每个节点都运行
123systemctl start pcsdsystemctl enable pcsd - 关闭防火墙
为了避免不必要的麻烦,关闭所有节点的防火墙
12systemctl stop firewalld - 更新hacluster密码
hacluster是软件安装过程中自动创建的账号,集群节点必须通过这个账号进行通讯
所有节点的密码必须相同
12echo "password" | passwd --stdin hacluster - 授权集群节点
让节点间可以正常通讯
123456[root@centos7-host1 ~]# pcs cluster auth centos7-host1 centos7-host2Username: haclusterPassword:centos7-host2: Authorizedcentos7-host1: Authorized - 添加集群节点成员
创建一个名为mycluster的集群,并把centos7-host1和centos7-host2添加为成员。
12pcs cluster setup --name mycluster centos7-host1 centos7-host2 - 启动集群节点
下面的命令成功启动后,corosync和pacemaker守护进程会一同被启动,可以通过
systemctl status corosync
和systemctl status pacemaker
观察。12pcs cluster start --all也可以单独启动某个节点
12pcs cluster start centos7-host1
添加资源
- 添加虚拟ip
虚拟ip用于给外界提供服务,因此也可以叫服务ip。
12pcs resource create VirtualIP ocf:heartbeat:IPaddr2 ip=192.168.11.200 cidr_netmask=24 op monitor interval=10s - 添加http服务
12pcs resource create WebSite ocf:heartbeat:apache op monitor interval=1min
默认情况下,资源启动后,pacemaker并不会保证它们会正常运作,如果希望pacemaker对资源进行监视,则可以设置op的值为
monitor
,interval
用于设置监视时间间隔。 -
检查资源配置是否有错误
如果资源没有启动成功(
pcs status
显示 stop),可以使用下面的命令检查配置是否有错误,如果有error,则要先修复错误,资源才能被正常启动1234567[root@centos7-host1 ~]# crm_verify -LVerror: unpack_resources: Resource start-up disabled since no STONITH resources have been definederror: unpack_resources: Either configure some or disable STONITH with the stonith-enabled optionerror: unpack_resources: NOTE: Clusters with shared data need STONITH to ensure data integrityErrors found during check: config not valid上面的错误提示我们要么配置STONITH,要么禁用STONITH,可以通过下面的命令禁用
12pcs property set stonith-enabled=false
管理资源
- 添加约束
约束是可选的功能,它只是为资源添加了额外的限制
- colocation约束
colocation约束了资源必须处于同一个节点的时候才能启动,例如下面命令行的意思是:WebSite资源启动的节点必须是VirtualIP所在的节点。
12pcs constraint colocation add WebSite with VirtualIP- order约束
order约束了资源的启动顺序,例如先启动VirtualIP然后启动WebSite
12pcs constraint order VirtualIP then WebSite- 地点约束
地点约束指定资源优先在哪一个节点启动,例如让资源在性能更好的节点上运行,以下命令假设centos7-host2性能更好。
12pcs constraint location WebSite prefers centos7-host2=50注意50只是一个权重,权重越大,则约束效果优先执行。相同权重的约束可以同时存在于两个节点,因此如果有相同权重的约束,则需要删除其中一个约束才能生效。
- 检查约束是否生效
12345678[root@centos7-host1 ~]# pcs constraintLocation Constraints:Ordering Constraints:start VirtualIP then start WebSite (kind:Mandatory)Colocation Constraints:VirtualIP with WebSite (score:INFINITY)Ticket Constraints:- 删除约束
约束的删除需要引用约束的完全限定名称,可以通过下面的命令查看
12345678910[root@centos7-host1 ~]# pcs constraint --fullLocation Constraints:Resource: VirtualIPEnabled on: centos7-host1 (score:INFINITY) (id:location-VirtualIP-centos7-host1-INFINITY)Ordering Constraints:start VirtualIP then start WebSite (kind:Mandatory) (id:order-VirtualIP-WebSite-mandatory)Colocation Constraints:WebSite with VirtualIP (score:INFINITY) (id:colocation-WebSite-VirtualIP-INFINITY)Ticket Constraints:id后面就是完全限定名称,然后可以通过remove指令删除约束
12pcs constraint remove location-VirtualIP-centos7-host1-INFINITY- 查看约束的权重(score值)
12345678910111213141516[root@centos7-host1 ~]# crm_simulate -sLCurrent cluster status:Online: [ centos7-host1 centos7-host2 ]VirtualIP (ocf::heartbeat:IPaddr2): Started centos7-host1WebSite (ocf::heartbeat:apache): Started centos7-host1Allocation scores:native_color: VirtualIP allocation score on centos7-host1: INFINITYnative_color: VirtualIP allocation score on centos7-host2: 0native_color: WebSite allocation score on centos7-host1: 0native_color: WebSite allocation score on centos7-host2: -INFINITYTransition Summary: - 手动在节点间移动资源
资源的移动也是依赖于约束来实现的,只需要设置location约束的权重为
INFINITY
即可。12pcs constraint location VirtualIP prefers centos7-host2=INFINITY
克隆资源
只有能够同时在多个节点中处于活跃状态的资源适合克隆,也就是说,资源必须具有以下两个特点才可以被克隆
- 资源可以同时在各个节点中存在
- 资源可以同时处于激活状态
常见的例如DRBD就是可以克隆的资源。
克隆资源中的多状态资源
多状态资源是特殊的克隆资源,它们允许资源处于Master和Slave状态的其中之一。可以通过下面两种方法创建多状态资源
方法1,创建资源的同时利用--master
提升资源
1 2 |
pcs resource create <resource_id> standard:provider:type --master [meta master_options] |
方法2,创建资源和提升资源分开两个步骤
1 2 3 |
pcs create <resource_id> standard:provider:type [resource options] #创建资源 pcs resource master [<master_name>] resource_id [master_options] #提升资源 |
如果不指定克隆名称,则名称默认为resource_id-master
常用资源克隆选项
- master-max 可将多少资源副本提升至master状态,默认1
- master-node-max 单一节点中可将多少资源副本提升至master状态,默认1
- clone-max 需要多少资源副本才可以启动,默认为该集群中的节点数
- clone-node-max 需要多少资源副本才可以在单一节点中启动,默认1
- notiry 停止或启动克隆副本时,应在开始操作前,以及操作成功后通知其他所有副本,默认值false。
配合DRBD实现高可用
安装DRBD
1 2 3 4 5 6 7 8 |
## 先导入public key rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org ## 针对centos6的仓库 rpm -Uvh http://www.elrepo.org/elrepo-release-6-8.el6.elrepo.noarch.rpm ## 针对centos7的仓库 rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm yum install -y drbd84-utils kmod-drbd84 |
添加DRBD资源
编辑/etc/drbd.d/r0.res
文件,如果没有则创建一个。
/dev/sdb1
如果不存在则需要先创建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
resource r0{ #r0是资源名称 protocol C; #实时同步 on centos7-host1 { #centos6-host1上的资源 device /dev/drbd1; #逻辑设备,必须以drbd开头,由drbd创建 disk /dev/sdb1; #实际数据储存设备 address 192.168.11.4:7789; #本机IP地址 meta-disk internal; #元数据和普通数据保存在同一设备 } on centos7-host2 { device /dev/drbd1; disk /dev/sdb1; address 192.168.11.5:7789; meta-disk internal; } } |
使用pacemaker控制drbd资源
需要先通过pcs命令创建drbd资源并提升为master,下面的命令创建了一个DrbdData-master的克隆资源
1 2 3 |
pcs resource create DrbdData ocf:linbit:drbd drbd_resource=r0 op monitor interval=60s pcs resource master DrbdData master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true |
为drbd设备挂载文件系统
1 2 |
pcs resource create WebFS ocf:heartbeat:Filesystem device=/dev/drbd1 directory=/mnt fstype=xfs |
添加约束
当一方节点离线,需要先把drbd资源提升为master(即drbd中的primary),因为drbd中的secondary是无法挂载设备的。
1 2 |
pcs constraint order promote DrbdData-master then start WebFS |
添加其他可选约束,目的是使资源更加健壮
colocation让资源必须在同一节点生效
1 2 3 |
pcs constraint colocation add WebFS with DrbdData-master INFINITY with-rsc-role=Master pcs constraint colocation add WebSite with WebFS INFINITY |
with-src-role
选项的意思是WebFS所在的节点必须是Master节点,pcs命令最终都会把这些信息写入到cib文件中,可以使用下面命令显示cib信息,cib以xml格式保存这些内容,该命令会把信息输出到标准输出:
1 2 |
pcs cluster cib |
可以看到cib中的约束
1 2 3 4 5 6 7 8 9 |
<constraints> <rsc_order first="VirtualIP" first-action="start" id="order-VirtualIP-WebSite-mandatory" then="WebSite" then-action="start"/> <rsc_colocation id="colocation-WebSite-VirtualIP-INFINITY" rsc="WebSite" score="INFINITY" with-rsc="VirtualIP"/> <rsc_location id="location-VirtualIP-centos7-host1-INFINITY" node="centos7-host1" rsc="VirtualIP" score="INFINITY"/> <rsc_order first="DrbdData-master" first-action="promote" id="order-DrbdData-master-WebFS-mandatory" then="WebFS" then-action="start" /> <rsc_colocation id="colocation-WebFS-DrbdData-master-INFINITY" rsc="WebFS" score="INFINITY" with-rsc="DrbdData-master" with-rsc-role="Master" /> <rsc_colocation id="colocation-WebSite-WebFS-INFINITY" rsc="WebSite" score="INFINITY" with-rsc="WebFS"/> </constraints> |
其他相关命令
- 启动集群
1234pcs cluster start --allpcs cluster start centos7-host1pcs cluster start centos7-host2
- 停止集群
1234pcs cluster stop --allpcs cluster stop centos7-host1pcs cluster stop centos7-host2
- 查看集群状态
1234567891011121314151617181920212223242526[root@centos7-host2 ~]# pcs statusCluster name: myclusterStack: corosyncCurrent DC: centos7-host1 (version 1.1.18-11.el7_5.3-2b07d5c5a9) - partition with quorumLast updated: Mon Jul 16 10:33:25 2018Last change: Fri Jul 13 18:01:39 2018 by root via cibadmin on centos7-host12 nodes configured5 resources configuredOnline: [ centos7-host1 centos7-host2 ]Full list of resources:VirtualIP (ocf::heartbeat:IPaddr2): Started centos7-host1WebSite (ocf::heartbeat:apache): Started centos7-host1Master/Slave Set: DrbdData-master [DrbdData]Masters: [ centos7-host1 ]Slaves: [ centos7-host2 ]WebFS (ocf::heartbeat:Filesystem): Started centos7-host1Daemon Status:corosync: active/disabledpacemaker: active/disabledpcsd: active/enabled
可以看到集群的一些信息,例如名称、节点数,资源数等,
Current DC
是指当前启动资源的节点 -
查看系统提供的资源
这些资源都是一些脚本文件,例如最常用的ocf提供的资源,保存在
/usr/lib/ocf/resource.d/
目录中。12345678910111213141516171819[root@centos7-host1 ~]# pcs resource standardslsbocfservicesystemd[root@centos7-host1 ~]# pcs resource providersheartbeatlinbitopenstackpacemaker[root@centos7-host1 ~]# pcs resource agentsapachearp-ethersarp-ethersattributeauditd......省略部分输出 - 查看资源的使用帮助
例如想知道关于资源ocf:heartbeat:nginx
的使用帮助1234567891011[root@centos6-host1 ~]# pcs resource describe ocf:heartbeat:nginxocf:heartbeat:nginx - Manages an Nginx web/proxy server instanceThis is the resource agent for the Nginx web/proxy server.This resource agent does not monitor POP or IMAP servers, aswe don't know how to determine meaningful status for them....... 省略部分输出Default operations:start: timeout=60sstop: timeout=60smonitor: interval=20s timeout=30s