2026-11-11 12:57:24 +08:00
<!DOCTYPE html>
< html lang = "zh-CN" data-theme = "light" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< title > 第 16 章 故障诊断方法论:六类故障,一张诊断树 · Kubernetes 专题知识库< / title >
< meta name = "description" content = "凌晨九个 Pod 三个 Pending, 三个人分头查调度器、镜像与网络, 没人先看 Event。本章把 Pending、CrashLoopBackOff、ImagePullBackOff、OOMKilled、Evicted、NodeNotReady 六类故障各注入一次,用一张诊断树分流,再为每类写一份四段式 runbook。" >
< meta name = "keywords" content = "故障诊断, Pending, CrashLoopBackOff, ImagePullBackOff, OOMKilled, Evicted, NodeNotReady, runbook, kubectl describe" >
< link rel = "stylesheet" href = "assets/doc.css" >
< / head >
< body >
< header class = "topbar" >
< div class = "tb-in" >
< a class = "brand" href = "index.html" > < span class = "logo" > K8S< / span > < div > Kubernetes 专题< small > 从第一个 Pod 到生产平台< / small > < / div > < / a >
< div class = "crumbs" >
< a href = "index.html" > 总目录< / a > < span class = "sep" > /< / span > < b > 第 16 章< / b >
< / div >
< div class = "spacer" > < / div >
< button class = "btn" id = "tocToggle" > ☰ 导航< / button >
< button class = "btn" id = "themeBtn" title = "切换到深色主题" > 深色< / button >
< / div >
< / header >
< div class = "layout" id = "layout" >
< aside class = "sidebar" >
< h2 > 章节目录< / h2 >
2026-11-11 21:57:56 +08:00
< nav id = "chapters" > < div class = "toc-stage" > < b > S0< / b > < span class = "h" > 起步:环境与心智< / span > < / div > < a class = "toc-a" href = "00-why.html" > < i class = "cn" > 0< / i > 从一次线上事故理解 K8s< / a > < a class = "toc-a" href = "01-env.html" > < i class = "cn" > 1< / i > 装第一个集群: Windows 11 + WSL2 与 Linux 双轨< / a > < a class = "toc-a" href = "02-kubectl.html" > < i class = "cn" > 2< / i > kubectl 与集群解剖:把黑盒变成可观测对象< / a > < div class = "toc-stage" > < b > S1< / b > < span class = "h" > 跑起来:单服务最小闭环< / span > < / div > < a class = "toc-a" href = "03-pod.html" > < i class = "cn" > 3< / i > Pod: 最小调度单元< / a > < a class = "toc-a" href = "04-deployment.html" > < i class = "cn" > 4< / i > Deployment: 副本与滚动更新< / a > < a class = "toc-a" href = "05-service.html" > < i class = "cn" > 5< / i > Service 与集群 DNS< / a > < a class = "toc-a" href = "06-config.html" > < i class = "cn" > 6< / i > 配置与密钥: ConfigMap 与 Secret< / a > < a class = "toc-a" href = "07-probe.html" > < i class = "cn" > 7< / i > 探针与资源:让它活得像生产< / a > < div class = "toc-stage" > < b > S2< / b > < span class = "h" > 撑得住:状态、入口与多服务< / span > < / div > < a class = "toc-a" href = "08-storage.html" > < i class = "cn" > 8< / i > 存储:从 emptyDir 到 StatefulSet< / a > < a class = "toc-a" href = "09-ingress.html" > < i class = "cn" > 9< / i > 入口与网关: Ingress 与 Gateway API< / a > < a class = "toc-a" href = "10-job.html" > < i class = "cn" > 10< / i > Job 与 CronJob: 迁移与定时任务< / a > < a class = "toc-a" href = "11-multisvc.html" > < i class = "cn" > 11< / i > 多服务与网络策略< / a > < div class = "toc-stage" > < b > S3< / b > < span class = "h" > 看得见:可观测、发布与排障< / span > < / div > < a class = "toc-a" href = "12-logging.html" > < i class = "cn" > 12< / i > 日志:从 kubectl logs 到可检索< / a > < a class = "toc-a" href = "13-metrics.html" > < i class = "cn" > 13< / i > 指标、扩缩与告警< / a > < a class = "toc-a" href = "14-release.html" > < i class = "cn" > 14< / i > 发布策略:滚动、蓝绿、金丝雀< / a > < a class = "toc-a" href = "15-supply.html" > < i class = "cn" > 15< / i > 密钥与镜像供应链< / a > < span class = "toc-a active" aria-current = "page" > < i class = "cn" > 16< / i > 故障诊断方法论:六类故障,一张诊断树< / span > < div class = "subnav current" > < a class = "toc-a lv3" href = "#sec-16-1" > 16.1 本章在哪:诊断是前面所有章节的回收站< / a > < a class = "toc-a lv3" href = "#sec-16-2" > 16.2 学习目标< / a > < a class = "toc-a lv3" href = "#sec-16-3" > 16.3 场景:三个人查了三层,没人看 Event< / a > < a class = "toc-a lv3" href = "#sec-16-4" > 16.4 心智模型:分诊台,不是侦探< / a > < a class = "toc-a lv3" href = "#sec-16-5" > 16.5 概念与字段速查< / a > < a class = "toc-a lv3" href = "#sec-16-6" > 16.6 动手:六类各注入一次,各写一份 runbook< / a > < a class = "toc-a lv3" href = "#sec-16-7" > 16.7 验收清单< / a > < a class = "toc-a lv3" href = "#sec-16-8" > 16.8 故障实验室:三类展开,三类列表< / a > < a class = "toc-a lv3" href = "#sec-16-9" > 16.9 生产对照< / a > < a class = "toc-a lv3" href = "#sec-16-10" > 16.10 清理与残留检查< / a > < a class = "toc-a lv3" href = "#sec-16-11" > 16.11 小结与自测< / a > < a class = "toc-a lv3" href = "#sec-16-12" > 16.12 衔接< / a > < a class = "toc-a lv3" href = "#sec-16-13" > 16.13 参考与验证版本< / a > < / div > < div class = "toc-stage" > < b > S4< / b > < span class = "h" > 上生产:集群、调度、安全、交付< / span > < / div > < a class = "toc-a" href = "17-kubeadm.html" > < i class = "cn" > 17< / i > 生产集群搭建: kubeadm HA< / a > < a class = "toc-a" href = "18-schedule.html" > < i class = "cn" > 18< / i > 节点、调度与容量< / a > < a class = "toc-a" href = "19-rbac.html" > < i class = "cn" > 19< / i > 安全基线: RBAC 与准入< / a > < a class = "toc-a" href = "20-gitops.html" > < i class = "cn" > 20< / i > GitOps: Helm/Kustomize + Argo CD< / a > < a class = "toc-a" href = "21-ci.html" > < i class = "cn" > 21< / i > CI/CD 流水线< / a > < a class = "toc-a" href = "22-backup.html" > < i class = "cn" > 22< / i > 备份、恢复与升级< / a > < div class = "toc-stage" > < b > S5< / b > < span class = "h" > 守得住:值班、成本与评审< / span > < / div > < span class = "toc-a todo" title = "待写" > < i class = "cn" > 23< / i > 值班与事故响应< / span > < span class = "toc-a todo" title = "待写" > < i class = "cn" > 24< / i > 成本与容量< / span > < span class = "toc-a todo" title = "待写" > < i class = "cn" > 25< / i > 生产就绪评审与下
2026-11-11 12:57:24 +08:00
< div class = "side-foot" >
< a class = "btn" href = "index.html" > ← 总目录< / a >
< / div >
< / aside >
< div class = "mask" id = "mask" > < / div >
< main class = "main-col" >
< article >
< p class = "chapnav" >
< a href = "index.html" > Kubernetes 专题< / a >
< span > ·< / span >
< span > S3 看得见:可观测、发布与排障< / span >
< span > ·< / span >
< span > 第 < b class = "pos" > 16< / b > / 26 章< / span >
< / p >
< h2 id = "ch16" > < span class = "n" > 16< / span > 故障诊断方法论:六类故障,一张诊断树< / h2 >
< div class = "lead" > < b > 30 秒速览:< / b > 凌晨的 < code > Pending< / code > 不是一种病,是六种病共用的症状。< b > 顺序< / b > 决定你花五分钟还是两小时:先读 STATUS 分类,再看第一个信号在哪一层,最后才进容器。本章产出六个可回滚的注入、一张诊断树、六份四段式 runbook。< / div >
< h3 id = "sec-16-1" > 16.1 本章在哪:诊断是前面所有章节的回收站< / h3 >
< p > 第 7 章用一次 requests 写太大的实验见过 Pending( < a href = "07-probe.html#sec-7-8" > 7.8< / a > ),第 1 章用 < code > docker pause< / code > 见过 NotReady( < a href = "01-env.html#sec-1-8" > 1.8< / a > )。本章不重讲那两个现场,只把它们的判断顺序固化成树。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 位置< / th > < th > 内容< / th > < / tr > < / thead >
< tbody >
< tr > < td style = "white-space:nowrap" > 主线之前< / td > < td > 第 4 章的滚动更新、第 7 章的探针与 requests、第 8 章的 PVC、第 15 章的镜像 tag。前 15 章造的每个对象都是本章的病人。< / td > < / tr >
< tr > < td style = "white-space:nowrap" > 主线之后< / td > < td > 第 17 章起在三控制面集群上重建同一套服务。树不变,「看节点」的成本变高。< / td > < / tr >
< tr > < td style = "white-space:nowrap" > 本章产物< / td > < td > < code > runbooks/< / code > 下六个 < code > .md< / code > ,每份四段;一条从 STATUS 到具体工具的命令路径。< / td > < / tr >
< tr > < td style = "white-space:nowrap" > 适用环境< / td > < td > Windows 11 + WSL2 与 Ubuntu 24.04 命令相同,唯一差异在节点容器的暂停方式,见 16.8。< / td > < / tr >
< / tbody >
< / table >
< / div >
< h3 id = "sec-16-2" > 16.2 学习目标< / h3 >
< div class = "box note" > < span class = "bt" > 学完你能< / span >
< ul >
< li > 读一列 STATUS 选出分支。判定标准:不看日志就能说出下一步看 Event 还是看容器。< / li >
< li > 用 Events 与 < code > --sort-by=.lastTimestamp< / code > 排时间线。判定标准:说得出第一次失败在几点、哪个对象先动。< / li >
< li > 区分「镜像拉不到」与「进程起不来」。判定标准:前者没有容器 ID, 后者有。< / li >
< li > 为六类故障各写一份四段 runbook。判定标准: 同事照它执行, 十分钟内给出下一步动作。< / li >
< li > 回滚每次注入。判定标准:< code > make verify-ch16< / code > 每行都是 < code > [ok]< / code > 。< / li >
< / ul >
< / div >
< h3 id = "sec-16-3" > 16.3 场景:三个人查了三层,没人看 Event< / h3 >
< p > 03:07, 告警说下单成功率跌破 90%。你打开终端,九个 Pod 里三个 < code > Pending< / code > 。三个人同时上手:一个翻调度器日志,一个查镜像 tag, 一个在节点上抓包。四十分钟后才发现, < code > aurora-worker2< / code > 上有一个昨晚加的 < code > NoSchedule< / code > 污点。三层都没查错,只是没人先读 < code > describe< / code > 最后那二十行 Events。< / p >
< div class = "box case" > < span class = "bt" > 现象< / span >
< p > 三个 Pod 的 STATUS 是 < code > Pending< / code > , RESTARTS 为 0, Node 列显示 < code > < none> < / code > ;而 < code > get nodes< / code > 三个节点全 < code > Ready< / code > 。节点、调度器、镜像都健康,故障只存在「这批 Pod 与这台节点」的关系里。< / p >
< / div >
< h3 id = "sec-16-4" > 16.4 心智模型:分诊台,不是侦探< / h3 >
< p > 把诊断想成急诊分诊: 护士先量体温血压( STATUS) , 据此决定送内科还是外科( 分支) , 不会一进门就推去做 CT( 进容器、抓包、翻内核日志) 。见图 1, 树只有三层判断, 每层只回答一个问题。< / p >
< figure class = "figure" >
< svg viewBox = "0 0 760 380" role = "img" aria-label = "六类 Pod 故障的诊断树:从 STATUS 分出六条分支,每条标注第一个该看的信号" >
< defs >
< marker id = "a16" markerWidth = "9" markerHeight = "9" refX = "8" refY = "4.5" orient = "auto" > < path d = "M0,0 L9,4.5 L0,9 z" style = "fill:var(--muted)" > < / path > < / marker >
< / defs >
< rect x = "15" y = "140" width = "160" height = "86" rx = "10" style = "fill:var(--brand-soft);stroke:var(--brand)" > < / rect >
< text x = "95" y = "172" text-anchor = "middle" style = "fill:var(--ink);font-size:14px;font-weight:700" > 先读 STATUS< / text >
< text x = "95" y = "194" text-anchor = "middle" style = "fill:var(--muted);font-size:12px" > 分类 → 信号层< / text >
< text x = "95" y = "212" text-anchor = "middle" style = "fill:var(--muted);font-size:12px" > → 谁能改< / text >
< path d = "M175,183 H215" style = "stroke:var(--muted);fill:none" > < / path >
< path d = "M215,34 V324" style = "stroke:var(--muted);fill:none" > < / path >
< path d = "M215,34 H280" marker-end = "url(#a16)" style = "stroke:var(--muted);fill:none" > < / path >
< path d = "M215,92 H280" marker-end = "url(#a16)" style = "stroke:var(--muted);fill:none" > < / path >
< path d = "M215,150 H280" marker-end = "url(#a16)" style = "stroke:var(--muted);fill:none" > < / path >
< path d = "M215,208 H280" marker-end = "url(#a16)" style = "stroke:var(--muted);fill:none" > < / path >
< path d = "M215,266 H280" marker-end = "url(#a16)" style = "stroke:var(--muted);fill:none" > < / path >
< path d = "M215,324 H280" marker-end = "url(#a16)" style = "stroke:var(--muted);fill:none" > < / path >
< rect x = "280" y = "10" width = "460" height = "48" rx = "8" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "296" y = "30" style = "fill:var(--ink);font-size:13.5px;font-weight:700" > Pending 未调度< / text >
< text x = "296" y = "48" style = "fill:var(--muted);font-size:12px" > 第一信号: Events 里的 FailedScheduling< / text >
< rect x = "280" y = "68" width = "460" height = "48" rx = "8" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "296" y = "88" style = "fill:var(--ink);font-size:13.5px;font-weight:700" > CrashLoopBackOff 起不来< / text >
< text x = "296" y = "106" style = "fill:var(--muted);font-size:12px" > 第一信号: logs --previous 的最后一行< / text >
< rect x = "280" y = "126" width = "460" height = "48" rx = "8" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "296" y = "146" style = "fill:var(--ink);font-size:13.5px;font-weight:700" > ImagePullBackOff 拉不到< / text >
< text x = "296" y = "164" style = "fill:var(--muted);font-size:12px" > 第一信号: Events 里的 Failed to pull image< / text >
< rect x = "280" y = "184" width = "460" height = "48" rx = "8" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "296" y = "204" style = "fill:var(--ink);font-size:13.5px;font-weight:700" > OOMKilled 被杀< / text >
< text x = "296" y = "222" style = "fill:var(--muted);font-size:12px" > 第一信号: Last State 的 Exit Code 137< / text >
< rect x = "280" y = "242" width = "460" height = "48" rx = "8" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "296" y = "262" style = "fill:var(--ink);font-size:13.5px;font-weight:700" > Evicted 被驱逐< / text >
< text x = "296" y = "280" style = "fill:var(--muted);font-size:12px" > 第一信号: Reason 与驱逐文案< / text >
< rect x = "280" y = "300" width = "460" height = "48" rx = "8" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "296" y = "320" style = "fill:var(--ink);font-size:13.5px;font-weight:700" > NodeNotReady 节点掉线< / text >
< text x = "296" y = "338" style = "fill:var(--muted);font-size:12px" > 第一信号: get nodes 与节点的 Conditions< / text >
< text x = "15" y = "372" style = "fill:var(--muted);font-size:11.5px" > 六类都不是:回到第 7 章的 Service / Endpoints 与探针< / text >
< / svg >
< div class = "figcap" > < b > 图 1 ·< / b > 六类 Pod 故障的总诊断树,每片叶子只标注第一个该看的信号。< / div >
< / figure >
< p > 树是窄的:同一时刻你只该在一条分支上。新手常横向铺开——< code > describe< / code > 打完看 < code > logs< / code > ,再 < code > exec< / code > 进去敲 < code > ps< / code > ,证据越多结论越晚。分诊的纪律是「这一层能排除什么」,排除完就走。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 常见误解< / th > < th > 事实< / th > < / tr > < / thead >
< tbody >
< tr > < td style = "white-space:nowrap" > Pending 就是调度器坏了< / td > < td > 多数 Pending 是 requests 与可分配量对不上,调度器只是如实拒绝并写下原因。< / td > < / tr >
< tr > < td style = "white-space:nowrap" > CrashLoopBackOff 要重启集群< / td > < td > 它只是退避重启的外在表现,根因在进程或配置。重启集群只让现场消失。< / td > < / tr >
< tr > < td style = "white-space:nowrap" > 节点 NotReady 要立刻删 Pod< / td > < td > 300 秒容忍期内删掉更慢。先恢复节点,多数 Pod 会自己回到 Running。< / td > < / tr >
< / tbody >
< / table >
< / div >
< p class = "take" > < b > 一句话:< / b > 诊断不是猜,是按层排除;每下一层,成本翻十倍。< / p >
< h3 id = "sec-16-5" > 16.5 概念与字段速查< / h3 >
< p > 下面这些默认值决定了「症状多久出现」。记住哪些可调、调它要改哪一层,比记住数值有用。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 对象 / 字段< / th > < th > 作用< / th > < th > 默认值< / th > < th > 生产建议< / th > < / tr > < / thead >
< tbody >
< tr > < td > < code > Pod.status.phase< / code > < / td > < td > STATUS 列的来源,只有五个取值< / td > < td > 无(只读状态)< / td > < td > 别当告警指标,用「期望副本数 vs Ready 数」< / td > < / tr >
< tr > < td > < code > restartPolicy< / code > < / td > < td > 容器退出后是否重启< / td > < td > < code > Always< / code > < / td > < td > Job 用 < code > Never< / code > ,否则失败被重启掩盖< / td > < / tr >
< tr > < td > CrashLoop 退避< / td > < td > 连续失败后拉长重启间隔< / td > < td > 10 秒起,逐次翻倍< / td > < td > 退避期间别急着重启,先读 < code > --previous< / code > < / td > < / tr >
< tr > < td > < code > terminationGracePeriodSeconds< / code > < / td > < td > 删除时留给进程收尾的时间< / td > < td > 30 秒< / td > < td > 有长连接的网关调到 60 秒以上< / td > < / tr >
< tr > < td > 探针 < code > periodSeconds< / code > < / td > < td > 探测间隔,决定故障多快被发现< / td > < td > 10 秒< / td > < td > 与 < code > failureThreshold< / code > 一起算容忍时间< / td > < / tr >
< tr > < td > < code > node.kubernetes.io/not-ready< / code > 容忍< / td > < td > 节点失联后 Pod 还能待多久< / td > < td > < code > NoExecute< / code > 300 秒< / td > < td > 无状态服务可调小,数据库别动< / td > < / tr >
< tr > < td > < code > node-monitor-grace-period< / code > < / td > < td > 多久没心跳判定 NotReady< / td > < td > 本集群 50 秒< / td > < td > 调大只会让故障发现更慢< / td > < / tr >
< tr > < td > < code > emptyDir.sizeLimit< / code > < / td > < td > 临时卷能写多大< / td > < td > 不限制(用节点盘)< / td > < td > 一定要设,它保护的是节点< / td > < / tr >
< tr > < td > < code > ephemeral-storage< / code > limit< / td > < td > Pod 级临时存储上限< / td > < td > 不限制< / td > < td > 与 < code > sizeLimit< / code > 一起设,驱逐判据看它< / td > < / tr >
< tr > < td > Event 保留时长< / td > < td > 事件在 etcd 里留多久< / td > < td > 1 小时< / td > < td > 复盘靠日志与监控,别指望 Event< / td > < / tr >
< / tbody >
< / table >
< / div >
< details > < summary > 可跳过 · 为什么 STATUS 只有五种,却能看到十几种词< / summary >
< p > < code > phase< / code > 只有 Pending、Running、Succeeded、Failed、Unknown 五个值。你看到的 < code > CrashLoopBackOff< / code > 、< code > ImagePullBackOff< / code > 、< code > Evicted< / code > 是 kubectl 拼出的展示态:它把容器状态里的 waiting reason 与 terminated reason 提到 STATUS 列。判断时别只读字符串,要读背后是 < code > waiting< / code > 还是 < code > terminated< / code > 。< / p >
< / details >
< h3 id = "sec-16-6" > 16.6 动手:六类各注入一次,各写一份 runbook< / h3 >
< p > 六个注入都不碰业务对象。先认清信号分层(见图 2) : 同一故障在不同层留下不同痕迹, 越往下越接近真相, 也越贵。< / p >
< figure class = "figure" >
< svg viewBox = "0 0 720 300" role = "img" aria-label = "三层信号: API 对象层、kubelet 层、运行时与内核层,以及各自对应的工具" >
< defs >
< marker id = "a16b" markerWidth = "9" markerHeight = "9" refX = "8" refY = "4.5" orient = "auto" > < path d = "M0,0 L9,4.5 L0,9 z" style = "fill:var(--muted)" > < / path > < / marker >
< / defs >
< rect x = "30" y = "16" width = "660" height = "66" rx = "9" style = "fill:var(--brand-soft);stroke:var(--brand)" > < / rect >
< text x = "48" y = "43" style = "fill:var(--ink);font-size:14px;font-weight:700" > API 对象层( etcd 里的记录)< / text >
< text x = "48" y = "66" style = "fill:var(--muted);font-size:12px" > kubectl get / describe / events 看「控制面认为发生了什么」< / text >
< rect x = "30" y = "106" width = "660" height = "66" rx = "9" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "48" y = "133" style = "fill:var(--ink);font-size:14px;font-weight:700" > kubelet 层(节点上的代理)< / text >
< text x = "48" y = "156" style = "fill:var(--muted);font-size:12px" > logs / debug / cordon 看「这台节点做到了哪一步」< / text >
< rect x = "30" y = "196" width = "660" height = "66" rx = "9" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "48" y = "223" style = "fill:var(--ink);font-size:14px;font-weight:700" > 运行时与内核层< / text >
< text x = "48" y = "246" style = "fill:var(--muted);font-size:12px" > crictl / nsenter 看「进程、网络、磁盘到底怎么了」< / text >
< path d = "M14,82 V196" marker-end = "url(#a16b)" style = "stroke:var(--muted);fill:none" > < / path >
< text x = "352" y = "290" text-anchor = "middle" style = "fill:var(--muted);font-size:11.5px" > 往下走一层,权限更高、风险更大,也更接近真相< / text >
< / svg >
< div class = "figcap" > < b > 图 2 ·< / b > 三层信号与各自的主力工具,顺序永远从上往下。< / div >
< / figure >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 工具< / th > < th > 看到哪一层< / th > < th > 什么时候用它< / th > < th > 它看不到什么< / th > < / tr > < / thead >
< tbody >
< tr > < td > < code > get< / code > / < code > describe< / code > / < code > events< / code > < / td > < td > API 对象与调度、拉镜像的记录< / td > < td > 永远的第一站< / td > < td > 容器内的进程与文件< / td > < / tr >
< tr > < td > < code > logs< / code > (含 < code > --previous< / code > ) < / td > < td > kubelet 收集的 stdout/stderr< / td > < td > 容器起来过、退出了< / td > < td > 进程在写日志前就崩时的现场< / td > < / tr >
< tr > < td > < code > kubectl debug< / code > < / td > < td > Pod 的命名空间与进程视图< / td > < td > 镜像里没有 shell 或工具< / td > < td > kubelet 与节点层< / td > < / tr >
< tr > < td > < code > crictl< / code > < / td > < td > 节点上的容器运行时< / td > < td > 容器反复创建失败、< code > logs< / code > 拿不到< / td > < td > Pod 语义与服务发现< / td > < / tr >
< tr > < td > < code > nsenter< / code > < / td > < td > 节点内核视角< / td > < td > 网络、挂载、磁盘的疑难< / td > < td > 任何上层解释,误操作代价最大< / td > < / tr >
< / tbody >
< / table >
< / div >
< p > 顺序是从上往下:能读对象就别进节点,能看日志就别碰内核。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 故障< / th > < th > 第一个信号< / th > < th > 第二个信号< / th > < th > 第三个信号< / th > < / tr > < / thead >
< tbody >
< tr > < td > Pending< / td > < td > STATUS 与 Node 列< / td > < td > Events 的 FailedScheduling< / td > < td > 节点 Allocatable 与污点< / td > < / tr >
< tr > < td > CrashLoopBackOff< / td > < td > RESTARTS 递增< / td > < td > < code > logs --previous< / code > < / td > < td > Last State 的 Reason< / td > < / tr >
< tr > < td > ImagePullBackOff< / td > < td > STATUS 与容器 ID 为空< / td > < td > Events 的 Failed to pull< / td > < td > 镜像地址与 tag< / td > < / tr >
< tr > < td > OOMKilled< / td > < td > STATUS 与 137< / td > < td > Last State 的 OOMKilled< / td > < td > limits 与 < code > top pod< / code > < / td > < / tr >
< tr > < td > Evicted< / td > < td > STATUS 为 Evicted< / td > < td > Reason 与 Message< / td > < td > 节点磁盘与驱逐阈值< / td > < / tr >
< tr > < td > NodeNotReady< / td > < td > < code > get nodes< / code > < / td > < td > node 的 Conditions< / td > < td > 节点上的 kubelet 日志< / td > < / tr >
< / tbody >
< / table >
< / div >
< h4 > Step 1 · 校验第 15 章的现场,建好目录< / h4 >
< p > < span class = "tag" > 意图< / span > 确认起点干净:九个 Pod 全 Ready, < code > runbooks/< / code > 里已有第 14 章的 < code > release.md< / code > 。< / p >
< pre data-lang = "bash" data-name = "校验起点" > # env: all
kubectl -n shop get pods
ls runbooks 2>/dev/null || mkdir -p runbooks< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
NAME READY STATUS RESTARTS AGE
shop-api-7d9f8c6b5d-2xk4l 1/1 Running 0 2d
shop-api-7d9f8c6b5d-9v7bp 1/1 Running 0 2d
shop-api-7d9f8c6b5d-tc4wm 1/1 Running 0 2d
shop-web-v1-5b8f6d7c9-lm3qn 1/1 Running 0 2d
shop-web-v1-5b8f6d7c9-x8qpt 1/1 Running 0 2d
shop-web-v2-6c8f9d7b5c-r7ktn 1/1 Running 0 2d
shop-web-v2-6c8f9d7b5c-w3dlm 1/1 Running 0 2d
shop-worker-6b9c4f7d8-x2m4q 1/1 Running 0 2d
postgres-0 1/1 Running 0 2d
release.md< / pre >
< p > < span class = "tag" > 不一致怎么办< / span > Pod 没全 Running, 先回第 15 章收尾;带着两个故障开始,后面每个症状都有两种解释。< / p >
< h4 > Step 2 · Pending: 把 requests 要成节点给不起的数< / h4 >
< p > < span class = "tag" > 意图< / span > 复现「节点健康但 Pod 落不下去」,并保留原地改小 requests 恢复的路径。< / p >
< pre data-lang = "bash" data-name = "注入 Pending" > # env: all
kubectl -n shop create deployment pending-demo --image=busybox:1.36 -- sleep 3600
kubectl -n shop set resources deployment/pending-demo --requests=cpu=64 --limits=cpu=64
sleep 10
kubectl -n shop get pods -l app=pending-demo
POD=$(kubectl -n shop get pod -l app=pending-demo -o jsonpath='{.items[0].metadata.name}')
kubectl -n shop describe pod "$POD" | tail -n 6< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
NAME READY STATUS RESTARTS AGE
pending-demo-8c7f9d6b5-4tj2p 0/1 Pending 0 10s
Events:
Type Reason Age From Message
Warning FailedScheduling 8s default-scheduler 0/3 nodes are available: 3 Insufficient cpu.< / pre >
< p > < span class = "tag" > 不一致怎么办< / span > 读到 < code > untolerated taint< / code > 也正常,那是 16.3 的现场还在:一条说装不下,一条说不许来。< / p >
< h4 > Step 3 · CrashLoopBackOff: 让进程每次启动都退出< / h4 >
< p > < span class = "tag" > 意图< / span > 划清「容器起不来」与「起来了又死」的边界,它决定看 Event 还是看日志。< / p >
< pre data-lang = "bash" data-name = "注入 CrashLoop" > # env: all
kubectl -n shop run crash-demo --image=busybox:1.36 --restart=Always \
-- sh -c 'echo boot; sleep 2; exit 1'
sleep 45
kubectl -n shop get pod crash-demo
kubectl -n shop logs crash-demo --previous
kubectl -n shop describe pod crash-demo | grep -A3 "Last State"< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
NAME READY STATUS RESTARTS AGE
crash-demo 0/1 CrashLoopBackOff 4 (35s ago) 1m
boot
Last State: Terminated
Reason: Error
Exit Code: 1< / pre >
< p > < span class = "tag" > 不一致怎么办< / span > < code > --previous< / code > 报 < code > not found< / code > 说明还没死过第二次,等 30 秒;日志为空而 RESTARTS 在涨,说明进程写日志之前就崩了。< / p >
< h4 > Step 4 · ImagePullBackOff: 指向一个不存在的 tag< / h4 >
< p > < span class = "tag" > 意图< / span > 看「拉不到」的 Pod: 没有容器 ID, 也没有重启计数。< / p >
< pre data-lang = "bash" data-name = "注入 ImagePull" > # env: all
kubectl -n shop run pull-demo --image=localhost:5001/aurora/shop-api:nope \
--restart=Never -- sleep 3600
sleep 20
kubectl -n shop get pod pull-demo
kubectl -n shop describe pod pull-demo | grep -E "Image:|Failed"< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
NAME READY STATUS RESTARTS AGE
pull-demo 0/1 ImagePullBackOff 0 20s
Image: localhost:5001/aurora/shop-api:nope
Warning Failed 18s kubelet Failed to pull image: ... not found< / pre >
< p > < span class = "tag" > 不一致怎么办< / span > 第 15 章若走 < code > kind load< / code > 退路而没有本地仓库,换成 < code > aurora/shop-api:nope< / code > ;报 < code > unauthorized< / code > 是凭据问题,别改 tag。< / p >
< h4 > Step 5 · OOMKilled: 32Mi 的限额配一个会长大的变量< / h4 >
< p > < span class = "tag" > 意图< / span > 拿到 < code > Exit Code 137< / code > 与 < code > OOMKilled< / code > 这对组合,并记住 137 只说明收到 SIGKILL。< / p >
< pre data-lang = "bash" data-name = "注入 OOM" > # env: all
kubectl -n shop apply -f - < < 'EOF'
apiVersion: v1
kind: Pod
metadata:
name: oom-demo
spec:
restartPolicy: Never
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "x=xx; while true; do x=$x$x; done"]
resources:
limits:
memory: 32Mi
EOF
sleep 30
kubectl -n shop get pod oom-demo
kubectl -n shop describe pod oom-demo | grep -A3 "Last State"< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
NAME READY STATUS RESTARTS AGE
oom-demo 0/1 OOMKilled 0 30s
Last State: Terminated
Reason: OOMKilled
Exit Code: 137< / pre >
< p > < span class = "tag" > 不一致怎么办< / span > STATUS 是 < code > Error< / code > 而非 < code > OOMKilled< / code > ,说明进程自己退了:先用 < code > sleep 5< / code > 验证镜像能跑,再回来跑这个循环。< / p >
< h4 > Step 6 · Evicted: emptyDir 写超 Pod 的临时存储限额< / h4 >
< p > < span class = "tag" > 意图< / span > 看到 < code > Evicted< / code > 与那句限额文案。这里设了两个数:< code > sizeLimit< / code > 护节点盘,< code > ephemeral-storage< / code > 才是驱逐判据。< / p >
< pre data-lang = "bash" data-name = "注入 Evicted" > # env: all
kubectl -n shop apply -f - < < 'EOF'
apiVersion: v1
kind: Pod
metadata:
name: evict-demo
spec:
restartPolicy: Never
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "dd if=/dev/zero of=/cache/fill bs=1M count=200; sleep 3600"]
resources:
limits:
ephemeral-storage: 64Mi
volumeMounts:
- name: cache
mountPath: /cache
volumes:
- name: cache
emptyDir:
sizeLimit: 256Mi
EOF
sleep 60
kubectl -n shop get pod evict-demo
kubectl -n shop describe pod evict-demo | grep -A2 "Reason"< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
NAME READY STATUS RESTARTS AGE
evict-demo 0/1 Evicted 0 70s
Status: Failed
Reason: Evicted
Message: Pod ephemeral local storage usage exceeds the total limit of containers 64Mi.< / pre >
< p > < span class = "tag" > 不一致怎么办< / span > 仍 Running 就等下一个 10 秒 housekeeping 周期;若是 < code > Error< / code > 且带 < code > No space left on device< / code > ,说明 < code > sizeLimit< / code > 先到顶,把它调到限额两倍以上。< / p >
< h4 > Step 7 · NodeNotReady: 暂停 50 秒,再等 300 秒容忍期< / h4 >
< p > < span class = "tag" > 意图< / span > 把「节点失联」与「Pod 被驱逐」拆成两个时刻, 50 秒的判定窗口见 1.8。< / p >
< pre data-lang = "bash" data-name = "注入 NodeNotReady 并恢复" > # env: all
docker pause aurora-worker
# 影响: aurora-worker 上的 Pod 在 50 秒后失联, 但不会重建( 300 秒容忍期内)
sleep 60
kubectl get nodes
docker unpause aurora-worker
sleep 20
kubectl get nodes< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
NAME STATUS ROLES AGE VERSION
aurora-control-plane Ready control-plane 12d v1.36.1
aurora-worker NotReady worker 12d v1.36.1
aurora-worker2 Ready worker 12d v1.36.1
...( unpause 后三个节点全部 Ready) < / pre >
< p > < span class = "tag" > 不一致怎么办< / span > < code > No such container< / code > 说明节点名与容器名不同,先 < code > docker ps --format '{{.Names}}'< / code > 找真实名字; 60 秒后仍 Ready, 查是否有人把 grace period 调大了。< / p >
< div class = "box warn" > < span class = "bt" > 易错点 · Event 只留一小时< / span >
< p > < b > 现象< / b > :第二天复盘想找 < code > FailedScheduling< / code > , < code > get events< / code > 里什么都没有。< / p >
< p > < b > 原因< / b > : Event 是短期对象,默认一小时后被清掉,它服务「现在」,不服务「历史」。< / p >
< p > < b > 怎么验证< / b > :出故障时立刻 < code > get events --sort-by=.lastTimestamp | tail -20< / code > 存档。< / p >
< / div >
< div class = "box warn" > < span class = "bt" > 易错点 · 137 不等于一定是 OOM< / span >
< p > < b > 现象< / b > :看到 137 就加 limits, 加完还是被杀。< / p >
< p > < b > 原因< / b > : 137 只代表进程收到 SIGKILL。节点回收、驱逐、强制删除都可能是 137。< / p >
< p > < b > 怎么验证< / b > : < code > describe< / code > 里 < code > Reason< / code > 是否为 < code > OOMKilled< / code > ,再看 < code > top pod< / code > 是否贴住 limits。< / p >
< / div >
< div class = "box warn" > < span class = "bt" > 易错点 · Evicted 的 Pod 不会自己消失< / span >
< p > < b > 现象< / b > :修好之后 < code > get pods< / code > 里还是一片 < code > Evicted< / code > ,看着像没修。< / p >
< p > < b > 原因< / b > :被驱逐的 Pod 停在 < code > Failed< / code > 终态,控制器不替你删。< / p >
< p > < b > 怎么验证< / b > : < code > get pods --field-selector status.phase=Failed< / code > ,确认都是旧对象再批量删。< / p >
< / div >
< p > 六类走完,回到 < code > runbooks/< / code > 。四段固定:症状、三条命令、判断、修复。判断段最容易写空,因为人只记得「改了什么」,不记得「什么现象排除了别的可能」。下面这份 Pending 是完整示例。< / p >
< pre data-lang = "bash" data-name = "写入 runbooks/pending.md" > # env: all
cat > runbooks/pending.md < < 'EOF'
# 症状
STATUS 为 Pending, RESTARTS 为 0, Node 列为 < none> , Events 只有 FailedScheduling。
# 三条命令
kubectl -n shop get pods -o wide
kubectl -n shop describe pod $POD | tail -n 20
kubectl -n shop get events --sort-by=.lastTimestamp | tail -n 20
# 判断
Insufficient cpu/memory → 看 requests 是否超过单节点 Allocatable。
untolerated taint → 看节点污点与 Pod 的 tolerations。
didn't match node selector/affinity → 看 nodeSelector 与亲和。
waiting for first consumer → 不是调度问题,去看 PVC( 8.8)。
# 修复
requests 按实测 P95 下调,或加节点;污点侧解除:
kubectl taint nodes aurora-worker2 dedicated=ops:NoSchedule-
改完 kubectl -n shop rollout status deployment/shop-api 等到 3/3。
EOF
ls -l runbooks< / pre >
< p > 其余五份不再抄全文,「判断依据」一列就是各自第三段的浓缩。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 文件< / th > < th > 判断依据< / th > < th > 修复动作< / th > < / tr > < / thead >
< tbody >
< tr > < td style = "white-space:nowrap" > < code > crashloop.md< / code > < / td > < td > < code > --previous< / code > 有输出且非零退出,容器 ID 存在< / td > < td > 回滚镜像 tag 或改回配置:< code > rollout undo< / code > < / td > < / tr >
< tr > < td style = "white-space:nowrap" > < code > imagepull.md< / code > < / td > < td > Events 报 < code > Failed to pull image< / code > ,容器从未创建< / td > < td > 修 tag、补 < code > imagePullSecret< / code > ,或换回内网仓库< / td > < / tr >
< tr > < td style = "white-space:nowrap" > < code > oom.md< / code > < / td > < td > < code > Last State< / code > 为 < code > OOMKilled< / code > , < code > top< / code > 贴住 limits< / td > < td > 抬 < code > limits.memory< / code > 或降堆,保持 requests 等于 limits< / td > < / tr >
< tr > < td style = "white-space:nowrap" > < code > evicted.md< / code > < / td > < td > < code > Reason: Evicted< / code > 且文案指向 ephemeral storage< / td > < td > 清 < code > emptyDir< / code > 、抬限额,或换 PVC< / td > < / tr >
< tr > < td style = "white-space:nowrap" > < code > nodenotready.md< / code > < / td > < td > 节点 Conditions 为 < code > Unknown< / code > , Pod 仍 Running< / td > < td > 恢复心跳,等 300 秒后再看是否重建< / td > < / tr >
< / tbody >
< / table >
< / div >
< div class = "box warn" > < span class = "bt" > 易错点 · 日志只留当前容器< / span >
< p > < b > 现象< / b > : < code > kubectl logs< / code > 是空的,可 Pod 明明在重启。< / p >
< p > < b > 原因< / b > :默认读当前实例的 stdout, 崩溃重启后旧日志在 < code > --previous< / code > 里。< / p >
< p > < b > 怎么验证< / b > : < code > logs crash-demo --previous --tail=50< / code > ;仍为空就看 < code > describe< / code > 的 Command。< / p >
< / div >
< p class = "take" > < b > 一句话:< / b > runbook 最难写的是「判断」,它逼你说出否证条件。< / p >
< h3 id = "sec-16-7" > 16.7 验收清单< / h3 >
< p > 验收要回答「集群真的好了吗」,而不是「我记得删过」。< code > make verify-ch16< / code > 的九行断言在 16.10 跑过,这里补人工检查与一条反向验证。< / p >
< ol class = "steps" >
< li > 六份齐全:< code > ls runbooks/*.md | wc -l< / code > 输出 7( 含 < code > release.md< / code > )。< / li >
< li > < code > kubectl -n shop get pods< / code > 七行全 Running, READY 满。< / li >
< li > < code > kubectl get nodes< / code > 三行全 Ready, 无 < code > SchedulingDisabled< / code > 。< / li >
< li > 没有污点残留:< code > kubectl get nodes -o jsonpath='{.items[*].spec.taints}'< / code > 只剩 control-plane 的。< / li >
< li > 反向验证:删掉一份 runbook, 验收必须失败。< / li >
< / ol >
< pre data-lang = "bash" data-name = "反向验证" > # env: all
mv runbooks/oom.md /tmp/oom.md & & make verify-ch16; mv /tmp/oom.md runbooks/oom.md< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
[fail] runbooks/oom.md missing
make: *** [Makefile:VERIFY] Error 1< / pre >
< p > 这条通过了,说明 verify 检查的是「产物齐不齐」,而不只是「集群绿不绿」——后者在对象被误删后会给出虚假的安心。< / p >
< h3 id = "sec-16-8" > 16.8 故障实验室:三类展开,三类列表< / h3 >
< p > 六个注入在 16.6 已各做一次。这里换个视角:记下「从注入到症状出现」的时间,它决定告警能提前多久看到。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 故障< / th > < th > 注入手法< / th > < th > 症状出现< / th > < th > 回滚动作< / th > < / tr > < / thead >
< tbody >
< tr > < td > Pending< / td > < td > requests 改成 64 核< / td > < td > 几秒内,调度失败即产生 Event< / td > < td > requests 改回 100m< / td > < / tr >
< tr > < td > CrashLoopBackOff< / td > < td > 进程启动后 exit 1< / td > < td > 首次崩溃即重启,约 1 分钟进入退避< / td > < td > 删除 Pod 或换回正确命令< / td > < / tr >
< tr > < td > ImagePullBackOff< / td > < td > tag 写成 nope< / td > < td > 首次拉取失败后立刻退避< / td > < td > 改回真实 tag< / td > < / tr >
< tr > < td > OOMKilled< / td > < td > 32Mi 限额 + 增长循环< / td > < td > 10 到 30 秒< / td > < td > 删 Pod 或抬 limits< / td > < / tr >
< tr > < td > Evicted< / td > < td > emptyDir 写超 Pod 限额< / td > < td > 1 到 2 个 housekeeping 周期< / td > < td > 删 Pod, 清空目录< / td > < / tr >
< tr > < td > NodeNotReady< / td > < td > < code > docker pause< / code > < / td > < td > 50 秒判定, 300 秒后才驱逐< / td > < td > < code > docker unpause< / code > < / td > < / tr >
< / tbody >
< / table >
< / div >
< figure class = "figure" >
< svg viewBox = "0 0 720 200" role = "img" aria-label = "节点失联后的时间轴: 0 秒暂停节点, 50 秒判定 NotReady, 300 秒容忍期结束才开始驱逐" >
< defs >
< marker id = "a16c" markerWidth = "9" markerHeight = "9" refX = "8" refY = "4.5" orient = "auto" > < path d = "M0,0 L9,4.5 L0,9 z" style = "fill:var(--muted)" > < / path > < / marker >
< / defs >
< path d = "M40,120 H690" marker-end = "url(#a16c)" style = "stroke:var(--muted);fill:none" > < / path >
< path d = "M90,112 V128" style = "stroke:var(--danger);fill:none" > < / path >
< path d = "M320,112 V128" style = "stroke:var(--warn);fill:none" > < / path >
< path d = "M590,112 V128" style = "stroke:var(--warn);fill:none" > < / path >
< text x = "90" y = "150" text-anchor = "middle" style = "fill:var(--ink);font-size:12.5px" > 0 秒: docker pause< / text >
< text x = "320" y = "150" text-anchor = "middle" style = "fill:var(--ink);font-size:12.5px" > 50 秒:判定 NotReady< / text >
< text x = "590" y = "150" text-anchor = "middle" style = "fill:var(--ink);font-size:12.5px" > 300 秒:容忍期结束< / text >
< rect x = "40" y = "40" width = "250" height = "52" rx = "8" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "56" y = "62" style = "fill:var(--muted);font-size:12px" > Pod 仍 Running, 流量开始超时< / text >
< text x = "56" y = "80" style = "fill:var(--muted);font-size:12px" > 业务已经在受损< / text >
< rect x = "320" y = "40" width = "250" height = "52" rx = "8" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "336" y = "62" style = "fill:var(--muted);font-size:12px" > 节点标 NotReady, Pod 不删< / text >
< text x = "336" y = "80" style = "fill:var(--muted);font-size:12px" > 此时恢复节点代价最小< / text >
< / svg >
< div class = "figcap" > < b > 图 3 ·< / b > 节点失联后的两个时间常数,恢复窗口在 40 到 300 秒之间。< / div >
< / figure >
< div class = "box danger" > < span class = "bt" > 故障 1 · Evicted: 临时盘不是免费空间< / span >
< p > < b > 注入< / b > : Step 6 的 < code > evict-demo< / code > , < code > sizeLimit< / code > 256Mi、限额 64Mi, 写入 200Mi。< / p >
< p > < b > 症状< / b > :一分钟后 STATUS 变 < code > Evicted< / code > , Message 点名 ephemeral storage, 同节点其他 Pod 正常。< / p >
< p > < b > 诊断< / b > :① < code > get pods< / code > 看 AGE; ② < code > describe< / code > 读 Message 里的限额;③ < code > events< / code > 按对象名过滤。< / p >
< p > < b > 根因< / b > : < code > emptyDir< / code > 的写入计入 Pod 级临时存储用量,超限后 kubelet 标记 Failed 并清容器。< / p >
< p > < b > 修复< / b > :删 Pod; 业务侧改用 PVC, 或把限额抬到正常用量两倍, < code > sizeLimit< / code > 继续兜底。< / p >
< p > < b > 预防< / b > :带 < code > emptyDir< / code > 的工作负载两个数都写死,用量基线进 runbook。< / p >
< / div >
< div class = "box danger" > < span class = "bt" > 故障 2 · CrashLoopBackOff: 退避是保护, 不是故障< / span >
< p > < b > 注入< / b > : Step 3 的 < code > crash-demo< / code > ,打印一行 < code > boot< / code > 后 < code > exit 1< / code > 。< / p >
< p > < b > 症状< / b > : RESTARTS 递增, STATUS 在 Running 与 CrashLoopBackOff 间跳, Endpoints 里没有它。< / p >
< p > < b > 诊断< / b > :① < code > logs --previous< / code > ;② < code > describe< / code > 的 Last State; ③ < code > events< / code > 里的 BackOff。< / p >
< p > < b > 根因< / b > :进程退出后 kubelet 按 < code > restartPolicy< / code > 重启,连续失败逐次拉长间隔。< / p >
< p > < b > 修复< / b > :先读日志定原因再改镜像或配置;改配置用 < code > rollout undo< / code > 最快,裸 Pod 只能重建。< / p >
< p > < b > 预防< / b > : CI 里用同一镜像跑一次启动自检,别用 < code > initialDelaySeconds< / code > 掩盖启动失败。< / p >
< / div >
< div class = "box danger" > < span class = "bt" > 故障 3 · NodeNotReady: 两个时间常数决定一切< / span >
< p > < b > 注入< / b > : Step 7 的 < code > docker pause aurora-worker< / code > ,手法与第 1 章相同(< a href = "01-env.html#sec-1-8" > 1.8< / a > )。< / p >
< p > < b > 症状< / b > :约 50 秒后节点 NotReady, Pod 仍 Running, 流量先超时; 300 秒后才驱逐重建。< / p >
< p > < b > 诊断< / b > :① < code > get nodes< / code > ;② < code > describe node< / code > 的 Conditions 与心跳;③ < code > events< / code > 里的 NodeNotReady。< / p >
< p > < b > 根因< / b > : kubelet 停止上报心跳,节点控制器在 grace period 后标记 Unknown, Pod 靠容忍度撑着。< / p >
< p > < b > 修复< / b > : < code > docker unpause< / code > , 半分钟恢复, Pod 不用重建。< b > 不要< / b > 在这 300 秒里删 Pod。< / p >
< p > < b > 预防< / b > :加 NotReady 持续 1 分钟的告警;维护节点先 < code > cordon< / code > 再 < code > drain< / code > 。< / p >
< / div >
< p > 剩下三类没展开成六段式,判断路径已在 Step 与 runbook 表里走完,一句话版如下。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 故障< / th > < th > 判断依据< / th > < th > 修复< / th > < th > 预防< / th > < / tr > < / thead >
< tbody >
< tr > < td > Pending< / td > < td > Node 列为空, Events 只有 FailedScheduling< / td > < td > 降 requests 或解除污点< / td > < td > requests 按实测 P95 设< / td > < / tr >
< tr > < td > ImagePullBackOff< / td > < td > 容器从未创建, Events 报拉取失败< / td > < td > 改 tag、补凭据< / td > < td > tag 用不可变 digest< / td > < / tr >
< tr > < td > OOMKilled< / td > < td > Last State 为 OOMKilled, 137< / td > < td > 抬 limits 或降堆< / td > < td > requests 等于 limits, 堆外留 25%< / td > < / tr >
< / tbody >
< / table >
< / div >
< div class = "grid2" >
< div class = "box" >
< h4 > Windows 11 + WSL2< / h4 >
< p > 节点是 Docker Desktop 里的容器,< code > docker pause aurora-worker< / code > 在 PowerShell 里同样可用。别用 < code > wsl --shutdown< / code > 代替:它会把引擎一起关掉,节点不是 NotReady 而是整个消失。< / p >
< p > 读事件用 < code > Select-Object -Last 20< / code > ,这里没有 < code > tail< / code > 。< / p >
< / div >
< div class = "box" >
< h4 > Linux (Ubuntu 24.04)< / h4 >
< p > 节点同样是容器,< code > docker pause< / code > 直接作用在容器进程上;也可以用 < code > kill -STOP< / code > 停 kubelet 做同类实验,记得 < code > kill -CONT< / code > 恢复。< / p >
< p > 读事件用 < code > tail -20< / code > ,与文档写法一致。< / p >
< / div >
< / div >
< p > < b > 差异结论< / b > :暂停手法一致,差别只在「别把引擎一起停掉」和管道命令的名字;诊断树两侧相同。< / p >
< p class = "take" > < b > 一句话:< / b > 先看 Event 的人,通常比先看日志的人早半小时定位。< / p >
< h3 id = "sec-16-9" > 16.9 生产对照< / h3 >
< p > 实验里你是唯一的医生,生产里要和监控、告警、值班表一起工作。同样是 Pending, 处理半径差得很远。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 维度< / th > < th > 学习环境( kind) < / th > < th > 生产环境< / th > < th > 为什么不同< / th > < th > 迁移动作< / th > < / tr > < / thead >
< tbody >
< tr > < td style = "white-space:nowrap" > 黄金信号与 SLO< / td > < td > 你看 STATUS 与 Event< / td > < td > 先看错误率、延迟、饱和度、流量< / td > < td > 单集群没有真实流量基线< / td > < td > 给 < code > shop-api< / code > 定 99.9% 与 P95 300ms< / td > < / tr >
< tr > < td style = "white-space:nowrap" > 告警分级< / td > < td > 一个 Pending 就立刻处理< / td > < td > P1 只给「用户受损且 SLO 在烧」< / td > < td > 注意力稀缺,叫醒一次成本很高< / td > < td > 按燃烧率分级, Warning 降为 P3< / td > < / tr >
< tr > < td style = "white-space:nowrap" > on-call 手册< / td > < td > 六份 runbook 在仓库里< / td > < td > 告警正文链到 runbook 锚点< / td > < td > 凌晨三点没人读长文< / td > < td > 每条告警加 < code > runbook_url< / code > 标签< / td > < / tr >
< tr > < td style = "white-space:nowrap" > 事后复盘< / td > < td > 你记下「下次先看 Event」< / td > < td > blameless, 只问系统为何允许< / td > < td > 追责会让下一次故障被隐瞒< / td > < td > 固定输出检测、缓解、修复三段< / td > < / tr >
< tr > < td > 故障注入< / td > < td > 手动 pause, 一次一类< / td > < td > 常态化混沌实验,带爆炸半径< / td > < td > 生产不能靠人肉试错< / td > < td > 先每周低峰期注入一次< / td > < / tr >
< / tbody >
< / table >
< / div >
< p > 「告警分级」最容易做错。把每条 Warning 都设成 P1, 一周后所有人都会静音这个渠道, 真正该起身的那次也被静音。< / p >
< h3 id = "sec-16-10" > 16.10 清理与残留检查< / h3 >
< p > 产物要留,六份 runbook 是下一章的输入;演示对象要清,只删本章建的四个 Pod 与一个 Deployment。< / p >
< pre data-lang = "bash" data-name = "清理本章资源" > # env: all
kubectl -n shop delete deployment pending-demo --ignore-not-found
kubectl -n shop delete pod crash-demo pull-demo oom-demo evict-demo --ignore-not-found
docker unpause aurora-worker
# 影响: 只删本章的演示对象; shop-api / shop-web / postgres 不动
kubectl -n shop get pods
kubectl get nodes
make verify-ch16< / pre >
< pre data-lang = "text" data-name = "残留检查" > # 预期输出
NAME READY STATUS RESTARTS AGE
shop-api-7d9f8c6b5d-2xk4l 1/1 Running 0 2d
shop-api-7d9f8c6b5d-9v7bp 1/1 Running 0 2d
shop-api-7d9f8c6b5d-tc4wm 1/1 Running 0 2d
shop-web-v1-5b8f6d7c9-lm3qn 1/1 Running 0 2d
shop-web-v1-5b8f6d7c9-x8qpt 1/1 Running 0 2d
shop-web-v2-6c8f9d7b5c-r7ktn 1/1 Running 0 2d
shop-web-v2-6c8f9d7b5c-w3dlm 1/1 Running 0 2d
shop-worker-6b9c4f7d8-x2m4q 1/1 Running 0 2d
postgres-0 1/1 Running 0 2d
NAME STATUS ROLES AGE VERSION
aurora-control-plane Ready control-plane 12d v1.36.1
aurora-worker Ready worker 12d v1.36.1
aurora-worker2 Ready worker 12d v1.36.1
[ok] runbooks/pending.md exists
[ok] runbooks/crashloop.md exists
[ok] runbooks/imagepull.md exists
[ok] runbooks/oom.md exists
[ok] runbooks/evicted.md exists
[ok] runbooks/nodenotready.md exists
[ok] pods ready 9/9 in shop
[ok] nodes ready 3/3
[ok] warning events only historical< / pre >
< p > 顺序有讲究:先删 Pod, 再 unpause。反过来也能恢复, 但节点会先把一堆 Failed 对象同步一遍, Event 里多出无关噪声。
< code > verify-ch16< / code > 放在清理之后跑,是因为它的断言里既有「六个 runbook 在」也有「九个 Pod 全 Ready」——
演示对象没清掉,第二条必然失败。< / p >
< h3 id = "sec-16-11" > 16.11 小结与自测< / h3 >
< p class = "take" > < b > 一句话:< / b > STATUS 是分诊号, Event 是病历,日志才是主诉。< / p >
< p class = "take" > < b > 一句话:< / b > 先判断哪一层没动静,再选工具;顺序反了就是四十分钟。< / p >
< p class = "take" > < b > 一句话:< / b > runbook 的价值不在写,在于别人能照着走。< / p >
< p > 把六个「第一个信号」背下来比背命令有用: FailedScheduling、< code > --previous< / code > 、Failed to pull、137、Reason、< code > get nodes< / code > 。这六个词就是树的六个出口。< / p >
< details > < summary > 自测 3 题(点击展开答案)< / summary >
< p > < b > 概念< / b > :为什么 < code > ImagePullBackOff< / code > 没有 RESTARTS 计数,< code > CrashLoopBackOff< / code > 有?< / p >
< p > 答:前者容器从未创建,没有「重启」可计;后者容器起来过、退出了、被 kubelet 重启过。< / p >
< p > < b > 排错< / b > :节点 NotReady 已两分钟, Pod 还 Running, 先做什么? < / p >
< p > 答:先恢复节点,不要删 Pod。容忍期内删掉只会让副本数短暂不足。< / p >
< p > < b > 决策< / b > : Job 因 OOM 反复重试,抬 limits 还是改 < code > restartPolicy: Never< / code > ? < / p >
< p > 答:先抬 limits 或降单次处理量。改 Never 只是让失败显形,不解决容量。< / p >
< / details >
< div class = "box tip" > < span class = "bt" > 延伸挑战( ≤30 分钟)< / span >
< p > 给六类各补一列「我会先排除什么」,拿给没读过本章的同事,看他能否只按这一列走到正确的命令。走不通的地方,就是你的 runbook 还缺的那句话。< / p >
< / div >
< h3 id = "sec-16-12" > 16.12 衔接< / h3 >
< p > < b > 回收< / b > :第 7 章的 requests 与探针、第 8 章的 PVC、第 15 章的镜像 tag 与回滚,在本章被同一条路径串起来——它们不再是三个知识点,而是同一棵树的三个叶子。< / p >
< p > < b > 钩子< / b > :到现在你只有一个 kind 集群,控制面只有一台。第 17 章用 kubeadm 搭三控制面, etcd 变三副本, API Server 前多一个负载均衡器;诊断树不变,但 NodeNotReady 可能只是负载均衡器把流量切走了。< a href = "17-kubeadm.html" > 第 17 章< / a > 见。< / p >
< h3 id = "sec-16-13" > 16.13 参考与验证版本< / h3 >
< ul >
< li > < a href = "https://kubernetes.io/docs/tasks/debug/" > Troubleshooting( 官方总览) < / a > < / li >
< li > < a href = "https://kubernetes.io/docs/tasks/debug/debug-application/debug-running-pod/" > Debug Running Pods( kubectl debug) < / a > < / li >
< li > < a href = "https://kubernetes.io/docs/tasks/debug/debug-cluster/crictl/" > Debugging nodes with crictl< / a > < / li >
< li > < a href = "https://kubernetes.io/docs/concepts/storage/ephemeral-storage/" > Local Ephemeral Storage< / a > < / li >
< li > < a href = "https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/" > Pod Lifecycle( 重启策略与退避) < / a > < / li >
< / ul >
< p > 验证版本: k8s v1.36 / kubectl v1.36 / busybox:1.36 · 最后验证: 2026-11-11< / p >
< p > 变更记录: 2026-11-11 首版,六类故障与六份 runbook 一次性建立。< / p >
< / article >
< nav class = "pager" >
< a class = "prev" href = "15-supply.html" > < span class = "k" > ← 上一章< / span > < span class = "v" > 第 15 章 · 密钥与镜像供应链< / span > < / a >
2026-11-11 21:57:56 +08:00
< a class = "next" href = "17-kubeadm.html" > < span class = "k" > 下一章 →< / span > < span class = "v" > 第 17 章 · 生产集群搭建: kubeadm HA< / span > < / a >
2026-11-11 12:57:24 +08:00
< / nav >
< p class = "foot" >
Kubernetes 专题 · S3 第 16 章 故障诊断方法论:六类故障,一张诊断树< br >
< a href = "index.html" > 总目录< / a > · < a href = "../../index.html" > 知识库首页< / a > · < a href = "../../SRE工程师/sre-learning-path/index.html" > SRE 学习路径< / a >
< / p >
< / main >
< / div >
< button id = "toTop" title = "回到顶部" > ↑< / button >
< script src = "assets/doc.js" > < / script >
< script src = "../../assets/back.js" data-portal-back = "knowledge/kubernetes" data-portal-label = "知识库 › Kubernetes 专题知识库" > < / script >
< / body >
< / html >