一、集群概述

集群由两个或者多个计算机组成来一起执行任务。有四种主要类型的集群:

  • 存储
  • 高可用性
  • 负载均衡
  • High performance

kvm集群属于其中的高可用性集群,通过消除单点失效以及在一个节点停止运作时将服务从一个群集节点切换到另外一个节点来提供高可用性服务。通过它的高可用性服务管理组件 Pacemaker 来提供高可用性集群。

1.Pacemaker介绍

pacemaker作为集群资源管理器,它通过使用集群基础结构的消息和成员资格功能来实现集群服务和资源的最大可用性,主要有以下功能:

  • 集群管理
  • 锁定管理
  • 隔离
  • 集群配置管理

使用 Pacemaker 配置的集群由独立的组件守护进程组成,这些守护进程监控集群成员资格、管理服务的脚本以及监控不同资源的管理子系统。以下组件组成 Pacemaker 架构:

  • Cluster Information Base(CIB)

Pacemaker 信息守护进程使用 XML 在内部向所有其他集群节点分发和同步当前配置和状态信息。这些信息来自于 DC(Designated Coordinator),它是由 Pacemaker 分配的用来存储和发布集群状态和动作的节点。

  • 集群资源管理守护进程(CRMd)

Pacemaker 集群资源操作通过这个守护进程进行路由。由 CRMd 管理的资源可由客户端系统查询,并在需要时进行移动、实例化和更改。

每个节点还包括一个本地资源管理器守护进程(LRMd),它充当 CRMd 和资源间的接口。LRMd 将命令从 CRMd 传递给代理,如启动和停止状态信息。

  • STONITH(Shoot the Other Node in the Head)

通常与电源交换机一同部署,STONITH 在 Pacemaker 中作为集群资源使用,用于处理保护请求,强制关闭节点并从集群中移除它们以确保数据的完整性。STONITH 在 CIB 中配置,它可以作为普通的集群资源被监控。

  • corosync

corosync 是用于高可用性集群的核心成员资格和成员沟通需要的组件。它使用一个同名称的守护进程。它是 High Availability Add-On 正常运行所必需的功能。

除成员资格和消息功能外,corosync 还:

  • 管理仲裁规则并进行裁定。
  • 为在集群的多个成员间协调或操作的应用程序提供消息功能,因此必须在实例间进行有状态或其他信息通信。

Pacemaker由以下两种管理工具(其他的发行版可能使用crmsh)

  • pcs

pcs 可以控制 Pacemaker 和 Corosync heartbeat 守护进程的所有方面。基于命令行的程序 pcs 可以执行以下集群管理任务:

  • 创建并配置 Pacemaker/Corosync 集群
  • 在集群运行时修改集群配置
  • 远程配置 Pacemaker 和 Corosync 以及启动、停止和显示集群状态信息
  • pcsd Web UI

用于创建和配置 Pacemaker/Corosync 集群的图形用户界面,其功能与基于命令行的 pcs 工具相同。在任意系统上,打开浏览器到以下 URL,指定您授权的一个节点(请注意,这使用 https 协议)。这将调出 pcsd Web UI 登录屏幕。

https://nodename:2224

kvm你学废了么2——高可用集群安装与配置_存储位置

2.仲裁与隔离

  • 仲裁

为了保持集群的完整性和可用性,集群系统使用一个称为仲裁(quorum) 的概念来防止数据崩溃和丢失。当超过一半的集群节点在线时,集群就已被“仲裁”。为减少由于失败造成数据崩溃的机会,在不满足仲裁数量的情况下,Pacemaker 会默认停止所有资源。

仲裁是通过一个投票(voting)系统来建立的。当一个集群节点工作不正常,或丢掉了与其他集群部分的通信,则大多数工作的节点可以通过投票来分离有问题的节点,如果需要,对节点进行隔离。

  • 隔离

如果与集群中的单个节点通信失败,则集群中的其他节点必须能够限制或释放对故障集群节点可能有权访问的资源的访问。无法通过联系集群节点本身来实现。因为集群节点本身可能无法响应。反之,必须提供一个外部的方法来实现。这个方法为称为隔离(fencing)。隔离设备是一个外部设备,集群使用它用来限制错误节点对共享资源的访问,或对集群的节点执行硬重启。

当隔离进行时,不允许执行其他集群操作。在隔离完成前,或集群节点重启后重新加入集群前,集群的正常操作不能恢复。

3.集群资源

集群资源是一个由集群服务管理的程序、数据或应用程序实例。这些资源通过代理(agent)进行管理。

为确保资源健康,您可以在资源的定义中添加监控操作。如果您没有为资源指定监控操作,则会默认添加一个。

您可以通过配置约束(constraint)来决定集群中的资源行为。您可以配置以下约束类别:

  • 位置约束 - 位置约束决定资源可在哪个节点上运行。
  • 排序约束 - 排序约束决定资源运行的顺序。
  • 共同位置约束 - 共同位置约束(colocation constraint)决定资源相对于其他资源的位置。

最常见的元素是一组资源,这些资源需要放置在一起,并按顺序启动并按反顺序停止。为简化此配置,Pacemaker 支持组的概念。

kvm你学废了么2——高可用集群安装与配置_centos_02

二、实施前准备

1.安装集群软件包

[root@node4 ~]# yum install -y corosync pacemaker pcs fence-agents-all.x86_64

2.配置NTP

将NTP都指向阿里NTP,保证集群内时钟一致

kvm你学废了么2——高可用集群安装与配置_共享存储_03

3.配置主机名解析

将不同网络平面的主机名都做名称解析,便于后面部署

kvm你学废了么2——高可用集群安装与配置_存储位置_04

4.节点间免密登录(非必须)

为了便于操作,将节点之间配置免密登录,在node4节点上生成一个密钥对

[root@node4 .ssh]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:etsrEUKZMECflv65OVQR8k1tXcMSly+f//y4RZXuV9A root@node4
The key's randomart image is:
+---[RSA 2048]----+
| .o.o..o.... oo+o|
|   . ++o.o  o.o+o|
|    =.  ....  o.E|
|   o  . o     o.o|
|    .  oS.     ++|
|     ..o.     .oo|
|     .+ ..     .+|
|      .+.o     +o|
|      o...o.  o.*|
+----[SHA256]-----+

将公钥发送给node5节点,这样node4节点就可以凭借私钥登录node5节点

[root@node4 .ssh]# ssh-copy-id -i id_rsa.pub root@node5-hb
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node5-hb's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@node5-hb'"
and check to make sure that only the key(s) you wanted were added.

node5节点做类似操作,不再赘述

5.配置pcs守护程序

pcs是一个corosync和pacemaker的配置工具,支持命令与图形化界面,这一点pcs的描述信息中可以看到

[root@node4 ~]# rpm -qi pcs
Name        : pcs
Version     : 0.9.169
Release     : 3.el7.centos.3
Architecture: x86_64
Install Date: Sat 13 Apr 2024 01:00:15 PM CST
Group       : System Environment/Base
Size        : 11483086
License     : GPLv2
Signature   : RSA/SHA256, Sat 12 Nov 2022 12:15:48 AM CST, Key ID 24c6a8a7f4a80eb5
Source RPM  : pcs-0.9.169-3.el7.centos.3.src.rpm
Build Date  : Thu 10 Nov 2022 09:56:56 PM CST
Build Host  : x86-02.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : https://github.com/ClusterLabs/pcs
Summary     : Pacemaker Configuration System
Description :
pcs is a corosync and pacemaker configuration tool.  It permits users to
easily view, modify and create pacemaker based clusters.

使用pcs工具,可以简化集群配置的复杂度,需要将该工具已一个守护进程的方式运行

[root@node4 ~]# systemctl enable --now pcsd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/pcsd.service to /usr/lib/systemd/system/pcsd.service.
[root@node4 ~]#
[root@node4 ~]# systemctl status pcsd.service
● pcsd.service - PCS GUI and remote configuration interface
   Loaded: loaded (/usr/lib/systemd/system/pcsd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2024-04-13 20:40:20 CST; 3s ago
     Docs: man:pcsd(8)
           man:pcs(8)
 Main PID: 16197 (pcsd)
    Tasks: 4
   CGroup: /system.slice/pcsd.service
           └─16197 /usr/bin/ruby /usr/lib/pcsd/pcsd

Apr 13 20:40:19 node4 systemd[1]: Starting PCS GUI and remote configuration interface...
Apr 13 20:40:20 node4 systemd[1]: Started PCS GUI and remote configuration interface.

6.配置hacluster用户码

已mysql为例,linux HA故障切换是指当位于A节点的mysql应用发生故障时,作为集群资源管理器的pacemaker可以将A节点的mysql服务终止,并在B节点上启用,这就需要有一个户用在集群中各个节点上都能进行该操作,所以在安装pacemaker包时,它默认会创建一个hacluster用户来实现该功能

[root@node4 ~]# tail -1 /etc/passwd
hacluster:x:189:189:cluster user:/home/hacluster:/sbin/nologin

通过shadow文件第二字段能看出,该账户默认被禁用,此时需要在每一个节点上为该账户设置相同的密码,以启用账户

[root@node4 ~]# tail -1 /etc/shadow
hacluster:!!:19826::::::
[root@node4 ~]#
[root@node4 ~]# echo "passw0rd" | passwd --stdin hacluster
Changing password for user hacluster.
passwd: all authentication tokens updated successfully.
[root@node4 ~]#
[root@node4 ~]# tail -1 /etc/shadow
hacluster:$6$jnPqOj/k$OaUJyvUDo49ia05mD0WkMCpekqP1GC69r.AWImrhJ7oUsNzB9jo0wOo0POeek.HqHxMIjXArgfW1rjR9wmmga/:19826::::::

7.集群及pacemaker配置文件

集群的配置文件是 corosync.conf 和 cib.xml,通常不要直接编辑,改为使用 pcs 或 pcsd 接口。

corosync.conf 文件提供了 corosync(Pacemaker 构建的集群管理器)使用的集群参数。

cib.xml 文件是一个 XML 文件,它代表集群的配置和群集中所有资源的当前状态。Pacemaker 的集群信息基础(Cluster Information Base,CIB)会使用这个文件。CIB 的内容会在整个集群中自动保持同步。

kvm你学废了么2——高可用集群安装与配置_centos_05

三、创建集群

1.认证组成集群的节点

根据前期规划,集群有专门的心跳网络平面,并已通过hosts文件做了地址解析,所以使用专门的心跳网络

[root@node4 ~]# pcs cluster auth node4-hb node5-hb -u hacluster -p passw0rd
node5: Authorized
node4: Authorized

2.配置和同步集群节点

[root@node4 ~]# pcs cluster setup --name halinux node4-hb node5-hb
Destroying cluster on nodes: node4-hb, node5-hb...
node5-hb: Stopping Cluster (pacemaker)...
node4-hb: Stopping Cluster (pacemaker)...
node5-hb: Successfully destroyed cluster
node4-hb: Successfully destroyed cluster

Sending 'pacemaker_remote authkey' to 'node4-hb', 'node5-hb'
node4-hb: successful distribution of the file 'pacemaker_remote authkey'
node5-hb: successful distribution of the file 'pacemaker_remote authkey'
Sending cluster config files to the nodes...
node4-hb: Succeeded
node5-hb: Succeeded

Synchronizing pcsd certificates on nodes node4-hb, node5-hb...
node5-hb: Success
node4-hb: Success
Restarting pcsd on the nodes in order to reload the certificates...
node5-hb: Success
node4-hb: Success

可以看到,当使用pcs命令设置完集群后,在/etc/corosync/目录下,会生成corosync.conf文件作为集群的配置信息,同时告诉你log日志位置

[root@node4 ~]# ls /etc/corosync/
corosync.conf  corosync.conf.example  corosync.conf.example.udpu  corosync.xml.example  uidgid.d
[root@node4 ~]#
[root@node4 ~]# cat /etc/corosync/corosync.conf
totem {
    version: 2
    cluster_name: halinux
    secauth: off
    transport: udpu
}

nodelist {
    node {
        ring0_addr: node4-hb
        nodeid: 1
    }

    node {
        ring0_addr: node5-hb
        nodeid: 2
    }
}

quorum {
    provider: corosync_votequorum
    two_node: 1
}

logging {
    to_logfile: yes
    logfile: /var/log/cluster/corosync.log
    to_syslog: yes
}

3.在节点上启动集群

需要注意的是,如果不加“--all”则只启用本节点的corosync和pacemaker服务。从另一个层面说,corosync和pacemaker应该通过pcs工具管理,不应该通过systemctl enable启用,此外所有由集群管理的服务与资源,都不应该使用systemctl enable启用

[root@node4 ~]# pcs cluster start --all
node4-hb: Starting Cluster (corosync)...
node5-hb: Starting Cluster (corosync)...
node5-hb: Starting Cluster (pacemaker)...
node4-hb: Starting Cluster (pacemaker)...

在 /var/lib/pacemaker/cib/目录下,可以看到自动生成的资源信息

[root@node4 ~]# ll /var/lib/pacemaker/cib/
total 36
-rw------- 1 hacluster haclient 259 Apr 14 12:15 cib-1.raw
-rw------- 1 hacluster haclient  32 Apr 14 12:15 cib-1.raw.sig
-rw------- 1 hacluster haclient 417 Apr 14 12:15 cib-2.raw
-rw------- 1 hacluster haclient  32 Apr 14 12:15 cib-2.raw.sig
-rw------- 1 hacluster haclient 724 Apr 14 12:16 cib-3.raw
-rw------- 1 hacluster haclient  32 Apr 14 12:16 cib-3.raw.sig
-rw-r----- 1 hacluster haclient   1 Apr 14 12:16 cib.last
-rw------- 1 hacluster haclient 961 Apr 14 12:16 cib.xml
-rw------- 1 hacluster haclient  32 Apr 14 12:16 cib.xml.sig

此时集群已经启动,该集群目前没有任何资源

[root@node4 ~]# pcs status
Cluster name: halinux

WARNINGS:
No stonith devices and stonith-enabled is not false

Stack: corosync
Current DC: node5-hb (version 1.1.23-1.el7_9.1-9acf116022) - partition with quorum
Last updated: Sun Apr 14 17:37:35 2024
Last change: Sun Apr 14 12:16:06 2024 by hacluster via crmd on node5-hb

2 nodes configured
0 resource instances configured

Online: [ node4-hb node5-hb ]

No resources


Daemon Status:
  corosync: active/disabled
  pacemaker: active/disabled
  pcsd: active/enabled

并且提到此时既没有隔离设备,也没有禁用隔离功能,先通过修改属性禁用隔离功能,后期演示

[root@node4 ~]# pcs property set stonith-enabled=false
[root@node4 ~]#
[root@node4 ~]# pcs property
Cluster Properties:
 cluster-infrastructure: corosync
 cluster-name: halinux
 dc-version: 1.1.23-1.el7_9.1-9acf116022
 have-watchdog: false
 stonith-enabled: false