- 新增 00-why / 01-env / 02-kubectl 三章页面,13 模块骨架,0 处机检不合格 - 第 1 章 7 个 Step 从裸机到 3 节点 kind 集群,3 处双环境分栏 - 第 2 章三遍读集群:discovery / 建命名空间 / diff 与 dry-run - tools/check-pages.js:命令块下限可按章配置、命名空间白名单修正 - 新增 tools/REVIEW.md:机检覆盖不到的人检清单与 S0 复审结论
844 lines
57 KiB
HTML
844 lines
57 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>第 2 章 kubectl 与集群解剖:把黑盒变成可观测对象 · Kubernetes 专题知识库</title>
|
||
<meta name="description" content="用 discovery 摸清集群有哪些 API 资源,把对象拆成 spec 与 status 两半,建出 shop 命名空间并用 events 还原现场,再用选择器、diff 与 dry-run 把 kubectl 变成观测工具。">
|
||
<meta name="keywords" content="kubectl, api-resources, discovery, spec 与 status, namespace, events, field-selector, kubectl diff">
|
||
<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>第 2 章</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><span class="toc-a active" aria-current="page"><i class="cn">2</i>kubectl 与集群解剖:把黑盒变成可观测对象</span><div class="subnav current"><a class="toc-a lv3" href="#sec-2-1">2.1 本章在哪:主线之前与之后</a><a class="toc-a lv3" href="#sec-2-2">2.2 学习目标</a><a class="toc-a lv3" href="#sec-2-3">2.3 场景:三个人,三个集群</a><a class="toc-a lv3" href="#sec-2-4">2.4 心智模型:spec 是你要的,status 是发生的</a><a class="toc-a lv3" href="#sec-2-5">2.5 概念与字段速查</a><a class="toc-a lv3" href="#sec-2-6">2.6 实操:把集群读成资源清单,再改一个对象</a><a class="toc-a lv3" href="#sec-2-7">2.7 验收清单</a><a class="toc-a lv3" href="#sec-2-8">2.8 故障实验室</a><a class="toc-a lv3" href="#sec-2-9">2.9 生产对照</a><a class="toc-a lv3" href="#sec-2-10">2.10 清理与残留检查</a><a class="toc-a lv3" href="#sec-2-11">2.11 小结与自测</a><a class="toc-a lv3" href="#sec-2-12">2.12 衔接</a><a class="toc-a lv3" href="#sec-2-13">2.13 参考与验证版本</a></div><div class="toc-stage"><b>S1</b><span class="h">跑起来:单服务最小闭环</span></div><span class="toc-a todo" title="待写"><i class="cn">3</i>Pod:最小调度单元</span><span class="toc-a todo" title="待写"><i class="cn">4</i>Deployment:副本与滚动更新</span><span class="toc-a todo" title="待写"><i class="cn">5</i>Service 与集群 DNS</span><span class="toc-a todo" title="待写"><i class="cn">6</i>配置与密钥:ConfigMap 与 Secret</span><span class="toc-a todo" title="待写"><i class="cn">7</i>探针与资源:让它活得像生产</span><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><span class="toc-a todo" title="待写"><i class="cn">9</i>入口与网关:Ingress 与 Gateway API</span><span class="toc-a todo" title="待写"><i class="cn">10</i>Job 与 CronJob:迁移与定时任务</span><span class="toc-a todo" title="待写"><i class="cn">11</i>多服务与网络策略</span><div class="toc-stage"><b>S3</b><span class="h">看得见:可观测、发布与排障</span></div><span class="toc-a todo" title="待写"><i class="cn">12</i>日志:从 kubectl logs 到可检索</span><span class="toc-a todo" title="待写"><i class="cn">13</i>指标、扩缩与告警</span><span class="toc-a todo" title="待写"><i class="cn">14</i>发布策略:滚动、蓝绿、金丝雀</span><span class="toc-a todo" title="待写"><i class="cn">15</i>密钥与镜像供应链</span><span class="toc-a todo" title="待写"><i class="cn">16</i>故障诊断方法论</span><div class="toc-stage"><b>S4</b><span class="h">上生产:集群、调度、安全、交付</span></div><span class="toc-a todo" title="待写"><i class="cn">17</i>生产集群搭建:kubeadm HA</span><span class="toc-a todo" title="待写"><i class="cn">18</i>节点、调度与容量</span><span class="toc-a todo" title="待写"><i class="cn">19</i>安全基线:RBAC 与准入</span><span class="toc-a todo" title="待写"><i class="cn">20</i>GitOps:Helm/Kustomize + Argo CD</span><span class="toc-a todo" title="待写"><i class="cn">21</i>CI/CD 流水线</span><span class="toc-a todo" title="待写"><i class="cn">22</i>备份、恢复与升级</span><div class="toc-stage"><b>S5</b><span class="h">守得住:值班、成本与评审</span></div><span class="toc-a todo" title="待写"><i class="cn">23</i>值班与事故响应</span><span class="toc-a todo" title="待写"><i class="cn">24</i>成本与容量</span><span class="toc-a todo" title="待写"><i class="cn">25</i>生产就绪评审与下一步</span></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>S0 起步:环境与心智</span>
|
||
<span>·</span>
|
||
<span>第 <b class="pos">2</b> / 26 章</span>
|
||
</p>
|
||
|
||
<h2 id="ch2"><span class="n">2</span>kubectl 与集群解剖:把黑盒变成可观测对象</h2>
|
||
|
||
<p>第 2 / 26 章 · 阶段 S0 · 难度 ★★☆☆☆ · 约 120 分钟 · 前置:第 1 章</p>
|
||
|
||
<div class="lead">
|
||
<b>30 秒速览:</b>集群里没有「看看到底怎么了」的按钮。本章把 <code>kubectl</code> 变成一套观测顺序:
|
||
先问有哪些资源类型,再看 <code>spec</code> 与 <code>status</code> 各由谁写,然后用 <code>events</code> 还原现场。
|
||
你会建出 <code>shop</code> 命名空间,产出 <code>make verify-ch02</code>。约 120 分钟。
|
||
</div>
|
||
|
||
<h3 id="sec-2-1">2.1 本章在哪:主线之前与之后</h3>
|
||
|
||
<div class="tablewrap">
|
||
<table>
|
||
<thead><tr><th>项</th><th>内容</th></tr></thead>
|
||
<tbody>
|
||
<tr><td>主线之前</td><td>集群能连上,但你说不清里面有哪些资源类型,也说不清这条命令打到了哪个集群</td></tr>
|
||
<tr><td>主线之后</td><td><code>shop</code> 存在且带标签;每条命令都能报出 context 与 namespace;<code>verify-ch02</code> 可判定</td></tr>
|
||
<tr><td>本章产物</td><td><code>deploy/base/namespace.yaml</code>、<code>Makefile</code> 里的 <code>verify-ch02</code></td></tr>
|
||
<tr><td>适用环境</td><td>ENV-A(Win11 + WSL2 + kind)必做;ENV-B 命令相同,差异只在 kubectl 从哪来</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<p>本章不产生工作负载,命令打错的成本极低:放心制造一次报错、看清返回,再改对。第 3 章才往
|
||
<code>shop</code> 放第一个 Pod。</p>
|
||
|
||
<h3 id="sec-2-2">2.2 学习目标</h3>
|
||
|
||
<div class="box note">
|
||
<span class="bt">学完你能</span>
|
||
<ul>
|
||
<li>用 <code>explain</code> 查字段:不翻文档能说出 <code>spec.strategy.type</code> 的默认值。</li>
|
||
<li>说清 <code>spec</code> 与 <code>status</code> 各由谁写:能在 <code>-o yaml</code> 里指出这两段。</li>
|
||
<li>建出 <code>shop</code> 并解释它与 <code>default</code> 的边界。</li>
|
||
<li>60 秒内定位「对象不在预期位置」:context、namespace、集群三层各一条命令排除。</li>
|
||
<li>说清 <code>diff</code> 与 <code>--dry-run=server</code> 的分工:不改集群就能预演变更。</li>
|
||
</ul>
|
||
</div>
|
||
|
||
<h3 id="sec-2-3">2.3 场景:三个人,三个集群</h3>
|
||
|
||
<p>周四 21:10,<code>shop-api</code> 开始返回 502。三个人同时上手,十分钟后得出三个矛盾的结论。甲敲
|
||
<code>kubectl get pods</code>,得到 <code>No resources found in default namespace.</code>,判断 Pod 被删光了。乙看到
|
||
<code>shop-api</code> 好端端 <code>Running</code>,判断是网络问题。丙的终端连不上,只留一行:</p>
|
||
|
||
<pre data-lang="text" data-name="丙的终端"># 预期输出
|
||
The connection to the server localhost:8080 was refused - did you specify the right host or port?</pre>
|
||
|
||
<div class="box danger">
|
||
<span class="bt">现象</span>
|
||
<p>同一台笔记本、同一份 YAML,输出可以完全不同,而且不报错:甲和乙都拿到了「成功」,只是一个落在默认那一格,
|
||
一个落在业务命名空间。</p>
|
||
</div>
|
||
|
||
<p>排障方法留给第 16 章。本章只解决一件事:说出这条命令打到了哪个集群的哪个命名空间。</p>
|
||
|
||
<h3 id="sec-2-4">2.4 心智模型:spec 是你要的,status 是发生的</h3>
|
||
|
||
<figure class="figure">
|
||
<svg viewBox="0 0 720 300" role="img" aria-label="apply 写入 spec,控制器读取 spec 并回写 status">
|
||
<defs>
|
||
<marker id="a2" 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="14" y="104" width="178" height="78" rx="10" style="fill:var(--card);stroke:var(--line)"></rect>
|
||
<text x="103" y="132" text-anchor="middle" font-size="14" style="fill:var(--ink)">你的清单</text>
|
||
<text x="103" y="154" text-anchor="middle" font-size="11.5" style="fill:var(--muted)">apiVersion / kind / spec</text>
|
||
|
||
<rect x="252" y="104" width="196" height="78" rx="10" style="fill:var(--soft);stroke:var(--line)"></rect>
|
||
<text x="350" y="132" text-anchor="middle" font-size="14" style="fill:var(--ink)">API Server</text>
|
||
<text x="350" y="154" text-anchor="middle" font-size="11.5" style="fill:var(--muted)">spec 与 status 都存这里</text>
|
||
|
||
<rect x="514" y="104" width="192" height="78" rx="10" style="fill:var(--brand-soft);stroke:var(--brand)"></rect>
|
||
<text x="610" y="132" text-anchor="middle" font-size="14" style="fill:var(--brand)">控制器</text>
|
||
<text x="610" y="154" text-anchor="middle" font-size="11.5" style="fill:var(--brand)">读 spec,写 status</text>
|
||
|
||
<rect x="252" y="222" width="196" height="48" rx="9" style="fill:var(--card);stroke:var(--line)"></rect>
|
||
<text x="350" y="243" text-anchor="middle" font-size="12.5" style="fill:var(--ink)">kubectl get / describe</text>
|
||
<text x="350" y="261" text-anchor="middle" font-size="11.5" style="fill:var(--muted)">把两半摊开给你看</text>
|
||
|
||
<path d="M192,143 L246,143" style="stroke:var(--muted);fill:none" marker-end="url(#a2)"></path>
|
||
<text x="219" y="134" text-anchor="middle" font-size="11" style="fill:var(--muted)">apply</text>
|
||
|
||
<path d="M448,126 L508,126" style="stroke:var(--muted);fill:none" marker-end="url(#a2)"></path>
|
||
<text x="478" y="118" text-anchor="middle" font-size="11" style="fill:var(--muted)">watch</text>
|
||
|
||
<path d="M508,160 L452,160" style="stroke:var(--muted);fill:none" marker-end="url(#a2)"></path>
|
||
<text x="452" y="177" font-size="11" style="fill:var(--muted)">写 status</text>
|
||
|
||
<path d="M350,182 L350,218" style="stroke:var(--muted);fill:none" marker-end="url(#a2)"></path>
|
||
<text x="356" y="205" font-size="11" style="fill:var(--muted)">get</text>
|
||
|
||
<text x="14" y="290" font-size="11.5" style="fill:var(--muted)">左边你能改;右边每轮协调都会被重写。</text>
|
||
</svg>
|
||
<div class="figcap"><b>图 1 ·</b>一次 apply 只写 spec,status 由控制器回写。</div>
|
||
</figure>
|
||
|
||
<p>见图 1:左边是你写进文件的东西,中间是 API Server(唯一保存对象的地方),右边是控制器。一次 <code>apply</code>
|
||
只完成「左到中」;「中到右、再回中」才是实际发生。所以对象只有两半:<code>spec</code> 是期望状态,
|
||
<code>status</code> 是控制器回写的实际状态。<code>get</code> 只给摘要,<code>-o yaml</code> 摊开两半,
|
||
<code>describe</code> 把 <code>status</code> 翻成人话再附 Events。</p>
|
||
|
||
<div class="tablewrap">
|
||
<table>
|
||
<thead><tr><th>常见误解</th><th>事实</th></tr></thead>
|
||
<tbody>
|
||
<tr><td>「<code>get</code> 看到的字段都能改」</td><td><code>status</code> 归控制器,改了下一轮协调就覆盖</td></tr>
|
||
<tr><td>「<code>-o yaml</code> 存成文件就能 apply」</td><td>输出带 <code>resourceVersion</code> 与整段 <code>status</code>,会被拒</td></tr>
|
||
<tr><td>「命名空间是个目录,写错也能建」</td><td>命名空间是对象,不存在时 apply 直接 <code>NotFound</code></td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<p class="take"><b>一句话:</b><code>spec</code> 是你写的,<code>status</code> 是控制器写的,冲突时以控制器为准。</p>
|
||
|
||
<h3 id="sec-2-5">2.5 概念与字段速查</h3>
|
||
|
||
<div class="tablewrap">
|
||
<table>
|
||
<thead><tr><th>对象 / 字段</th><th>作用</th><th>默认值</th><th>生产建议</th></tr></thead>
|
||
<tbody>
|
||
<tr><td><code>apiVersion</code> / <code>kind</code></td><td>声明对象属于哪个 API 组</td><td>无,必填</td><td>用 <code>explain</code> 查到的写法</td></tr>
|
||
<tr><td><code>metadata.namespace</code></td><td>对象落在哪个命名空间</td><td>context 的 namespace,为空即 <code>default</code></td><td>清单里写死 <code>shop</code></td></tr>
|
||
<tr><td><code>metadata.labels</code></td><td>供选择器筛选的键值对</td><td>空</td><td>至少三个 <code>app.kubernetes.io/*</code></td></tr>
|
||
<tr><td><code>spec</code></td><td>你声明的期望状态</td><td>无</td><td>只写你能解释的字段</td></tr>
|
||
<tr><td><code>status</code></td><td>控制器回写的实际状态</td><td>创建后由控制器写入</td><td>永远不手写、不进 Git</td></tr>
|
||
<tr><td><code>resourceVersion</code></td><td>乐观锁版本号</td><td>服务端生成</td><td>不要复制进清单</td></tr>
|
||
<tr><td><code>kubectl -n</code></td><td>命令作用的命名空间</td><td>context 的 namespace,否则 <code>default</code></td><td>每条命令都带</td></tr>
|
||
<tr><td><code>--field-selector</code></td><td>按内置字段过滤</td><td>无,返回全部</td><td>只用于白名单字段</td></tr>
|
||
<tr><td>Event 保留时长</td><td>事件在 etcd 里活多久</td><td>1 小时(apiserver <code>--event-ttl</code>)</td><td>导出到日志平台</td></tr>
|
||
<tr><td><code>kubectl diff</code> 退出码</td><td>有无差异</td><td><code>0</code> 无、<code>1</code> 有、<code>2</code> 以上出错</td><td>脚本里显式判断</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<p>「默认值」一列比「作用」重要:困惑多半来自不知道不写会发生什么。<code>metadata.namespace</code> 不写时取 context 的
|
||
命名空间,也没有就落到 <code>default</code>。</p>
|
||
|
||
<details>
|
||
<summary>可跳过 · <code>api-resources</code> 那五列怎么读</summary>
|
||
<p><code>SHORTNAMES</code> 是缩写(<code>po</code>、<code>svc</code>、<code>deploy</code>),<code>NAMESPACED</code>
|
||
表示命名空间级还是集群级,<code>KIND</code> 是写进 YAML 的词。同一个 <code>KIND</code> 可能在多个组里,必须配 <code>apiVersion</code>。</p>
|
||
</details>
|
||
|
||
<h3 id="sec-2-6">2.6 实操:把集群读成资源清单,再改一个对象</h3>
|
||
|
||
<p>三遍、七个 Step:第一遍只读,第二遍建东西,第三遍改它。工作目录是 <code>~/work/aurora-shop</code>。</p>
|
||
|
||
<h4>第一遍 · 只读(Step 1–3)</h4>
|
||
<p><span class="tag">意图</span>弄清集群会什么、对象长什么样、命令打到哪。</p>
|
||
<p><b>Step 1 · 校验起点,问集群会什么</b> 先跑 <code>verify-ch01</code>,起点干净,异常才能归因到本章。</p>
|
||
<pre data-lang="bash" data-name="校验并 discovery"># env: all
|
||
make verify-ch01
|
||
kubectl api-resources | head -n 8
|
||
kubectl api-versions | head -n 6
|
||
kubectl explain deployment.spec.strategy</pre>
|
||
<pre data-lang="text" data-name="预期输出"># 预期输出
|
||
[ok] kind-config.yaml 存在
|
||
[ok] context kind-aurora 可用
|
||
[ok] 3 个节点已注册
|
||
[ok] 所有节点 Ready
|
||
[ok] kube-system 全部 Running
|
||
[ok] 80/443 已映射到节点容器
|
||
|
||
NAME SHORTNAMES APIVERSION NAMESPACED KIND
|
||
bindings v1 true Binding
|
||
componentstatuses cs v1 false ComponentStatus
|
||
configmaps cm v1 true ConfigMap
|
||
endpoints ep v1 true Endpoints
|
||
events ev v1 true Event
|
||
limitranges limits v1 true LimitRange
|
||
namespaces ns v1 false Namespace
|
||
nodes no v1 false Node
|
||
|
||
admissionregistration.k8s.io/v1
|
||
apiextensions.k8s.io/v1
|
||
apiregistration.k8s.io/v1
|
||
apps/v1
|
||
authentication.k8s.io/v1
|
||
authorization.k8s.io/v1
|
||
|
||
KIND: Deployment
|
||
VERSION: apps/v1
|
||
|
||
FIELD: strategy <DeploymentStrategy>
|
||
|
||
DESCRIPTION:
|
||
The deployment strategy to use to replace existing pods with new ones.
|
||
|
||
FIELDS:
|
||
type <string>
|
||
Type of deployment. Can be "Recreate" or "RollingUpdate". Default is RollingUpdate.</pre>
|
||
|
||
<p>三个命令回答三件事:有哪些、几个版本、默认值多少,都来自 API Server 的 discovery。
|
||
注意 <code>Default is RollingUpdate</code>:默认值不是「没有」,是「你没看见」。</p>
|
||
|
||
<div class="box warn">
|
||
<span class="bt">易错点 · <code>explain</code> 的路径必须逐层精确</span>
|
||
<p><b>现象</b>:<code>kubectl explain pod.spec.container.resources</code> 报
|
||
<code>error: field "container" does not exist</code>。</p>
|
||
<p><b>原因</b>:走 OpenAPI schema,字段名必须一字不差(是 <code>containers</code>),默认只展开一层。</p>
|
||
<p><b>怎么验证</b>:<code>explain pod.spec.containers.resources</code> 能找到 <code>limits</code>;
|
||
加 <code>--recursive</code> 看整棵树。</p>
|
||
</div>
|
||
|
||
<p><b>Step 2 · 解剖一个对象:spec 与 status</b> 在真实对象上看见那两半的分界。</p>
|
||
<pre data-lang="bash" data-name="读一个节点"># env: all
|
||
kubectl get node aurora-worker -o wide
|
||
kubectl describe node aurora-worker | head -n 18
|
||
kubectl get node aurora-worker -o yaml | grep -nE '^(spec|status):'
|
||
kubectl get node aurora-worker -o jsonpath='{.status.capacity.cpu}{" "}{.status.nodeInfo.containerRuntimeVersion}{"\n"}'</pre>
|
||
<pre data-lang="text" data-name="预期输出"># 预期输出
|
||
NAME STATUS ROLES AGE VERSION INTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
|
||
aurora-worker Ready <none> 23m v1.36.1 172.18.0.3 Ubuntu 24.04.3 LTS 6.6.87.2-microsoft-standard-WSL2 containerd://2.1.3
|
||
|
||
Name: aurora-worker
|
||
Roles: <none>
|
||
Labels: kubernetes.io/hostname=aurora-worker
|
||
Capacity:
|
||
cpu: 8
|
||
memory: 16384256Ki
|
||
pods: 110
|
||
Allocatable:
|
||
cpu: 8
|
||
memory: 16281856Ki
|
||
Conditions:
|
||
Type Status
|
||
MemoryPressure False
|
||
Ready True
|
||
|
||
513:spec:
|
||
551:status:
|
||
|
||
8 containerd://2.1.3</pre>
|
||
|
||
<p><code>spec</code> 从第 513 行起只有 <code>podCIDR</code>、<code>providerID</code>;<code>status</code> 从第 551 行起,
|
||
容量、条件、镜像全在里面。节点上你几乎改不了什么,都由 kubelet 写回。第 18 章判断能否放 Pod,
|
||
看的就是 <code>status.allocatable</code>。<code>-o jsonpath</code> 只取一个值:<code>{.status.capacity.cpu}</code>
|
||
取核数,<code>{"\n"}</code> 是换行,没有它多个值会粘成一行。</p>
|
||
|
||
<div class="box warn">
|
||
<span class="bt">易错点 · <code>-o yaml</code> 的输出不能直接 apply</span>
|
||
<p><b>现象</b>:<code>get node aurora-worker -o yaml > node.yaml</code> 后 apply,报
|
||
<code>(Conflict): Operation cannot be fulfilled on nodes "aurora-worker": the object has been modified</code>。</p>
|
||
<p><b>原因</b>:输出带着 <code>resourceVersion</code>、<code>uid</code> 与整段 <code>status</code>,<code>apply</code>
|
||
把它当成「更新已有对象」,版本号对不上就判冲突。</p>
|
||
<p><b>怎么验证</b>:<code>apply --dry-run=server -f node.yaml</code> 同样报错;删掉 <code>resourceVersion</code>、
|
||
<code>managedFields</code> 与 <code>status</code> 才可用。</p>
|
||
</div>
|
||
|
||
<p><b>Step 3 · 确认你在跟哪个集群说话</b> 命令打到哪由 <code>current-context</code> 决定。</p>
|
||
<pre data-lang="bash" data-name="看 context"># env: all
|
||
kubectl config get-contexts
|
||
kubectl config current-context
|
||
kubectl config view --minify -o yaml | sed -n '/^contexts:/,$p'
|
||
kubectl --kubeconfig ~/.kube/config config current-context</pre>
|
||
<pre data-lang="text" data-name="预期输出"># 预期输出
|
||
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
|
||
* kind-aurora kind-aurora kind-aurora
|
||
kind-aurora
|
||
contexts:
|
||
- context:
|
||
cluster: kind-aurora
|
||
user: kind-aurora
|
||
name: kind-aurora
|
||
current-context: kind-aurora
|
||
kind-aurora</pre>
|
||
|
||
<p><code>contexts</code> 里没有 <code>namespace</code> 字段。没写就是空,空就是 <code>default</code>——2.3 节的甲就是这么
|
||
落过去的。</p>
|
||
|
||
<figure class="figure">
|
||
<svg viewBox="0 0 720 300" role="img" aria-label="kubeconfig 中的 context 把集群、身份与默认命名空间绑在一起">
|
||
<defs>
|
||
<marker id="a3" 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="14" y="30" width="178" height="240" rx="10" style="fill:var(--soft);stroke:var(--line)"></rect>
|
||
<text x="103" y="56" text-anchor="middle" font-size="13" style="fill:var(--ink)">~/.kube/config</text>
|
||
<text x="28" y="94" font-size="11.5" style="fill:var(--muted)">clusters:</text>
|
||
<text x="28" y="114" font-size="11.5" style="fill:var(--muted)">- kind-aurora</text>
|
||
<text x="28" y="144" font-size="11.5" style="fill:var(--muted)">users:</text>
|
||
<text x="28" y="164" font-size="11.5" style="fill:var(--muted)">- kind-aurora</text>
|
||
<text x="28" y="194" font-size="11.5" style="fill:var(--muted)">contexts:</text>
|
||
<text x="28" y="224" font-size="11.5" style="fill:var(--muted)">current-context:</text>
|
||
<text x="28" y="244" font-size="11.5" style="fill:var(--muted)">kind-aurora</text>
|
||
|
||
<rect x="252" y="44" width="210" height="92" rx="10" style="fill:var(--card);stroke:var(--brand)"></rect>
|
||
<text x="357" y="74" text-anchor="middle" font-size="13" style="fill:var(--brand)">context kind-aurora</text>
|
||
<text x="357" y="98" text-anchor="middle" font-size="11.5" style="fill:var(--muted)">cluster: kind-aurora</text>
|
||
<text x="357" y="118" text-anchor="middle" font-size="11.5" style="fill:var(--muted)">namespace: 未设置</text>
|
||
|
||
<rect x="252" y="176" width="210" height="92" rx="10" style="fill:var(--card);stroke:var(--line)" stroke-dasharray="6 5"></rect>
|
||
<text x="357" y="206" text-anchor="middle" font-size="13" style="fill:var(--muted)">context kind-staging</text>
|
||
<text x="357" y="230" text-anchor="middle" font-size="11.5" style="fill:var(--muted)">cluster: kind-staging</text>
|
||
<text x="357" y="250" text-anchor="middle" font-size="11.5" style="fill:var(--muted)">namespace: platform</text>
|
||
|
||
<rect x="524" y="96" width="182" height="104" rx="10" style="fill:var(--brand-soft);stroke:var(--brand)"></rect>
|
||
<text x="615" y="132" text-anchor="middle" font-size="14" style="fill:var(--brand)">kubectl</text>
|
||
<text x="615" y="156" text-anchor="middle" font-size="11.5" style="fill:var(--brand)">只认 current-context</text>
|
||
|
||
<path d="M192,108 C 220,108 224,90 248,90" style="stroke:var(--muted);fill:none" marker-end="url(#a3)"></path>
|
||
<path d="M192,206 C 220,206 224,222 248,222" style="stroke:var(--muted);fill:none" marker-end="url(#a3)"></path>
|
||
|
||
<path d="M462,90 L518,124" style="stroke:var(--brand);fill:none" marker-end="url(#a3)"></path>
|
||
<text x="470" y="80" font-size="11" style="fill:var(--brand)">现在用它</text>
|
||
|
||
<path d="M462,222 L518,176" style="stroke:var(--danger);fill:none" stroke-dasharray="6 5" marker-end="url(#a3)"></path>
|
||
<text x="360" y="292" text-anchor="middle" font-size="11.5" style="fill:var(--danger)">切到 kind-staging,命令就打到另一个集群的 platform</text>
|
||
</svg>
|
||
<div class="figcap"><b>图 2 ·</b>context 把集群、身份、默认命名空间绑成一组,current-context 决定去向。</div>
|
||
</figure>
|
||
|
||
<p>见图 2:<code>kubeconfig</code> 并列存放 clusters、users、contexts 三段,context 是组合键。多集群最危险的是切过去了你忘了。<code>--kubeconfig</code> 指定读哪份文件,<code>KUBECONFIG</code> 可给多个路径(冒号分隔,靠前优先)。</p>
|
||
|
||
<div class="grid2">
|
||
<div class="box">
|
||
<h4>Windows 11 + WSL2</h4>
|
||
<pre data-lang="bash" data-name="确认 WSL 里这个 kubectl 是谁"># env: wsl
|
||
which kubectl && kubectl version --client</pre>
|
||
<p>由 apt 源装上(第 1 章)。Windows 侧 Docker Desktop 另带 <code>kubectl.exe</code>,不是同一个程序,也不是同一份 kubeconfig。</p>
|
||
</div>
|
||
<div class="box">
|
||
<h4>Linux (Ubuntu 24.04)</h4>
|
||
<pre data-lang="bash" data-name="官方二进制安装"># env: linux
|
||
curl -LO https://dl.k8s.io/release/v1.36.1/bin/linux/amd64/kubectl && sudo install -m 0755 kubectl /usr/local/bin/kubectl</pre>
|
||
<p>单机直接用官方二进制,版本号写死,不用发行版仓库里的旧包。</p>
|
||
</div>
|
||
</div>
|
||
<p><b>差异结论</b>:装法不同,要盯的是同一件事——与服务端相差不超过一个 minor。Windows 的 <code>kubectl.exe</code>
|
||
与 WSL 的是两套 kubeconfig,同一条命令可能打到不同的地方。</p>
|
||
|
||
<p><b>不一致怎么办</b>:先看 <code>verify-ch01</code> 哪行是 <code>[fail]</code>,回第 1 章修;补丁版本号差一档不影响后续。</p>
|
||
|
||
<h4>第二遍 · 建出 shop(Step 4–5)</h4>
|
||
<p><span class="tag">意图</span>先划边界再放东西,再让事件把「刚才发生了什么」讲出来。</p>
|
||
<p><b>Step 4 · 建出 shop,并故意拼错一次</b> 拼错是故意的:它把两种「空」分开。</p>
|
||
<pre data-lang="bash" data-name="建命名空间"># env: all
|
||
kubectl create namespace shop
|
||
kubectl get ns
|
||
kubectl get ns shop --show-labels</pre>
|
||
<pre data-lang="text" data-name="预期输出"># 预期输出
|
||
namespace/shop created
|
||
NAME STATUS AGE
|
||
default Active 22m
|
||
kube-node-lease Active 22m
|
||
kube-public Active 22m
|
||
kube-system Active 22m
|
||
local-path-storage Active 22m
|
||
shop Active 4s
|
||
|
||
NAME STATUS AGE LABELS
|
||
shop Active 12s <none></pre>
|
||
|
||
<p>最后一列的 <code><none></code> 值得记住:命令建出来的命名空间没有标签,也没有配额,标签等第 7 步补。</p>
|
||
|
||
<div class="tablewrap">
|
||
<table>
|
||
<thead><tr><th>命名空间</th><th>里面有什么</th><th>你该不该动</th></tr></thead>
|
||
<tbody>
|
||
<tr><td><code>default</code></td><td>不指定命名空间时的落点</td><td>可以读,不要放业务对象</td></tr>
|
||
<tr><td><code>kube-system</code></td><td>apiserver、etcd、coredns、kube-proxy、kindnet</td><td>只看,删对象等于拆集群</td></tr>
|
||
<tr><td><code>kube-public</code></td><td>公共信息,通常只有 <code>cluster-info</code></td><td>不用动</td></tr>
|
||
<tr><td><code>kube-node-lease</code></td><td>每节点一个 Lease,用来判断节点是否活着</td><td>不用动,第 18 章会再看</td></tr>
|
||
<tr><td><code>shop</code></td><td>你的业务对象,本章里还是空的</td><td>本章唯一要维护的边界</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<p>为什么单独建 <code>shop</code>?三个理由都跟边界有关:删除命名空间会连带删除其中的对象,边界就是回滚单位;
|
||
配额、网络策略、RBAC 都按命名空间挂(第 19 章);<code>-n shop</code> 写出来,作用域不靠记忆。</p>
|
||
|
||
<pre data-lang="bash" data-name="负向:拼错命名空间"># env: all
|
||
kubectl get pods -n sho
|
||
kubectl get pods -n shop
|
||
kubectl get events -n shop</pre>
|
||
<pre data-lang="text" data-name="预期输出"># 预期输出
|
||
Error from server (NotFound): namespaces "sho" not found
|
||
No resources found in shop namespace.
|
||
No resources found in shop namespace.</pre>
|
||
|
||
<p>两种输出对应两种结论:<code>NotFound</code> 是没建,<code>No resources found</code> 是建了但空。分不清这两句,
|
||
就会在「对象被删了」和「我打错字了」之间横跳。第三条为空还有一层原因:<code>events</code> 挂在对象上。</p>
|
||
|
||
<p><b>Step 5 · 让 events 说话</b> 建立「先看事件时间线」的条件反射。</p>
|
||
<pre data-lang="bash" data-name="看事件"># env: all
|
||
kubectl get events -A --sort-by=.lastTimestamp | tail -n 8
|
||
kubectl get events -n kube-system --field-selector type=Warning
|
||
kubectl get events -A --field-selector involvedObject.name=aurora-worker</pre>
|
||
<pre data-lang="text" data-name="预期输出"># 预期输出
|
||
NAMESPACE LAST SEEN TYPE REASON OBJECT MESSAGE
|
||
kube-system 2m41s Normal Scheduled pod/coredns-6f7c9d8b5-4m2xk Successfully assigned kube-system/coredns-6f7c9d8b5-4m2xk to aurora-control-plane
|
||
kube-system 2m38s Normal Pulled pod/coredns-6f7c9d8b5-4m2xk Container image "registry.k8s.io/coredns/coredns:v1.12.1" already present on machine
|
||
kube-system 2m36s Normal Created pod/kindnet-x7k2p Created container kindnet-cni
|
||
default 48s Normal Starting node/aurora-worker Starting kubelet.
|
||
default 41s Normal NodeReady node/aurora-worker Node aurora-worker status is now: NodeReady
|
||
|
||
No resources found in kube-system namespace.
|
||
|
||
NAMESPACE LAST SEEN TYPE REASON OBJECT MESSAGE
|
||
default 41s Normal NodeReady node/aurora-worker Node aurora-worker status is now: NodeReady
|
||
default 48s Normal Starting node/aurora-worker Starting kubelet.</pre>
|
||
|
||
<p>事件是唯一按时间记录「尝试」的对象:调度器试过放哪台、拉镜像成没成、挂载为什么失败。两个易踩属性:
|
||
作用域在命名空间(节点事件落在 <code>default</code>),默认只留 1 小时。所以「Pod 起不来先看 events」是证据链问题。
|
||
</p>
|
||
|
||
<p>等第 3 章有了 Pod,顺序会固定成:<code>get pods</code> 找谁不正常 →
|
||
<code>get events -n shop --sort-by=.lastTimestamp</code> 看最后发生了什么 → <code>describe pod</code> 看 spec、status
|
||
与 Events → <code>logs</code> 看进程说了什么。<code>get</code> 只告诉你 <code>Pending</code>,原因全在事件里。</p>
|
||
|
||
<details>
|
||
<summary>可跳过 · 事件为什么只留一小时</summary>
|
||
<p>Event 是普通 API 对象,写在 etcd 里,由 <code>--event-ttl</code> 控制保留时长,默认 <code>1h</code>;每分钟都有事件,
|
||
长期保留会撑爆 etcd。后果是事故复盘常晚于 1 小时,那时 <code>get events</code> 已经空了,所以生产上要把 Warning
|
||
实时导出到日志平台(第 12 章)。</p>
|
||
</details>
|
||
|
||
<p class="take"><b>一句话:</b>对象说「现在是什么样」,事件说「为什么变成这样」,只看前者你永远在猜。</p>
|
||
|
||
<div class="box tip">
|
||
<span class="bt">预告 · 三条只读命令</span>
|
||
<p>三条都不改集群状态。<code>shop</code> 还空着,第 3 章有 Pod 才能跑;组合成排障套路是第 16 章的事。</p>
|
||
<pre data-lang="bash" data-name="logs / exec / port-forward"># env: all
|
||
kubectl logs deploy/shop-api -n shop
|
||
kubectl exec -it deploy/shop-api -n shop -- sh
|
||
kubectl port-forward -n shop svc/shop-api 8080:8080</pre>
|
||
</div>
|
||
|
||
<p><b>不一致怎么办</b>:<code>AlreadyExists</code> 说明命名空间早建好了,跳过;事件时间戳与本机不同属正常。</p>
|
||
|
||
<h4>第三遍 · 先筛再改(Step 6–7)</h4>
|
||
<p><span class="tag">意图</span>从「按名字找一个」升级到「按条件找一批」,改之前先看差异。</p>
|
||
<p><b>Step 6 · 选择器:<code>-l</code> 与 <code>--field-selector</code></b> 排障按状态筛,业务查询按标签筛。</p>
|
||
<pre data-lang="bash" data-name="两种筛法"># env: all
|
||
kubectl get nodes -L kubernetes.io/hostname,node-role.kubernetes.io/control-plane
|
||
kubectl get nodes -l node-role.kubernetes.io/control-plane
|
||
kubectl get nodes --field-selector metadata.name=aurora-worker
|
||
kubectl get pods -n kube-system --field-selector status.phase!=Running</pre>
|
||
<pre data-lang="text" data-name="预期输出"># 预期输出
|
||
NAME STATUS ROLES AGE VERSION HOSTNAME CONTROL-PLANE
|
||
aurora-control-plane Ready control-plane 24m v1.36.1 aurora-control-plane
|
||
aurora-worker Ready <none> 23m v1.36.1 aurora-worker
|
||
|
||
NAME STATUS ROLES AGE VERSION
|
||
aurora-control-plane Ready control-plane 24m v1.36.1
|
||
|
||
NAME STATUS ROLES AGE VERSION
|
||
aurora-worker Ready <none> 23m v1.36.1
|
||
|
||
No resources found in kube-system namespace.</pre>
|
||
|
||
<p>最后一条在刚建好的集群里是空的,因为 <code>kube-system</code> 的 Pod 全在 Running;一旦有 Pod 卡在
|
||
Pending 或 Completed,它就会把行列出来——这正是排障时最快的那一刀。</p>
|
||
|
||
<p>第二条只选出一行,但 <code>CONTROL-PLANE</code> 列是空的——这个标签的值就是空字符串。<code>-l</code> 判「键存在」,
|
||
不判「键等于某值」,所以空值也能选中。</p>
|
||
|
||
<div class="tablewrap">
|
||
<table>
|
||
<thead><tr><th>维度</th><th>标签选择器 <code>-l</code></th><th>字段选择器 <code>--field-selector</code></th></tr></thead>
|
||
<tbody>
|
||
<tr><td>筛的是什么</td><td><code>metadata.labels</code> 里的键值</td><td>对象的内置字段</td></tr>
|
||
<tr><td>可用字段</td><td>任何标签键,随对象自定义</td><td>预声明的白名单,写错直接报错</td></tr>
|
||
<tr><td>Pod 上常用</td><td><code>app.kubernetes.io/name=shop-api</code></td><td><code>status.phase=Running</code>、<code>spec.nodeName=aurora-worker</code></td></tr>
|
||
<tr><td>操作符</td><td><code>=</code>、<code>!=</code>、<code>in</code>、<code>notin</code>、存在性</td><td>只有 <code>=</code>、<code>!=</code></td></tr>
|
||
<tr><td>筛不到时</td><td>静默返回 <code>No resources found</code></td><td>不在白名单则报 <code>field label not supported</code></td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<p>排障用字段选择器,业务查询用标签。<code>get pods -w</code> 会挂住持续输出变化,
|
||
写进脚本必须套 <code>timeout</code>。</p>
|
||
|
||
<div class="box warn">
|
||
<span class="bt">易错点 · <code>--field-selector</code> 可以按标签筛</span>
|
||
<p><b>现象</b>:<code>kubectl get pods -n kube-system --field-selector k8s-app=kube-dns</code> 报
|
||
<code>Error from server (BadRequest): field label not supported: k8s-app</code>。</p>
|
||
<p><b>原因</b>:字段选择器只认资源预声明的白名单。Pod 上有 <code>metadata.name</code>、<code>spec.nodeName</code>、
|
||
<code>status.phase</code>、<code>status.podIP</code>,标签不在其中。</p>
|
||
<p><b>怎么验证</b>:换成 <code>-l k8s-app=kube-dns</code> 能返回 Pod;<code>--field-selector status.phase=Running</code>
|
||
也能返回。</p>
|
||
</div>
|
||
|
||
<p><b>Step 7 · 先预演,再 apply</b> 把「apply 完再看」改成「看清差异再 apply」,并把 Step 4 的命令固化成文件。</p>
|
||
|
||
<pre data-lang="yaml" data-name="deploy/base/namespace.yaml"># file: deploy/base/namespace.yaml
|
||
apiVersion: v1
|
||
kind: Namespace
|
||
metadata:
|
||
name: shop
|
||
labels:
|
||
app.kubernetes.io/name: shop
|
||
app.kubernetes.io/part-of: aurora-shop</pre>
|
||
|
||
<p>与现状差两处:多了两个标签,并且它归 Git 管了。</p>
|
||
|
||
<pre data-lang="bash" data-name="先看差异,再试运行"># env: all
|
||
kubectl diff -f deploy/base/namespace.yaml; echo "exit=$?"
|
||
kubectl apply --dry-run=server -f deploy/base/namespace.yaml</pre>
|
||
<pre data-lang="text" data-name="预期输出"># 预期输出
|
||
diff -u -N /tmp/LIVE-4012/v1.Namespace.shop /tmp/MERGED-4013/v1.Namespace.shop
|
||
--- /tmp/LIVE-4012/v1.Namespace.shop
|
||
+++ /tmp/MERGED-4013/v1.Namespace.shop
|
||
@@ -3,6 +3,10 @@
|
||
metadata:
|
||
creationTimestamp: "2026-11-08T06:12:44Z"
|
||
+ labels:
|
||
+ app.kubernetes.io/name: shop
|
||
+ app.kubernetes.io/part-of: aurora-shop
|
||
name: shop
|
||
resourceVersion: "1842"
|
||
exit=1
|
||
namespace/shop configured (server dry run)</pre>
|
||
|
||
<p><code>diff</code> 把「服务端现在是什么」与清单做文本对比;<code>--dry-run=server</code> 让 API Server 走一遍准入与配额检查,
|
||
不落盘。分工:<code>diff</code> 说「会改什么」,dry-run 说「能不能过」。</p>
|
||
|
||
<pre data-lang="bash" data-name="确认后再 apply"># env: all
|
||
kubectl apply -f deploy/base/namespace.yaml
|
||
kubectl diff -f deploy/base/namespace.yaml; echo "exit=$?"</pre>
|
||
<pre data-lang="text" data-name="预期输出"># 预期输出
|
||
namespace/shop configured
|
||
exit=0</pre>
|
||
|
||
<p>第二次 <code>diff</code> 退出码为 <code>0</code>,这就是「集群与 Git 一致」的机器可判定形式。</p>
|
||
|
||
<div class="box warn">
|
||
<span class="bt">易错点 · <code>diff</code> 的退出码 1 会打断脚本</span>
|
||
<p><b>现象</b>:CI 里 <code>kubectl diff -f deploy/</code> 在 <code>set -e</code> 下失败,日志只有一段 diff。</p>
|
||
<p><b>原因</b>:它沿用 Unix <code>diff</code> 语义:<code>0</code> 无差异、<code>1</code> 有差异、<code>2</code> 以上才是真错误。
|
||
「有差异」在语义上是成功,在退出码上是失败。</p>
|
||
<p><b>怎么验证</b>:<code>kubectl diff -f deploy/base/namespace.yaml; echo "exit=$?"</code> 有差异为 <code>1</code>,
|
||
apply 之后为 <code>0</code>。</p>
|
||
</div>
|
||
|
||
<div class="grid2">
|
||
<div class="box">
|
||
<h4>Windows 11 + WSL2</h4>
|
||
<pre data-lang="bash" data-name="WSL 默认是 bash"># env: wsl
|
||
echo 'source <(kubectl completion bash)' >> ~/.bashrc</pre>
|
||
<p>重开终端生效。之后 <code>kubectl get na</code> 按 Tab 补成 <code>namespaces</code>,写错的机会少一半。</p>
|
||
</div>
|
||
<div class="box">
|
||
<h4>Linux (Ubuntu 24.04)</h4>
|
||
<pre data-lang="bash" data-name="桌面版常换 zsh"># env: linux
|
||
echo 'source <(kubectl completion zsh)' >> ~/.zshrc</pre>
|
||
<p>zsh 要先 <code>autoload -Uz compinit && compinit</code> 才会注册补全;只加一行 source,按 Tab 什么也不会发生。</p>
|
||
</div>
|
||
</div>
|
||
<p><b>差异结论</b>:差异在 shell——WSL 默认 bash,Ubuntu 桌面常是 zsh,而 zsh 少了 compinit 就静默失效。</p>
|
||
|
||
<pre data-lang="bash" data-name="提交本章产物"># env: all
|
||
git add deploy/base/namespace.yaml Makefile
|
||
git commit -m "ch02: 建 shop 命名空间,固化 verify-ch02"</pre>
|
||
|
||
<p><b>不一致怎么办</b>:<code>diff</code> 的行号与临时目录每次不同,只要 <code>+</code> 行是那两个标签就对了;补全没反应先 <code>source</code> 一次 rc 文件。</p>
|
||
|
||
<h3 id="sec-2-7">2.7 验收清单</h3>
|
||
|
||
<ol class="steps">
|
||
<li><code>kubectl config current-context</code> → <code>kind-aurora</code></li>
|
||
<li><code>kubectl get ns shop -o jsonpath='{.status.phase}'</code> → <code>Active</code></li>
|
||
<li><code>kubectl get ns shop --show-labels</code> → 含 <code>app.kubernetes.io/name=shop</code></li>
|
||
<li>反向验证:<code>get pods -n sho</code> 必须报 <code>namespaces "sho" not found</code>,「空」与「不存在」是两回事</li>
|
||
<li><code>kubectl diff -f deploy/base/namespace.yaml; echo $?</code> → <code>0</code>,集群与 Git 一致</li>
|
||
<li><code>make verify-ch02</code> 全绿</li>
|
||
</ol>
|
||
|
||
<pre data-lang="bash" data-name="一次跑完"># env: all
|
||
make verify-ch02</pre>
|
||
<pre data-lang="text" data-name="预期输出"># 预期输出
|
||
[ok] context kind-aurora
|
||
[ok] api server reachable (v1.36.1)
|
||
[ok] kubectl client v1.36.1
|
||
[ok] namespace shop exists (Active)
|
||
[ok] namespace shop labelled app.kubernetes.io/name=shop</pre>
|
||
|
||
<pre data-lang="text" data-name="Makefile 里新增的目标"># file: Makefile
|
||
verify-ch02:
|
||
kubectl config current-context | grep -qx kind-aurora
|
||
kubectl get ns shop -o jsonpath='{.status.phase}' | grep -qx Active
|
||
kubectl get ns shop -o jsonpath='{.metadata.labels.app\.kubernetes\.io/name}' | grep -qx shop</pre>
|
||
|
||
<h3 id="sec-2-8">2.8 故障实验室</h3>
|
||
|
||
<p>三个故障的症状都像「连不上」或「东西不见了」,答案分属三层:配置、context、集群。见图 3。</p>
|
||
|
||
<figure class="figure">
|
||
<svg viewBox="0 0 720 340" role="img" aria-label="从连不上到配置、context、集群三层的判定树">
|
||
<defs>
|
||
<marker id="a4" 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="250" y="8" width="220" height="44" rx="9" style="fill:var(--card);stroke:var(--line)"></rect>
|
||
<text x="360" y="36" text-anchor="middle" font-size="13" style="fill:var(--ink)">连不上 API Server</text>
|
||
|
||
<path d="M360,52 L360,60" style="stroke:var(--muted);fill:none" marker-end="url(#a4)"></path>
|
||
|
||
<polygon points="360,62 470,96 360,130 250,96" style="fill:var(--soft);stroke:var(--line)"></polygon>
|
||
<text x="360" y="101" text-anchor="middle" font-size="12.5" style="fill:var(--ink)">配置读到了吗</text>
|
||
|
||
<path d="M250,96 L216,96" style="stroke:var(--danger);fill:none" marker-end="url(#a4)"></path>
|
||
<text x="228" y="88" text-anchor="middle" font-size="11" style="fill:var(--danger)">否</text>
|
||
<rect x="20" y="72" width="190" height="48" rx="8" style="fill:var(--warn-bg);stroke:var(--warn)"></rect>
|
||
<text x="115" y="92" text-anchor="middle" font-size="12" style="fill:var(--warn)">看 $KUBECONFIG 指向哪</text>
|
||
<text x="115" y="110" text-anchor="middle" font-size="11.5" style="fill:var(--warn)">文件到底在不在</text>
|
||
|
||
<path d="M360,130 L360,158" style="stroke:var(--muted);fill:none" marker-end="url(#a4)"></path>
|
||
|
||
<polygon points="360,162 470,196 360,230 250,196" style="fill:var(--soft);stroke:var(--line)"></polygon>
|
||
<text x="360" y="201" text-anchor="middle" font-size="12.5" style="fill:var(--ink)">context 与 ns 对吗</text>
|
||
|
||
<path d="M470,196 L496,196" style="stroke:var(--danger);fill:none" marker-end="url(#a4)"></path>
|
||
<text x="483" y="188" text-anchor="middle" font-size="11" style="fill:var(--danger)">否</text>
|
||
<rect x="500" y="172" width="200" height="48" rx="8" style="fill:var(--warn-bg);stroke:var(--warn)"></rect>
|
||
<text x="600" y="192" text-anchor="middle" font-size="12" style="fill:var(--warn)">config use-context</text>
|
||
<text x="600" y="210" text-anchor="middle" font-size="11.5" style="fill:var(--warn)">或显式加 -n shop</text>
|
||
|
||
<path d="M360,230 L360,258" style="stroke:var(--muted);fill:none" marker-end="url(#a4)"></path>
|
||
|
||
<polygon points="360,262 470,296 360,330 250,296" style="fill:var(--soft);stroke:var(--line)"></polygon>
|
||
<text x="360" y="301" text-anchor="middle" font-size="12.5" style="fill:var(--ink)">集群在跑吗</text>
|
||
|
||
<path d="M250,296 L216,296" style="stroke:var(--danger);fill:none" marker-end="url(#a4)"></path>
|
||
<text x="228" y="288" text-anchor="middle" font-size="11" style="fill:var(--danger)">否</text>
|
||
<rect x="20" y="272" width="190" height="48" rx="8" style="fill:var(--warn-bg);stroke:var(--warn)"></rect>
|
||
<text x="115" y="292" text-anchor="middle" font-size="12" style="fill:var(--warn)">docker ps</text>
|
||
<text x="115" y="310" text-anchor="middle" font-size="11.5" style="fill:var(--warn)">kind get clusters</text>
|
||
|
||
<path d="M470,296 L496,296" style="stroke:var(--muted);fill:none" marker-end="url(#a4)"></path>
|
||
<text x="483" y="288" text-anchor="middle" font-size="11" style="fill:var(--muted)">是</text>
|
||
<rect x="500" y="272" width="200" height="48" rx="8" style="fill:var(--ok-bg);stroke:var(--ok)"></rect>
|
||
<text x="600" y="292" text-anchor="middle" font-size="12" style="fill:var(--ok)">接着看 events</text>
|
||
<text x="600" y="310" text-anchor="middle" font-size="11.5" style="fill:var(--ok)">与 describe</text>
|
||
</svg>
|
||
<div class="figcap"><b>图 3 ·</b>同一个「连不上」的症状,答案落在配置、context、集群哪一层。</div>
|
||
</figure>
|
||
|
||
<p>见图 3:三层有顺序——先看配置读到了,再看 context 指向哪,最后看集群还在不在。</p>
|
||
|
||
<div class="box danger">
|
||
<span class="bt">故障 1 · 配置读到了,但是空的</span>
|
||
<p><b>注入</b>:<code>touch ~/.kube/empty && export KUBECONFIG=~/.kube/empty</code>,再敲
|
||
<code>kubectl get ns shop</code>。</p>
|
||
<p><b>症状</b>:<code>The connection to the server localhost:8080 was refused - did you specify the right host or port?</code>
|
||
而 <code>~/.kube/config</code> 完好。</p>
|
||
<p><b>诊断</b>:① <code>echo $KUBECONFIG</code>;② <code>kubectl config view</code> 输出为空即坐实;
|
||
③ <code>unset KUBECONFIG</code>。</p>
|
||
<p><b>根因</b>:<code>KUBECONFIG</code> 存在但内容为空时没有可用配置,<code>kubectl</code> 退回内置的
|
||
<code>localhost:8080</code>,那个端口上没有 apiserver。</p>
|
||
<p><b>修复</b>:<code>unset KUBECONFIG</code>,或 <code>kubectl --kubeconfig ~/.kube/config get ns shop</code>。</p>
|
||
<p><b>预防</b>:不要在 <code>~/.bashrc</code> 里全局 export <code>KUBECONFIG</code>。</p>
|
||
</div>
|
||
|
||
<div class="box danger">
|
||
<span class="bt">故障 2 · 对象还在,命令看不见它</span>
|
||
<p><b>注入</b>:<code>kubectl config set-context --current --namespace=shop</code>(前一天为省事改的),再敲不带
|
||
<code>-n</code> 的 <code>kubectl get pods</code> 去看 coredns。</p>
|
||
<p><b>症状</b>:<code>No resources found in shop namespace.</code> 于是判断「coredns 全挂了」。换个人看却是满屏
|
||
<code>Running</code>。</p>
|
||
<p><b>诊断</b>:① <code>kubectl config view --minify -o yaml</code> 看 <code>contexts</code> 里的 <code>namespace</code>;
|
||
② <code>kubectl get pods -A</code> 全局扫一遍。</p>
|
||
<p><b>根因</b>:默认命名空间来自 context,不来自你脑子里的项目结构。context 被改过,所有不带 <code>-n</code>
|
||
的命令都静默换了作用域。</p>
|
||
<p><b>修复</b>:<code>kubectl get pods -n kube-system</code>,或
|
||
<code>kubectl config set-context --current --namespace=kube-system</code>。</p>
|
||
<p><b>预防</b>:脚本与 Makefile 一律显式带 <code>-n</code>;多人共用的机器不要共用 kubeconfig。</p>
|
||
</div>
|
||
|
||
<div class="box danger">
|
||
<span class="bt">故障 3 · 集群根本没在跑</span>
|
||
<p><b>注入</b>:<code>docker stop aurora-control-plane</code>(模拟 Docker Desktop 重启后容器没起来),再敲
|
||
<code>kubectl get ns</code>。</p>
|
||
<p><b>症状</b>:<code>Unable to connect to the server: dial tcp 127.0.0.1:6443: connect: connection refused</code>,
|
||
端口是 <code>6443</code> 而不是 <code>8080</code>——配置读到了,对端不在。</p>
|
||
<p><b>诊断</b>:① <code>kind get clusters</code> 仍列出 <code>aurora</code>,所以「集群存在」会误导你;
|
||
② <code>docker ps -a --filter name=aurora</code> 看到容器 <code>Exited</code>。</p>
|
||
<p><b>根因</b>:kind 的「集群」就是一组 docker 容器,apiserver 跑在控制面容器里。容器停了集群就停,
|
||
而 kubeconfig 的地址不变,于是报「连接被拒绝」。</p>
|
||
<p><b>修复</b>:<code>docker start aurora-control-plane aurora-worker aurora-worker2</code>,再
|
||
<code>kubectl wait --for=condition=Ready node/aurora-control-plane --timeout=120s</code>。</p>
|
||
<p><b>预防</b>:用 <code>make up</code> 这类幂等脚本启动集群;见到 <code>connection refused</code> 先看
|
||
<code>docker ps</code>(第 1 章)。</p>
|
||
</div>
|
||
|
||
<h3 id="sec-2-9">2.9 生产对照</h3>
|
||
|
||
<div class="tablewrap">
|
||
<table>
|
||
<thead><tr><th>维度</th><th>学习环境(kind)</th><th>生产环境</th><th>为什么不同</th><th>迁移动作</th></tr></thead>
|
||
<tbody>
|
||
<tr><td>访问路径</td><td>本机 kubectl 直连 <code>127.0.0.1:6443</code></td><td>跳板机 + 审计代理,或云 CLI 换临时凭据</td><td>apiserver 暴露在公网,凭据泄露等于集群失守</td><td>换成短时凭据,不落长期证书</td></tr>
|
||
<tr><td>权限</td><td>admin 证书,等于 <code>cluster-admin</code></td><td>按命名空间的 RoleBinding,最小权限</td><td>一张证书管全集群,误删成本是零</td><td>开发用只读 Role,CI 用 <code>shop-deployer</code>(第 19 章)</td></tr>
|
||
<tr><td>context 数量</td><td>1 个 <code>kind-aurora</code></td><td>每环境每区域各一个,名字带环境</td><td>名字里没有环境,切错时没有任何提示</td><td>用 <code>rename-context</code> 成 <code>prod-hk</code> 这类名字</td></tr>
|
||
<tr><td>命名空间划分</td><td>一个 <code>shop</code></td><td>每团队每环境一组,配 ResourceQuota</td><td>配额与 RBAC 都挂在命名空间上</td><td>清单里写死 namespace,不依赖 context</td></tr>
|
||
<tr><td>事件留存</td><td>默认 1 小时,随集群消失</td><td>导出到日志平台,保留 30 天</td><td>事故复盘通常在事件过期之后才开始</td><td>部署事件导出器,Warning 接进告警</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<p>最容易被忽略的是第四条:本地只有一个 <code>shop</code>,生产上有几十个,
|
||
而 <code>kubectl</code> 默认静默落到 <code>default</code>。</p>
|
||
|
||
<h3 id="sec-2-10">2.10 清理与残留检查</h3>
|
||
|
||
<pre data-lang="bash" data-name="清理本章资源"># env: all
|
||
kubectl delete namespace shop
|
||
# 影响:删除 shop 及其中全部对象;本章 shop 里没有工作负载,删除即回到第 1 章状态
|
||
# 回滚:kubectl apply -f deploy/base/namespace.yaml</pre>
|
||
|
||
<pre data-lang="bash" data-name="残留检查"># env: all
|
||
kubectl get ns
|
||
kubectl get all,cm,secret,pvc,ingress -n shop</pre>
|
||
<pre data-lang="text" data-name="预期输出"># 预期输出
|
||
NAME STATUS AGE
|
||
default Active 46m
|
||
kube-node-lease Active 46m
|
||
kube-public Active 46m
|
||
kube-system Active 46m
|
||
local-path-storage Active 46m
|
||
shop Active 24m
|
||
|
||
No resources found in shop namespace.</pre>
|
||
|
||
<p>残留检查要读懂两件事:<code>shop</code> 还在,说明边界没被误删;<code>No resources found</code> 说明本章没留下工作负载。
|
||
回滚上一章用
|
||
<code>git checkout HEAD~1 -- Makefile deploy/base/</code>,再跑 <code>make verify-ch01</code> 确认回到 S0 起点。</p>
|
||
|
||
<h3 id="sec-2-11">2.11 小结与自测</h3>
|
||
|
||
<p class="take"><b>一句话:</b>命令打到哪里由 context 决定,你记住的集群名不算数。</p>
|
||
<p class="take"><b>一句话:</b>先 diff 再 apply,比 apply 完再回滚便宜一个数量级。</p>
|
||
|
||
<p>把这一章三句话:<code>spec</code> 归你、<code>status</code> 归控制器;「空」有两种,不存在与
|
||
没东西;观测有顺序:discovery、对象、事件。</p>
|
||
|
||
<details>
|
||
<summary>自测 3 题(点击展开答案)</summary>
|
||
<p><b>1(概念)</b><code>spec</code> 和 <code>status</code> 都能改吗?</p>
|
||
<p>只有 <code>spec</code> 属于你。<code>status</code> 是子资源、由控制器写,即便 <code>patch</code> 成功,下一轮协调也会覆盖。</p>
|
||
<p><b>2(排错)</b>同事说「Pod 没了」,<code>get pods -n shop</code> 返回 <code>No resources found</code>,先敲哪三条?</p>
|
||
<p><code>config current-context</code> 确认集群、<code>get ns shop</code> 确认边界、<code>get pods -A</code> 确认作用域。</p>
|
||
<p><b>3(决策)</b><code>diff</code> 与 <code>--dry-run=server</code>,CI 里选哪个?</p>
|
||
<p>都用:先 <code>diff</code> 看「会改什么」,再 <code>--dry-run=server</code> 看「能不能过」。只有前者会漏掉准入拒绝。</p>
|
||
</details>
|
||
|
||
<div class="box tip">
|
||
<span class="bt">延伸挑战(≤30 分钟)</span>
|
||
<p>给 <code>Makefile</code> 加一个 <code>nodes</code> 目标,用一条 <code>-o jsonpath</code> 把三个节点的名字、内网 IP、
|
||
运行时打成三列。提示:<code>{range .items[*]}</code> 与 <code>{end}</code> 配套。</p>
|
||
</div>
|
||
|
||
<h3 id="sec-2-12">2.12 衔接</h3>
|
||
|
||
<p><b>回收</b>:第 0 章把「期望状态与实际状态」讲成一对概念,当时只有一张示意图;本章的 <code>spec</code> 与
|
||
<code>status</code> 就是它在 API 里的实体。</p>
|
||
|
||
<p><b>钩子</b>:<code>shop</code> 还是空的。第一个 Pod 放进去之后,<code>get pods</code> 会多出 <code>Pending</code>、
|
||
<code>ContainerCreating</code> 这些中间态——那是控制器把 <code>spec</code> 变成 <code>status</code> 的过程。
|
||
<a href="03-pod.html">第 3 章</a>从这些中间态开始讲 Pod。</p>
|
||
|
||
<h3 id="sec-2-13">2.13 参考与验证版本</h3>
|
||
|
||
<ul>
|
||
<li><a href="https://kubernetes.io/docs/reference/kubectl/">kubectl 命令参考</a></li>
|
||
<li><a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/">Kubernetes 对象</a></li>
|
||
<li><a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/">命名空间</a></li>
|
||
<li><a href="https://kubernetes.io/docs/reference/kubectl/generated/kubectl_diff/">kubectl diff</a></li>
|
||
<li><a href="https://kubernetes.io/docs/tasks/tools/included/optional-kubectl-configs-bash-linux/">bash 补全</a></li>
|
||
</ul>
|
||
|
||
<p>验证版本:k8s v1.36.1 / kubectl v1.36.1 / containerd 2.x / Ubuntu 24.04 · 最后验证:2026-11-08</p>
|
||
</article>
|
||
|
||
<nav class="pager">
|
||
<a class="prev" href="01-env.html"><span class="k">← 上一章</span><span class="v">第 1 章 · 装第一个集群:Windows 11 + WSL2 与 Linux 双轨</span></a>
|
||
<a class="next disabled"><span class="k">下一章 →</span><span class="v">第 3 章 · 待写</span></a>
|
||
</nav>
|
||
|
||
<p class="foot">
|
||
Kubernetes 专题 · S0 第 2 章 kubectl 与集群解剖:把黑盒变成可观测对象<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>
|