pacemaker
Pacemaker是一个集群资源管理器。它利用集群基础构件(OpenAIS 、heartbeat或corosync)提供的消息和成员管理能力来探测并从节点或资源级别的故障中恢复,以实现群集服务(亦称资源)的高可用。
corosync
Corosync是集群管理套件的一部分,它在传递信息的时候可以通过一个简单的配置文件来定义信息传递的方式和协议。Corosync可以实现HA心跳信息传输的功能。corosync集群引擎是一个组通信系统,具有在应用程序中实现高可用性的附加功能
使用pacmaker与corosync搭建高可用集群
1、实验环境:
准备后端两台服务器
服务器1:172.25.66.2
服务器2:172.25.66.3
关闭防火墙设置及selinux为disabled
以下配置,在集群中的所有服务器都需要设置,设置过程一致
2、准备后端两台服务器的yum源,
[kiosk@foundation66 ~]$ cd /var/www/html/westos/
[kiosk@foundation66 westos]$ cd addons/
[kiosk@foundation66 addons]$ ls
HighAvailability ResilientStorage 这两个为所需要添加的yum 源
修改yum源,vim /etc/yum.repos.d/lucky.repo
[lucky]
name=lucky
baseurl=http://172.25.66.250/westos
gpgcheck=0
[HighAvailability] 高可用存储安装包
name=HighAvailability
baseurl=http://172.25.66.250/westos/addons/HighAvailability
gpgcheck=0
[ResilientStorage] 弹性存储安装包
name=ResilientStorage
baseurl=http://172.25.66.250/westos/addons/ResilientStorage
gpgcheck=0
检验yum 源是否正常:
[root@lucky2 ~]# yum clean all
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Cleaning repos: HighAvailability ResilientStorage lucky
Cleaning up everything
[root@lucky2 ~]# yum repolist
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
HighAvailability | 4.1 kB 00:00:00
ResilientStorage | 4.1 kB 00:00:00
lucky | 4.1 kB 00:00:00
(1/6): HighAvailability/group_gz | 3.4 kB 00:00:00
(2/6): HighAvailability/primary_db | 27 kB 00:00:00
(3/6): ResilientStorage/group_gz | 4.9 kB 00:00:00
(4/6): ResilientStorage/primary_db | 33 kB 00:00:00
(5/6): lucky/primary_db | 3.9 MB 00:00:00
(6/6): lucky/group_gz | 136 kB 00:00:00
repo id repo name status
HighAvailability HighAvailability 37
ResilientStorage ResilientStorage 42
lucky lucky 4,751
repolist: 4,830
3 、设置本地解析,使得集群中的服务器可以通过主机名称定位到具体主机。
vim /etc/hosts
172.25.66.2 lucky2
172.25.66.3 lucky3
172.25.66.4 lucky4
4 、安装软件 pacmaker、corosync以及pcs
yum install pacemaker corosync pcs -y
systemctl start pcsd
systemctl enable pcsd 设置开机启动
5、配置ssh,分别给两台服务设置免密,使得两台服务器操作更为方便
ssh-keygen 生成密钥
ssh-copy-id lucky2
ssh-copy-id lucky3 将生成的密钥发送给所有的集群服务器,包括自己
6、在下载了pcsd默认存在haclusetr这个用户,给这个用户添加密码
安装完成软件之后,系统会自动生成一个用户:hacluster,使用该用户对集群进行管理。需要在所有的集群服务器中设置该用户的密码,此时所有的服务器的密码设置必须一致。
[root@lucky2 ~]# id hacluster 查看用户
uid=189(hacluster) gid=189(haclient) groups=189(haclient)
[root@lucky2 ~]# passwd hacluster 设置密码
Changing password for user hacluster.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
二、创建服务器集群(任意一台服务器)
1 、通过用户和密码对集群中的服务器进行认证:
pcs cluster auth lucky2 lucky3
用户为:hacluster
密码为上面为该用户设置的密码。
[root@lucky2 ~]# pcs cluster auth lucky2 lucky3 认证服务器
Username: hacluster
Password: 密码
lucky2: Authorized
lucky3: Authorized 全部认证成功
2、创建集群,并且指定集群中的服务器
pcs cluster setup --name mycluster lucky2 lucky3
创建集群
[root@lucky2 ~]# pcs cluster setup --name mycluster lucky2 lucky3
Destroying cluster on nodes: lucky2, lucky3...
lucky2: Stopping Cluster (pacemaker)...
lucky3: Stopping Cluster (pacemaker)...
lucky3: Successfully destroyed cluster
lucky2: Successfully destroyed cluster
Sending cluster config files to the nodes...
lucky2: Succeeded
lucky3: Succeeded
Synchronizing pcsd certificates on nodes lucky2, lucky3...
lucky2: Success
lucky3: Success
Restarting pcsd on the nodes in order to reload the certificates...
lucky2: Success
lucky3: Success
3、将所有服务器启动,并且开机自启pcs cluster start --all
# 设置集群所有服务器开启pcs cluster enable --all
# 设置集群中所有服务器自动开启pcs status
# 查看集群的状态信息
[root@lucky2 ~]# pcs cluster start --all
lucky2: Starting Cluster...
lucky3: Starting Cluster...
[root@lucky2 ~]# pcs cluster enable --all
lucky2: Cluster Enabled
lucky3: Cluster Enabled
[root@lucky2 ~]# pcs status
Cluster name: mycluster
WARNING: no stonith devices and stonith-enabled is not false
Stack: corosync
Current DC: lucky2 (version 1.1.15-11.el7-e174ec8) - partition with quorum
Last updated: Mon Jul 1 20:10:06 2019 Last change: Mon Jul 1 20:09:51 2019 by hacluster via crmd on lucky2
2 nodes and 0 resources configured
Online: [ lucky2 lucky3 ]
No resources
Daemon Status:
corosync: active/enabled
pacemaker: active/enabled
pcsd: active/enabled
[root@lucky2 ~]#
4 检查配置并进行错误修改:crm_verify -L -V
[root@lucky2 ~]# 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 这个是因为我们还没有安装fence的时候,就会出现报错,所以我们需要将fence暂时的关闭和忽略pcs property set stonith
错误产生的原因:为了保证数据的安全,起搏器配备了Stonith功能。但是,它还知道何时没有提供stonith配置,并将其报告为问题
5、关闭fence和忽略pcs
[root@lucky2 ~]# pcs property set stonith-enabled=false 关闭fence
[root@lucky2 ~]# crm_verify -L -V 再次检测没有出现报错
三 、添加资源(任意一台服务器即可)
1 、添加vip
首先要做的是配置一个IP地址,不管集群服务在哪运行,我们要一个固定的地址来提供服务,给它取一个名字luckyvip 并且告诉集群 每30秒检查它一次。选择的IP地址不能被节点所占用。
pcs resource create luckyvip ocf:heartbeat:IPaddr2 ip=172.25.66.100 cidr_netmask=32 op monitor interval=30s
[root@lucky2 ~]# pcs property set no-quorum-policy=ignore 忽略pcs property set stonith
当有半数以上的节点在线时,这个集群就认为自己拥有法定人数了,是“合法”的。当在线节点不能满足使得集群拥有法定人数,为了减少这种可能性对于数据损坏,起搏器的默认行为是停止所有资源。
两个节点的集群只有在两个节点都在运行时才具有仲裁,我们可以告诉集群完全忽略仲裁
当设置忽略仲裁之后,将定资源现在运行在服务器1,默认服务器1宕机之后,资源会转移到服务器2,然后将服务器1恢复,资源并不会切换会服务器1,而是继续运行在服务器2.
2、 添加apache服务
集群中的所有的服务器均安装apache服务。并设置共享页面。注意不用开启服务,集群会根据资源情况自动开启。
pcs resource create apache systemd:httpd op monitor interval=1min # 添加apache服务资源,并且指定启动脚本以及对服务健康状况的监控周期为1分钟。yum install httpd -y
下载httpdvim /var/www/html/index.html
书写页面
[root@lucky2 ~]# pcs status
Cluster name: mycluster
Stack: corosync
Current DC: lucky2 (version 1.1.15-11.el7-e174ec8) - partition with quorum
Last updated: Mon Jul 1 20:27:51 2019 Last change: Mon Jul 1 20:27:48 2019 by root via cibadmin on lucky2
2 nodes and 2 resources configured
Online: [ lucky2 lucky3 ]
Full list of resources:
luckyvip (ocf::heartbeat:IPaddr2): Started lucky3 vip在lucky3上运行
apache (systemd:httpd): Started lucky2 apache服务在lucky2上运行
Daemon Status:
corosync: active/enabled
pacemaker: active/enabled
pcsd: active/enabled
3 、创建资源组,并将vip以及apache资源添加到同一个资源组中
显示vip资源和apoache服务资源分别运行在不同的后端服务器。这是由于集群具有对资源平均分配到各个后端服务器的特性设置的。但是实际需要是vip必须和apache服务运行在同一个后端服务器,才能保证客户的正常访问。此时需要设置资源组。
pcs resource group add lucky_group luckyvip apache
# 创建资源组lucky_group,并将资源luckyvip和apache添加到资源组中。
查看集群资源信息状态:所有资源在同一个资源组中,运行在同一个资源组中。
[root@lucky2 ~]# pcs resource group add lucky_group luckyvip apache
[root@lucky2 ~]# pcs status
Cluster name: mycluster
Stack: corosync
Current DC: lucky2 (version 1.1.15-11.el7-e174ec8) - partition with quorum
Last updated: Mon Jul 1 20:30:13 2019 Last change: Mon Jul 1 20:29:52 2019 by root via cibadmin on lucky2
2 nodes and 2 resources configured
Online: [ lucky2 lucky3 ]
Full list of resources:
Resource Group: lucky_group 在同一个组中,可以更好的管理
luckyvip (ocf::heartbeat:IPaddr2): Started lucky3
apache (systemd:httpd): Started lucky3
Daemon Status:
corosync: active/enabled
pacemaker: active/enabled
pcsd: active/enabled
测试:
1、模拟节点宕机
此时资源运行在节点lucky3
[root@lucky2 ~]# pcs status
Cluster name: mycluster
Stack: corosync
Current DC: lucky2 (version 1.1.15-11.el7-e174ec8) - partition with quorum
Last updated: Mon Jul 1 20:32:46 2019 Last change: Mon Jul 1 20:29:52 2019 by root via cibadmin on lucky2
2 nodes and 2 resources configured
Online: [ lucky2 lucky3 ]
Full list of resources:
Resource Group: lucky_group
luckyvip (ocf::heartbeat:IPaddr2): Started lucky3
apache (systemd:httpd): Started lucky3
Daemon Status:
corosync: active/enabled
pacemaker: active/enabled
pcsd: active/enabled
将节点lucky3服务器关闭,资源会自动移动到节点lucky2,保证正常提供服务。
[root@lucky3 ~]# pcs cluster stop lucky3 模拟节点宕机
lucky3: Stopping Cluster (pacemaker)...
lucky3: Stopping Cluster (corosync)...
crm_mon查看资源管理
Stack: corosync
Current DC: lucky2 (version 1.1.15-11.el7-e174ec8) - partition with quorum
Last updated: Mon Jul 1 20:36:03 2019 Last change: Mon Jul 1 20:29:52 2019 by root via cibadmin o
n lucky2
2 nodes and 2 resources configured
Online: [ lucky2 ]
OFFLINE: [ lucky3 ]
Active resources:
Resource Group: lucky_group
luckyvip (ocf::heartbeat:IPaddr2): Started lucky2 此时的所有资源都已经转移到lucky2上了
apache (systemd:httpd): Started lucky2
重新启动节点lucky3,集群会根据健康检查情况将该结点自动添加集群,资源还是不会重新转移到lcuky3的,还是会在lucky2上的
节点状态正常,但是节点中的资源apache被关闭。
该情况下,集群会认为切换资源到另外一个节点所需要的资源以及时间远远大于检查并将目前运行主机的apache服务开启。所以不会切换资源,而是会自动开启目前资源运行阶段上的被关闭的apache服务。
[root@lucky2 ~]# systemctl stop httpd # 关闭资源所在节点的httpd服务
Stack: corosync
Current DC: lucky2 (version 1.1.15-11.el7-e174ec8) - partition with quorum
Last updated: Mon Jul 1 20:41:39 2019 Last change: Mon Jul 1 20:29:52 2019 by root via cibadmin o
n lucky2
2 nodes and 2 resources configured
Online: [ lucky2 lucky3 ]
Active resources:
Resource Group: lucky_group
luckyvip (ocf::heartbeat:IPaddr2): Started lucky2
apache (systemd:httpd): Started lucky2
Failed Actions:
* apache_monitor_60000 on lucky2 'not running' (7): call=35, status=complete, exitreason='none',
last-rc-change='Mon Jul 1 20:40:59 2019', queued=0ms, exec=0ms
#再次查看节点toto2上的httpd服务状态,集群自动开启可该服务。
[root@lucky2 ~]# systemctl status httpd
● httpd.service - Cluster Controlled httpd
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Drop-In: /run/systemd/system/httpd.service.d
└─50-pacemaker.conf
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)
稍等一会是会自动重新启动的
添加fence
为了防止两台节点服务端一直进行资源的争抢,造成客户体验端的不稳定;需要添加fence,即相当于1个开关,可以在抢占对方节点的资源之间对对方节点进行断电重启。
1、所有的集群节点中,全部安装fence服务,并且将已经设置好的密钥传给各个节点服务器。
yum install fence-virt -y # 安装fence服务
mkdir /etc/cluster # 创建存放密钥目录
将fence密钥文件分别传给各个集群节点服务器的密钥目录中
需要在真实主机中下载 fence报头插件
yum install fence-virtd.x86_64 fence-virtd-libvirt.x86_64 fence-virtd-multicast.x86_64 -y
[root@foundation66 cluster]# fence_virtd -c
Interface [virbr0]: br0 只需要将网卡改成br0
[root@foundation66 etc]# mkdir /etc/cluster
[root@foundation66 cluster]# dd if=/dev/urandom of=/etc/cluster/fence_xvm.key bs=128 count=1
1+0 records in
1+0 records out
[root@foundation66 cluster]# scp fence_xvm.key root@172.25.66.2:/etc/cluster/
root@172.25.66.2's password:
fence_xvm.key 100% 128 0.1KB/s 00:00
[root@foundation66 cluster]# scp fence_xvm.key root@172.25.66.3:/etc/cluster/
root@172.25.66.3's password:
fence_xvm.key 100% 128 0.1KB/s 00:00
2 、集群中添加fence资源
pcs stonith create vmfence fence_xvm pcmk_host_map="lucky2:lucky2;lucky3:lucky3" op monitor interval=1min
添加fence资源,主机名称:虚拟机名称
pcs resource delete 资源名称 这个为删除fence资源的命令
pcs property set stonith-enabled=true
# 开启stonish。
查看集群节点资源信息:其中资源组和fence资源运行在不同节点上面,是因为fence需要运行在没有资源的节点,好在抢夺资源的时候,可以关闭运行资源的节点。
[root@lucky2 cluster]# pcs status
Cluster name: mycluster
Stack: corosync
Current DC: lucky3 (version 1.1.15-11.el7-e174ec8) - partition with quorum
Last updated: Mon Jul 1 23:04:41 2019 Last change: Mon Jul 1 23:02:32 2019 by root via cibadmin on lucky2
2 nodes and 3 resources configured
Online: [ lucky2 lucky3 ]
Full list of resources:
Resource Group: lucky_group
luckyvip (ocf::heartbeat:IPaddr2): Started lucky3
apache (systemd:httpd): Started lucky3
vmfence (stonith:fence_xvm): Started lucky2
Daemon Status:
corosync: active/enabled
pacemaker: active/enabled
pcsd: active/enabled
3 测试:
目前资源运行在节点lucky3,模拟节点lucky3宕机,fence会自动关闭重启节点lucky3,将资源运行在节点lucky2。当节点lucky3重新启动正常之后,fence运行转移到节点lucky3.
echo c >/proc/sysrq-trigger 宕机
重启之后,资源还是会在lucky2上的,但是fence就会在lucky3 上了。
这样就形成了其高可用