2026-11-11 21:57:56 +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 > 第 18 章 节点、调度与容量 · Kubernetes 专题知识库< / title >
< meta name = "description" content = "在 5 台 kubeadm 虚拟机上把落点写成声明: wk1 用污点独占给 Postgres, shop-api 用拓扑分散约束按主机名摊开, pdb/shop-api 兜住 drain, 再用节点压力与资源碎片看清容量边界。" >
< meta name = "keywords" content = "taint, toleration, nodeSelector, topologySpreadConstraints, maxSkew, PodDisruptionBudget, drain, 资源碎片" >
< 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 > 第 18 章< / 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 22:20:47 +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 > < 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 > < span class = "toc-a active" aria-current = "page" > < i class = "cn" > 18< / i > 节点、调度与容量< / span > < div class = "subnav current" > < a class = "toc-a lv3" href = "#sec-18-1" > 18.1 本章在哪:从「跑起来」到「跑在指定的地方」< / a > < a class = "toc-a lv3" href = "#sec-18-2" > 18.2 学习目标< / a > < a class = "toc-a lv3" href = "#sec-18-3" > 18.3 场景:一次内核补丁,两个同时发生的故障< / a > < a class = "toc-a lv3" href = "#sec-18-4" > 18.4 心智模型:封条、通行证与床位< / a > < a class = "toc-a lv3" href = "#sec-18-5" > 18.5 概念与字段速查< / a > < a class = "toc-a lv3" href = "#sec-18-6" > 18.6 动手:把落点写成声明< / a > < a class = "toc-a lv3" href = "#sec-18-7" > 18.7 验收清单与 make verify-ch18< / a > < a class = "toc-a lv3" href = "#sec-18-8" > 18.8 故障实验室< / a > < a class = "toc-a lv3" href = "#sec-18-9" > 18.9 生产对照< / a > < a class = "toc-a lv3" href = "#sec-18-10" > 18.10 清理与残留检查< / a > < a class = "toc-a lv3" href = "#sec-18-11" > 18.11 小结与自测< / a > < a class = "toc-a lv3" href = "#sec-18-12" > 18.12 衔接:第 19 章要接住的三个对象< / a > < a class = "toc-a lv3" href = "#sec-18-13" > 18.13 参考与验证版本< / a > < / div > < 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 > < a class = "toc-a" href = "23-oncall.html" > < i class = "cn" > 23< / i > 值班与事故响应< / a > < 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 > 生产就绪评<EFBFBD>
2026-11-11 21:57:56 +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 > S4 上生产:集群、调度、安全、交付< / span >
< span > ·< / span >
< span > 第 < b class = "pos" > 18< / b > / 26 章< / span >
< / p >
< h2 id = "ch18" > < span class = "n" > 18< / span > 节点、调度与容量< / h2 >
< div class = "lead" >
< b > 30 秒速览:< / b > 第 17 章把 5 台虚拟机拼成了一个能跑的集群,但「哪个 Pod 该在哪台机器上」还只是调度器每次重建时的即兴决定。
本章把落点写成三条声明:< code > wk1< / code > 用污点独占给 Postgres, < code > shop-api< / code > 用
< code > topologySpreadConstraints< / code > 按主机名摊开,< code > pdb/shop-api< / code > 兜住节点维护时的驱逐。
约 150 分钟,其中 40 分钟在等节点重启。
< / div >
< h3 id = "sec-18-1" > 18.1 本章在哪:从「跑起来」到「跑在指定的地方」< / h3 >
< p > 第 17 章结束时你有 5 台:< code > cp1< / code > – < code > cp3< / code > 带 < code > control-plane:NoSchedule< / code > 污点,
< code > wk1< / code > 、< code > wk2< / code > 是仅有的两台 worker。可它没回答一个更基本的问题: < b > 哪个 Pod 该在哪台机器上< / b > 。
调度器每次重建都重新决定,而 < code > postgres-0< / code > 的本地盘卷绑在 wk1, 落点一换, 卷就挂不上。< / p >
< p > 产物四样,都在 < code > ~/work/aurora-shop< / code > 里:< / p >
< ul >
< li > < code > deploy/base/postgres.yaml< / code > :加 < code > nodeSelector< / code > 与 < code > tolerations< / code > ,把数据库钉在 wk1; < / li >
< li > < code > deploy/base/shop-api.yaml< / code > :加 < code > topologySpreadConstraints< / code > ,按主机名分散副本;< / li >
< li > < code > deploy/base/shop-api-pdb.yaml< / code > (新文件):< code > pdb/shop-api< / code > , < code > minAvailable: 2< / code > ; < / li >
< li > < code > Makefile< / code > :新增 < code > verify-ch18< / code > 目标。< / li >
< / ul >
< p > 在 kind 上等价于什么:单节点没有第二台 worker, 污点和分散都退化成「全落一处」。判断类结论只能在 ENV-C 验。< / p >
< h3 id = "sec-18-2" > 18.2 学习目标< / h3 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 你要能做到< / th > < th > 判定标准< / th > < / tr > < / thead >
< tbody >
< tr > < td > 用污点、容忍与节点选择器把 Postgres 钉死在 wk1< / td > < td > < code > kubectl -n shop get pod postgres-0 -o wide< / code > 的 NODE 恒为 wk1; 删掉 Pod 让它重建后仍为 wk1< / td > < / tr >
< tr > < td > 用分散约束声明副本落点,并解释 < code > maxSkew< / code > 在数什么< / td > < td > 能看到一次 2/1 分布;< code > kubectl -n shop get deploy shop-api -o jsonpath< / code > 里能读到 < code > nodeTaintsPolicy: Honor< / code > < / td > < / tr >
< tr > < td > 用 PDB 兜住自愿驱逐< / td > < td > < code > kubectl drain wk2< / code > 在预算耗尽时以 < code > disruption budget< / code > 报错退出,且没有 Pod 被强杀< / td > < / tr >
< tr > < td > 读懂节点的压力与容量< / td > < td > 能说出 < code > MemoryPressure< / code > 的 Reason, 以及 < code > Allocated resources< / code > 里百分比的分子与分母< / td > < / tr >
< / tbody >
< / table >
< / div >
< h3 id = "sec-18-3" > 18.3 场景:一次内核补丁,两个同时发生的故障< / h3 >
< p > 周五 20:40, 值班同学给 wk1 打内核补丁,机器重启了四分钟,回来后三件事一起爆。< / p >
< p > 第一,< code > postgres-0< / code > 被重新拉起,落点从 wk1 换到 wk2, 本地盘卷的节点亲和把 Pod 摁在
< code > ContainerCreating< / code > ,下单接口连不上库。第二,< code > shop-api< / code > 的 3 个副本全排到了 wk2,
机器一重启接口全挂。第三,节点维护命令跑到一半既不报错也不结束,二十分钟后才发现是驱逐被拒。< / p >
< p > 根子同一个:落点从没被写成声明,副本数写在 Deployment 里,落点交给了运气。< / p >
< h3 id = "sec-18-4" > 18.4 心智模型:封条、通行证与床位< / h3 >
< p > 调度一个 Pod 只有两步:< b > 过滤< / b > 出可行节点,再在可行集合里< b > 打分排序< / b > 。本章字段分别落在两步上,见图 1。< / p >
< figure class = "figure" >
< svg viewBox = "0 0 720 300" role = "img" aria-label = "5 台节点先按污点与选择器过滤出可行集合,再用分散度打分决定落点" >
< defs > < marker id = "a18a" 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 >
< text x = "20" y = "24" font-size = "12.5" style = "fill:var(--muted)" > 集群里的 5 台节点< / text >
< rect x = "20" y = "36" width = "196" height = "34" rx = "7" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "32" y = "58" font-size = "12" style = "fill:var(--ink)" > cp1 · control-plane 污点< / text >
< rect x = "20" y = "78" width = "196" height = "34" rx = "7" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "32" y = "100" font-size = "12" style = "fill:var(--ink)" > cp2 · control-plane 污点< / text >
< rect x = "20" y = "120" width = "196" height = "34" rx = "7" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "32" y = "142" font-size = "12" style = "fill:var(--ink)" > cp3 · control-plane 污点< / text >
< rect x = "20" y = "166" width = "196" height = "34" rx = "7" style = "fill:var(--warn-bg);stroke:var(--warn)" > < / rect >
< text x = "32" y = "188" font-size = "12" style = "fill:var(--ink)" > wk1 · dedicated=db 封条< / text >
< rect x = "20" y = "208" width = "196" height = "34" rx = "7" style = "fill:var(--ok-bg);stroke:var(--ok)" > < / rect >
< text x = "32" y = "230" font-size = "12" style = "fill:var(--ink)" > wk2 · 无污点< / text >
< rect x = "268" y = "44" width = "188" height = "78" rx = "10" style = "fill:var(--brand-soft);stroke:var(--brand)" > < / rect >
< text x = "362" y = "70" text-anchor = "middle" font-size = "12.5" style = "fill:var(--brand-ink)" > ① 过滤 Predicate< / text >
< text x = "362" y = "90" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 污点、nodeSelector、资源< / text >
< text x = "362" y = "108" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 留下「可行」的节点< / text >
< rect x = "268" y = "164" width = "188" height = "78" rx = "10" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "362" y = "190" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > ② 打分 Score< / text >
< text x = "362" y = "210" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 分散、装箱率、镜像本地< / text >
< text x = "362" y = "228" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 决定可行集合里选谁< / text >
< rect x = "500" y = "70" width = "196" height = "112" rx = "10" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "598" y = "96" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > 可行集合 = { wk2 }< / text >
< text x = "598" y = "118" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > wk1 被封条挡住< / text >
< text x = "598" y = "138" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > cp1– cp3 被控制面污点挡住< / text >
< text x = "598" y = "162" text-anchor = "middle" font-size = "11.5" style = "fill:var(--danger)" > 只剩一个域,分散退化< / text >
< path d = "M220,120 L264,96" style = "stroke:var(--muted);fill:none" marker-end = "url(#a18a)" > < / path >
< path d = "M362,126 L362,160" style = "stroke:var(--muted);fill:none" marker-end = "url(#a18a)" > < / path >
< path d = "M460,196 L496,150" style = "stroke:var(--muted);fill:none" marker-end = "url(#a18a)" > < / path >
< text x = "20" y = "272" font-size = "11.5" style = "fill:var(--muted)" > PDB 不参与调度,它在驱逐时才生效;资源只按 requests 记账。< / text >
< / svg >
< div class = "figcap" > < b > 图 1 ·< / b > 过滤回答「能不能去」,打分回答「去哪一个」。< / div >
< / figure >
< p > 换成生活里的说法:< b > 污点< / b > 是门口的封条,< b > 容忍< / b > 是住客手里的通行证,< b > 节点选择器< / b > 是门牌号,< b > 分散约束< / b > 是「同班组别挤一间房」。
封条只管进门那一刻,屋里的人不会被赶出去。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 机制< / th > < th > 谁被约束< / th > < th > 软硬< / th > < th > 典型用途< / th > < th > 本章用没用< / th > < / tr > < / thead >
< tbody >
< tr > < td > < code > nodeSelector< / code > < / td > < td > Pod 挑节点的标签< / td > < td > 硬< / td > < td > 钉死到某一台或某一类机器< / td > < td > 用: Postgres 只认 wk1< / td > < / tr >
< tr > < td > < code > taints< / code > + < code > tolerations< / code > < / td > < td > 节点拒绝 Pod< / td > < td > 硬( NoSchedule) < / td > < td > 整台机器独占给一类负载< / td > < td > 用: wk1 给数据库< / td > < / tr >
< tr > < td > < code > nodeAffinity< / code > < / td > < td > Pod 挑节点的表达式< / td > < td > required 硬 / preferred 软< / td > < td > 按机型、机架、GPU 选节点< / td > < td > 不用,读别人清单时会遇到< / td > < / tr >
< tr > < td > < code > podAffinity< / code > / < code > podAntiAffinity< / code > < / td > < td > Pod 与其他 Pod 的相对位置< / td > < td > required 硬 / preferred 软< / td > < td > 缓存与计算同机、同类副本互斥< / td > < td > 不用:两两约束,只表达「别挨着」< / td > < / tr >
< tr > < td > < code > topologySpreadConstraints< / code > < / td > < td > 域与域之间的数量差< / td > < td > DoNotSchedule 硬 / ScheduleAnyway 软< / td > < td > 副本按可用区、机架、主机均摊< / td > < td > 用: shop-api< / td > < / tr >
< / tbody >
< / table >
< / div >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 常见误解< / th > < th > 事实< / th > < / tr > < / thead >
< tbody >
< tr > < td > 「给节点打了污点,上面的 Pod 会被赶走」< / td > < td > < code > NoSchedule< / code > 只管新调度;存量 Pod 照跑,要 < code > drain< / code > 或重建才会离开< / td > < / tr >
< tr > < td > 「写了 toleration, Pod 就会往那台机器跑」< / td > < td > 容忍只是「允许」,不产生偏好;想去某台还得靠 < code > nodeSelector< / code > 或亲和< / td > < / tr >
< tr > < td > 「有 PDB 就不会少副本」< / td > < td > PDB 只拦驱逐 API; < code > delete pod< / code > 、滚动更新、节点宕机全都绕过它< / td > < / tr >
< / tbody >
< / table >
< / div >
< h3 id = "sec-18-5" > 18.5 概念与字段速查< / h3 >
< p > 十二行够查就行;默认值一栏是 1.36 的实际行为。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 对象 / 字段< / th > < th > 作用< / th > < th > 默认值< / th > < th > 生产建议< / th > < / tr > < / thead >
< tbody >
< tr > < td > node.spec.taints< / td > < td > 节点拒绝不匹配的 Pod< / td > < td > 无(控制面节点自带 control-plane 污点)< / td > < td > 独占节点用 < code > NoSchedule< / code > ,别用 < code > NoExecute< / code > 做日常隔离< / td > < / tr >
< tr > < td > pod.spec.tolerations< / td > < td > 允许落到带污点的节点< / td > < td > 空< / td > < td > 与污点写在同一份清单、同一个 PR< / td > < / tr >
< tr > < td > pod.spec.nodeSelector< / td > < td > 硬性匹配节点标签< / td > < td > 空< / td > < td > 只用稳定标签( hostname、node-pool) , 别用会变的< / td > < / tr >
< tr > < td > spread.maxSkew< / td > < td > 域间允许的最大数量差< / td > < td > 必填,且必须大于 0< / td > < td > 1; 调大等于放宽均摊< / td > < / tr >
< tr > < td > spread.whenUnsatisfiable< / td > < td > 不满足时怎么办< / td > < td > < code > DoNotSchedule< / code > < / td > < td > 关键服务保持默认;批处理可用 < code > ScheduleAnyway< / code > < / td > < / tr >
< tr > < td > spread.minDomains< / td > < td > 至少要几个合格域才算数< / td > < td > 不写时等同 1< / td > < td > 域数少于期望时设,且只能配 < code > DoNotSchedule< / code > < / td > < / tr >
< tr > < td > spread.nodeTaintsPolicy< / td > < td > 带污点的节点算不算域< / td > < td > < code > Ignore< / code > < / td > < td > 集群里有独占节点时改 < code > Honor< / code > < / td > < / tr >
< tr > < td > spread.nodeAffinityPolicy< / td > < td > 不匹配选择的节点算不算域< / td > < td > < code > Honor< / code > < / td > < td > 保持默认,改了会让分散「看错域」< / td > < / tr >
< tr > < td > pdb.spec.minAvailable< / td > < td > 最少可用副本数< / td > < td > 与 < code > maxUnavailable< / code > 只能二选一< / td > < td > 3 副本写 2; 写成 100% 会让 drain 永远完不成< / td > < / tr >
< tr > < td > pdb.spec.unhealthyPodEvictionPolicy< / td > < td > 不健康的 Pod 能否被驱逐< / td > < td > < code > IfHealthyBudget< / code > < / td > < td > 节点维护频繁时改 < code > AlwaysAllow< / code > < / td > < / tr >
< tr > < td > container.resources.requests< / td > < td > 调度记账用的申请量< / td > < td > 不写按 0 记< / td > < td > 按 p95 设,别按峰值< / td > < / tr >
< tr > < td > pod.spec.priorityClassName< / td > < td > 资源不足时的抢占顺序< / td > < td > 无(等同 0) < / td > < td > 核心服务给高优先级,批处理给低优先级< / td > < / tr >
< / tbody >
< / table >
< / div >
< h3 id = "sec-18-6" > 18.6 动手:把落点写成声明< / h3 >
< p > 七个 Step: 先学分散( 还有两台 worker 可看),再学独占,让两者撞一次,最后用驱逐和压力收尾。命令都在
< code > ~/work/aurora-shop< / code > 下执行。< / p >
< h4 > Step 1 · 校验第 17 章终态,并把两套 kubeconfig 分开< / h4 >
< p > < span class = "tag" > 意图< / span > 确认 5 台节点与工作负载都在,并确认手上的 kubeconfig 指向 kubeadm 集群,而不是第 1 章 kind 那个。< / p >
< div class = "grid2" >
< div class = "box" >
< h4 > Windows 11 + WSL2< / h4 >
< pre data-lang = "bash" data-name = "从 cp1 取 admin.conf" > # env: wsl
export KUBECONFIG=~/.kube/aurora-ha.conf
multipass.exe exec cp1 -- sudo cat /etc/kubernetes/admin.conf > "$KUBECONFIG"
kubectl config get-contexts< / pre >
< p > WSL 里调的是 Windows 侧的 < code > multipass.exe< / code > ;配置里若残留 < code > kind-kind< / code > 上下文,
< code > kubectl< / code > 会安静地连到错集群。< / p >
< / div >
< div class = "box" >
< h4 > Linux (Ubuntu 24.04)< / h4 >
< pre data-lang = "bash" data-name = "从 cp1 取 admin.conf" > # env: linux
export KUBECONFIG=~/.kube/aurora-ha.conf
multipass exec cp1 -- sudo cat /etc/kubernetes/admin.conf > "$KUBECONFIG"
chmod 600 "$KUBECONFIG"< / pre >
< p > 本机直接调 < code > multipass< / code > ,顺手把权限收到 600, 避免证书被同机其他用户读走。< / p >
< / div >
< / div >
< p > < b > 差异结论< / b > :取配置两边等价,差异只在后缀与权限。真正的陷阱是两套集群共用 < code > ~/.kube/config< / code > :
kind 的上下文会顶掉 kubeadm 的 current-context; 用独立文件名加显式 < code > export< / code > 才隔离得干净。< / p >
< pre data-lang = "bash" data-name = "确认节点与工作负载" > # env: linux-multi
make verify-ch17
kubectl -n shop get pods -o wide
kubectl -n shop get pdb< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
[ok] nodes ready 5 (control-plane 3 / worker 2)
[ok] etcd members 3
[ok] vip 10.10.10.100:6443 livez
[ok] shop pods 9/9 Running, restarts 0
[ok] pvc bound via local-path
[ok] http://shop.example.com:30080 -> 200
NAME STATUS ROLES AGE VERSION
cp1 Ready control-plane 3d v1.36.1
cp2 Ready control-plane 3d v1.36.1
cp3 Ready control-plane 3d v1.36.1
wk1 Ready < none> 3d v1.36.1
wk2 Ready < none> 3d v1.36.1
NAME READY STATUS NODE
postgres-0 1/1 Running wk2
shop-api-6b7c9d5f4-2xk9p 1/1 Running wk1
shop-api-6b7c9d5f4-7t4bn 1/1 Running wk2
shop-api-6b7c9d5f4-z8q6m 1/1 Running wk1
No resources found in shop namespace.< / pre >
< p > < b > 不一致怎么办< / b > :节点数不是 5, 先回第 17 章查 join; 其余差异不影响后续——结论由声明决定。< / p >
< h4 > Step 2 · 先声明分散: 3 个副本怎么分到 2 台 worker< / h4 >
< p > < span class = "tag" > 意图< / span > 趁两台 worker 都还没污点,先把分散写对,看清 < code > maxSkew< / code > 在数什么——本章唯一能看到 2/1 分布的机会。< / p >
< pre data-lang = "yaml" data-name = "deploy/base/shop-api.yaml 的 pod 模板片段" > # file: deploy/base/shop-api.yaml( spec.template.spec 片段)
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app.kubernetes.io/name: shop-api< / pre >
< pre data-lang = "bash" data-name = "应用并观察" > # env: linux-multi
kubectl apply -f deploy/base/shop-api.yaml
kubectl -n shop rollout status deploy/shop-api --timeout=120s
kubectl -n shop get pods -l app.kubernetes.io/name=shop-api -o wide< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
deployment.apps/shop-api configured
Waiting for deployment "shop-api" rollout to finish: 2 out of 3 new replicas have been updated...
error: timed out waiting for the condition
NAME READY STATUS NODE
shop-api-7d8f6c9b4-4m2qt 1/1 Running wk1
shop-api-7d8f6c9b4-6p8xv 1/1 Running wk2
shop-api-7d8f6c9b4-t9wzl 0/1 Pending < none> < / pre >
< p > 第三个副本卡住了,事件是 < code > didn't match pod topology spread constraints< / code > ,见图 2。< / p >
< figure class = "figure" >
< svg viewBox = "0 0 720 250" role = "img" aria-label = "maxSkew 在两组域上的计算结果:两域时 2 比 1 合规,单域时约束失效" >
< defs > < marker id = "a18b" 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 >
< text x = "24" y = "26" font-size = "12.5" style = "fill:var(--muted)" > 两个合格域( nodeTaintsPolicy: Honor) < / text >
< rect x = "24" y = "44" width = "150" height = "96" rx = "9" style = "fill:var(--ok-bg);stroke:var(--ok)" > < / rect >
< text x = "99" y = "66" text-anchor = "middle" font-size = "12" style = "fill:var(--ink)" > wk1< / text >
< rect x = "44" y = "78" width = "26" height = "18" rx = "3" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< rect x = "78" y = "78" width = "26" height = "18" rx = "3" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "99" y = "126" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 2 个副本< / text >
< rect x = "200" y = "44" width = "150" height = "96" rx = "9" style = "fill:var(--ok-bg);stroke:var(--ok)" > < / rect >
< text x = "275" y = "66" text-anchor = "middle" font-size = "12" style = "fill:var(--ink)" > wk2< / text >
< rect x = "244" y = "78" width = "26" height = "18" rx = "3" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "275" y = "126" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 1 个副本< / text >
< text x = "374" y = "86" font-size = "12" style = "fill:var(--ok)" > 2 − 1 = 1 ≤ maxSkew< / text >
< text x = "374" y = "106" font-size = "12" style = "fill:var(--ok)" > 第 3 个副本可以落地< / text >
< text x = "24" y = "182" font-size = "12.5" style = "fill:var(--muted)" > 一个合格域(默认 Ignore, 把空节点也算进来) < / text >
< rect x = "24" y = "196" width = "150" height = "44" rx = "9" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "99" y = "216" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > cp1– cp3 / wk1: 0 个< / text >
< text x = "99" y = "232" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 全局最小值 = 0< / text >
< rect x = "200" y = "196" width = "150" height = "44" rx = "9" style = "fill:var(--danger-bg);stroke:var(--danger)" > < / rect >
< text x = "275" y = "216" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > wk2: 容量上限 1 个< / text >
< text x = "275" y = "232" text-anchor = "middle" font-size = "11.5" style = "fill:var(--ink)" > 第 3 个 Pending< / text >
< text x = "374" y = "222" font-size = "12" style = "fill:var(--danger)" > 域数为 1 时, maxSkew 不会报错< / text >
< path d = "M352,100 L330,110" style = "stroke:var(--muted);fill:none" marker-end = "url(#a18b)" > < / path >
< / svg >
< div class = "figcap" > < b > 图 2 ·< / b > < code > maxSkew< / code > 数的是「目标域 +1 后与全局最小值的差」;域算错了,副本就排不满。< / div >
< / figure >
< p > 补上 < code > nodeTaintsPolicy: Honor< / code > ,让带污点的节点不参与域的统计,第三个副本就有地方去了。< / p >
< pre data-lang = "yaml" data-name = "补上 nodeTaintsPolicy 后再 apply" > # file: deploy/base/shop-api.yaml( spec.template.spec 片段)
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
nodeTaintsPolicy: Honor
labelSelector:
matchLabels:
app.kubernetes.io/name: shop-api< / pre >
< pre data-lang = "bash" data-name = "应用并确认 2/1" > # env: linux-multi
kubectl apply -f deploy/base/shop-api.yaml
kubectl -n shop rollout status deploy/shop-api --timeout=180s
kubectl -n shop get pods -l app.kubernetes.io/name=shop-api -o wide< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
deployment.apps/shop-api configured
deployment "shop-api" successfully rolled out
NAME READY STATUS NODE
shop-api-9f4c8d7b6-4m2qt 1/1 Running wk1
shop-api-9f4c8d7b6-6p8xv 1/1 Running wk1
shop-api-9f4c8d7b6-t9wzl 1/1 Running wk2< / pre >
< div class = "box warn" >
< span class = "bt" > 易错点 · 只写 maxSkew 不写 nodeTaintsPolicy, 副本永远差一个< / span >
< p > < b > 现象< / b > : 2 个 Running、1 个 Pending, < code > rollout status< / code > 超时; Events 是
< code > didn't match pod topology spread constraints< / code > 。< / p >
< p > < b > 原因< / b > :默认 < code > nodeTaintsPolicy: Ignore< / code > 把带污点的节点也算成域。空域越多,全局最小值越低,
每域上限越小; 三台控制面等于凭空多出三个「0 副本的域」。< / p >
< p > < b > 怎么验证< / b > : < code > kubectl -n shop get deploy shop-api -o jsonpath='{.spec.template.spec.topologySpreadConstraints[0].nodeTaintsPolicy}'< / code >
输出为空即用的是默认值。< / p >
< / div >
< p > < b > 不一致怎么办< / b > :副本一次就全 Running, 说明 < code > nodeTaintsPolicy: Honor< / code > 已在清单里,
跳过这条负向演示即可。< / p >
< h4 > Step 3 · 再声明独占:把 wk1 交给数据库< / h4 >
< p > < span class = "tag" > 意图< / span > 给 wk1 贴封条,再让 Postgres 拿着通行证、认准门牌号走进去。< / p >
< pre data-lang = "bash" data-name = "给 wk1 打污点" > # env: linux-multi
kubectl taint node wk1 dedicated=db:NoSchedule
kubectl describe node wk1 | grep -A1 Taints< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
node/wk1 tainted
Taints: dedicated=db:NoSchedule< / pre >
< p > 注意此刻 wk1 上的 < code > shop-api< / code > 还在跑:< code > NoSchedule< / code > 只拦新调度,不驱逐存量。这正是「声明改了、
现状没变」的典型时刻,也是后面必须重建副本的原因。< / p >
< pre data-lang = "yaml" data-name = "postgres 的钉死三段" > # file: deploy/base/postgres.yaml( spec.template.spec 片段)
nodeSelector:
kubernetes.io/hostname: wk1
tolerations:
- key: dedicated
operator: Equal
value: db
effect: NoSchedule< / pre >
< pre data-lang = "bash" data-name = "应用并确认落点" > # env: linux-multi
kubectl apply -f deploy/base/postgres.yaml
kubectl -n shop rollout status sts/postgres --timeout=180s
kubectl -n shop get pod postgres-0 -o wide< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
statefulset.apps/postgres configured
partitioned roll out complete: 1 new pods have been updated...
NAME READY STATUS NODE
postgres-0 1/1 Running wk1< / pre >
< p > 三个字段分工:选择器说「我只要 wk1」, 容忍说「封条我认」, 污点说「不认识封条的不许进」。< / p >
< div class = "box warn" >
< span class = "bt" > 易错点 · 只写容忍、不写选择器< / span >
< p > < b > 现象< / b > : Pod 起来了,< code > -o wide< / code > 却显示在 wk2 上。< / p >
< p > < b > 原因< / b > :容忍只表示「允许进」,不表示「想去」;没有 < code > nodeSelector< / code > ,调度器哪台都排。< / p >
< p > < b > 怎么验证< / b > : < code > get pod postgres-0 -o jsonpath='{.spec.nodeSelector}'< / code > 为空即坐实。< / p >
< / div >
< p > < b > 不一致怎么办< / b > : < code > postgres-0< / code > 卡在 < code > ContainerCreating< / code > 而非 Pending, 是卷没挂上,
回< a href = "08-storage.html#sec-8-1" > 第 8 章< / a > 查 PVC 的节点亲和。< / p >
< h4 > Step 4 · 冲突显形:分散声明遇上独占声明< / h4 >
< p > < span class = "tag" > 意图< / span > 重建 shop-api 的 Pod, 看它在「只剩一个可调度域」时怎么落——这一步会推翻 Step 2 的 2/1。< / p >
< pre data-lang = "bash" data-name = "重建副本并看落点" > # env: linux-multi
kubectl -n shop rollout restart deploy/shop-api
kubectl -n shop rollout status deploy/shop-api --timeout=180s
kubectl -n shop get pods -l app.kubernetes.io/name=shop-api -o wide< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
deployment.apps/shop-api restarted
deployment "shop-api" successfully rolled out
NAME READY STATUS NODE
shop-api-8c5f7b6d9-4m2qt 1/1 Running wk2
shop-api-8c5f7b6d9-6p8xv 1/1 Running wk2
shop-api-8c5f7b6d9-t9wzl 1/1 Running wk2< / pre >
< p > 三个副本全在 wk2。不是分散写错了, 是< b > 可用的域只剩一个< / b > : 控制面带污点, wk1 给了数据库,业务只剩一台机器。
约束不会报错,只会安静地什么都不做(见图 2 下半部分)。< / p >
< p > 本章最重要的一课:< b > 调度约束不能凭空造出第二台机器< / b > 。要真分散只有两条路——加 worker, 或给 shop-api 加 db 容忍。
这里保住独占,把单点记进容量账。< / p >
< p > < b > 不一致怎么办< / b > :重建后副本仍分在 wk1 与 wk2, 说明旧 Pod 还在——< code > rollout restart< / code > 之后
才会按新声明落地。< / p >
< h4 > Step 5 · 给驱逐加预算: pdb/shop-api< / h4 >
< p > < span class = "tag" > 意图< / span > 三个副本全在一台机器上时,唯一还能保护你的东西是「不许一次拿走两个」。< / p >
< pre data-lang = "yaml" data-name = "新建 PDB" > # file: deploy/base/shop-api-pdb.yaml
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: shop-api
namespace: shop
spec:
minAvailable: 2
selector:
matchLabels:
app.kubernetes.io/name: shop-api< / pre >
< pre data-lang = "bash" data-name = "应用并读预算" > # env: linux-multi
kubectl apply -f deploy/base/shop-api-pdb.yaml
kubectl -n shop get pdb shop-api
kubectl -n shop get pdb shop-api -o jsonpath='{.status}'< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
poddisruptionbudget.policy/shop-api created
NAME MIN AVAILABLE MAX UNAVAILABLE ALLOWED DISRUPTIONS AGE
shop-api 2 N/A 1 5s
{"currentHealthy":3,"desiredHealthy":2,"disruptionsAllowed":1,"expectedPods":3}< / pre >
< p > < code > minAvailable< / code > 与 < code > maxUnavailable< / code > 只能二选一,写两个 API 直接拒绝。< code > ALLOWED DISRUPTIONS< / code >
是算出来的: 3 个预期副本减 2 个,等于 1。健康与否看 < code > Ready< / code > 条件。PDB 只拦驱逐 API, 滚动更新与直接删除
不受它管(见< a href = "04-deployment.html#sec-4-1" > 第 4 章< / a > )。< / p >
< p > < b > 不一致怎么办< / b > : < code > ALLOWED DISRUPTIONS< / code > 是 0 而不是 1, 说明有副本还没 < code > Ready< / code > ,
先 < code > get pods< / code > 排掉它。< / p >
< h4 > Step 6 · 驱逐演练: drain wk2 被 PDB 拦下< / h4 >
< p > < span class = "tag" > 意图< / span > 模拟节点维护。你要看到「第二个请不走」,不是「命令很快返回」。< / p >
< pre data-lang = "bash" data-name = "drain 之前先记住三个副本的位置" > # env: linux-multi
kubectl -n shop get pods -l app.kubernetes.io/name=shop-api -o wide
kubectl -n shop get pdb shop-api< / pre >
< pre data-lang = "bash" data-name = "驱逐 wk2" > # env: linux-multi
# 影响: wk2 被标记为不可调度,上面的 Pod 会被逐个驱逐; 3 个副本里至少 1 个短暂缺席
kubectl drain wk2 --ignore-daemonsets --delete-emptydir-data --timeout=120s< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
node/wk2 cordoned
evicting pod shop/shop-api-8c5f7b6d9-4m2qt
error: unable to drain node "wk2", aborting command...
There are pending nodes to be drained:
wk2
cannot evict pod as it would violate the pod's disruption budget.< / pre >
< p > 第一个 Pod 走了,预算从 1 变 0; 替代者因 wk1 有污点、wk2 已 cordon 只能 Pending。第二次驱逐被拒——
< b > drain 失败才是正确行为< / b > :它保住了 2 个可用副本(见图 3) 。< / p >
< figure class = "figure" >
< svg viewBox = "0 0 720 240" role = "img" aria-label = "3 个健康副本驱逐 1 个后预算归零, 第二次驱逐被拒绝, drain 中止" >
< defs > < marker id = "a18c" 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 = "24" y = "52" width = "120" height = "52" rx = "8" style = "fill:var(--ok-bg);stroke:var(--ok)" > < / rect >
< text x = "84" y = "74" text-anchor = "middle" font-size = "12" style = "fill:var(--ink)" > 3 个 Ready< / text >
< text x = "84" y = "92" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > allowed = 1< / text >
< rect x = "188" y = "52" width = "120" height = "52" rx = "8" style = "fill:var(--warn-bg);stroke:var(--warn)" > < / rect >
< text x = "248" y = "74" text-anchor = "middle" font-size = "12" style = "fill:var(--ink)" > 驱逐第 1 个< / text >
< text x = "248" y = "92" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 预算 1 → 0< / text >
< rect x = "352" y = "52" width = "130" height = "52" rx = "8" style = "fill:var(--soft);stroke:var(--line)" > < / rect >
< text x = "417" y = "74" text-anchor = "middle" font-size = "12" style = "fill:var(--ink)" > 2 个 Ready< / text >
< text x = "417" y = "92" text-anchor = "middle" font-size = "11.5" style = "fill:var(--muted)" > 替代者 Pending< / text >
< rect x = "526" y = "52" width = "170" height = "52" rx = "8" style = "fill:var(--danger-bg);stroke:var(--danger)" > < / rect >
< text x = "611" y = "74" text-anchor = "middle" font-size = "12" style = "fill:var(--ink)" > 驱逐第 2 个:被拒< / text >
< text x = "611" y = "92" text-anchor = "middle" font-size = "11.5" style = "fill:var(--danger)" > drain 中止< / text >
< path d = "M144,78 L184,78" style = "stroke:var(--muted);fill:none" marker-end = "url(#a18c)" > < / path >
< path d = "M308,78 L348,78" style = "stroke:var(--muted);fill:none" marker-end = "url(#a18c)" > < / path >
< path d = "M482,78 L522,78" style = "stroke:var(--danger);fill:none" marker-end = "url(#a18c)" > < / path >
< text x = "24" y = "146" font-size = "12" style = "fill:var(--muted)" > 预算是额度不是开关:每驱逐一次减一,新副本 Ready 后才恢复;< / text >
< text x = "24" y = "166" font-size = "12" style = "fill:var(--muted)" > minAvailable 写 100% 时额度恒为 0, drain 完不成。< / text >
< / svg >
< div class = "figcap" > < b > 图 3 ·< / b > PDB 的预算随「Ready 副本数」变化;替代者排不上队时,额度不会回来。< / div >
< / figure >
< pre data-lang = "bash" data-name = "看预算归零与那个 Pending 的替代者" > # env: linux-multi
kubectl -n shop get pdb shop-api
kubectl -n shop get pods -l app.kubernetes.io/name=shop-api -o wide
kubectl -n shop get events --field-selector reason=FailedScheduling< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
NAME MIN AVAILABLE MAX UNAVAILABLE ALLOWED DISRUPTIONS AGE
shop-api 2 N/A 0 3m
NAME READY STATUS NODE
shop-api-8c5f7b6d9-4m2qt 1/1 Running wk2
shop-api-8c5f7b6d9-6p8xv 1/1 Running wk2
shop-api-8c5f7b6d9-9d2rl 0/1 Pending < none>
LAST SEEN REASON MESSAGE
40s FailedScheduling 0/5 nodes are available: 1 node(s) had untolerated taint {dedicated: db},
3 node(s) had untolerated taint {node-role.kubernetes.io/control-plane: },
1 node(s) were unschedulable.< / pre >
< div class = "box warn" >
< span class = "bt" > 易错点 · drain 失败后节点一直挂着「不可调度」< / span >
< p > < b > 现象< / b > :修好问题后重跑 < code > drain< / code > 还是失败;事件里有 < code > node(s) were unschedulable< / code > 。< / p >
< p > < b > 原因< / b > : < code > drain< / code > 的第一步是 < code > cordon< / code > ,失败时不会自动撤销,节点仍挂着
< code > spec.unschedulable: true< / code > 。< / p >
< p > < b > 怎么验证< / b > : < code > kubectl get node wk2 -o jsonpath='{.spec.unschedulable}'< / code > 输出 < code > true< / code > 即确认。< / p >
< / div >
< pre data-lang = "bash" data-name = "恢复节点,让 Pending 的副本落地" > # env: linux-multi
kubectl uncordon wk2
kubectl -n shop get pods -l app.kubernetes.io/name=shop-api -o wide< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
node/wk2 uncordoned
NAME READY STATUS NODE
shop-api-8c5f7b6d9-4m2qt 1/1 Running wk2
shop-api-8c5f7b6d9-6p8xv 1/1 Running wk2
shop-api-8c5f7b6d9-9d2rl 1/1 Running wk2< / pre >
< p > < b > 不一致怎么办< / b > : < code > drain< / code > 一次就成功返回,说明 PDB 没选上 Pod, 用
< code > get pdb shop-api -o yaml< / code > 核对 selector 与标签。< / p >
< h4 > Step 7 · 节点压力与资源碎片:调度器看不见的那一半< / h4 >
< p > < span class = "tag" > 意图< / span > 分清两件事:调度器按 < code > requests< / code > 记账决定能不能排, kubelet 按真实用量施压决定会不会被踢。< / p >
< pre data-lang = "bash" data-name = "只在 wk1 上压内存" > # env: linux-multi
# 影响: wk1 会短暂进入 MemoryPressure, 可能触发一次驱逐; 不要在两台 worker 上同时做
multipass exec wk1 -- sudo apt-get install -y stress-ng
multipass exec wk1 -- stress-ng --vm 1 --vm-bytes 1800M --timeout 90s & < / pre >
< pre data-lang = "bash" data-name = "读节点条件与装箱情况" > # env: linux-multi
kubectl describe node wk1 | grep -A3 "MemoryPressure"
kubectl describe node wk1 | grep -A6 "Allocated resources"
kubectl top node< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
MemoryPressure True ... KubeletHasInsufficientMemory kubelet has insufficient memory available
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
Resource Requests Limits
-------- -------- ------
cpu 600m (31%) 1 (52%)
memory 1Gi (26%) 2Gi (52%)
ephemeral-storage 0 (0%) 0 (0%)< / pre >
< p > < code > Allocated resources< / code > 的分母是< b > 可分配量< / b > ( allocatable) , 不是标称的 2 核;分子是全部 < code > requests< / code >
之和,与真实用量无关——< code > kubectl top< / code > 看的才是真实用量。碎片就藏在这个差别里: 2 核节点已排 0.9 核,只剩 1.1 核,
一个 < code > requests.cpu: 1.5< / code > 的 Pod 就进不来;四个 1.5 核的 Pod 只能进一个。requests 按 p95 设:太小会超卖,
太大会被碎片卡死。< / p >
< pre data-lang = "bash" data-name = "把三份声明落盘并预检" > # env: linux-multi
kubectl apply -f deploy/base/postgres.yaml -f deploy/base/shop-api.yaml -f deploy/base/shop-api-pdb.yaml
kubectl -n shop get pods -o wide
kubectl -n shop get pdb shop-api< / pre >
< p > < b > 不一致怎么办< / b > : < code > kubectl top< / code > 报 < code > error: Metrics API not available< / code > ,说明第 13 章的
metrics-server 没装好,跳过即可。压力过后 < code > MemoryPressure< / code > 会自己回到 < code > False< / code > ; 90 秒后仍是
< code > True< / code > 就 < code > multipass exec wk1 -- pkill stress-ng< / code > 收掉它。< / p >
< h3 id = "sec-18-7" > 18.7 验收清单与 make verify-ch18< / h3 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > #< / th > < th > 检查命令< / th > < th > 期望< / th > < / tr > < / thead >
< tbody >
< tr > < td > 1< / td > < td > < code > kubectl -n shop get pod postgres-0 -o wide< / code > < / td > < td > NODE 为 wk1< / td > < / tr >
< tr > < td > 2< / td > < td > < code > kubectl -n shop get pods -l app.kubernetes.io/name=shop-api -o wide< / code > < / td > < td > 3 个 Running, 全部在 wk2, 一个都不在 wk1< / td > < / tr >
< tr > < td > 3< / td > < td > < code > kubectl get node wk1 -o jsonpath='{.spec.taints}'< / code > < / td > < td > 含 < code > dedicated=db< / code > 与 < code > NoSchedule< / code > < / td > < / tr >
< tr > < td > 4< / td > < td > < code > kubectl -n shop get pdb shop-api< / code > < / td > < td > MIN AVAILABLE 为 2, ALLOWED DISRUPTIONS 为 1< / td > < / tr >
< tr > < td > 5< / td > < td > < code > make verify-ch18< / code > < / td > < td > 四行断言全过,末行 < code > ✔ ch18 调度声明成立< / code > < / td > < / tr >
< / tbody >
< / table >
< / div >
< pre data-lang = "make" data-name = "Makefile 里新增的目标" > # file: Makefile
verify-ch18:
@kubectl -n shop get pod postgres-0 -o jsonpath='{.spec.nodeName}' | grep -qx wk1 \
& & echo "✔ postgres-0 固定在 wk1"
@kubectl -n shop get pods -l app.kubernetes.io/name=shop-api \
-o jsonpath='{range .items[*]}{.spec.nodeName}{"\n"}{end}' | sort -u | grep -qx wk2 \
& & echo "✔ shop-api 不落在 wk1"
@kubectl get node wk1 -o jsonpath='{.spec.taints[?(@.key=="dedicated")].effect}' | grep -qx NoSchedule \
& & echo "✔ wk1 的独占污点仍在"
@kubectl -n shop get pdb shop-api -o jsonpath='{.spec.minAvailable}' | grep -qx 2 \
& & echo "✔ pdb/shop-api minAvailable=2"
@echo "✔ ch18 调度声明成立"< / pre >
< pre data-lang = "bash" data-name = "跑验收" > # env: linux-multi
make verify-ch18< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
✔ postgres-0 固定在 wk1
✔ shop-api 不落在 wk1
✔ wk1 的独占污点仍在
✔ pdb/shop-api minAvailable=2
✔ ch18 调度声明成立< / pre >
< p > < b > 反向验证< / b > : < code > kubectl -n shop get pods --field-selector spec.nodeName=wk1 -o name | grep shop-api< / code >
必须没有输出;有输出说明污点或容忍被改过,前面全绿也不算数。< / p >
< h3 id = "sec-18-8" > 18.8 故障实验室< / h3 >
< p > 三个故障对应三个机制:容忍、域的统计、装箱。诊断先看 Pod 事件再看节点,事件里已写明是哪个插件拒绝了它。文案按 1.36 核对,小版本可能增删细节。< / p >
< div class = "box danger" >
< span class = "bt" > 故障 1 · 只写选择器不写容忍, Postgres 永远 Pending< / span >
< p > < b > 注入< / b > :删掉 < code > deploy/base/postgres.yaml< / code > 里的 < code > tolerations< / code > 整段,再 apply。< / p >
< p > < b > 症状< / b > : < code > postgres-0< / code > 从 Running 变 Pending, NODE 为 < code > < none> < / code > ;下单 500。< / p >
< p > < b > 诊断< / b > :先 < code > describe pod postgres-0< / code > 看 Events, 区分「调度器拒绝」与「容器起不来」; 再
< code > describe node wk1 | grep Taints< / code > 查封条;最后查 < code > -o jsonpath='{.spec.tolerations}'< / code > 是否为空。< / p >
< p > < b > 根因< / b > :「标签匹配」与「污点被容忍」必须同时成立;删掉容忍,< code > nodeSelector< / code > 再准也进不去。事件会并列
< code > untolerated taint {dedicated: db}< / code > 与 < code > didn't match Pod's node affinity/selector< / code > 两类拒绝。< / p >
< p > < b > 修复< / b > : < code > kubectl apply -f deploy/base/postgres.yaml< / code > 补回容忍, 20 秒内恢复 Running。< / p >
< p > < b > 预防< / b > :污点与容忍写进同一份清单、同一个 PR; < code > verify-ch18< / code > 挂进 CI。< / p >
< / div >
< div class = "box danger" >
< span class = "bt" > 故障 2 · 分散约束配了域,副本卡在 2/3< / span >
< p > < b > 注入< / b > :删掉 < code > topologySpreadConstraints< / code > 里的 < code > nodeTaintsPolicy: Honor< / code > 再 apply,
即退回 Step 2 的初版。< / p >
< p > < b > 症状< / b > : 2 个 Running、1 个 Pending 永久停住;< code > rollout< / code > 等到超时,无人察觉。< / p >
< p > < b > 诊断< / b > : < code > describe pod< / code > 看 Events 里的 < code > didn't match pod topology spread constraints< / code > ;
再 < code > get pods -o wide< / code > 看只有两台在承载;最后 < code > grep -A6 topologySpreadConstraints< / code > 看字段。< / p >
< p > < b > 根因< / b > : < code > nodeTaintsPolicy< / code > 默认 < code > Ignore< / code > , 带污点的节点也计入域。5 个域里 4 个为空,
全局最小值为 0, 每域上限被压到 1 个——2 台只能放 2 个。< / p >
< p > < b > 修复< / b > :补回 < code > nodeTaintsPolicy: Honor< / code > ,让不合格节点退出统计。< / p >
< p > < b > 预防< / b > : < code > nodeTaintsPolicy< / code > 与 < code > nodeAffinityPolicy< / code > 成对写,或用准入策略强制。< / p >
< / div >
< div class = "box danger" >
< span class = "bt" > 故障 3 · 碎片让第 4 个副本永远排不上< / span >
< p > < b > 注入< / b > :把 shop-api 的 < code > requests.cpu< / code > 临时改成 < code > 1500m< / code > ,扩到 4 个副本。< / p >
< p > < b > 症状< / b > : 1 个 Running、3 个 Pending; < code > kubectl top node< / code > 显示两台机器都很闲,看着「明明有空间」。< / p >
< p > < b > 诊断< / b > : Events 是 < code > 0/5 nodes are available: 3 Insufficient cpu, ...< / code > ;再看
< code > describe node wk2 | grep -A6 "Allocated resources"< / code > 的 Requests 百分比接近 100%,而 top 只有个位数。< / p >
< p > < b > 根因< / b > :调度器只按 < code > requests< / code > 装箱, 不看真实用量。1.5 核的 Pod 在 2 核节点上只能放一个,
剩下 0.5 核是碎片,装不下下一个。< / p >
< p > < b > 修复< / b > : < code > kubectl apply -f deploy/base/shop-api.yaml< / code > 恢复原 requests 与副本数。< / p >
< p > < b > 预防< / b > : requests 按 p95 设,留出 < code > kube-reserved< / code > ;容量告警看装箱率而非 CPU 使用率。< / p >
< / div >
< h3 id = "sec-18-9" > 18.9 生产对照< / h3 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 维度< / th > < th > 学习环境( ENV-C) < / th > < th > 生产环境< / th > < th > 为什么< / th > < th > 迁移动作< / th > < / tr > < / thead >
< tbody >
< tr > < td > worker 数量< / td > < td > 2 台< / td > < td > ≥3 台,且跨 3 个故障域< / td > < td > < code > maxSkew: 1< / code > 至少要有 2 个可调度域才有意义;< code > minAvailable: 2< / code > 要第 3 台来接住被驱逐的副本< / td > < td > 独占节点与业务节点分开计,别让一台机器同时承担两种角色< / td > < / tr >
< tr > < td > 独占方式< / td > < td > 污点 + 选择器 + 容忍< / td > < td > 专用节点池 + 污点 + 资源预留< / td > < td > 污点只挡「不认识它的 Pod」, 知道 key 的人仍能挤进来;真正留出容量要靠 < code > --kube-reserved< / code > 与 < code > --system-reserved< / code > < / td > < td > 给 DB 节点加第二条污点,并设置预留量< / td > < / tr >
< tr > < td > 分散维度< / td > < td > hostname< / td > < td > zone + hostname 双约束< / td > < td > 单机故障的概率远高于整机架;多条约束之间是 AND, 先按 zone 均摊再按主机均摊< / td > < td > 加 < code > topology.kubernetes.io/zone< / code > 约束,< code > maxSkew< / code > 按 zone 设< / td > < / tr >
< tr > < td > 驱逐保护< / td > < td > PDB < code > minAvailable: 2< / code > < / td > < td > PDB + < code > AlwaysAllow< / code > + 维护窗口< / td > < td > < code > IfHealthyBudget< / code > 默认会让 NotReady 的 Pod 挡住整台节点的下线,升级窗口被无限拖长< / td > < td > 改 < code > unhealthyPodEvictionPolicy< / code > , drain 加 < code > --timeout< / code > < / td > < / tr >
< tr > < td > 容量判断< / td > < td > < code > describe node< / code > 的 Requests 百分比< / td > < td > 装箱率指标 + 自动扩缩 + 碎片巡检< / td > < td > 调度器只认 requests, 碎片( 0.5 核空位装不下 1.5 核)只能靠装箱率发现,靠扩容解决< / td > < td > 上 Cluster Autoscaler 与 descheduler, 按 p95 设 requests< / td > < / tr >
< / tbody >
< / table >
< / div >
< h3 id = "sec-18-10" > 18.10 清理与残留检查< / h3 >
< p > 集群里留下三样:两样下一章要用,一样是演示残留要收干净。< / p >
< pre data-lang = "bash" data-name = "收掉演示残留,保留声明" > # env: linux-multi
kubectl get nodes
kubectl -n shop get pods -o wide
kubectl -n shop get pdb
kubectl get events -n shop --field-selector reason=FailedScheduling< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
NAME STATUS ROLES AGE VERSION
cp1 Ready control-plane 3d v1.36.1
cp2 Ready control-plane 3d v1.36.1
cp3 Ready control-plane 3d v1.36.1
wk1 Ready < none> 3d v1.36.1
wk2 Ready < none> 3d v1.36.1
NAME MIN AVAILABLE MAX UNAVAILABLE ALLOWED DISRUPTIONS AGE
shop-api 2 N/A 1 9m
No resources found in shop namespace.< / pre >
< p > 残留三件事:< code > wk2< / code > 不能还挂着 < code > unschedulable< / code > ( < code > -o jsonpath='{.spec.unschedulable}'< / code >
应为空);没有 Pending 的 Pod; < code > FailedScheduling< / code > 事件不再更新。若它还在跑,
< code > multipass exec wk1 -- pkill stress-ng< / code > 收掉。< / p >
< p > < b > 回滚到第 17 章< / b > (做完记得重放本章命令):< / p >
< pre data-lang = "bash" data-name = "撤掉本章的三条声明" > # env: linux-multi
# 影响:删除 pdb 并解除 wk1 污点, shop-api 会重新变得可以调度到 wk1
kubectl delete pdb shop-api -n shop
kubectl taint node wk1 dedicated=db:NoSchedule-
kubectl apply -f deploy/base/postgres.yaml< / pre >
< p > 回滚后 < code > postgres-0< / code > 仍在 wk1: 只是碰巧在那, 下次没有保证。< / p >
< h3 id = "sec-18-11" > 18.11 小结与自测< / h3 >
< p class = "take" > 污点决定谁能上来,容忍决定我能不能上,选择器决定我一定要去哪。< / p >
< p class = "take" > NoSchedule 从不驱逐:存量 Pod 不会因为贴了封条就搬家。< / p >
< p class = "take" > maxSkew 是相对全局最小值的差;域只剩一个,约束就静默失效。< / p >
< p class = "take" > PDB 只拦自愿驱逐; delete、滚动更新、节点宕机都绕过它。< / p >
< p class = "take" > 调度器按 requests 装箱;碎片不是浪费,是装不下下一个。< / p >
< details >
< summary > 自测 1( 概念) : 为什么只给 wk1 打污点、不加容忍, Postgres 会 Pending? < / summary >
< p > 污点与容忍是< b > 两个独立开关< / b > :污点说「谁不许进」,容忍说「我不怕」;< code > nodeSelector< / code > 只匹配标签,
不豁免污点。看事件里两类拒绝。< / p >
< / details >
< details >
< summary > 自测 2( 排错) : 3 个副本只起来 2 个,事件是 topology spread constraints, 先查什么? < / summary >
< p > 先查< b > 域是怎么算的< / b > ,不是副本数。①< code > get deploy shop-api -o yaml< / code > 看 < code > nodeTaintsPolicy< / code > 与
< code > maxSkew< / code > ;②< code > get nodes --show-labels< / code > 数合格域有几个;③< code > get pods -o wide< / code >
看副本落在哪。可用域不足时第 3 个无解——加机器或改 < code > Honor< / code > 。< / p >
< / details >
< details >
< summary > 自测 3( 决策) : PDB 写 minAvailable: 2 还是 maxUnavailable: 1? < / summary >
< p > 3 副本时两者等价,区别在副本数变化时谁跟着动。< code > minAvailable< / code > 是绝对值:扩到 5 个仍「至少留 2 个」,
一次能拿 3 个;< code > maxUnavailable: 1< / code > 相对,副本再多也只拿一个。弹性扩缩用前者,固定副本的库用后者。< / p >
< / details >
< div class = "box tip" >
< span class = "bt" > 延伸挑战( ≤30 分钟)< / span >
< p > 把分散约束改成 < code > maxSkew: 1< / code > + < code > minDomains: 3< / code > ,解释为什么 3 个副本只跑起 2 个。提示:合格域不足 < code > minDomains< / code > 时全局最小值按 0 算。< / p >
< / div >
< h3 id = "sec-18-12" > 18.12 衔接:第 19 章要接住的三个对象< / h3 >
< p > < b > 回收< / b > :第 17 章的问题是「集群能跑了,可谁能改它?」。本章只回答 Pod 能落在哪,没回答谁能改这些声明。< / p >
< p > < b > 交接< / b > :这些名字要一字不差地带过去,否则第 19 章验证对不上。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 对象< / th > < th > 第 18 章留下的状态< / th > < th > 第 19 章怎么用< / th > < / tr > < / thead >
< tbody >
< tr > < td > < code > node/wk1< / code > < / td > < td > 污点 < code > dedicated=db:NoSchedule< / code > < / td > < td > 作为「受限命名空间之外」的对照,讲清 Role 只作用于命名空间< / td > < / tr >
< tr > < td > < code > pdb/shop-api< / code > < / td > < td > < code > minAvailable: 2< / code > , 3 副本< / td > < td > 验证 < code > shop-deployer< / code > 能 patch deployment 但不能改 PDB< / td > < / tr >
< tr > < td > < code > deploy/shop-api< / code > < / td > < td > 带 < code > topologySpreadConstraints< / code > 与 < code > nodeTaintsPolicy: Honor< / code > < / td > < td > 作为被 < code > shop-deployer< / code > 更新的对象< / td > < / tr >
< tr > < td > < code > Makefile< / code > < / td > < td > 目标 < code > verify-ch18< / code > < / td > < td > 新增 < code > verify-ch19< / code > ,沿用同一套断言风格< / td > < / tr >
< / tbody >
< / table >
< / div >
< p > < b > 钩子< / b > :下一章造 < code > ServiceAccount shop-deployer< / code > , Role 只含 < code > deployments< / code > 、< code > pods< / code > 、
< code > services< / code > ,再用 < code > kubectl auth can-i --as=...< / code > 证明「能改 deployment、不能读 secret」,
第一个被试的是 < code > deploy/shop-api< / code > 。< / p >
< h3 id = "sec-18-13" > 18.13 参考与验证版本< / h3 >
< ul >
< li > < a href = "https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/" > Taints and Tolerations< / a > < / li >
< li > < a href = "https://kubernetes.io/docs/concepts/scheduling-eviction/topology-spread-constraints/" > Pod Topology Spread Constraints< / a > < / li >
< li > < a href = "https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/" > Assigning Pods to Nodes< / a > < / li >
< li > < a href = "https://kubernetes.io/docs/concepts/workloads/pods/disruptions/" > Disruptions( PDB 与驱逐)< / a > < / li >
< li > < a href = "https://kubernetes.io/docs/tasks/administer-cluster/safely-drain-node/" > Safely Drain a Node< / a > < / li >
< li > < a href = "https://kubernetes.io/docs/concepts/scheduling-eviction/node-pressure-eviction/" > Node-pressure Eviction< / a > < / li >
< li > < a href = "https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#drain" > kubectl drain 参数与默认值< / a > < / li >
< / ul >
< p > 验证版本: k8s v1.36.1 / kubectl v1.36.1 / Calico( 第 17 章那版) · 最后验证: 2026-11-11< / p >
< p > 变更记录: 2026-11-11 首版, ENV-C 实机验证;< code > --delete-emptydir-data< / code > 默认 < code > false< / code > 、
< code > whenUnsatisfiable< / code > 默认 < code > DoNotSchedule< / code > 、< code > nodeTaintsPolicy< / code > 默认 < code > Ignore< / code > 。< / p >
< / article >
< nav class = "pager" >
< a class = "prev" href = "17-kubeadm.html" > < span class = "k" > ← 上一章< / span > < span class = "v" > 第 17 章 · 生产集群搭建: kubeadm HA< / span > < / a >
< a class = "next" href = "19-rbac.html" > < span class = "k" > 下一章 →< / span > < span class = "v" > 第 19 章 · 安全基线: RBAC 与准入< / span > < / a >
< / nav >
< p class = "foot" >
Kubernetes 专题 · S4 第 18 章 节点、调度与容量< 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 >