CentOS 8设置高可用性群集

时间:2020-02-23 14:40:28  来源:igfitidea点击:

在本文中,介绍在CentOS 8上的"使用LVM设置3节点高可用性群集"的分步指南。

构建高可用性集群的组件

要构建高可用性群集,我们不仅需要将几个服务器捆绑在一起。

通常,大多数群集中使用以下组件:

  • 共享存储

  • 不同的网络

  • 绑定网络设备

  • 多路径

  • 击剑/STONITH设备

我们可以在Linux高可用性群集中了解有关各个组件的更多信息。

配置环境

我在Linux服务器上安装的Oracle VirtualBox上创建了3个具有CentOS 8.1的虚拟机,而一个具有CentOS 7的虚拟机。

3个虚拟机将成为我们Linux HA群集的一部分,而第4个虚拟机将用于配置iSCSI存储。
我们将使用此iSCSI存储在所有群集之间配置共享存储。

我已经在Oracle VirtualBox中配置了一个内部网络,群集通过该内部网络相互通信,并使用一个独立的网络连接外部网络

前提条件:

将CentOS 8更新到最新可用版本

从CentOS 8.1开始,可以安装诸如pacemaker之类的高可用性rpm所需的存储库,因此,如果我们使用的是CentOS 8.0,则必须使用以下方法更新Linux环境:

# dnf update

这会将Linux服务器更新为最新的可用CentOS版本。
以下是我的CentOS发行详细信息

[root@centos8-1 ~]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)

" HighAvailability"存储库已添加到:

2019-12-17 - [email protected] - 8-1.el8
- Add the HighAvailability repository

更新/etc/hosts或者使用DNS服务器

高可用性群集的所有节点都必须能够使用FQDN相互通信,因此我们可以

  • 配置DNS服务器以进行名称解析

  • 更新/etc/hosts

下面是所有集群节点中我的/etc/hosts的输出

[root@centos8-1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.10.17   centos8-3         centos8-3.example.com
10.10.10.16   centos8-2         centos8-2.example.com
10.10.10.12   centos8-1         centos8-1.example.com

安装EPEL仓库

我们可能还需要EPEL存储库,因此也可以在CentOS 8 Linux节点上安装EPEL存储库。

[root@centos8-1 ~]# dnf install epel-release

配置计时NTP

在高可用性群集中,必须同时配置所有群集节点,这一点很重要。
因此,我们必须在所有群集节点上设置chrony服务。

"让我们从CentOS 8高可用性集群配置分步指南开始。
"

配置共享存储

在本文中,由于我们计划"为LVM资源设置高可用性群集",因此我们将需要共享存储。
由于这是我的实验室环境,因此我无法访问SAN存储,因此我将使用iSCSI存储。

我已经写了另一篇文章,其中详细介绍了使用RHEL/CentOS 7/8 Linux配置iSCSI存储的步骤。

因此,我将不再其中重复这些步骤。

发现iSCSI之后,我在所有群集节点上都可以使用/dev/sdc并从iSCSI目标作为iSCSI存储连接。
在下一章中,我们将在/dev/sdc创建LVM资源

[root@centos8-1 ~]# lsscsi
[0:0:0:0]    cd/dvd  VBOX     CD-ROM           1.0   /dev/sr0
[1:0:0:0]    cd/dvd  VBOX     CD-ROM           1.0   /dev/sr1
[2:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sda
[3:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sdb
[4:0:0:0]    disk    LIO-ORG  sdb1             4.0   /dev/sdc

安装起搏器和其他高可用性rpm

默认情况下,根据以下提交,在CentOS 8上禁用高可用性存储库

2019-12-19 - [email protected] - 8-1.0.7
- Typo fixes
- Disable the HA repo by default

因此,在安装CentOS HA Cluster rpm之前,我们将启用" HighAvailability"存储库。

[root@centos8-1 ~]# dnf config-manager --set-enabled HighAvailability

列出启用的存储库

[root@centos8-1 ~]# dnf repolist
Last metadata expiration check: 0:00:07 ago on Sun 05 Apr 2017 01:40:56 PM IST.
repo id                          repo name                                                               status
AppStream                        CentOS-8 - AppStream                                                    5,124
BaseOS                           CentOS-8 - Base                                                         2,126
HighAvailability                 CentOS-8 - HA                                                             130
PowerTools                       CentOS-8 - PowerTools                                                   1,525
*epel                            Extra Packages for Enterprise Linux 8 - x86_64                          5,138
*epel-modular                    Extra Packages for Enterprise Linux Modular 8 - x86_64                      0
extras                           CentOS-8 - Extras                                                          12

使用DNF(这是RHEL/CentOS 8中的默认软件包管理器)安装起搏器Linux和其他高可用性rpm。

[root@centos8-1 ~]# dnf install pcs pacemaker fence-agents-all -y

启动心脏起搏器群集管理器服务

在配置Linux HA Cluster之前,必须启动pcs守护程序并将其启用,以使其在引导时在Linux HA Cluster的每个节点上启动。
该守护程序与pcs命令行界面一起使用,以管理集群中所有节点之间的corosync配置同步。

[root@centos8-1 ~]# systemctl enable pcsd.service --now
Created symlink /etc/systemd/system/multi-user.target.wants/pcsd.service → /usr/lib/systemd/system/pcsd.service.

第4步:为hacluster分配密码

在使用禁用的密码安装高可用性群集rpm之后创建" hacluster"用户。
在Linux HA群集中的每个节点上为用户" hacluster"设置密码,并在要从其运行pcs命令的节点上为群集中的每个节点认证用户" hacluster"。

[root@centos8-1 ~]# passwd hacluster
Changing password for user hacluster.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

配置firewalld

如果运行的是firewalld守护程序,请在所有群集节点上启用CentOS高可用性存储库所需的端口。

[root@centos8-1 ~]# firewall-cmd --permanent --add-service=high-availability
success
[root@centos8-1 ~]# firewall-cmd --reload
success

提示:

在POC阶段,我们可以选择禁用firewalld(" systemctl stop firewalld")以验证初始配置,然后在启用生产资格之前启用防火墙

配置Corosync

在任一群集节点上,使用pcs host auth身份验证为hacluster用户。
使用以下语法:

pcs host auth [node1] [node2] [node3] ..

在带有起搏器Linux的RHEL/CentOS 7高可用性集群中,我们使用了" pcs cluster auth"来对集群进行身份验证,但是在RHEL/CentOS 8中已更改为pcs主机认证

[root@centos8-1 ~]# pcs host auth centos8-1.example.com centos8-2.example.com centos8-3.example.com
Username: hacluster
Password:
centos8-2.example.com: Authorized
centos8-3.example.com: Authorized
centos8-1.example.com: Authorized

现在,由于群集节点已获得授权,因此我们可以继续进行下一步以设置高可用性群集。
我将创建一个名为my_cluster的三节点Linux HA集群。

[root@centos8-1 ~]# pcs cluster setup my_cluster centos8-1.example.com centos8-2.example.com centos8-3.example.com
No addresses specified for host 'centos8-1.example.com', using 'centos8-1.example.com'
No addresses specified for host 'centos8-2.example.com', using 'centos8-2.example.com'
No addresses specified for host 'centos8-3.example.com', using 'centos8-3.example.com'
Destroying cluster on hosts: 'centos8-1.example.com', 'centos8-2.example.com', 'centos8-3.example.com'...
centos8-1.example.com: Successfully destroyed cluster
centos8-3.example.com: Successfully destroyed cluster
centos8-2.example.com: Successfully destroyed cluster
Requesting remove 'pcsd settings' from 'centos8-1.example.com', 'centos8-2.example.com', 'centos8-3.example.com'
centos8-1.example.com: successful removal of the file 'pcsd settings'
centos8-3.example.com: successful removal of the file 'pcsd settings'
centos8-2.example.com: successful removal of the file 'pcsd settings'
Sending 'corosync authkey', 'pacemaker authkey' to 'centos8-1.example.com', 'centos8-2.example.com', 'centos8-3.example.com'
centos8-1.example.com: successful distribution of the file 'corosync authkey'
centos8-1.example.com: successful distribution of the file 'pacemaker authkey'
centos8-2.example.com: successful distribution of the file 'corosync authkey'
centos8-2.example.com: successful distribution of the file 'pacemaker authkey'
centos8-3.example.com: successful distribution of the file 'corosync authkey'
centos8-3.example.com: successful distribution of the file 'pacemaker authkey'
Sending 'corosync.conf' to 'centos8-1.example.com', 'centos8-2.example.com', 'centos8-3.example.com'
centos8-1.example.com: successful distribution of the file 'corosync.conf'
centos8-3.example.com: successful distribution of the file 'corosync.conf'
centos8-2.example.com: successful distribution of the file 'corosync.conf'
Cluster has been successfully set up.

启动和验证集群

.1:启动Linux HA群集

现在已经配置了corosync,该启动"启动集群"了。
下面的命令将在集群中所有节点上启动corosync和心脏起搏器Linux。

[root@centos8-1 ~]# pcs cluster start --all

.2:验证corosync安装

" corosync-cfgtool"是用于在" corosync"中显示和配置活动参数的工具

[root@centos8-1 ~]# corosync-cfgtool -s
Printing link status.
Local node ID 1
LINK ID 0
        addr    = 10.10.10.12
        status:
                nodeid  1:      link enabled:1  link connected:1
                nodeid  2:      link enabled:1  link connected:1
                nodeid  3:      link enabled:1  link connected:1

其中

-s      Displays  the  status  of the current links on this node for UDP/UDPU, with extended status for KNET. 
        If any interfaces are faulty, 1 is returned by the binary. If all interfaces are active 0 is returned to the shell.

我们其中可以看到"一切看起来都很正常",其中列出了我们的固定IP地址(不是127.0.0.x回送地址)作为id,并且状态没有任何问题。
如果我们看到不同的内容,则可能首先要检查节点的网络,防火墙和SELinux配置。

接下来,检查成员资格和仲裁API:

[root@centos8-1 ~]# corosync-cmapctl | grep members
runtime.members.1.config_version (u64) = 0
runtime.members.1.ip (str) = r(0) ip(10.10.10.12)
runtime.members.1.join_count (u32) = 1
runtime.members.1.status (str) = joined
runtime.members.2.config_version (u64) = 0
runtime.members.2.ip (str) = r(0) ip(10.10.10.16)
runtime.members.2.join_count (u32) = 3
runtime.members.2.status (str) = joined
runtime.members.3.config_version (u64) = 0
runtime.members.3.ip (str) = r(0) ip(10.10.10.17)
runtime.members.3.join_count (u32) = 3
runtime.members.3.status (str) = joined

检查跨群集节点的corosync的状态

[root@centos8-1 ~]# pcs status corosync
Membership information
---------------------
    Nodeid      Votes Name
         1          1 centos8-1.example.com (local)
         2          1 centos8-2.example.com
         3          1 centos8-3.example.com

.3:验证Pacemaker Linux安装

既然我们已经确认Corosync是可以正常工作的,那么我们就可以检查其余的堆栈了。
Pacemaker Linux已经启动,因此请验证必要的进程正在运行:

[root@centos8-1 ~]# pcs status
Cluster name: my_cluster
WARNINGS:
No stonith devices and stonith-enabled is not false
Stack: corosync
Current DC: centos8-1.example.com (version 2.0.2-3.el8_1.2-744a30d655) - partition with quorum
Last updated: Sun Apr  5 15:25:03 2017
Last change: Sun Apr  5 15:24:23 2017 by hacluster via crmd on centos8-1.example.com
3 nodes configured
0 resources configured
Online: [ centos8-1.example.com centos8-2.example.com centos8-3.example.com ]
No resources

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

我们还将使corosync和心脏起搏器服务能够在所有Linux HA Cluster节点上启动时自动启动。

[root@centos8-1 ~]# systemctl enable corosync
Created symlink /etc/systemd/system/multi-user.target.wants/corosync.service → /usr/lib/systemd/system/corosync.service.
[root@centos8-1 ~]# systemctl enable pacemaker
Created symlink /etc/systemd/system/multi-user.target.wants/pacemaker.service → /usr/lib/systemd/system/pacemaker.service.

禁用stonith(可选)

高可用性群集要求我们为群集配置防护,以控制群集节点。
击剑也被称为STONITH,是头上的另一个节点射击的缩写,因为击剑的最流行形式是切断主机的电源。

但是,在本文中,"我仅打算显示基本的Pacemaker Linux命令",因此我们通过将"启用stonith-enabled"的群集选项设置为false来禁用防护。

[root@centos8-1 ~]# pcs property set stonith-enabled=false

警告:

使用stonith-enabled = false完全不适合生产集群。
它告诉集群简单地假装发生故障的节点已安全地处于关闭状态。
一些供应商将拒绝支持STONITH禁用的群集。

步骤9:创建主动/被动HA LVM群集

我们将使用LVM资源配置主动/被动Linux HA群集。

步骤9.1:定义system_id_source

使用RHEL/CentOS 7,我们曾经在所有群集节点上创建LVM以设置HA LVM群集。

但是通过RHEL/CentOS 8,我们可以在lvm.conf中使用system_id_source功能,该功能将使用系统ID在HA LVM群集内进行通信。

  • LVM用于设置本地系统ID的方法。

  • lvm(系统ID限制卷组(VG)对一个主机的访问。

  • 当将VG放置在共享存储设备上时,或者当主机和来宾操作系统都可以看到本地设备时,这很有用。

  • 在这种情况下,一个VG可以一次对多个主机可见,并且需要某种机制来保护它一次不被多个主机使用。

有关更多信息,请检查lvmsystemid的手册页。

在所有集群节点上为system_id_source更新/etc/lvm/lvm.conf文件,并将其值指定为uname

system_id_source = "uname"

验证节点上的" LVM系统ID"是否与该节点的uname相匹配。

[root@centos8-1 ~]# lvm systemid
  system ID: centos8-1.example.com
  
[root@centos8-1 ~]# uname -n
centos8-1.example.com

步骤9.2:重建initramfs

通过以下步骤在所有群集节点上重建" initramfs"。
在所有群集节点上备份当前的initramfs文件。

# cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.$(date +%m-%d-%H%M%S).bak

在两个集群节点上重建" initramfs"文件。

# dracut -f -v

重新启动所有群集节点。

步骤9.3:在共享存储上创建逻辑卷

一旦所有Linux HA群集节点重新联机,就可以从任一群集节点在PV/VG/LV上进行创建。
在以下示例中,VG名称为cluster_vg,LV名称为cluster_lv

仅在任何一个集群节点上执行以下步骤

我的共享存储已映射到/dev/sdc

[root@centos8-1 ~]# lsscsi
[0:0:0:0]    cd/dvd  VBOX     CD-ROM           1.0   /dev/sr0
[1:0:0:0]    cd/dvd  VBOX     CD-ROM           1.0   /dev/sr1
[2:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sda
[3:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sdb
[4:0:0:0]    disk    LIO-ORG  sdb1             4.0   /dev/sdc

/dev/sdc上创建物理卷

[root@centos8-1 ~]# pvcreate /dev/sdc
  Physical volume "/dev/sdc" successfully created.

创建一个新的卷组" cluster_vg",该卷组由物理卷"/dev/sdc"组成

[root@centos8-1 ~]# vgcreate cluster_vg /dev/sdc
  Volume group "cluster_vg" successfully created with system ID centos8-1.example.com

验证新卷组是否具有我们正在其上运行并从中创建该卷组的节点的系统ID。

[root@centos8-1 ~]# vgs -o+systemid
  VG         #PV #LV #SN Attr   VSize  VFree  System ID
  cluster_vg   1   1   0 wz--n-  9.96g     0  centos8-1.example.com
  rhel         2   5   0 wz--n- 22.49g <3.00g

创建逻辑卷" cluster_lv"

[root@centos8-1 ~]# lvcreate -l 100%FREE -n cluster_lv cluster_vg
  Logical volume "cluster_lv" created.

在其他群集节点上,我们将找不到此卷组cluster_vg

[root@centos8-2 ~]# vgs -o+systemid
  VG   #PV #LV #SN Attr   VSize  VFree  System ID
  rhel   2   5   0 wz--n- 22.49g <3.00g

我们可以看到新的VG仅在我们创建的centos8-1上可见,并且具有与主机名相同的系统ID。
在第二个节点上时,vgs命令不会导致显示cluster_vg,因为它具有centos8-1.example.com作为其当前的系统ID。

在新创建的LVM设备上根据要求(ext4/xfs)创建文件系统。
" mkfs"命令应在VG处于活动状态的群集节点上执行。
我正在这个新的逻辑卷上创建XFS文件系统。

[root@centos8-1 ~]# mkfs.xfs /dev/cluster_vg/cluster_lv
meta-data=/dev/cluster_vg/cluster_lv isize=512    agcount=4, agsize=653056 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=2612224, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

在所有群集节点上为HA逻辑卷创建安装点。

[root@centos8-1 ~]# mkdir /lvm_cluster

我正在将要装载我的逻辑卷资源的所有群集节点上创建"/lvm_cluster"。
我们可以根据需要使用其他安装点

步骤9.4:创建集群资源

在创建LVM群集资源之前,让我们验证群集的运行状况:

[root@centos8-1 ~]# pcs status
Cluster name: my_cluster
Stack: corosync
Current DC: centos8-1.example.com (version 2.0.2-3.el8_1.2-744a30d655) - partition with quorum
Last updated: Sun Apr  5 15:30:22 2017
Last change: Sun Apr  5 15:30:12 2017 by root via cibadmin on centos8-1.example.com
3 nodes configured
0 resources configured
Online: [ centos8-1.example.com centos8-2.example.com centos8-3.example.com ]
No resources

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

我们将所有群集节点标记为联机。

接下来,使用资源代理" ocf:heartbeat:LVM-activate"创建集群资源,以便可以由集群管理VG。

[root@centos8-1 ~]# pcs resource create my-vg ocf:heartbeat:LVM-activate vgname=cluster_vg activation_mode=exclusive vg_access_mode=system_id --group HA-LVM

其中资源名称为" my-vg",资源组名称为" HA-LVM"。
这些值可以根据要求进行更新。

使用资源代理" ocf:heartbeat:Filesystem"创建集群资源,因此集群将控制文件系统的安装并使其在集群节点之一上可用。

[root@centos8-1 ~]# pcs resource create my-fs ocf:heartbeat:Filesystem device=/dev/cluster_vg/cluster_lv directory=/lvm_cluster fstype=xfs --group HA-LVM

资源名称为" my-fs",在设备"/dev/cluster_vg/cluster_lv"上创建的文件系统的目录将挂载在"/lvm_cluster",文件系统的类型为xfs,并且该资源保存在同一资源组中,即HA-LVM

0:验证高可用性群集配置

接下来,检查两个集群HA LVM资源的集群资源状态:

[root@centos8-1 ~]# pcs status
Cluster name: my_cluster
Stack: corosync
Current DC: centos8-2.example.com (version 2.0.2-3.el8_1.2-744a30d655) - partition with quorum
Last updated: Sun Apr  5 15:46:40 2017
Last change: Sun Apr  5 15:46:37 2017 by root via cibadmin on centos8-1.example.com
3 nodes configured
2 resources configured
Online: [ centos8-1.example.com centos8-2.example.com centos8-3.example.com ]
Full list of resources:
 Resource Group: HA-LVM
     my-fs      (ocf::heartbeat:Filesystem):    Started centos8-1.example.com
     my-vg      (ocf::heartbeat:LVM-activate):  Started centos8-1.example.com
Daemon Status:
  corosync: active/enabled
  pacemaker: active/enabled
  pcsd: active/enabled

现在,VG已在centos8-1.example.com上激活,文件系统已安装在同一节点上。

这可以通过vgs,lvs,df和其他类似命令进行验证

[root@centos8-1 ~]# mount | grep lvm_cluster
/dev/mapper/cluster_vg-cluster_lv on /lvm_cluster type xfs (rw,relatime,attr2,inode64,noquota)
[root@centos8-1 ~]# df -Th | grep lvm_cluster
/dev/mapper/cluster_vg-cluster_lv xfs        10G  104M  9.9G   2% /lvm_cluster

现在,我们将" centos8-1"的"集群状态"从"活动"更改为"备用",以确保我们的HA LVM集群资源自动从其他集群节点启动

[root@centos8-1 ~]# pcs node standby centos8-1.example.com

现在,如果我们检查Linux HA群集状态,则" centos8-1"处于备用状态,而我们的群集HA LVM资源正在" centos8-2"群集节点上运行。

[root@centos8-2 ~]# pcs status
Cluster name: my_cluster
Stack: corosync
Current DC: centos8-1.example.com (version 2.0.2-3.el8_1.2-744a30d655) - partition with quorum
Last updated: Sun Apr  5 19:29:10 2017
Last change: Sun Apr  5 19:26:39 2017 by hacluster via crmd on centos8-1.example.com
3 nodes configured
2 resources configured
Node centos8-1.example.com: standby
Online: [ centos8-2.example.com centos8-3.example.com ]
Full list of resources:
 Resource Group: HA-LVM
     my-vg      (ocf::heartbeat:LVM-activate):  Started centos8-2.example.com
     my-fs      (ocf::heartbeat:Filesystem):    Started centos8-2.example.com
Daemon Status:
  corosync: active/enabled
  pacemaker: active/enabled
  pcsd: active/enabled

我们也可以在centos8-2上验证已安装的分区。

[root@centos8-2 ~]# vgs
  VG         #PV #LV #SN Attr   VSize  VFree
  cluster_vg   1   1   0 wz--n-  9.96g     0
  rhel         2   5   0 wz--n- 22.49g <3.00g
[root@centos8-2 ~]# mount | grep lvm_cluster
/dev/mapper/cluster_vg-cluster_lv on /lvm_cluster type xfs (rw,relatime,attr2,inode64,noquota)
[root@centos8-2 ~]# df -Th /lvm_cluster/
Filesystem                        Type  Size  Used Avail Use% Mounted on
/dev/mapper/cluster_vg-cluster_lv xfs    10G  104M  9.9G   2% /lvm_cluster

重要的提示:

如果我们错过了所有集群节点上的system_id_source更改,那么当我们将活动集群节点切换为备用数据库时,HA LVM集群资源可能无法启动,并显示错误消息。
指定的vg_access_mode与VG上的lock_type不匹配元数据。
为了克服这种错误情况,

使用当前主机名手动添加VG的系统ID:

# vgchange --systemid $(uname -n) <vg name>

手动设置系统ID之后,当资源从该点开始进行故障转移时,群集将管理分配适当的系统ID。

可以使用以下vgs命令验证系统ID

# vgs -o+systemid