645 lines
50 KiB
HTML
645 lines
50 KiB
HTML
|
|
<!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>第 23 章 值班与事故响应 · Kubernetes 专题知识库</title>
|
|||
|
|
<meta name="description" content="凌晨一次 PVC 写满让下单全挂:从接警、分级、止损到复盘归档,走完 40 分钟闭环,讲清 local-path 为什么不能扩卷,并产出 runbook 与 postmortem。">
|
|||
|
|
<meta name="keywords" content="值班, 事故响应, P1, PVC 写满, 告警状态机, for, runbook, postmortem, local-path">
|
|||
|
|
<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>第 23 章</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>
|
|||
|
|
<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><a class="toc-a" href="16-debug.html"><i class="cn">16</i>故障诊断方法论:六类故障,一张诊断树</a><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 active" aria-current="page"><i class="cn">23</i>值班与事故响应</span><div class="subnav current"><a class="toc-a lv3" href="#sec-23-1">23.1 本章在哪:主线之前与之后</a><a class="toc-a lv3" href="#sec-23-2">23.2 学习目标</a><a class="toc-a lv3" href="#sec-23-3">23.3 场景:01:45 的电话</a><a class="toc-a lv3" href="#sec-23-4">23.4 心智模型:分诊,不是查案</a><a class="toc-a lv3" href="#sec-23-5">23.5 概念与字段速查</a><a class="toc-a lv3" href="#sec-23-6">23.6 动手:从告警到归档</a><a class="toc-a lv3" href="#sec-23-7">23.7 验收清单与 make verify-ch23</a><a class="toc-a lv3" href="#sec-23-8">23.8 故障实验室:三种「以为修好了」</a><a class="toc-a lv3" href="#sec-23-9">23.9 生产对照</a><a class="toc-a lv3" href="#sec-23-10">23.10 清理与残留检查</a><a class="toc-a lv3" href="#sec-23-11">23.11 小结与自测</a><a class="toc-a lv3" href="#sec-23-12">23.12 衔接</a><a class="toc-a lv3" href="#sec-23-13">23.13 参考与验证版本</a></div><a class="toc-a" href="24-cost.html"><i class="cn">24</i>成本与容量</a><a class="toc-a" href="25-prr.html"><i class="cn">25</i>生产就绪评审与下一步</a></nav>
|
|||
|
|
<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>S5 守得住:值班、成本与评审</span>
|
|||
|
|
<span>·</span>
|
|||
|
|
<span>第 <b class="pos">23</b> / 26 章</span>
|
|||
|
|
</p>
|
|||
|
|
|
|||
|
|
<h2 id="ch23"><span class="n">23</span>值班与事故响应</h2>
|
|||
|
|
|
|||
|
|
<div class="lead">
|
|||
|
|
<b>30 秒速览:</b>这一章不装新组件。它把前 22 章攒下的告警、GitOps、备份与 RBAC 拿去受一次审:凌晨一次「PVC 写满」让下单全部 500,你在 40 分钟里走完接警、分级、止损、沟通、归档,并亲手撞上「local-path 不能扩卷」这堵墙。产物是两份进 Git 的文档。约 120 分钟。
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-1">23.1 本章在哪:主线之前与之后</h3>
|
|||
|
|
|
|||
|
|
<p>前面二十章在回答「怎么把它建起来」,本章回答「它半夜坏掉时,你先动哪只手」。集群、告警、备份都在位,缺的是顺序。</p>
|
|||
|
|
|
|||
|
|
<div class="tablewrap">
|
|||
|
|
<table>
|
|||
|
|
<thead><tr><th>项</th><th>内容</th></tr></thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td>主线之前</td><td>第 22 章终态:5 台 VM 跑 <b>v1.36.6</b>,<code>shop</code> 刚从 Velero 恢复过一次,<code>ShopApiHighErrorRate</code>(<code>for: 2m</code>)在 <code>platform</code> 里生效</td></tr>
|
|||
|
|
<tr><td>主线之后</td><td>同一次事故被完整处置:告警回到 <code>inactive</code>、<code>orders</code> 一行没丢、<code>runbooks/p1-pvc-full.md</code> 与 <code>docs/postmortem-2026-11-13.md</code> 已提交</td></tr>
|
|||
|
|
<tr><td>本章产物</td><td>两份 Markdown、一份 <code>Makefile</code> 里的 <code>verify-ch23</code>、一条「local-path 不可扩」的判断</td></tr>
|
|||
|
|
<tr><td>适用环境</td><td>ENV-C(Multipass 5 台 VM)。kind 上也能复现,但它没有 <code>wk1</code> 这块独立节点盘,<code>df</code> 那一节会失真</td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-2">23.2 学习目标</h3>
|
|||
|
|
|
|||
|
|
<div class="box note">
|
|||
|
|
<span class="bt">学完你能</span>
|
|||
|
|
<ul>
|
|||
|
|
<li>五分钟内定级:一句话说清「谁受影响、影响多大、有没有绕行」。</li>
|
|||
|
|
<li>读懂告警为何响、为何没响:从 <code>for</code> 与 <code>ALERTS</code> 各取一条证据。</li>
|
|||
|
|
<li>判定「扩卷」是死路:背出 API 拒绝的原文,给出两条替代路径。</li>
|
|||
|
|
<li>写出能照着执行的 runbook,与时间线到分钟、行动项带 owner 的 postmortem。</li>
|
|||
|
|
</ul>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-3">23.3 场景:01:45 的电话</h3>
|
|||
|
|
|
|||
|
|
<p>01:45,手机响。先看 Pod:三个副本都 <code>Running</code>,重启 0 次,看起来什么都没坏。</p>
|
|||
|
|
|
|||
|
|
<p>再打开 <code>shop.example.com</code> 下单,转圈二十秒,500。第一反应是刚发的版本,可 <code>argocd app history shop-prod</code> 显示同步在六小时前,digest 没动。第二反应是数据库,日志给了难听的答案:</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="text" data-name="写满那一刻两边的日志"># 预期输出
|
|||
|
|
postgres-0 LOG: could not extend file "base/16384/16400": No space left on device
|
|||
|
|
postgres-0 HINT: Check free disk space.
|
|||
|
|
postgres-0 PANIC: could not write to file "pg_wal/00000001000000000000004A": No space left on device
|
|||
|
|
shop-api-... ERROR create order: pq: could not extend file "base/16384/16400": No space left on device</pre>
|
|||
|
|
|
|||
|
|
<div class="box case">
|
|||
|
|
<span class="bt">现场</span>
|
|||
|
|
<p>三条信息同时成立:告警 <code>firing</code> 两分钟;下单全挂而浏览正常;没人知道 <code>wk1</code> 那块盘还剩多少。此刻最容易做的三件事——重启、回滚、扩 PVC——只有一件有用。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-4">23.4 心智模型:分诊,不是查案</h3>
|
|||
|
|
|
|||
|
|
<p>值班不是当侦探。侦探可以花三天找凶手,分诊护士只有四分钟决定送哪个科室。现场四件事有固定顺序:确认影响面、定级、止损、根因与归档。</p>
|
|||
|
|
|
|||
|
|
<p>见图 1:前两格只花十四分钟,却决定后面二十六分钟有没有意义。</p>
|
|||
|
|
|
|||
|
|
<figure class="figure">
|
|||
|
|
<svg viewBox="0 0 720 250" role="img" aria-label="一次 P1 事故从 01:41 到 02:21 的五个阶段与时间分配">
|
|||
|
|
<defs>
|
|||
|
|
<marker id="a23a" 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="12" y="54" width="124" height="76" rx="8" style="fill:var(--danger-bg);stroke:var(--danger)"></rect>
|
|||
|
|
<text x="74" y="76" text-anchor="middle" font-size="11" style="fill:var(--muted)">01:41–01:45</text>
|
|||
|
|
<text x="74" y="98" text-anchor="middle" font-size="14" style="fill:var(--ink)">发现与接警</text>
|
|||
|
|
<text x="74" y="118" text-anchor="middle" font-size="11" style="fill:var(--muted)">500 起 · firing</text>
|
|||
|
|
<rect x="148" y="54" width="124" height="76" rx="8" style="fill:var(--card);stroke:var(--line)"></rect>
|
|||
|
|
<text x="210" y="76" text-anchor="middle" font-size="11" style="fill:var(--muted)">01:45–01:56</text>
|
|||
|
|
<text x="210" y="98" text-anchor="middle" font-size="14" style="fill:var(--ink)">定位</text>
|
|||
|
|
<text x="210" y="118" text-anchor="middle" font-size="11" style="fill:var(--muted)">排除发布嫌疑</text>
|
|||
|
|
<rect x="284" y="54" width="124" height="76" rx="8" style="fill:var(--warn-bg);stroke:var(--warn)"></rect>
|
|||
|
|
<text x="346" y="76" text-anchor="middle" font-size="11" style="fill:var(--muted)">01:56–02:03</text>
|
|||
|
|
<text x="346" y="98" text-anchor="middle" font-size="14" style="fill:var(--ink)">决策</text>
|
|||
|
|
<text x="346" y="118" text-anchor="middle" font-size="11" style="fill:var(--muted)">扩不了,删还是换</text>
|
|||
|
|
<rect x="420" y="54" width="124" height="76" rx="8" style="fill:var(--brand-soft);stroke:var(--brand)"></rect>
|
|||
|
|
<text x="482" y="76" text-anchor="middle" font-size="11" style="fill:var(--muted)">02:03–02:14</text>
|
|||
|
|
<text x="482" y="98" text-anchor="middle" font-size="14" style="fill:var(--brand)">止血</text>
|
|||
|
|
<text x="482" y="118" text-anchor="middle" font-size="11" style="fill:var(--muted)">腾空间 + VACUUM</text>
|
|||
|
|
<rect x="556" y="54" width="124" height="76" rx="8" style="fill:var(--ok-bg);stroke:var(--ok)"></rect>
|
|||
|
|
<text x="618" y="76" text-anchor="middle" font-size="11" style="fill:var(--muted)">02:14–02:21</text>
|
|||
|
|
<text x="618" y="98" text-anchor="middle" font-size="14" style="fill:var(--ink)">收尾</text>
|
|||
|
|
<text x="618" y="118" text-anchor="middle" font-size="11" style="fill:var(--muted)">通报与解冻</text>
|
|||
|
|
<path d="M136,92 L144,92" style="stroke:var(--muted);fill:none" marker-end="url(#a23a)"></path>
|
|||
|
|
<path d="M272,92 L280,92" style="stroke:var(--muted);fill:none" marker-end="url(#a23a)"></path>
|
|||
|
|
<path d="M408,92 L416,92" style="stroke:var(--muted);fill:none" marker-end="url(#a23a)"></path>
|
|||
|
|
<path d="M544,92 L552,92" style="stroke:var(--muted);fill:none" marker-end="url(#a23a)"></path>
|
|||
|
|
<line x1="148" y1="156" x2="556" y2="156" style="stroke:var(--brand)" stroke-width="1.5"></line>
|
|||
|
|
<line x1="148" y1="150" x2="148" y2="162" style="stroke:var(--brand)" stroke-width="1.5"></line>
|
|||
|
|
<line x1="556" y1="150" x2="556" y2="162" style="stroke:var(--brand)" stroke-width="1.5"></line>
|
|||
|
|
<text x="352" y="180" text-anchor="middle" font-size="12" style="fill:var(--brand)">变更冻结:01:52 首报之后,到 02:20 解冻</text>
|
|||
|
|
<text x="360" y="214" text-anchor="middle" font-size="12" style="fill:var(--muted)">复盘不在窗口里:次日 11:00,45 分钟,产出三条带 owner 的行动项</text>
|
|||
|
|
</svg>
|
|||
|
|
<div class="figcap"><b>图 1 ·</b>四十分钟里只有第三格在「修」,前两格决定修什么,最后一格决定下次还修不修。</div>
|
|||
|
|
</figure>
|
|||
|
|
|
|||
|
|
<p>止损的定义很窄:让错误率回到 0,或让用户绕开故障,不包括找到根因。凌晨最容易吵的是「没查清楚怎么能改」,正确答案是「先改回去,明天再查」。</p>
|
|||
|
|
|
|||
|
|
<div class="tablewrap">
|
|||
|
|
<table>
|
|||
|
|
<thead><tr><th>误解</th><th>实际</th></tr></thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td>事故处理就是找出根因</td><td>前二十分钟的目标是让用户能用;根因明天上午再找,用户不会等你</td></tr>
|
|||
|
|
<tr><td>告警响了说明监控到位</td><td>这次响了,更常见的是没响:<code>for</code> 被调大、路由丢了、没人接(见 23.8 故障 2)</td></tr>
|
|||
|
|
<tr><td>复盘是追责会</td><td>复盘产出行动项,不产出责任人;写「谁删的」不如写「为什么删之前没有拦」</td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-5">23.5 概念与字段速查</h3>
|
|||
|
|
|
|||
|
|
<p>值班时只查这几行。默认值一列最重要——多数「配置被人改过」的事故,先卡在认不出默认值。</p>
|
|||
|
|
|
|||
|
|
<div class="tablewrap">
|
|||
|
|
<table>
|
|||
|
|
<thead><tr><th>对象 / 字段</th><th>默认值</th><th>本章用的值</th><th>值班时看它做什么</th></tr></thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td><code>PrometheusRule</code> 的 <code>for</code></td><td>无(0 秒)</td><td><code>2m</code></td><td>表达式连续为真多久才从 <code>pending</code> 进 <code>firing</code></td></tr>
|
|||
|
|
<tr><td><code>Alertmanager</code> 的 <code>group_wait</code></td><td><code>30s</code></td><td><code>30s</code></td><td>首条通知的聚合等待;它藏在「为什么晚了一分钟」里</td></tr>
|
|||
|
|
<tr><td><code>Alertmanager</code> 的 <code>resolve_timeout</code></td><td><code>5m</code></td><td><code>5m</code></td><td>告警消失后多久发 resolved,决定收尾通报的时间点</td></tr>
|
|||
|
|
<tr><td><code>pvc.spec.resources.requests.storage</code></td><td>必填,无默认</td><td><code>8Gi</code></td><td>在 <code>local-path</code> 上只是账面条目,不是配额</td></tr>
|
|||
|
|
<tr><td><code>StorageClass.allowVolumeExpansion</code></td><td><code>false</code></td><td>未设</td><td>为 false 时 <code>patch</code> 直接被 API 拒绝,不是权限问题</td></tr>
|
|||
|
|
<tr><td><code>volumeClaimTemplates</code></td><td>不可变</td><td>—</td><td>改它会被 <code>Forbidden</code> 挡住,扩容不能靠改 StatefulSet</td></tr>
|
|||
|
|
<tr><td><code>argocd</code> 的 <code>syncPolicy.automated</code></td><td>未设(手动)</td><td><code>true</code></td><td>冻结时置 false,解冻时恢复;只管 Git 同步</td></tr>
|
|||
|
|
<tr><td><code>kubectl get pvc</code> 的 <code>CAPACITY</code></td><td>—</td><td><code>8Gi</code></td><td>它不会因为节点盘满而变,别拿它当告警依据</td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="box warn">
|
|||
|
|
<span class="bt">易错点 · 8Gi 的 PVC 把 30GiB 的节点写满了</span>
|
|||
|
|
<p><b>现象</b>:<code>kubectl get pvc</code> 一直显示 <code>8Gi</code> / <code>Bound</code>,没有任何异常事件;节点上 <code>df -h /</code> 却是 <code>100%</code>。</p>
|
|||
|
|
<p><b>原因</b>:<code>local-path</code> 把 PV 做成宿主机上的一个目录,不做文件系统配额,8Gi 只写在 API 里。</p>
|
|||
|
|
<p><b>怎么验证</b>:在 <code>wk1</code> 上 <code>sudo du -sh /opt/local-path-provisioner/pvc-*</code>,与 <code>df -h /</code> 对比。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<details>
|
|||
|
|
<summary>可跳过 · 为什么 <code>for</code> 不能为了降噪随便调大</summary>
|
|||
|
|
<p><code>for</code> 是拿发现时间换误报率的旋钮。调到 <code>20m</code>,误报少了,但真实 P1 也要跑满 20 分钟才叫你——这次事故总共 35 分钟,等你接电话,事故已经结束。</p>
|
|||
|
|
<p>合理的做法是分两级:<code>warning</code> 用 <code>for: 5m</code> 进群,<code>critical</code> 用 <code>for: 2m</code> 打电话。</p>
|
|||
|
|
</details>
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-6">23.6 动手:从告警到归档</h3>
|
|||
|
|
|
|||
|
|
<p>下面六步按事故时钟走,演练从 01:41 开始。命令都在 <code>~/work/aurora-shop</code> 下执行,第一步的失败是设计好的。</p>
|
|||
|
|
|
|||
|
|
<h4>Step 1 · 注入:把 wk1 的盘写满</h4>
|
|||
|
|
|
|||
|
|
<p><span class="tag">意图</span>用十分钟复制出「三个月的 outbox 堆积」,让报错原文与真实事故一字不差。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="写满节点盘,再触发一次真实写入"># env: all
|
|||
|
|
# 影响:wk1 根盘写满,postgres 立刻不可写;fill.bin 是演练占位文件,Step 5 会删掉
|
|||
|
|
kubectl -n shop exec postgres-0 -- dd if=/dev/zero of=/var/lib/postgresql/data/fill.bin bs=1M count=20000
|
|||
|
|
kubectl -n shop exec postgres-0 -- psql -U shop -d shop -c \
|
|||
|
|
"insert into orders (user_id, amount, status) select g % 97, 19.90, 'pending' from generate_series(1, 5000) g"</pre>
|
|||
|
|
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
dd: error writing '/var/lib/postgresql/data/fill.bin': No space left on device
|
|||
|
|
20000+0 records in
|
|||
|
|
19999+0 records out
|
|||
|
|
ERROR: could not extend file "base/16384/16400": No space left on device
|
|||
|
|
HINT: Check free disk space.</pre>
|
|||
|
|
|
|||
|
|
<p>第一条命令失败就对了,那是注入成功的标志。第二条把 postgres 推到 <code>PANIC</code>:WAL 写不进去,postmaster 选择自杀,而不是带着写坏的页继续跑。</p>
|
|||
|
|
|
|||
|
|
<div class="box warn">
|
|||
|
|
<span class="bt">易错点 · 注入之后 psql 连不上</span>
|
|||
|
|
<p><b>现象</b>:<code>psql</code> 报 <code>connection refused</code>,<code>postgres-0</code> 变成 <code>CrashLoopBackOff</code>,<code>RESTARTS</code> 一路涨。</p>
|
|||
|
|
<p><b>原因</b>:磁盘 100% 时 WAL 写失败会触发 <code>PANIC</code>,容器退出后被拉起,重启又要写 WAL,于是循环。</p>
|
|||
|
|
<p><b>怎么验证</b>:<code>kubectl -n shop logs postgres-0 --previous --tail=20 | grep -i panic</code>,再回节点看 <code>df</code>;两条都对上就别再重启,去腾空间。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h4>Step 2 · 接警:先读告警,不读日志</h4>
|
|||
|
|
|
|||
|
|
<p><span class="tag">意图</span>日志告诉你「怎么坏的」,告警告诉你「坏了多久、影响多大」。凌晨先要后者。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="读规则状态与活跃告警"># env: all
|
|||
|
|
kubectl -n platform port-forward svc/kps-kube-prometheus-stack-prometheus 9090:9090 &
|
|||
|
|
sleep 3
|
|||
|
|
curl -s localhost:9090/api/v1/rules \
|
|||
|
|
| jq -r '.data.groups[].rules[] | select(.name=="ShopApiHighErrorRate") | [.state, .duration, .alerts[0].activeAt] | @tsv'
|
|||
|
|
curl -s localhost:9090/api/v1/alerts | jq -r '.data.alerts[] | [.labels.alertname, .state] | @tsv'</pre>
|
|||
|
|
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
firing 120 2026-11-13T01:43:30Z
|
|||
|
|
ShopApiHighErrorRate firing</pre>
|
|||
|
|
|
|||
|
|
<p><code>duration</code> 是 120 秒,说明 <code>for: 2m</code> 刚走满;<code>activeAt</code> 是 01:43:30,比电话早 90 秒。这 90 秒里 30 秒是 <code>group_wait</code>,剩下是路由与电话网关——「告警延迟」都藏在这两个数字之间。</p>
|
|||
|
|
|
|||
|
|
<p>再看 Alertmanager 那一侧,它只认 <code>firing</code> 与 <code>resolved</code> 两端:</p>
|
|||
|
|
|
|||
|
|
<div class="grid2">
|
|||
|
|
<div class="box">
|
|||
|
|
<h4>Windows 11 + WSL2</h4>
|
|||
|
|
<pre data-lang="powershell" data-name="转发后在浏览器里看"># env: windows-ps
|
|||
|
|
kubectl -n platform port-forward svc/kps-kube-prometheus-stack-alertmanager 9093:9093
|
|||
|
|
# 保持这个窗口,另开浏览器访问 http://localhost:9093/#/alerts</pre>
|
|||
|
|
<p>浏览器里能看分组、静默与 silence 按钮,适合判断「是不是有人压过告警」。</p>
|
|||
|
|
</div>
|
|||
|
|
<div class="box">
|
|||
|
|
<h4>Linux (Ubuntu 24.04)</h4>
|
|||
|
|
<pre data-lang="bash" data-name="curl 直接取活跃告警"># env: linux
|
|||
|
|
kubectl -n platform port-forward svc/kps-kube-prometheus-stack-alertmanager 9093:9093 &
|
|||
|
|
sleep 2
|
|||
|
|
curl -s localhost:9093/api/v2/alerts | jq -r '.[] | [.labels.alertname, .status.state, .startsAt] | @tsv'</pre>
|
|||
|
|
<p>输出可以直接贴进事故频道,带时间戳,事后能当证据。</p>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
<p><b>差异结论</b>:转发命令两边一样,差别在取证方式——浏览器能看分组与静默,<code>curl</code> 的输出能原样贴进事故频道;值班记录要后者。</p>
|
|||
|
|
|
|||
|
|
<details>
|
|||
|
|
<summary>可跳过 · firing 之前那个 pending 到底是什么</summary>
|
|||
|
|
<p>Prometheus 每次求值都给告警一个状态:表达式为假是 <code>inactive</code>,为真但没跑满 <code>for</code> 是 <code>pending</code>,跑满才 <code>firing</code>。中间只要有一次为假,计时器归零。</p>
|
|||
|
|
<p>所以「告警闪了一下又没了」通常不是规则坏了,是那一分钟的样本没够。</p>
|
|||
|
|
</details>
|
|||
|
|
|
|||
|
|
<h4>Step 3 · 分级与冻结变更</h4>
|
|||
|
|
|
|||
|
|
<p><span class="tag">意图</span>用两分钟决定「叫不叫第二个人」,然后立刻把变更入口关上。这次核心链路全挂、没有绕行,定 P1。</p>
|
|||
|
|
|
|||
|
|
<div class="tablewrap">
|
|||
|
|
<table>
|
|||
|
|
<thead><tr><th>级别</th><th>判定</th><th>响应</th><th>沟通节奏</th><th>本章对应</th></tr></thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td>P1</td><td>核心链路全挂或无绕行,收入受影响</td><td>立刻接警,5 分钟内首报,叫上第二个人</td><td>每 15 分钟一条进展</td><td>本次:下单 100% 失败</td></tr>
|
|||
|
|
<tr><td>P2</td><td>部分用户或部分功能受影响,有绕行</td><td>30 分钟内响应,工作时间处理</td><td>每小时一条</td><td>例如只有搜索慢</td></tr>
|
|||
|
|
<tr><td>P3</td><td>无用户感知,只是指标异常</td><td>次日处理,进 backlog</td><td>不需要</td><td>例如磁盘 70% 预警</td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<p>级别定完,先冻结变更——现场最贵的错误是一边救火一边有人发布:</p>
|
|||
|
|
|
|||
|
|
<p><code>argocd app set shop-prod --sync-policy none</code>——<code>--sync-policy</code> 只认 <code>manual</code>、<code>none</code>、<code>automated</code> 这几个词,写 <code>automated=false</code> 会直接报 <code>unknown sync policy</code>。再用 <code>argocd app get shop-prod -o json</code> 确认 <code>syncPolicy.automated</code> 是 <code>null</code>。解冻时要连 <code>--self-heal</code> 一起加回来。</p>
|
|||
|
|
|
|||
|
|
<p>冻结只挡 Git 同步。HPA 照样扩缩,<code>kubectl edit</code> 照样生效——冻结的是变更来源,不是集群。</p>
|
|||
|
|
|
|||
|
|
<p>然后是首报,三句话,不猜根因:</p>
|
|||
|
|
|
|||
|
|
<ul>
|
|||
|
|
<li><b>影响</b>:<code>shop.example.com</code> 下单全部 500,浏览正常,从 01:41 开始。</li>
|
|||
|
|
<li><b>已知</b>:不是发布导致的,digest 六小时未变;数据库报磁盘写满。</li>
|
|||
|
|
<li><b>下一步</b>:正在确认节点盘容量,五分钟后再报。</li>
|
|||
|
|
</ul>
|
|||
|
|
|
|||
|
|
<p>收尾在恢复后十分钟内发,写解封时间、残留影响与下次更新时间。</p>
|
|||
|
|
|
|||
|
|
<h4>Step 4 · 定位:在节点上看盘</h4>
|
|||
|
|
|
|||
|
|
<p><span class="tag">意图</span>把「发布」这个嫌疑彻底排除,再去盘上找证据。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="节点上看盘,而不是容器里"># env: all
|
|||
|
|
multipass exec wk1 -- df -h /
|
|||
|
|
multipass exec wk1 -- sudo du -sh /opt/local-path-provisioner/pvc-*/
|
|||
|
|
kubectl -n shop get pvc
|
|||
|
|
kubectl -n shop logs postgres-0 --previous --tail=3</pre>
|
|||
|
|
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
Filesystem Size Used Avail Use% Mounted on
|
|||
|
|
/dev/sda1 30G 29G 0 100% /
|
|||
|
|
|
|||
|
|
9.8G /opt/local-path-provisioner/pvc-3f1c9a2e-8d41-4a0e-9a77-1f2c5b6d9e10_shop_data-postgres-0
|
|||
|
|
|
|||
|
|
NAME STATUS VOLUME CAPACITY
|
|||
|
|
data-postgres-0 Bound pvc-3f1c9a2e 8Gi
|
|||
|
|
|
|||
|
|
PANIC: could not write to file "pg_wal/00000001000000000000004A": No space left on device</pre>
|
|||
|
|
|
|||
|
|
<p><code>df</code> 在节点上是 100%,PVC 却还是 8Gi / Bound。这两个数字放在一起就是本章第一个判断:<b>PVC 的容量不是配额</b>。所谓「PVC 写满」是口头说法,准确说是节点盘满了。</p>
|
|||
|
|
|
|||
|
|
<p>所以 <code>df</code> 必须在节点上跑:容器里不带路径的 <code>df -h</code> 看到的是 overlay(镜像层),永远显示还有十几 G。扩不动时路只剩两条,见图 2。</p>
|
|||
|
|
|
|||
|
|
<figure class="figure">
|
|||
|
|
<svg viewBox="0 0 720 300" role="img" aria-label="扩容决策树:local-path 被 API 拒绝,云盘类可以在线扩容">
|
|||
|
|
<defs>
|
|||
|
|
<marker id="a23b" 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="290" y="8" width="140" height="36" rx="6" style="fill:var(--card);stroke:var(--line)"></rect>
|
|||
|
|
<text x="360" y="31" text-anchor="middle" font-size="13" style="fill:var(--ink)">PVC 写满,想扩容</text>
|
|||
|
|
<line x1="360" y1="44" x2="360" y2="60" style="stroke:var(--muted)" marker-end="url(#a23b)"></line>
|
|||
|
|
|
|||
|
|
<polygon points="360,64 476,104 360,144 244,104" style="fill:var(--card);stroke:var(--line)"></polygon>
|
|||
|
|
<text x="360" y="99" text-anchor="middle" font-size="12" style="fill:var(--ink)">StorageClass 支持 resize</text>
|
|||
|
|
<text x="360" y="115" text-anchor="middle" font-size="11" style="fill:var(--muted)">且节点还有空间?</text>
|
|||
|
|
|
|||
|
|
<polyline points="244,104 152,104 152,150" style="fill:none;stroke:var(--muted)" marker-end="url(#a23b)"></polyline>
|
|||
|
|
<text x="196" y="96" text-anchor="middle" font-size="11" style="fill:var(--danger)">否</text>
|
|||
|
|
<rect x="56" y="152" width="192" height="66" rx="8" style="fill:var(--danger-bg);stroke:var(--danger)"></rect>
|
|||
|
|
<text x="152" y="176" text-anchor="middle" font-size="13" style="fill:var(--danger)">local-path:API 拒绝</text>
|
|||
|
|
<text x="152" y="196" text-anchor="middle" font-size="11" style="fill:var(--ink)">only dynamically provisioned</text>
|
|||
|
|
<text x="152" y="211" text-anchor="middle" font-size="11" style="fill:var(--ink)">pvc can be resized</text>
|
|||
|
|
|
|||
|
|
<polyline points="476,104 566,104 566,150" style="fill:none;stroke:var(--muted)" marker-end="url(#a23b)"></polyline>
|
|||
|
|
<text x="520" y="96" text-anchor="middle" font-size="11" style="fill:var(--ok)">是</text>
|
|||
|
|
<rect x="470" y="152" width="192" height="66" rx="8" style="fill:var(--ok-bg);stroke:var(--ok)"></rect>
|
|||
|
|
<text x="566" y="176" text-anchor="middle" font-size="13" style="fill:var(--ink)">云盘类:在线扩容</text>
|
|||
|
|
<text x="566" y="196" text-anchor="middle" font-size="11" style="fill:var(--muted)">1–2 分钟生效</text>
|
|||
|
|
<text x="566" y="211" text-anchor="middle" font-size="11" style="fill:var(--muted)">不用停库</text>
|
|||
|
|
|
|||
|
|
<line x1="152" y1="218" x2="152" y2="246" style="stroke:var(--muted)" marker-end="url(#a23b)"></line>
|
|||
|
|
<rect x="56" y="248" width="192" height="40" rx="6" style="fill:var(--card);stroke:var(--line)"></rect>
|
|||
|
|
<text x="152" y="266" text-anchor="middle" font-size="11" style="fill:var(--ink)">删旧数据 + VACUUM</text>
|
|||
|
|
<text x="152" y="281" text-anchor="middle" font-size="11" style="fill:var(--ink)">或换更大卷迁移</text>
|
|||
|
|
|
|||
|
|
<text x="576" y="272" text-anchor="middle" font-size="11" style="fill:var(--muted)">本章这条路不存在</text>
|
|||
|
|
</svg>
|
|||
|
|
<div class="figcap"><b>图 2 ·</b>左边是本章走的路:不是配置没开,是本地盘没有「更大」可以加。</div>
|
|||
|
|
</figure>
|
|||
|
|
|
|||
|
|
<h4>Step 5 · 止损:扩不了,就删或换</h4>
|
|||
|
|
|
|||
|
|
<p><span class="tag">意图</span>先判断能不能扩;再按「哪个能让服务先起来」排序。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="先撞墙,再止损"># env: all
|
|||
|
|
kubectl patch pvc data-postgres-0 -n shop -p '{"spec":{"resources":{"requests":{"storage":"16Gi"}}}}'
|
|||
|
|
multipass exec wk1 -- sudo rm -f /opt/local-path-provisioner/pvc-*/fill.bin
|
|||
|
|
kubectl -n shop rollout status sts/postgres --timeout=120s
|
|||
|
|
kubectl -n shop exec postgres-0 -- df -h /var/lib/postgresql/data
|
|||
|
|
kubectl -n shop exec postgres-0 -- psql -U shop -d shop -c "delete from outbox where sent_at < now() - interval '7 days'"
|
|||
|
|
kubectl -n shop exec postgres-0 -- df -h /var/lib/postgresql/data
|
|||
|
|
kubectl -n shop exec postgres-0 -- psql -U shop -d shop -c "vacuum full outbox"
|
|||
|
|
kubectl -n shop exec postgres-0 -- df -h /var/lib/postgresql/data</pre>
|
|||
|
|
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
error: persistentvolumeclaims "data-postgres-0" is forbidden: only dynamically provisioned pvc can be resized and the storageclass that provisions the pvc must support resize
|
|||
|
|
statefulset rolling update complete 1 pods...
|
|||
|
|
Filesystem Size Used Avail Use% Mounted on
|
|||
|
|
/dev/sda1 30G 20G 9.4G 69% /
|
|||
|
|
DELETE 18432
|
|||
|
|
Filesystem Size Used Avail Use% Mounted on
|
|||
|
|
/dev/sda1 30G 20G 9.4G 69% /
|
|||
|
|
VACUUM
|
|||
|
|
Filesystem Size Used Avail Use% Mounted on
|
|||
|
|
/dev/sda1 30G 13G 16G 45% /</pre>
|
|||
|
|
|
|||
|
|
<p>第一条就被 API 拒绝。不是权限问题,是 <code>local-path</code> 的 StorageClass 没开 <code>allowVolumeExpansion</code>——就算开了也没用,本地盘没有「更大」可加。改 <code>volumeClaimTemplates</code> 同样走不通,那个字段不可变。</p>
|
|||
|
|
|
|||
|
|
<p>于是只剩两条路:删数据,或换卷。半夜选前者,且先删占位文件让 postgres 能启动,再谈业务数据。</p>
|
|||
|
|
|
|||
|
|
<div class="box warn">
|
|||
|
|
<span class="bt">易错点 · DELETE 报了一万八千行,df 一点没降</span>
|
|||
|
|
<p><b>现象</b>:<code>DELETE 18432</code> 之后 <code>df</code> 还是 <code>69%</code>,看起来像命令没生效。</p>
|
|||
|
|
<p><b>原因</b>:<code>DELETE</code> 只把行标成死元组,页还留在数据文件里;MVCC 要让老事务看见旧版本,空间不能立刻还给操作系统。</p>
|
|||
|
|
<p><b>怎么验证</b>:对比 <code>pg_total_relation_size('outbox')</code> 前后,只有 <code>VACUUM FULL</code> 才会真的缩小。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<p>还有一层:磁盘 100% 时 <code>VACUUM FULL</code> 自己也要空间,会二次失败。先腾出能重写这张表的窗口再 vacuum,止损要按「先能跑、再干净」排。见图 3,告警会自己走完三个状态,收尾要等它落到 <code>inactive</code>。</p>
|
|||
|
|
|
|||
|
|
<figure class="figure">
|
|||
|
|
<svg viewBox="0 0 720 220" role="img" aria-label="告警状态机:pending 到 firing 到 inactive,以及本次事故的时间戳">
|
|||
|
|
<defs>
|
|||
|
|
<marker id="a23c" 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>
|
|||
|
|
<circle cx="140" cy="90" r="46" style="fill:var(--card);stroke:var(--line)" stroke-width="1.5"></circle>
|
|||
|
|
<text x="140" y="86" text-anchor="middle" font-size="14" style="fill:var(--ink)">pending</text>
|
|||
|
|
<text x="140" y="105" text-anchor="middle" font-size="11" style="fill:var(--muted)">for 未走满</text>
|
|||
|
|
<text x="140" y="160" text-anchor="middle" font-size="11" style="fill:var(--muted)">01:43:00</text>
|
|||
|
|
|
|||
|
|
<circle cx="360" cy="90" r="46" style="fill:var(--danger-bg);stroke:var(--danger)" stroke-width="1.5"></circle>
|
|||
|
|
<text x="360" y="86" text-anchor="middle" font-size="14" style="fill:var(--danger)">firing</text>
|
|||
|
|
<text x="360" y="105" text-anchor="middle" font-size="11" style="fill:var(--ink)">通知已发</text>
|
|||
|
|
<text x="360" y="160" text-anchor="middle" font-size="11" style="fill:var(--muted)">01:43:30</text>
|
|||
|
|
|
|||
|
|
<circle cx="580" cy="90" r="46" style="fill:var(--ok-bg);stroke:var(--ok)" stroke-width="1.5"></circle>
|
|||
|
|
<text x="580" y="86" text-anchor="middle" font-size="14" style="fill:var(--ink)">inactive</text>
|
|||
|
|
<text x="580" y="105" text-anchor="middle" font-size="11" style="fill:var(--muted)">恢复</text>
|
|||
|
|
<text x="580" y="160" text-anchor="middle" font-size="11" style="fill:var(--muted)">02:16:00</text>
|
|||
|
|
|
|||
|
|
<line x1="186" y1="90" x2="308" y2="90" style="stroke:var(--muted)" marker-end="url(#a23c)"></line>
|
|||
|
|
<text x="247" y="74" text-anchor="middle" font-size="11" style="fill:var(--brand)">for: 2m 走满</text>
|
|||
|
|
<line x1="406" y1="90" x2="528" y2="90" style="stroke:var(--muted)" marker-end="url(#a23c)"></line>
|
|||
|
|
<text x="467" y="74" text-anchor="middle" font-size="11" style="fill:var(--brand)">表达式为假</text>
|
|||
|
|
<text x="467" y="110" text-anchor="middle" font-size="10" style="fill:var(--muted)">resolve_timeout 5m</text>
|
|||
|
|
|
|||
|
|
<text x="360" y="200" text-anchor="middle" font-size="11" style="fill:var(--muted)">本次 pending 只活了 30 秒——因为 500 从 01:41 就开始了</text>
|
|||
|
|
</svg>
|
|||
|
|
<div class="figcap"><b>图 3 ·</b>三个状态各有责任:<code>pending</code> 是规则在攒证据,<code>firing</code> 才是叫人的许可。</div>
|
|||
|
|
</figure>
|
|||
|
|
|
|||
|
|
<h4>Step 6 · 归档:runbook 与 postmortem 进 Git</h4>
|
|||
|
|
|
|||
|
|
<p><span class="tag">意图</span>服务回来了,事故还没结束。剩下二十五分钟最有价值的,是把刚才的判断写成别人能用的东西。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="text" data-name="runbooks/p1-pvc-full.md"># file: runbooks/p1-pvc-full.md
|
|||
|
|
# P1 · PVC 写满导致下单 500
|
|||
|
|
|
|||
|
|
## 触发条件
|
|||
|
|
- ShopApiHighErrorRate 连续 firing ≥ 2m,浏览正常而 /orders 返回 500
|
|||
|
|
- 或 postgres 日志出现 could not extend file 与 No space left on device
|
|||
|
|
|
|||
|
|
## 前 5 分钟
|
|||
|
|
1. 认领:在事故频道说「我在看」,记下令牌时间
|
|||
|
|
2. 确认影响:curl -s -o /dev/null -w '%{http_code}' https://shop.example.com/orders
|
|||
|
|
3. 冻结变更:argocd app set shop-prod --sync-policy none
|
|||
|
|
4. 看节点盘:multipass exec wk1 -- df -h /
|
|||
|
|
|
|||
|
|
## 诊断命令
|
|||
|
|
kubectl -n shop get pod,pvc
|
|||
|
|
kubectl -n shop logs postgres-0 --previous --tail=30
|
|||
|
|
multipass exec wk1 -- sudo du -sh /opt/local-path-provisioner/pvc-*
|
|||
|
|
kubectl -n shop exec postgres-0 -- psql -U shop -d shop -c "select pg_size_pretty(pg_total_relation_size('outbox'))"
|
|||
|
|
|
|||
|
|
## 回滚与升级路径
|
|||
|
|
- 扩卷不可行:local-path 无 allowVolumeExpansion,API 报 only dynamically provisioned pvc
|
|||
|
|
- 一级:删 7 天前的 outbox 行,再 vacuum full 把空间还给文件系统
|
|||
|
|
- 二级:新建更大的 PVC,用 pg_basebackup 迁移,RTO 约 40 分钟
|
|||
|
|
- 三级:升级到平台组,走 docs/prr.md 的备份恢复路径</pre>
|
|||
|
|
|
|||
|
|
<pre data-lang="text" data-name="docs/postmortem-2026-11-13.md"># file: docs/postmortem-2026-11-13.md
|
|||
|
|
# 2026-11-13 下单 500 · P1 复盘
|
|||
|
|
|
|||
|
|
## 时间线
|
|||
|
|
01:41 wk1 根盘写满,postgres 首次报 could not extend file
|
|||
|
|
01:43 ShopApiHighErrorRate firing(for: 2m 走满)
|
|||
|
|
01:45 值班接警;01:48 确认影响面
|
|||
|
|
01:52 首报发出;01:56 节点 df 确认为 100%
|
|||
|
|
02:00 尝试扩 PVC,被 API 拒绝
|
|||
|
|
02:03 删除演练占位文件,postgres 恢复
|
|||
|
|
02:09 delete outbox 7 天前数据(18432 行)
|
|||
|
|
02:11 vacuum full 完成,df 回到 45%
|
|||
|
|
02:14 5xx 归零;02:16 告警 inactive;02:21 宣布恢复
|
|||
|
|
|
|||
|
|
## 根因
|
|||
|
|
直接原因:wk1 根盘写满,postgres 无法扩展数据文件。
|
|||
|
|
根本原因:没有任何容量告警;PVC 的 8Gi 不被 local-path enforce,团队以为有配额。
|
|||
|
|
|
|||
|
|
## 影响面
|
|||
|
|
35 分钟,1187 次下单失败,0 笔重复扣款;浏览与搜索不受影响。
|
|||
|
|
|
|||
|
|
## 行动项
|
|||
|
|
| 行动 | owner | deadline |
|
|||
|
|
| --- | --- | --- |
|
|||
|
|
| 加 node_filesystem_avail 告警(低于 20% 预警,低于 10% 打电话) | @lin | 2026-11-20 |
|
|||
|
|
| outbox 加 30 天清理 Job,进 deploy/base | @zhou | 2026-11-27 |
|
|||
|
|
| 把「扩卷不可行」写进 runbook 第一段并演练一次 | @lin | 2026-11-27 |</pre>
|
|||
|
|
|
|||
|
|
<p>两份都进 Git,跟着下一次 Argo 同步走。文档不进 Git 等于没写,因为下一个值班的人不会翻聊天记录。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="提交两份文档"># env: all
|
|||
|
|
git add runbooks/p1-pvc-full.md docs/postmortem-2026-11-13.md
|
|||
|
|
git commit -m "docs: 2026-11-13 下单 500 复盘与 pvc-full runbook"
|
|||
|
|
git log --oneline -2</pre>
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-7">23.7 验收清单与 make verify-ch23</h3>
|
|||
|
|
|
|||
|
|
<p>五条断言加一条命令。</p>
|
|||
|
|
|
|||
|
|
<ol class="steps">
|
|||
|
|
<li>告警曾 <code>firing</code>,现在 <code>inactive</code>。</li>
|
|||
|
|
<li><code>shop-api</code> 的 5xx 归零:<code>sum(rate(http_requests_total{status=~"5.."}[5m]))</code> 为 0。</li>
|
|||
|
|
<li><code>runbooks/p1-pvc-full.md</code> 四段齐全。</li>
|
|||
|
|
<li><code>docs/postmortem-2026-11-13.md</code> 含时间线、根因、三条带 owner 的行动项。</li>
|
|||
|
|
<li><code>orders</code> 仍是 12 行,没被清空。</li>
|
|||
|
|
</ol>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="一次跑完"># env: all
|
|||
|
|
make verify-ch23</pre>
|
|||
|
|
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
[ok] alert ShopApiHighErrorRate fired at 2026-11-13T01:43:30Z and is inactive now
|
|||
|
|
[ok] shop-api 5xx rate is 0 over the last 5m
|
|||
|
|
[ok] runbooks/p1-pvc-full.md has 4 sections
|
|||
|
|
[ok] docs/postmortem-2026-11-13.md has timeline, root cause and 3 action items
|
|||
|
|
[ok] orders table still has 12 rows</pre>
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-8">23.8 故障实验室:三种「以为修好了」</h3>
|
|||
|
|
|
|||
|
|
<p>三个故障都不是配置写错,是判断错;判断错的代价是整个团队一起错。</p>
|
|||
|
|
|
|||
|
|
<div class="box danger">
|
|||
|
|
<span class="bt">故障 1 · 磁盘满了,所有人都在扩 PVC</span>
|
|||
|
|
<p><b>注入</b>:Step 1 之后不止损,直接 <code>kubectl patch pvc data-postgres-0 -n shop -p '{"spec":{"resources":{"requests":{"storage":"16Gi"}}}}'</code>,再 <code>kubectl -n shop edit sts postgres</code> 把 <code>volumeClaimTemplates</code> 里的 8Gi 改成 16Gi。</p>
|
|||
|
|
<p><b>症状</b>:PVC 的 patch 报 <code>only dynamically provisioned pvc can be resized…</code>;改 StatefulSet 则报 <code>Forbidden: updates to statefulset spec…</code>。两条路都堵死,Pod 还在 <code>CrashLoopBackOff</code>。</p>
|
|||
|
|
<p><b>诊断</b>:看 <code>CAPACITY</code> 有没有变、Events 里有没有 <code>Resizing</code>、模板里的数字有没有被改。</p>
|
|||
|
|
<pre data-lang="bash" data-name="三条命令确认扩容是死路"># env: all
|
|||
|
|
kubectl -n shop get pvc
|
|||
|
|
kubectl -n shop describe pvc data-postgres-0 | grep -A3 Events
|
|||
|
|
kubectl -n shop get sts postgres -o jsonpath='{.spec.volumeClaimTemplates[0].spec.resources.requests.storage}{"\n"}'</pre>
|
|||
|
|
<p><b>根因</b>:<code>local-path</code> 的 PV 是宿主机目录,StorageClass 没开 <code>allowVolumeExpansion</code>,开了也没有更大的本地盘;<code>volumeClaimTemplates</code> 在 StatefulSet 里不可变,改它只能重建。</p>
|
|||
|
|
<p><b>修复</b>:删历史数据并 <code>VACUUM FULL</code>,或新建更大 PVC 用 <code>pg_basebackup</code> 迁移;本章走第一条。</p>
|
|||
|
|
<p><b>预防</b>:容量告警按节点盘做,不按 PVC 的 <code>CAPACITY</code> 做;runbook 第一段就写「扩卷不可行」。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="box danger">
|
|||
|
|
<span class="bt">故障 2 · 事故处理完了,告警一次都没响</span>
|
|||
|
|
<p><b>注入</b>:<code>kubectl -n platform patch prometheusrule shop-api-rules --type=json -p '[{"op":"replace","path":"/spec/groups/0/rules/0/for","value":"20m"}]'</code>,再重跑 Step 1。</p>
|
|||
|
|
<p><b>症状</b>:01:41 开始 500,01:55 值班的人还在睡;规则一直 <code>pending</code>,<code>duration</code> 涨到 840 秒,<code>alerts</code> 数组是空的。</p>
|
|||
|
|
<p><b>诊断</b>:把 <code>for</code> 与运行态两个数字并排看,对不上就是它。</p>
|
|||
|
|
<pre data-lang="bash" data-name="读 for 与运行态"># env: all
|
|||
|
|
kubectl -n platform get prometheusrule shop-api-rules -o jsonpath='{.spec.groups[0].rules[0].for}{"\n"}'
|
|||
|
|
curl -s localhost:9090/api/v1/rules | jq -r '.data.groups[].rules[] | select(.name=="ShopApiHighErrorRate") | [.state,.duration] | @tsv'
|
|||
|
|
curl -s localhost:9090/api/v1/alerts | jq '.data.alerts | length'</pre>
|
|||
|
|
<p><b>根因</b>:有人为了降噪把 <code>for</code> 从 2m 调到 20m,告警窗口比事故还长。另一种是 Alertmanager 的 <code>route</code> 只匹配 <code>critical</code>,而这条是 <code>warning</code>——API 里有 firing,通知一个都没发。</p>
|
|||
|
|
<p><b>修复</b>:<code>for</code> 改回 2m 并 <code>apply</code>,观察 <code>pending</code> → <code>firing</code> 的完整迁移;路由侧给 <code>warning</code> 加分支,或加 <code>continue: true</code>。</p>
|
|||
|
|
<p><b>预防</b>:PrometheusRule 进 Git、改动走 PR;每月做一次静默注入演练。只验证「规则存在」是不够的。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="box danger">
|
|||
|
|
<span class="bt">故障 3 · 止损把待处理订单删了</span>
|
|||
|
|
<p><b>注入</b>:在演练库 <code>shop_drill</code> 里执行 <code>delete from outbox;</code>——漏了 <code>WHERE</code>。先 <code>create database shop_drill template shop</code>,别在 <code>shop</code> 上做,否则要重走第 22 章的恢复。</p>
|
|||
|
|
<p><b>症状</b>:5xx 归零、告警恢复,但 <code>outbox</code> 行数是 0;37 笔已付款订单没进仓库,次日 09:00 客服开始接到「我的订单没发货」。</p>
|
|||
|
|
<p><b>诊断</b>:对比主表与事件表的计数,再看删除时间点。</p>
|
|||
|
|
<pre data-lang="bash" data-name="确认删过头了"># env: all
|
|||
|
|
kubectl -n shop exec postgres-0 -- psql -U shop -d shop_drill -c "select count(*) from outbox"
|
|||
|
|
kubectl -n shop exec postgres-0 -- psql -U shop -d shop_drill -c "select count(*) from orders where status='pending'"
|
|||
|
|
kubectl -n shop exec postgres-0 -- psql -U shop -d shop_drill -c "select n_tup_del, n_live_tup from pg_stat_user_tables where relname='outbox'"</pre>
|
|||
|
|
<p><b>根因</b>:止损时手比脑快,SQL 没写 <code>WHERE</code>,也没先 <code>SELECT</code> 预演;<code>outbox</code> 没有软删列,删掉就真的没了。</p>
|
|||
|
|
<p><b>修复</b>:从第 22 章的 <code>shop-full</code> 备份恢复到临时库,导出 24 小时内的 outbox 行补回;靠 <code>outbox.id</code> 幂等,不重复发。</p>
|
|||
|
|
<p><b>预防</b>:止损 SQL 固定写成 <code>begin; select count(*) …; delete … where …; rollback;</code>,看清行数再改 <code>commit</code>;on-call 账号不给全表 <code>DELETE</code>(第 19 章的 RBAC 正好用上)。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-9">23.9 生产对照</h3>
|
|||
|
|
|
|||
|
|
<p>每一步生产上都有人做过,差别在工具,不在顺序。</p>
|
|||
|
|
|
|||
|
|
<div class="tablewrap">
|
|||
|
|
<table>
|
|||
|
|
<thead><tr><th>维度</th><th>本章的做法</th><th>生产上的做法</th><th>为什么</th></tr></thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td>告警到人</td><td>Alertmanager 发到聊天群,靠人盯</td><td>值班表 + 升级链:5 分钟无应答自动叫第二个人</td><td>告警发给「一群人」等于没发;只有指向具体的人,才有应答时间可度量</td></tr>
|
|||
|
|
<tr><td>止损权限</td><td>一个 <code>cluster-admin</code> 一路做到底</td><td>预置 break-glass 角色,用完即回收,操作全审计</td><td>止损要快,但不能让「快」变成永久权限;审计日志是复盘唯一的客观证据</td></tr>
|
|||
|
|
<tr><td>容量</td><td>8Gi 的 <code>local-path</code>,写满才知道</td><td>云盘可在线扩容,配 20% / 10% 两级容量告警</td><td>本地盘没有扩容这个概念,只能提前告警;云盘能扩,但也要留出扩容生效的时间窗</td></tr>
|
|||
|
|
<tr><td>runbook</td><td>仓库里的 Markdown</td><td>告警正文里带 <code>runbook_url</code>,点开就是处置步骤</td><td>凌晨三点没人会去翻仓库目录;入口放在告警上,才有人真的用</td></tr>
|
|||
|
|
<tr><td>复盘</td><td>一份文档,三条行动项</td><td>blameless 复盘 + 行动项进 issue 跟踪,两周后复查关闭率</td><td>不复查的行动项等于许愿;关闭率才是复盘有没有用的唯一指标</td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-10">23.10 清理与残留检查</h3>
|
|||
|
|
|
|||
|
|
<p>演练对象要清,文档要留。先解冻,再查残留。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="解冻并检查残留"># env: all
|
|||
|
|
argocd app set shop-prod --sync-policy automated --self-heal
|
|||
|
|
kubectl -n shop exec postgres-0 -- psql -U shop -d shop -c "drop database if exists shop_drill"
|
|||
|
|
kubectl -n shop exec postgres-0 -- psql -U shop -d shop -c "select count(*) from orders"
|
|||
|
|
multipass exec wk1 -- df -h /
|
|||
|
|
kubectl -n shop get pvc,pod
|
|||
|
|
pkill -f "port-forward" || true</pre>
|
|||
|
|
|
|||
|
|
<p>预期:<code>automated.selfHeal</code> 回到 <code>true</code>;<code>orders</code> 仍是 12;<code>wk1</code> 的 <code>df</code> 是 <code>45%</code>;<code>data-postgres-0</code> 还是 <code>8Gi</code> / <code>Bound</code>;<code>postgres-0</code> 是 <code>1/1 Running</code>,<code>RESTARTS</code> 停在几次——那是注入的痕迹,不是故障。</p>
|
|||
|
|
|
|||
|
|
<p>还有两条不进命令:<code>fill.bin</code> 不在 PV 目录里;<code>ALERTS</code> 里不能再有 <code>firing</code> 样本,否则下次断言会被旧数据蒙混。</p>
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-11">23.11 小结与自测</h3>
|
|||
|
|
|
|||
|
|
<p class="take"><b>一句话:</b>前二十分钟的目标是让用户能用,不是把根因找出来。</p>
|
|||
|
|
<p class="take"><b>一句话:</b>告警的价值不看规则写得多漂亮,看它上次有没有叫对人。</p>
|
|||
|
|
<p class="take"><b>一句话:</b><code>local-path</code> 的 8Gi 是账面条目,写满的是节点盘。</p>
|
|||
|
|
<p class="take"><b>一句话:</b>扩不了的卷只有两条路:删数据,或换卷。</p>
|
|||
|
|
<p class="take"><b>一句话:</b>复盘产出的是行动项,不是责任人。</p>
|
|||
|
|
|
|||
|
|
<details>
|
|||
|
|
<summary>自测 3 题(点击展开答案)</summary>
|
|||
|
|
<p><b>1(概念)</b>同事说「PVC 写满了,扩一下就好」,你先回哪句?</p>
|
|||
|
|
<p>先问哪个 StorageClass。<code>local-path</code> 不支持 resize,而且它不做配额,真正满的是节点盘。</p>
|
|||
|
|
<p><b>2(排错)</b>错误率 90% 持续八分钟,规则却一直 <code>pending</code>,先看哪两个数?</p>
|
|||
|
|
<p>规则的 <code>for</code> 与运行态 <code>duration</code>。前者是 20m 就说明被人改过;两者都对但 <code>alerts</code> 为空,就去 Alertmanager 看路由有没有把 <code>warning</code> 丢掉。</p>
|
|||
|
|
<p><b>3(决策)</b>止损只能删一次数据,怎么保证不删错?</p>
|
|||
|
|
<p>事务里先 <code>select count(*)</code> 看清范围,再 <code>delete … where …</code>,行数对得上才 <code>commit</code>。</p>
|
|||
|
|
</details>
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-12">23.12 衔接</h3>
|
|||
|
|
|
|||
|
|
<p><b>回收</b>:第 13 章的 <code>for: 2m</code> 这次真的叫醒了人;第 22 章的备份是故障 3 唯一的退路;第 19 章的 RBAC 决定 on-call 能不能一把删空整张表;第 20 章的 Argo CD 提供了「冻结变更」。</p>
|
|||
|
|
|
|||
|
|
<p><b>钩子</b>:<code>shop-api</code> 的 <code>requests.cpu</code> 还是第 7 章拍下的 <code>500m</code>,这一夜它只用了 120m。<a href="24-cost.html">第 24 章</a>把这件事算成钱:用 <code>kubectl top</code> 与 <code>describe node</code> 收紧 requests,加 <code>ResourceQuota</code> 与 <code>LimitRange</code>,把装箱率推到 60% 以上。</p>
|
|||
|
|
|
|||
|
|
<h3 id="sec-23-13">23.13 参考与验证版本</h3>
|
|||
|
|
|
|||
|
|
<ul>
|
|||
|
|
<li><a href="https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/">Prometheus · Alerting rules(<code>for</code> 与状态机)</a></li>
|
|||
|
|
<li><a href="https://prometheus.io/docs/alerting/latest/configuration/">Alertmanager · Configuration(<code>group_wait</code> / <code>resolve_timeout</code>)</a></li>
|
|||
|
|
<li><a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#expanding-persistent-volumes-claims">Kubernetes · Expanding Persistent Volume Claims</a></li>
|
|||
|
|
<li><a href="https://github.com/rancher/local-path-provisioner">local-path-provisioner(为什么它不支持 resize)</a></li>
|
|||
|
|
<li><a href="https://sre.google/sre-book/postmortem-culture/">Google SRE Book · Postmortem Culture</a></li>
|
|||
|
|
</ul>
|
|||
|
|
|
|||
|
|
<p>验证版本:k8s v1.36.6 / kube-prometheus-stack(Prometheus 3.x 线)/ local-path-provisioner v0.0.37 / Argo CD 3.x · 最后验证:2026-11-13</p>
|
|||
|
|
</article>
|
|||
|
|
|
|||
|
|
<nav class="pager">
|
|||
|
|
<a class="prev" href="22-backup.html"><span class="k">← 上一章</span><span class="v">第 22 章 · 备份、恢复与升级</span></a>
|
|||
|
|
<a class="next" href="24-cost.html"><span class="k">下一章 →</span><span class="v">第 24 章 · 成本与容量</span></a>
|
|||
|
|
</nav>
|
|||
|
|
|
|||
|
|
<p class="foot">
|
|||
|
|
Kubernetes 专题 · S5 第 23 章 值班与事故响应<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>
|