[命令] Pacemaker 命令 pcs stonith (管理隔离)

注意:

在使用 Pacemaker 的 fence 之前要先安装 Pacemaker 集群 ,并且需要 root 权限

正文:

内容一:fence 介绍

fence 的作用是隔离不需要的主机,当一台主机和集群失去联系时,将其隔离,以防止脑裂

内容二:pcs stonith 命令的格式

# pcs stonith <option> name fencing_agent <parameter>

内容三:fence 管理

3.1 显示所有可用的 fence 的列表

# pcs stonith list

3.2 显示某种可用 fence 的详细信息

# pcs stonith describe fence_rhevm

3.3 显示所有已配置的 fence 状况

# pcs stonith fence show --full

3.4 删除 fence

3.4.1 删除某个 fence
# pcs stonith delete <fence>
3.4.2 删除当前所有的 fencing 资源
# for i in fence_node{a..d}; do
> pcs stonith delete &{I}
> done

3.5 创建 fence_vmware_soap 的案例

# pcs stonith create vmfence fence_vmware_soap ipaddr=192.168.0.254 login="vmfence" passwd="vmfence" ssl_insecure=1 pcmk_host_check="static-list" pcmk_host_list="pacemaker0 pacemaker1 pacemaker2"


补充:这里以创建
1) 名为 vmfence
2) fence 服务器的 IP 地址为 192.168.0.254
3) fence 服务器的用户名为
4) fence 服务器的密码为
5) 被 fence 监控的服务器为 pacemaker0、pacemaker1 和 pacemaker2
的 fence 为例

(注意:fence_vmware_soap 需要在 vmware 环境下才能被使用,且需要设置好 vmfence 的用户、密码、IP 等)

内容四:使用 fence 管理服务器

4.1 隔离某个台服务器

# pcs stonith fence <host>

4.2 从 fencing 删除某台服务器

# pcs stonith delete <host>

[命令] Pacemaker 命令 pcs status (显示 Pacemaker 的状态)

注意:

在使用 Pacemaker 命令之前要先安装 Pacemaker 集群 ,并且需要 root 权限

正文:

命令一:只显示与集群相关的信息

# pcs status cluster

命令二:只显示资源组和他们的资源

# pcs status groups

命令三:只显示资源组和它们的资源

# pcs status resources

命令四:只显示集群主机的配置状态

# pcs status nodes

命令五:只显示 corosync 的状态

# pcs status corosync

命令六:只显示 pcsd 在每个主机上的配置状态

# pcs status pcsd

命令七:显示 pcs 总共的主机数,当前的主机数,最大期望投票数,最低投票数要求,当前生效投票数

# pcs corosync-quorumtool -m

(注意:为了防止脑裂,PCS 的最低投票数必须高于总主机数的 50%)

命令八:显示集群状态和配置

# crm_mon -r

命令九:显示最近的投票信息

# watch -n1 corosync-quorumtool

(注意:当期望投票数发生变化时, # pcs corosync-quorumtool -m 命令不会自动更新,所以最好使用 # watch -n1 corosync-quorumtool 命令)

命令十:显示所有的 pcs 资源

# pcs resource show

命令十一:显示某个 pcs 资源

# pcs resourece show <resource>

命令十二:显示所有 pcs 资源被限制的列表

# pcs constraint list

命令十三:显示现有的资源限制信息

# pcs constraint

命令十四:显示现有的详细资源限制信息

# pcs constraint --full

命令十五:显示资源被限制到某台服务器的信息

# crm_simulate -sL

[命令] Pacemaker 命令 pcs cluster (管理节点)

注意:

在使用 Pacemaker 命令之前要先安装 Pacemaker 集群 ,并且需要 root 权限

正文:

内容一:启动服务器

1.1 启动某台服务器

# pcs cluster start <server>

1.2 启动所有服务器

# pcs cluster start --all
pacemaker0: Starting Cluster (corosync)...
pacemaker1: Starting Cluster (corosync)...
pacemaker2: Starting Cluster (corosync)...
pacemaker2: Starting Cluster (pacemaker)...
pacemaker1: Starting Cluster (pacemaker)...
pacemaker0: Starting Cluster (pacemaker)...

内容二:关闭服务器

2.1 关闭某台服务器

# pcs cluster stop <server>

2.2 关闭所有的服务器

# pcs cluster stop --all
pacemaker1: Stopping Cluster (pacemaker)...
pacemaker2: Stopping Cluster (pacemaker)...
pacemaker0: Stopping Cluster (pacemaker)...
pacemaker1: Stopping Cluster (corosync)...
pacemaker0: Stopping Cluster (corosync)...
pacemaker2: Stopping Cluster (corosync)...

(注意:当当前主机被关闭了之后,就无法在此服务器上使用 pcs 的其他相关命令,除非先启动本服务器)

内容三:设置随 pcs 服务器启动而自动启动服务器

3.1 让某台服务器随 pcs 服务一同启动

# pcs cluster enable <server>

3.2 让所有服务器都随 pcs 服务一同启动

# pcs cluster enable --all

内容四:设置取消随 pcs 服务器启动自动启动服务器

4.1 取消让某台主机随 pcs 服务器一同启动

# pcs cluster diablle <server>

4.2 取消让所有服务器都随 pcs 服务一同启动

# pcs cluster disable --all

内容五:在集群里新添加 1 台服务器

5.1 新添加 1 台服务器的准备工作

1) 在要被添加的服务器的防火墙上开放 pacemaker 的端口
2) 在要被添加的服务器上安装 pcs fence-agents-rht 以及他的依赖包
3) 在要被添加的服务器上启动 pcsd 服务,并将他设置为开机自启
4) 在要被添加的服务器上将 hacluster 用户的密码修改为 redhat

5.2 在集群里添加新的服务器

(在任意一台集群里的服务器上输入以下命令以加入新的服务器)

# pcs cluster node add <new server>
# pcs cluster auth
Username: hacluster
Password: 
......

(补充:在这里也可以使用 -u<用户> 和 -p<密码> 参数非交互实现命令输入)

内容六:在集群里删除 1 台服务器

6.1 在集群里删除 1 台服务器

# pcs cluster node remove <server>

6.2 在集群里删除 1 台服务器后,最好连 fence 监控也一同删除

# pcs stonith remove fence_deletednode

内容七:让服务器失效

7.1 让某台服务器失效并处于待机状态

# pcs cluster standby <server>

7.2 让所有服务器失效并处于待机状态

# pcs cluster standby --all

内容八:让失效的服务器重新回到活跃状态

8.1 让某台服务器从失效状态回归到活跃状态

# pcs cluster unstandby <server>

8.2 让所有服务器从失效状态回归到活跃状态

# pcs cluster unstandby --all

内容九:生成带配置集群的案例

# pcs cluster setup --start --name testcluster --last_man_standing --wait_for_all pacemkaer10 pacemaker11 pacemaker12

(补充:这里 pacemaker 集群的成员有 pacemaker10、pacemaker11 和 pacemaker12)

内容十:修改集群配置的案例

10.1 关闭所有的主机

# pcs cluster stop --all

10.2 修改 /etc/corosync/corosync.conf 文件中

# vim /etc/corosync/corosync.conf

将部分内容修改如下:

......
quorum {
provider: corosync_votequorum
last_man_standing: 1
wait_for_all: 1
}
......

10.3 同步当前服务器的 /etc/corosync/corosync.conf 文件到其他所有服务器

# pcs cluster sync

10.4 开启所有服务器

# pcs cluster start --all

补充:pcs cluster setup 命令的常用选项

1)  --wait_for_all

当所有集群成员都处于 online 的时候才启动集群投票,主要用于阻止被隔离的主机参与投票

2) --auto_tie_breaker

最低投票从从总数的 50% +1 变为 50% ,如果被分割的主机群两边的数量相等,则拥有最小主机 ID 的那一边才会生效

(补充:–auto_tie_breaker 主要用于集群主机数是双数的主机群)

3) –last_man_standing

有了这个参数之后每隔 10 秒钟,就重新计算一次期望投票数,主要用于人为关闭主机后快速进行重新投票
和 –auto_tie_breaker 选项结合可以让整个集群只有一台主机处于激活状态

(注意:当期望投票数发生变化时, # pcs corosync-quorumtool -m 命令不会自动更新,所以最好使用这个命令 watch -n1 corosync-quorumtool)

4) –two_node

设置整个集群只包含有两台主机,期望投票数是1,他会自动启用 wait_for_all 选项

[实验] Pacemaker 集群的搭建

纪念:站主于 2019 年 8 月完成了此开源实验,并将过程中的所有命令经过整理和注释以后,形成以下教程

步骤一:规划拓扑

1.1 服务器列表

pacemaker0 192.168.11.30
pacemaker1 192.168.11.31
pacemaker2 192.168.11.32

1.2 服务器列表简介

1) pacemaker0、pacemaker1、pacemaker2 都启用 pcs 服务,通过 pcs 实现 pacemaker
2) 3 台服务器都可通过 pcs 服务提供 web、nfs 等服务,但是 web、nfs 等服务在同一时间里只在一台服务器上生效
3) 如果正在提供 web、nfs 服务的服务器 down 掉了,则正在它上面运行的 web nfs 等服务将无缝自动迁移到另外两台服务器

步骤二:服务器系统要求

1) 所有服务器的系统都需要是 CentOS 7 版本
2) 所有服务器都要关闭防火墙
3) 所有服务器都要关闭 SELinux
4) 所有服务器系统都要配置好可用的软件源
5) 需要按照拓扑图给对应的服务器配置好 IP 地址和主机

步骤三:安装并启用 pcs

3.1 在所有服务器上安装 pcs 和 fence-agents-all 服务

(分别在 pacemaker0 pacemaker1 pacemaker2 上执行以下步骤)

# yum install pcs fence-agents-all

3.2 在所有服务器防火墙上开放 pcs

(分别在 pacemaker0 pacemaker1 pacemaker2 上执行以下步骤)

# firewall-cmd --permanent --add-service=high-availability
# firewall-cmd --reload

3.3 在所有服务器上启动 pcs ,并设置开机自起

(分别在 pacemaker0 pacemaker1 pacemaker2 上执行以下步骤)

# systemctl enable pcsd
# systemctl start pcsd

步骤四:让服务器中的各主机通过 pcs 验证

4.1 在所有服务器将 hacluster 用户的密码修改为 redhat

(分别在 pacemaker0 pacemaker1 pacemaker2 上执行以下步骤)

# echo redhat | passwd --stdin hacluster

4.2 在所有服务器上添加主机名到 IP 地址的解析

(分别在 pacemaker0 pacemaker1 pacemaker2 上执行以下步骤)

# echo "192.168.11.30 pacemaker0" >> /etc/hosts
# echo "192.168.11.31 pacemaker1" >> /etc/hosts
# echo "192.168.11.32 pacemaker2" >> /etc/hosts

4.3 让集群里的所有服务器通过 pcs 验证

(只在 pacemaker0 上执行以下步骤)

[root@pacemaker0 ~]# pcs cluster auth pacemaker0 pacemaker1 pacemaker2
Username: hacluster
Password: 
pacemaker2: Authorized
pacemaker1: Authorized
pacemaker0: Authorized

(补充:这一步也可以使用 -u 和 -p 参数实现非交互式配置)

步骤五:通过 pcs 服务生成 1 个名为 mycluster 的 pcs 集群

5.1 生成一个名为 mycluster 包含 pacemaker0 pacemaker1 pacemaker2 的集群组

(只在 pacemaker0 上执行以下步骤)

[root@pacemaker0 ~]# pcs cluster setup --start --name mycluster pacemaker0 pacemaker1 pacemaker2
Destroying cluster on nodes: pacemaker0, pacemaker1, pacemaker2...
pacemaker2: Stopping Cluster (pacemaker)...
pacemaker0: Stopping Cluster (pacemaker)...
pacemaker1: Stopping Cluster (pacemaker)...
pacemaker0: Successfully destroyed cluster
pacemaker1: Successfully destroyed cluster
pacemaker2: Successfully destroyed cluster

Sending 'pacemaker_remote authkey' to 'pacemaker0', 'pacemaker1', 'pacemaker2'
pacemaker1: successful distribution of the file 'pacemaker_remote authkey'
pacemaker0: successful distribution of the file 'pacemaker_remote authkey'
pacemaker2: successful distribution of the file 'pacemaker_remote authkey'
Sending cluster config files to the nodes...
pacemaker0: Succeeded
pacemaker1: Succeeded
pacemaker2: Succeeded

Starting cluster on nodes: pacemaker0, pacemaker1, pacemaker2...
pacemaker0: Starting Cluster (corosync)...
pacemaker1: Starting Cluster (corosync)...
pacemaker2: Starting Cluster (corosync)...
pacemaker1: Starting Cluster (pacemaker)...
pacemaker0: Starting Cluster (pacemaker)...
pacemaker2: Starting Cluster (pacemaker)...

Synchronizing pcsd certificates on nodes pacemaker0, pacemaker1, pacemaker2...
pacemaker2: Success
pacemaker1: Success
pacemaker0: Success
Restarting pcsd on the nodes in order to reload the certificates...
pacemaker2: Success
pacemaker1: Success
pacemaker0: Success


补充:这里可以通过以下方式强制重新生成集群

[root@pacemaker0 ~]# pcs cluster setup --start --name mycluster --force pacemaker0 pacemaker1 pacemaker2

5.2 让所有 pcs 集群都随 pcs 服务自动启动

(只在 pacemaker0 上执行以下步骤)

[root@pacemaker0 ~]# pcs cluster enable --all

5.3 显示 pcs 集群状态

(只在 pacemaker0 上执行以下步骤)

[root@pacemaker0 ~]# pcs cluster status
Cluster Status:
 Stack: corosync
 Current DC: pacemaker0 (version 1.1.19-8.el7-c3c624ea3d) - partition with quorum
 Last updated: Sun Aug  4 09:21:55 2019
 Last change: Sun Aug  4 09:20:38 2019 by hacluster via crmd on pacemaker0
 3 nodes configured
 0 resources configured

PCSD Status:
  pacemaker1: Online
  pacemaker2: Online
  pacemaker0: Online

(注意:如果在 PCSD Status 中出现了 down ,则要确保他们的主机没有在 /etc/hosts 中 127.0.0.1 那一行中出现)