精读笔记(RHCA 英文教材)· RH436 Chapter 4 Creating and Configuring Resources
精读笔记(RHCA 英文教材)· RH436 Chapter 4 Creating and Configuring Resources
教材原文:RHCA 官方英文教材(PDF 第 121~160 页)(OCR 整书版已从本站移除,本页为章节精读) 关联知识:Ch1(资源/资源组概念)、Ch3(fencing);本章=单资源→资源组→手工迁移/待命→三类约束(order/location/colocation),是 HA 服务落地核心章。 结构:4 个 Section(各带 Guided Exercise)+ 章末 Lab(lab: resources-review)。
Chapter Goal / Objectives(原文+译)
- GOAL: Create basic resources and resource groups to provide highly available services(建基础资源与资源组提供 HA 服务).
- OBJECTIVES: ① 创建并配置单个集群资源;② 把多个关联资源组成资源组;③ 手工控制与迁移资源组;④ 配置约束控制资源/资源组可跑在哪些节点。
4.1 Creating and Configuring a Cluster Resource
资源与资源代理
- 资源=向消费者提供服务的组件(IP、文件系统、httpd…),彼此独立监控,由**资源代理(resource agents)**控制。三类:
- LSB:兼容 LSB 的 init 脚本(/etc/init.d)。
- OCF:扩展 LSB、能收附加参数的脚本,位于
/usr/lib/ocf/resource.d/<provider>;Red Hat 自带多数 provider 为 heartbeat(如ocf:heartbeat:IPaddr2)。 - systemd:RHEL8 标准 systemd unit(如 apache 资源可直接管 httpd 服务)。
- 常用代理:IPaddr2(浮动 IP)、Filesystem(挂载点)、apache、nfsserver/exportfs、mysql 等。
- 查代理:
pcs resource list(全部可用)、pcs resource describe Filesystem(参数说明;device/directory/fstype 必填,options 可选)。
创建与查看单资源
pcs resource create wwwfs Filesystem \
device=storage.san01.example.com:/srv/www directory=/mnt \
fstype=nfs options=ro # NFS 只读挂载
pcs status resources # 看资源与运行节点
pcs resource config wwwfs # 属性+monitor/start/stop 操作设置- 不带
--group即独立资源;集群会挑一个节点启动,故障后(fencing 完成)迁到其它节点。
4.1 GE(lab: resources-create)
建单个 Filesystem 资源 wwwfs(NFS /srv/www 只读挂到 /mnt)→ pcs status 看 Started 节点 → ls /mnt(有 html/test.txt)→ 跑 firewall-block.sh 断网 → 观察集群 fence 原节点并把 wwwfs 迁走。
4.2 Creating and Configuring Resource Groups
为什么用资源组
单个资源东一个西一个不构成可用服务(IP 在 A、fs 在 B、httpd 在 C);资源组强制组内资源同节点运行、按加入顺序启动、逆序停止,故障时整组迁移。组内资源自动获得隐式 colocation+order 约束。
高可用 Apache 三件套(教材标准例)
pcs resource create firstwebip IPaddr2 ip=172.25.250.80 cidr_netmask=24 --group=firstweb
pcs resource create firstwebfs Filesystem \
device=storage.san01.example.com:/srv/www directory=/var/www \
fstype=nfs options=ro --group=firstweb
pcs resource create firstwebserver apache --group=firstweb- 前置(Ansible playbook 做):装 httpd、firewall 开 http、SELinux 布尔
httpd_use_nfs=true(httpd 才能读 NFS 上的 /var/www 内容)。 - 验证:
curl http://172.25.250.80/;故障演练:跑防火墙阻断脚本 → 另一节点watch pcs status看 fence+整组迁移。
4.2 GE(lab: resources-group)
workstation 跑 playbook 准备 httpd → nodea 上按上面三条命令建 firstweb 组 → curl 验证 → 阻断网络 → 观察整组迁移与恢复。
4.3 Managing Resource Groups(手工控制)
| 命令 | 作用 | | pcs resource disable <res\|组> | 停掉且不许集群再启动 | | pcs resource enable <res\|组> | 恢复可启动 | | pcs resource move <res> [node] | 迁走(目标 node 可省);生成临时 location 约束 cli-* | | pcs resource ban <res> <node> | 禁止迁到/跑在指定节点 | | pcs resource clear <res> | 删除该资源所有手工约束(如 cli-prefer/cli-ban) | | pcs constraint list | 列出 location/order/colocation/ticket 四类约束 |
- move/ban 都产生临时约束;clear 后资源不一定自动回原节点(约束没了就不影响当前运行点)。
4.3 GE(lab: resources-manage)
pcs resource disable firstweb → status 显示 Stopped (disabled)、constraint 为空 → enable → 定位运行节点 → pcs resource move firstweb(不带目标)→ 整组到另一节点 → pcs constraint list 见 Disabled on: Node: nodea (score:-INFINITY) (role:Started) → pcs resource clear firstweb 移除 cli-ban 约束,组仍在现节点运行。
4.4 Configuring Constraints
三类约束总览
- Order(顺序):控制启动/停止先后。
- Location(位置):控制可运行节点(优先级=score)。
- Colocation(同置):相对其它资源的放置位置。
- 资源组=最省事的约束封装(隐式同置+顺序);显式约束用于跨组场景(如两组必须在不同/同一节点)。
Order 约束
pcs constraint order A then B # 显示 Adding A B (kind: Mandatory) first-action=start then-action=start效果(书例):都停时 A 启动后 B 才允许启动;都在跑时 A 被 disable → 集群先停 B 再停 A;A 重启 → B 也重启;A 没跑且起不来(配置坏)→ B 也停。
Location 约束与分数
- 无约束时资源尽量均匀分布;location 给出“偏好节点”,分数越高越优先(有分差会立即迁移)。
pcs constraint location <res> prefers <node>=<score>(例 nodeb=499);avoids <node>(默认 -INFINITY,禁跑该节点)→ 触发迁走。- INFINITY 语义:
prefers且最高分=INFINITY → 只要该节点在线就必须在那跑(否则跑任意其它节点)。 - stickiness(黏性)默认 0;
pcs resource defaults update resource-stickiness=500改默认;组的有效黏性=每资源值×组内资源数(5 个×1000=5000,防迁移倾向很强)。决策:位置分 vs stickiness 谁高谁赢(200>0 会迁;499<500 不迁)。 - 预演分配:
crm_simulate -sL(看 allocation scores,含 native_allocate/clone_allocate/group_allocate)。
Colocation 约束
pcs constraint colocation add B with A # B 必须与 A 同节点;A 先放、B 后放;并行启动
pcs constraint colocation add B with A -INFINITY # 永不与 A 同节点- 规则:先决定 A 的位置再决定 B;A、B 同放的位置不存在 → B 保持停止;A 没地方 → B 也没地方。
4.4 GE(lab: resources-constraints)
先把 nfs 组 move 到 nodec、firstweb move 到 nodeb(随后 clear 掉约束)→ pcs constraint location firstweb prefers nodeb=200:因默认 stickiness=0,组迁到更高分节点(nodea 也可能被选,示例结果按分数走)→ pcs resource defaults update resource-stickiness=500 + prefers nodeb=499:499<500 不迁 → pcs constraint location nfs avoids nodec(默认 INFINITY,实际 -INFINITY):nfs 组从 nodec 迁走。
Lab 概要(lab: resources-review)
playbook 装 httpd 等 → 建 firstweb 组(IPaddr2 172.25.250.80/24 + Filesystem NFS /var/www ro + apache)→ pcs constraint colocation add firstweb with nfs 强制两组同节点 → pcs constraint list 核对 → lab grade resources-review。
命令速查表
| 用途 | 命令 |
|---|---|
| 资源代理清单/参数 | pcs resource list;pcs resource describe <agent> |
| 建资源(可入组) | pcs resource create <name> <agent> <k=v>… [--group=<grp>] |
| 看资源/配置 | pcs status resources;pcs resource config <res> |
| 启停资源 | pcs resource enable|disable <res> |
| 迁移/禁跑/清约束 | pcs resource move <res> [node];pcs resource ban <res> <node>;pcs resource clear <res> |
| 列表约束 | pcs constraint list |
| 顺序约束 | pcs constraint order A then B |
| 位置约束 | pcs constraint location <res> prefers <node>=<score>;avoids <node> |
| 同置约束 | pcs constraint colocation add B with A [-INFINITY] |
| 默认黏性 | pcs resource defaults update resource-stickiness=<n> |
| 分配预演 | crm_simulate -sL |
| 实验/评分 | lab start resources-create/group/manage/constraints;lab grade resources-review |
词汇表
| 英文 | 中文 | 速记 |
|---|---|---|
| LSB / OCF / systemd agent | 三类资源代理 | init 脚本 / 扩展 OCF(heartbeat provider) / unit |
| IPaddr2 | 浮动 IP 代理 | ip= + cidr_netmask= |
| Filesystem / apache | 文件系统/httpd 代理 | NFS 例:fstype=nfs options=ro |
| resource group | 资源组 | 同节点+加入序启动+逆序停止 |
| disable / enable | 停用/启用 | disable 停且不许再起 |
| move / ban / clear | 迁移/禁跑/清约束 | move/ban 造临时 cli-* 约束 |
| location / order / colocation | 位置/顺序/同置约束 | 控制“在哪/先后/与谁一起” |
| score / INFINITY | 分数/无穷 | 高分优先;avoids=-INFINITY |
| stickiness | 资源黏性 | 默认 0;组=值×组内资源数 |
| cli-prefer / cli-ban | 命令行临时约束 | move/ban 产物;clear 删除 |
| crm_simulate | 调度预演 | -sL 看 allocation scores |
自测 10 题
- 三类资源代理?(LSB/OCF/systemd;OCF 多在 /usr/lib/ocf/resource.d/heartbeat)
- 建 NFS 只读挂载资源最少给哪几个参数?(device/directory/fstype=nfs/options=ro)
- 资源组隐式给成员什么约束?(同节点 colocation + 按加入顺序 order)
- 组内资源启动/停止顺序?(加入序启动、逆序停止)
- disable 与 ban 区别?(disable 停且禁启;ban 禁跑指定节点)
- move 不带节点参数的效果?(从当前节点迁走,生成 -INFINITY location 约束)
- order 约束中 A 起不来时 B 会怎样?(停)
- colocation add B with A 的放置逻辑?(先放 A 再放 B;A 无位置 B 也无)
- 默认 stickiness 是多少?组的有效黏性怎么算?(0;每资源值×组内成员数)
- avoids 默认分数?(-INFINITY=禁跑该节点)
