精读笔记(RHCA 英文教材)· RH436 Chapter 6 Automating Cluster and Resource Deployment
精读笔记(RHCA 英文教材)· RH436 Chapter 6 Automating Cluster and Resource Deployment
教材原文:RHCA 官方英文教材(PDF 第 201~236 页)(OCR 整书版已从本站移除,本页为章节精读) 关联知识:DO447(Ansible 基本功)、Ch1/3/4(建集群/fencing/资源);本章=用 Ansible 把前几章手工 pcs 流程写成幂等 playbook。 结构:2 个 Section(各带 Guided Exercise)+ 章末 Lab(lab: auto-review)。内容属“Ansible × 集群”组合技,考点=run_once/changed_when/--wait/幂等判据。
Chapter Goal / Objectives(原文+译)
- GOAL: Deploy a new high availability cluster and cluster resources by using Ansible automation.
- OBJECTIVES: ① 用 Ansible 部署 HA 集群;② 用 Ansible 部署并配置资源/资源组。
6.1 Automating Cluster Deployment
节点准备(Ansible 标准模块)
- 在 inventory 用组(如
[hanodes])装所有节点。 - 准备步骤=照 Ch1 手工流程模块化:
yum(pcs + fence-agents-*)→firewalld(service: high-availability)→user(设 hacluster 密码,变量放 Vault 加密的 passwords.yml,如ha_password/ipmi_login/ipmi_password)→service(enable+start pcsd)。 - 跑含 Vault 文件的项目:
ansible-playbook --ask-vault-pass …(教材 Vault 密码 redhat)。
用 pcs 命令的 Ansible 要点(全章核心)
- 只在一台节点跑 pcs:play 的 hosts 直接写单节点(如
hosts: nodea),或用run_once: yes。 - 加
--wait:pcs resource create等后台异步,命令提前返回 0——加--wait=秒数(如 60/180)等子操作真正完成再返回,任务状态才真实。启集群用pcs cluster start --all --request-timeout=180 --wait=180。 - 幂等性:command 模块不天然幂等,用以下手法:
- 预检+条件:
register上一步输出,用when:判定,例如 auth 任务changed_when: "'Authorized' in auth_cluster['stdout']"、建资源任务when: "' firstwebserver ' not in resources['stdout']"(先pcs resource config收集)。 - 检查文件是否存在再 setup:
stat: path=/etc/corosync/corosync.conf→when: not cluster_config['stat']['exists']。 - 纯查询任务设
changed_when: false。 - STONITH 参数预检:
pcs property show stonith-timeout→ 输出里没有'stonith-timeout: 180s'才pcs property set。
- 预检+条件:
部署 playbook 骨架(02-deploying.yml,教材完整例)
hosts: nodea become: yes
vars_files: [passwords.yml]
vars: { ha_cluster_name: cluster1, ha_nodes: [nodea.…, nodeb.…, nodec.…] }
tasks:
- name: 认证节点
command: "pcs host auth -u hacluster -p {{ ha_password }} {{ ha_nodes | join(' ') }}"
register: auth_cluster
changed_when: "'Authorized' in auth_cluster['stdout']"
- name: 检查集群配置
stat: path=/etc/corosync/corosync.conf → register: cluster_config
- name: 建集群(不存在才建)
command: "pcs cluster setup {{ ha_cluster_name }} {{ ha_nodes | join(' ') }}"
changed_when: "'successfully set up' in create_cluster['stdout']"
when: not cluster_config['stat']['exists']
- name: 开机自启 + 启动
command: pcs cluster enable --all # changed_when: 'Cluster Enabled' in …
command: pcs cluster start --all --request-timeout=180 --wait=180- 小技巧:
{{ ha_nodes | join(' ') }}把 YAML 列表变空格分隔参数;FQDN 列表一次注入。
STONITH 自动化(03-stonith.yml + include_tasks)
- 先幂等设全局参数:
pcs property set stonith-timeout={{ stonith_timeout }}s(预检后条件执行)。 - 再
include_tasks: create_ipmi.yml+loop:(每节点一个 dict:id/node/ip/login/password);任务文件内先pcs stonith config <id>探测(failed_when: false、changed_when: false)→ rc==0 走pcs stonith create … power_timeout={{ stonith_timeout }}(教材 create_ipmi 探测后按 rc 分支:不存在创建/存在 update)。
6.1 GE(lab: auto-ha)
项目含 01-preparing.yml / 02-deploying.yml(需补全,可对照 solutions/)/ 03-stonith.yml / create_ipmi.yml / Vault 密码 redhat。补全 02:认证(host auth -u/-p + join(' '))→ 查 corosync.conf → setup → enable --all → start --all 带超时;补全 03 的 stonith 部分;跑完 pcs status 验证 3 节点 3 fence 资源。
6.2 Automating Resource and Resource Group Deployment
资源自动化原则
- 用 command 模块包
pcs resource create;只跑一台(hosts: nodes[0] 或 run_once)。 - 建前先收集:
pcs resource config→ register resources → 每个资源任务when: "' <name> ' not in resources['stdout']"(带空格防子串误判)。 - 一律
--wait(如 --wait=60)确保集群完成启动再报成功。 - 示例:firstweb 组三个资源(Filesystem NFS /var/www ro、apache、IPaddr2 172.25.250.80/24)逐条按上述 when 守卫创建。
6.2 GE(lab: auto-res)
01-deploy-httpd.yml 现成(装 httpd、开 http、seboolean httpd_use_nfs=true)→ 补全 02-create-resources.yml:收集现有资源→按 when: "' firstwebfs ' not in …" 建 firstwebfs → firstwebserver → firstwebip(都 --group=firstweb --wait=60)→ ansible-playbook --syntax-check → 跑 resourcegroup.yml(import_playbook 串两个 play)→ pcs status + curl http://172.25.250.80 验证。
Lab 概要(lab: auto-review)
在 nodeb/nodec/noded 三台(inventory 组 cluster_nodes)自主写 cluster.yml:第一 play 先跑现成 web_server.yml(httpd+内容);第二 play 在 nodeb 上:装包/防火墙/hacluster 密码(Vault)→ host auth(hosts 组名 cluster_nodes 的 FQDN 列表)→ cluster setup(cluster1)→ enable/start → stonith-timeout=180 → 3 个 fence 资源(fence_nodeb/c/d,BMC 192.168.0.102~104)→ 资源组 web(apache 资源 server + IPaddr2 资源 ip,ip={{ web_ip }} 172.25.250.80)→ curl 172.25.250.80 → 出错跑 reset.yml 重来 → lab grade auto-review。task_examples/ 提供片段模板(cluster_auth/setup/enable_start/stonith_timeout/stonith_resources/web_ip 等)。
命令/语法速查表
| 用途 | Ansible 写法 |
|---|---|
| 装集群包 | yum: name: [pcs, fence-agents-ipmilan] state: present |
| 防火墙 | firewalld: service: high-availability permanent: yes state: enabled immediate: yes |
| hacluster 密码 | user: name: hacluster password: "{{ ha_password }}"(Vault) |
| 起 pcsd | service: name: pcsd state: started enabled: yes |
| 认证(只跑一台) | command: pcs host auth -u hacluster -p {{ ha_password }} {{ ha_nodes|join(' ') }} |
| 幂等建集群 | stat 检查 corosync.conf → when: not cluster_config['stat']['exists'] |
| 启群 | pcs cluster start --all --request-timeout=180 --wait=180 |
| 设 stonith 超时 | pcs property set stonith-timeout={{ t }}s(预检输出判 when) |
| 批量 fence 资源 | include_tasks: create_ipmi.yml + loop: [{id,node,ip,login,password}] |
| 幂等建资源 | 先 pcs resource config register → pcs resource create … --group=… --wait=60 + when: "' 名 ' not in resources['stdout']" |
| 串联 playbook | import_playbook: 01-deploy-httpd.yml(resourcegroup.yml 模式) |
| 语法检查 | ansible-playbook --ask-vault-pass --syntax-check <file> |
词汇表
| 英文 | 中文 | 速记 |
|---|---|---|
| run_once | 只跑一次 | pcs 命令只在一台执行的捷径 |
| --wait / --request-timeout | 等待完成/请求超时 | 异步 pcs 命令的真实返回 |
| changed_when | 变更判定 | 用 stdout 关键字判幂等 |
| register / when | 注册变量/条件 | 预检后条件执行 |
| Vault / --ask-vault-pass | 加密变量文件 | 密码类变量入库 |
| include_tasks + loop | 循环任务文件 | 每节点生成 fence 资源 |
| import_playbook | 导入剧本 | 多 playbook 串联 |
| hosts: nodes[0] | 只打第一台 | 另一种“单节点跑 pcs” |
自测 10 题
- 为什么 pcs 任务要用 run_once 或 hosts 单节点?(pcs 只需从一台管集群)
pcs resource create不加 --wait 有什么坑?(后台执行先返回 0,任务状态失真)- 幂等建资源的判据怎么写?(pcs resource config 收集 → when: "' 名 ' not in stdout")
- 认证任务怎么判“已授权”?(changed_when: "'Authorized' in stdout")
- 建集群如何避免重复执行?(stat corosync.conf → when: not exists)
- stonith-timeout 设值前如何预检?(pcs property show → stdout 含目标值才跳过)
- 批量建 3 个 fence 资源的推荐结构?(include_tasks + loop over {id,node,ip})
- 密码放哪?怎么跑?(Vault 加密 passwords.yml;--ask-vault-pass)
- 列表变量怎么变命令行参数?(
{{ ha_nodes | join(' ') }}) - 资源组自动化的“组内顺序”由什么保证?(创建顺序即组内启动顺序——先 fs 再 apache 再 ip 也可,但教材按依赖放组内即可)
