用 ChaosBlade 模拟 200 ms 网络延迟,验证 orchestrate 超时重试。
解读
面试官想知道三件事:
- 你是否能在 国内主流环境(CentOS 7/8、EulerOS、Aliyun Linux) 里把 ChaosBlade 装起来并注入 200 ms 延迟;
- 你是否熟悉 SaltStack orchestrate 的 timeout / retry 语义,知道在哪一层生效;
- 你是否能设计一条 可观测、可重复、可回滚 的验证路径,把“延迟注入 → 触发超时 → 触发重试 → 断言成功”完整跑通,并给出量化证据。
一句话:不是“能写 SLS”,而是“能在受控故障场景下证明 SaltStack 的韧性机制真的生效”。
知识点
-
ChaosBlade 国内源与 rpm 安装:
阿里云开源镜像站提供 blade-1.7.2-1.x86_64.rpm,可yum localinstall直接装;注入命令
blade create network delay --time 200 --offset 10 --interface eth0 --destination-ip <Master_IP>
200 ms ±10 ms 延迟,仅影响 Minion 到 Master 方向,防止把 SSH 也打挂。 -
SaltStack 超时链:
- salt command CLI 的
-t是 socket 层超时,默认 5 s; - state.orchestrate 的
timeout:是 单个 state 执行 的超时,默认 0(无超时); - retry: 是 state 级别 的构造,语法
只在 声明式 SLS 里生效,CLI 一次调用无效。retry: attempts: 3 interval: 2 until: True
- salt command CLI 的
-
观测手段:
- Master 侧
salt-run jobs.lookup_jid拿返回字典,看duration与success; - Minion 侧
/var/log/salt/minion日志里WARNING | retrying state (1/3)字样; - 用
salt/event总线salt-run state.event pretty=True可实时抓到salt/job/*/ret/*事件,打印retry_num字段。
- Master 侧
-
回滚:
ChaosBlade 每次实验返回 UID,blade destroy UID即可立即解除延迟,保证后续 CI 任务不受影响。
答案
实验拓扑:
Master 192.168.0.10 ←→ Minion 192.168.0.20(被注入延迟)
步骤 1:Minion 安装 ChaosBlade
sudo yum install -y https://mirrors.aliyun.com/chaosblade/1.7.2/blade-1.7.2-1.x86_64.rpm
步骤 2:编写 orchestrate SLS /srv/salt/orch/test_retry.sls
{% set target = 'minion-20' %}
ping_and_sleep:
salt.function:
- name: test.ping
- tgt: {{ target }}
- timeout: 1 # 单 state 1 s 超时,故意小于 RTT
- retry:
attempts: 3
interval: 2
until: True
步骤 3:注入 200 ms 延迟(Minion 上执行)
UID=$(blade create network delay --time 200 --interface eth0 --destination-ip 192.168.0.10)
echo $UID > /tmp/uid
步骤 4:触发编排并采集日志
salt-run state.orch orch.test_retry -l debug 2>&1 | tee /tmp/orch.log
步骤 5:断言
grep -q "retrying state (1/3)" /var/log/salt/minion && \
grep -q "retrying state (2/3)" /var/log/salt/minion && \
salt-run jobs.list_jobs | jq '.[] | select(.Function=="state.orch") | .Result' | grep -q "True" \
&& echo "PASS" || echo "FAIL"
步骤 6:回滚
blade destroy $(cat /tmp/uid)
结果解释:
日志出现两次 retry 记录,第三次 RTT 仍 200 ms 但 state 在 1 s 内完成(test.ping 本身耗时 <1 s),最终返回 Success,证明 retry 机制在 orchestrate 层被正确触发并抵消了网络延迟带来的偶发超时。
拓展思考
- 如果延迟加到 2 s,单 state 超时 1 s,retry 3 次仍失败,SaltStack 会把失败结果写到 event 总线;可接 阿里 SLS / 腾讯 CLS 日志消费,做自动告警。
- 生产环境建议把 ChaosBlade 实验封装成 Salt 自定义 runner,通过
salt-run chaos.inject delay=200ms target='*' duration=60s一键下发,实验结束自动回滚并输出 JSON 报告,方便对接 夜莺 / Prometheus Alertmanager。 - 对于 无 Master 的 Salt-SSH 场景,网络延迟注入点应选在控制机到目标机的链路,此时 retry 依赖 salt-ssh 的
--ssh-timeout与 state 的 retry 并列生效,需要双重验证。