832 lines
56 KiB
HTML
832 lines
56 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>第 5 章 Service 与集群 DNS · Kubernetes 专题知识库</title>
|
|||
|
|
<meta name="description" content="给三个会换地址的 Pod 一个固定名字:建 ClusterIP Service、看 EndpointSlice 后端、用 CoreDNS 解析短名、对比 headless 的 A 记录,并分清解析失败与连接被拒。">
|
|||
|
|
<meta name="keywords" content="Service, ClusterIP, EndpointSlice, CoreDNS, headless Service, kube-proxy, iptables, IPVS, ndots">
|
|||
|
|
<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>第 5 章</b>
|
|||
|
|
</div>
|
|||
|
|
<div class="spacer"></div>
|
|||
|
|
<button class="btn" id="tocToggle">☰ 导航</button>
|
|||
|
|
<button class="btn" id="themeBtn" title="切换到深色主题">深色</button>
|
|||
|
|
</div>
|
|||
|
|
</header>
|
|||
|
|
|
|||
|
|
<div class="layout" id="layout">
|
|||
|
|
<aside class="sidebar">
|
|||
|
|
<h2>章节目录</h2>
|
|||
|
|
<nav id="chapters"><div class="toc-stage"><b>S0</b><span class="h">起步:环境与心智</span></div><a class="toc-a" href="00-why.html"><i class="cn">0</i>从一次线上事故理解 K8s</a><a class="toc-a" href="01-env.html"><i class="cn">1</i>装第一个集群:Windows 11 + WSL2 与 Linux 双轨</a><a class="toc-a" href="02-kubectl.html"><i class="cn">2</i>kubectl 与集群解剖:把黑盒变成可观测对象</a><div class="toc-stage"><b>S1</b><span class="h">跑起来:单服务最小闭环</span></div><a class="toc-a" href="03-pod.html"><i class="cn">3</i>Pod:最小调度单元</a><a class="toc-a" href="04-deployment.html"><i class="cn">4</i>Deployment:副本与滚动更新</a><span class="toc-a active" aria-current="page"><i class="cn">5</i>Service 与集群 DNS</span><div class="subnav current"><a class="toc-a lv3" href="#sec-5-1">5.1 本章在哪:主线之前与之后</a><a class="toc-a lv3" href="#sec-5-2">5.2 学习目标</a><a class="toc-a lv3" href="#sec-5-3">5.3 场景:三个地址,一个都用不了</a><a class="toc-a lv3" href="#sec-5-4">5.4 心智模型:一个名字,一张在岗名单</a><a class="toc-a lv3" href="#sec-5-5">5.5 概念与字段速查</a><a class="toc-a lv3" href="#sec-5-6">5.6 动手:给三个副本一个固定名字</a><a class="toc-a lv3" href="#sec-5-7">5.7 验收清单</a><a class="toc-a lv3" href="#sec-5-8">5.8 故障实验室</a><a class="toc-a lv3" href="#sec-5-9">5.9 生产对照</a><a class="toc-a lv3" href="#sec-5-10">5.10 清理与残留检查</a><a class="toc-a lv3" href="#sec-5-11">5.11 小结与自测</a><a class="toc-a lv3" href="#sec-5-12">5.12 衔接</a><a class="toc-a lv3" href="#sec-5-13">5.13 参考与验证版本</a></div><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><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>S1 跑起来:单服务最小闭环</span>
|
|||
|
|
<span>·</span>
|
|||
|
|
<span>第 <b class="pos">5</b> / 26 章</span>
|
|||
|
|
</p>
|
|||
|
|
|
|||
|
|
<h2 id="ch5"><span class="n">5</span>Service 与集群 DNS</h2>
|
|||
|
|
|
|||
|
|
<p>第 5 / 26 章 · 阶段 S1 · 难度 ★★☆☆☆ · 约 120 分钟 · 前置:第 4 章</p>
|
|||
|
|
|
|||
|
|
<div class="lead">
|
|||
|
|
<b>30 秒速览:</b>上一章留下的三个副本都活着,但没人敢调用它们——Pod IP 每次重建都会变。
|
|||
|
|
本章给它一个只在本集群可达的固定地址,看清名单背后是谁,再让 <code>shop-web</code> 按名字访问它。
|
|||
|
|
约 120 分钟,其中 15 分钟在等镜像与 DNS 缓存过期。
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-1">5.1 本章在哪:主线之前与之后</h3>
|
|||
|
|
|
|||
|
|
<div class="tablewrap">
|
|||
|
|
<table>
|
|||
|
|
<thead><tr><th>项</th><th>内容</th></tr></thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td>主线之前</td><td><code>shop</code> 里只有 Deployment <code>shop-api</code> 和 3 个副本;调用方只能写 Pod IP,而 Pod IP 每次重建都变</td></tr>
|
|||
|
|
<tr><td>主线之后</td><td><code>shop-api</code> 有一个不随 Pod 变化的 ClusterIP;<code>shop-web</code> 用名字访问它,滚动更新期间调用方配置不用改</td></tr>
|
|||
|
|
<tr><td>本章产物</td><td><code>deploy/base/shop-api-svc.yaml</code>、<code>shop-api-headless.yaml</code>、<code>shop-web.yaml</code>、<code>Makefile</code> 里的 <code>verify-ch05</code></td></tr>
|
|||
|
|
<tr><td>适用环境</td><td>ENV-A(Win11 + WSL2 + kind)与 ENV-B(Ubuntu 24.04)都做;只有「从集群外直接问 DNS」一节两边不同,见 <a href="#sec-5-6">5.6</a> 的 Step 7</td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<p>第 4 章留下的三个副本都在 <code>Running</code>,但 <code>get pods -o wide</code> 里的三个 IP 只在这一刻成立:
|
|||
|
|
重建、重启、副本变化都会换号,而换号对调用方不可见。</p>
|
|||
|
|
|
|||
|
|
<p>本章给 <code>shop-api</code> 一个名字,再建 <code>shop-web</code> 做第一个真实调用方,
|
|||
|
|
最后用一个不带 VIP 的 Service 观察名字背后有几个地址。</p>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-2">5.2 学习目标</h3>
|
|||
|
|
|
|||
|
|
<div class="box note">
|
|||
|
|
<span class="bt">学完你能</span>
|
|||
|
|
<ul>
|
|||
|
|
<li>给 Deployment 建 ClusterIP Service,判定标准:<code>EndpointSlice</code> 里的地址个数等于副本数。</li>
|
|||
|
|
<li>说清 <code>port</code> 与 <code>targetPort</code> 的分工,判定标准:能指出写错时报的是 <code>connection refused</code> 而非解析失败。</li>
|
|||
|
|
<li>短名、带命名空间的名字、全名各访问一次,判定标准:在 <code>shop</code> 里三种都通,并能解释为什么。</li>
|
|||
|
|
<li>区分普通与 headless 的解析结果,判定标准:前者一条 A 记录指向 VIP,后者三条指向 Pod。</li>
|
|||
|
|
<li>在三种报错之间做出判断,判定标准:每种都能说出该查哪个对象。</li>
|
|||
|
|
</ul>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-3">5.3 场景:三个地址,一个都用不了</h3>
|
|||
|
|
|
|||
|
|
<p>周一 09:20,<code>shop-api</code> 打完 v1.2.4 的镜像,滚动更新走完,页面大面积 502。
|
|||
|
|
上次上线时有人把后端地址写进了 <code>shop-web</code> 的配置:<code>http://10.244.1.7:8080</code>。</p>
|
|||
|
|
|
|||
|
|
<p>滚动更新逐个换掉三个 Pod,那个地址上已经没有进程。值班的人先怀疑 DNS,再看网络,最后才发现三个 IP 全换了号:</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="text" data-name="调用方拿到的报错"># 预期输出
|
|||
|
|
curl: (7) Failed to connect to 10.244.1.7 port 8080 after 0 ms: Connection refused</pre>
|
|||
|
|
|
|||
|
|
<div class="box danger">
|
|||
|
|
<span class="bt">现象</span>
|
|||
|
|
<p>三个副本都在 <code>Running</code>,服务却是坏的。坏的不是进程,是「怎么找到它」被写死了。
|
|||
|
|
地址由 CNI 分配,删除即回收,K8s 从不承诺它稳定。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<p>本章只做一件事:让调用方写一个名字,名字背后有几个地址、是谁,交给集群维护。</p>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-4">5.4 心智模型:一个名字,一张在岗名单</h3>
|
|||
|
|
|
|||
|
|
<p>先看一个反直觉的事实:在 <code>shop-web</code> 里 <code>curl http://shop-api:8080/healthz</code>
|
|||
|
|
拿到 200,但这个 <code>10.96.45.12</code> 不在任何 Pod 或节点上,<code>ping</code> 无回应,
|
|||
|
|
<code>ss -lntp</code> 也找不到它。它只在转发规则里存在,见图 1。</p>
|
|||
|
|
|
|||
|
|
<figure class="figure">
|
|||
|
|
<svg viewBox="0 0 720 300" role="img" aria-label="调用方经 CoreDNS 解析到 ClusterIP,再由 kube-proxy 按 EndpointSlice 转发到三个 Pod">
|
|||
|
|
<defs>
|
|||
|
|
<marker id="a5" 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="8" y="112" width="146" height="66" rx="9" style="fill:var(--card);stroke:var(--line)"></rect>
|
|||
|
|
<text x="81" y="138" text-anchor="middle" font-size="13" style="fill:var(--ink)">shop-web Pod</text>
|
|||
|
|
<text x="81" y="160" text-anchor="middle" font-size="11.5" style="fill:var(--muted)">curl shop-api:8080</text>
|
|||
|
|
|
|||
|
|
<rect x="196" y="12" width="180" height="58" rx="9" style="fill:var(--brand-soft);stroke:var(--brand)"></rect>
|
|||
|
|
<text x="286" y="36" text-anchor="middle" font-size="13" style="fill:var(--brand)">CoreDNS 10.96.0.10</text>
|
|||
|
|
<text x="286" y="56" text-anchor="middle" font-size="11.5" style="fill:var(--brand)">shop-api → 10.96.45.12</text>
|
|||
|
|
|
|||
|
|
<rect x="196" y="112" width="180" height="66" rx="9" style="fill:var(--card);stroke:var(--line)"></rect>
|
|||
|
|
<text x="286" y="138" text-anchor="middle" font-size="13" style="fill:var(--ink)">Service shop-api</text>
|
|||
|
|
<text x="286" y="160" text-anchor="middle" font-size="11.5" style="fill:var(--muted)">VIP 10.96.45.12:8080</text>
|
|||
|
|
|
|||
|
|
<rect x="404" y="112" width="152" height="66" rx="9" style="fill:var(--card);stroke:var(--line)"></rect>
|
|||
|
|
<text x="480" y="138" text-anchor="middle" font-size="13" style="fill:var(--ink)">kube-proxy</text>
|
|||
|
|
<text x="480" y="160" text-anchor="middle" font-size="11.5" style="fill:var(--muted)">iptables / IPVS 规则</text>
|
|||
|
|
|
|||
|
|
<rect x="196" y="232" width="180" height="52" rx="9" style="fill:var(--soft);stroke:var(--line)"></rect>
|
|||
|
|
<text x="286" y="254" text-anchor="middle" font-size="12.5" style="fill:var(--ink)">EndpointSlice</text>
|
|||
|
|
<text x="286" y="272" text-anchor="middle" font-size="11" style="fill:var(--muted)">3 个就绪地址</text>
|
|||
|
|
|
|||
|
|
<text x="647" y="40" text-anchor="middle" font-size="11.5" style="fill:var(--muted)">shop-api 的三个副本</text>
|
|||
|
|
<rect x="586" y="52" width="122" height="44" rx="8" style="fill:var(--card);stroke:var(--line)"></rect>
|
|||
|
|
<text x="647" y="79" text-anchor="middle" font-size="12" style="fill:var(--ink)">10.244.1.7</text>
|
|||
|
|
<rect x="586" y="124" width="122" height="44" rx="8" style="fill:var(--card);stroke:var(--line)"></rect>
|
|||
|
|
<text x="647" y="151" text-anchor="middle" font-size="12" style="fill:var(--ink)">10.244.2.4</text>
|
|||
|
|
<rect x="586" y="196" width="122" height="44" rx="8" style="fill:var(--card);stroke:var(--line)"></rect>
|
|||
|
|
<text x="647" y="223" text-anchor="middle" font-size="12" style="fill:var(--ink)">10.244.3.6</text>
|
|||
|
|
|
|||
|
|
<path d="M196,44 L81,44 L81,108" style="stroke:var(--muted);fill:none" stroke-dasharray="4 3" marker-end="url(#a5)"></path>
|
|||
|
|
<text x="120" y="36" text-anchor="middle" font-size="11" style="fill:var(--muted)">① 解析</text>
|
|||
|
|
|
|||
|
|
<path d="M158,145 L192,145" style="stroke:var(--muted);fill:none" marker-end="url(#a5)"></path>
|
|||
|
|
<text x="173" y="136" text-anchor="middle" font-size="11" style="fill:var(--muted)">② 连接</text>
|
|||
|
|
|
|||
|
|
<path d="M380,145 L400,145" style="stroke:var(--muted);fill:none" marker-end="url(#a5)"></path>
|
|||
|
|
|
|||
|
|
<path d="M286,236 L286,182" style="stroke:var(--muted);fill:none" stroke-dasharray="4 3" marker-end="url(#a5)"></path>
|
|||
|
|
<text x="330" y="212" text-anchor="middle" font-size="11" style="fill:var(--muted)">③ 读名单</text>
|
|||
|
|
|
|||
|
|
<path d="M556,140 L572,140 L572,74 L582,74" style="stroke:var(--muted);fill:none" marker-end="url(#a5)"></path>
|
|||
|
|
<path d="M556,146 L582,146" style="stroke:var(--muted);fill:none" marker-end="url(#a5)"></path>
|
|||
|
|
<path d="M556,152 L572,152 L572,218 L582,218" style="stroke:var(--muted);fill:none" marker-end="url(#a5)"></path>
|
|||
|
|
<text x="518" y="254" text-anchor="middle" font-size="11" style="fill:var(--muted)">④ 改写目的地址</text>
|
|||
|
|
</svg>
|
|||
|
|
<div class="figcap"><b>图 1 ·</b>一次 <code>curl</code> 的四步:解析出 VIP、连 VIP、按名单改写地址、落到某个 Pod</div>
|
|||
|
|
</figure>
|
|||
|
|
|
|||
|
|
<p>请求先解析出 <code>10.96.45.12</code>;报文的目的地址是它,在节点上被 <code>kube-proxy</code> 写好的规则
|
|||
|
|
改写成某个就绪 Pod 的 IP 与端口,依据是 <code>EndpointSlice</code> 里的在岗名单。</p>
|
|||
|
|
|
|||
|
|
<p>三个对象各管一段:<code>Service</code> 声明端口与选择器,<code>EndpointSlice</code> 记录此刻谁就绪,
|
|||
|
|
<code>kube-proxy</code> 把前两者编译成内核规则。总机号不变,名单天天换。</p>
|
|||
|
|
|
|||
|
|
<div class="tablewrap">
|
|||
|
|
<table>
|
|||
|
|
<thead><tr><th>常见误解</th><th>事实</th></tr></thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td>「ClusterIP 上有个进程在监听」</td><td>没有进程,只有内核里的转发规则;所以 <code>ping</code> 不通、<code>ss -lntp</code> 也看不到它</td></tr>
|
|||
|
|
<tr><td>「Service 自己会做健康检查」</td><td>它只转发到就绪的 Pod,判断依据来自 readinessProbe;本章还没有探针,第 7 章补齐</td></tr>
|
|||
|
|
<tr><td>「Pod 换 IP 就得重建 Service」</td><td>Service 不存 Pod IP,名单由控制器维护,<code>apply</code> 一次之后不用再动</td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<p>规则由 <code>kube-proxy</code> 生成,kind 默认 <code>iptables</code>。<code>IPVS</code> 在 v1.35 已废弃、
|
|||
|
|
计划 v1.43 移除;<code>nftables</code> v1.33 GA,新集群建议直接用。</p>
|
|||
|
|
|
|||
|
|
<div class="tablewrap">
|
|||
|
|
<table>
|
|||
|
|
<thead><tr><th>模式</th><th>机制</th><th>现状与建议</th></tr></thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td><code>iptables</code></td><td>为每个 Service 生成一条链,逐条匹配</td><td>仍是默认;几千个 Service 时规则数量会成为负担</td></tr>
|
|||
|
|
<tr><td><code>nftables</code></td><td>用 map 与集合代替线性链</td><td>v1.29 alpha、v1.31 beta、v1.33 GA;新集群优先选它</td></tr>
|
|||
|
|
<tr><td><code>IPVS</code></td><td>内核 LVS,哈希表查找,支持多种调度算法</td><td>v1.35 起废弃,v1.43 计划移除;不要在新集群上启用</td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<p class="take"><b>一句话:</b>ClusterIP 不是一台机器,是每个节点上的一条转发规则。</p>
|
|||
|
|
|
|||
|
|
<div class="box warn">
|
|||
|
|
<span class="bt">易错点 · 集群升级了,kube-proxy 还留在 IPVS 模式</span>
|
|||
|
|
<p><b>现象</b>:集群从 v1.34 升到 v1.36,功能都正常,只有 <code>kube-proxy</code> 启动时输出 IPVS 废弃提示,
|
|||
|
|
配置里仍写着 <code>mode: "ipvs"</code>。</p>
|
|||
|
|
<p><b>原因</b>:IPVS 在 v1.35 标记废弃,计划 v1.37 加特性门、v1.43 移除;升级组件不会替你改 ConfigMap。</p>
|
|||
|
|
<p><b>怎么验证</b>:<code>kubectl -n kube-system get cm kube-proxy -o jsonpath='{.data.config\.conf}' | grep mode</code>
|
|||
|
|
看到 <code>ipvs</code> 即待迁移,切成 <code>iptables</code> 或 <code>nftables</code> 后重启 DaemonSet。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-5">5.5 概念与字段速查</h3>
|
|||
|
|
|
|||
|
|
<p>下表只列默认值会咬人的字段。</p>
|
|||
|
|
|
|||
|
|
<div class="tablewrap">
|
|||
|
|
<table>
|
|||
|
|
<thead><tr><th>对象 / 字段</th><th>作用</th><th>默认值</th><th>生产建议</th></tr></thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td><code>spec.type</code></td><td>暴露方式</td><td><code>ClusterIP</code></td><td>集群内调用一律用它</td></tr>
|
|||
|
|
<tr><td><code>spec.selector</code></td><td>按标签选后端 Pod</td><td>必填;不写则不自动建 EndpointSlice</td><td>只写稳定标签,不要带 <code>version</code></td></tr>
|
|||
|
|
<tr><td><code>spec.ports[].port</code></td><td>Service 对外的端口</td><td>必填</td><td>与调用方约定,改它等于改协议</td></tr>
|
|||
|
|
<tr><td><code>spec.ports[].targetPort</code></td><td>转发到容器里的端口</td><td>等于 <code>port</code></td><td>显式写出来,别依赖默认</td></tr>
|
|||
|
|
<tr><td><code>spec.ports[].name</code></td><td>多端口时必须唯一</td><td>单端口可省</td><td>多端口一律命名,且不超过 15 字符</td></tr>
|
|||
|
|
<tr><td><code>spec.clusterIP</code></td><td>分配的虚拟地址</td><td>自动从 service 网段分配</td><td>只有 <code>None</code> 需要手写</td></tr>
|
|||
|
|
<tr><td><code>spec.sessionAffinity</code></td><td>会话保持</td><td><code>None</code></td><td>确有粘性需求才用 <code>ClientIP</code></td></tr>
|
|||
|
|
<tr><td><code>spec.publishNotReadyAddresses</code></td><td>未就绪的 Pod 是否进名单</td><td><code>false</code></td><td>只给有状态集的 headless 用</td></tr>
|
|||
|
|
<tr><td><code>metadata.name</code></td><td>同时就是 DNS 名</td><td>必填</td><td>改名等于换地址,调用方全挂</td></tr>
|
|||
|
|
<tr><td><code>discovery.k8s.io/v1 EndpointSlice</code></td><td>就绪后端清单</td><td>由控制器维护</td><td>只读,手改会被协调覆盖</td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<p><code>port</code> 与 <code>targetPort</code> 最容易错:前者是「别人怎么连你」,后者是「你往容器哪个端口送」。
|
|||
|
|
两者相等时写一个也够,但显式写两个能省一轮排查。</p>
|
|||
|
|
|
|||
|
|
<div class="tablewrap">
|
|||
|
|
<table>
|
|||
|
|
<thead><tr><th>类型</th><th>它做什么</th><th>什么时候用</th><th>本章</th></tr></thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td><code>ClusterIP</code></td><td>分配一个只在本集群可达的 VIP</td><td>集群内部服务间调用</td><td>展开,见 <a href="#sec-5-6">5.6</a></td></tr>
|
|||
|
|
<tr><td><code>NodePort</code></td><td>在每个节点上开同一个高端口</td><td>临时对外,且没有负载均衡器</td><td>一句带过,见第 9 章</td></tr>
|
|||
|
|
<tr><td><code>LoadBalancer</code></td><td>向云厂商申请一个外部负载均衡器</td><td>生产对外入口</td><td>一句带过,见第 9 章</td></tr>
|
|||
|
|
<tr><td><code>ExternalName</code></td><td>只返回一条 CNAME,不做转发</td><td>把外部依赖伪装成集群内名字</td><td>一句带过,见第 9 章</td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
<details>
|
|||
|
|
<summary>可跳过 · kind 的 service 网段为什么是 <code>10.96.0.0/16</code></summary>
|
|||
|
|
<p>kind 用 kubeadm 起控制面,默认 <code>--service-cidr=10.96.0.0/16</code>。
|
|||
|
|
<code>kube-dns</code> 这个 Service 固定拿网段里第一个可用地址,所以是 <code>10.96.0.10</code>,
|
|||
|
|
也就是 <code>/etc/resolv.conf</code> 里的 <code>nameserver</code>。两个网段不能重叠,因为 Pod 地址与 VIP
|
|||
|
|
都要参与节点上的路由判断。</p>
|
|||
|
|
</details>
|
|||
|
|
|
|||
|
|
<details>
|
|||
|
|
<summary>可跳过 · 从 Endpoints 到 EndpointSlice</summary>
|
|||
|
|
<p><code>Endpoints</code> 一个对象装全部地址,几千个 Pod 时每次变更都要整份传输;EndpointSlice 按每个 100 个地址切片,
|
|||
|
|
只传变化的那片:v1.16 alpha、v1.21 GA。<code>v1 Endpoints</code> 在 v1.33 正式废弃,读取会打印一条 warning。
|
|||
|
|
<code>kubectl get endpoints</code> 还能用,但它是给旧工具留的兼容层。</p>
|
|||
|
|
</details>
|
|||
|
|
|
|||
|
|
<p class="take"><b>一句话:</b>字段的默认值大多合理,只有 <code>targetPort</code> 与选择器值得每次显式写。</p>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-6">5.6 动手:给三个副本一个固定名字</h3>
|
|||
|
|
|
|||
|
|
<p>八个步骤按顺序做,每步都能单独验证;工作目录是 <code>~/work/aurora-shop</code>。</p>
|
|||
|
|
|
|||
|
|
<h4>Step 1 · 校验上一章状态</h4>
|
|||
|
|
<p><span class="tag">意图</span>确认起点是「3 个副本、没有 Service」,否则后面的地址个数对不上。</p>
|
|||
|
|
<pre data-lang="bash" data-name="校验 ch04"># env: all
|
|||
|
|
make verify-ch04</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
[ok] deployment shop-api ready 3/3
|
|||
|
|
[ok] image registry.local/aurora/shop-api:v1.2.3
|
|||
|
|
[ok] service count in shop is 0
|
|||
|
|
[ok] revisionHistoryLimit 5</pre>
|
|||
|
|
<div class="box warn">
|
|||
|
|
<span class="bt">如果不一样</span>
|
|||
|
|
<p><code>2 available of 3 desired</code> 说明滚动更新没走完,先 <code>kubectl rollout status deploy/shop-api -n shop</code>;
|
|||
|
|
<code>service count in shop is 1</code> 说明有人建过 Service,删掉再继续。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h4>Step 2 · 建 Service,并确认它选中了全部三个副本</h4>
|
|||
|
|
<p><span class="tag">意图</span>用一份二十行的清单,把「端口映射」与「选谁」写清楚。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="yaml" data-name="deploy/base/shop-api-svc.yaml"># file: deploy/base/shop-api-svc.yaml
|
|||
|
|
apiVersion: v1
|
|||
|
|
kind: Service
|
|||
|
|
metadata:
|
|||
|
|
name: shop-api # 这个名字同时就是 DNS 名
|
|||
|
|
namespace: shop
|
|||
|
|
labels:
|
|||
|
|
app.kubernetes.io/name: shop-api
|
|||
|
|
app.kubernetes.io/instance: shop
|
|||
|
|
app.kubernetes.io/part-of: aurora-shop
|
|||
|
|
spec:
|
|||
|
|
type: ClusterIP # 本章只展开这一种
|
|||
|
|
selector: # 只写稳定标签:name + instance
|
|||
|
|
app.kubernetes.io/name: shop-api
|
|||
|
|
app.kubernetes.io/instance: shop
|
|||
|
|
ports:
|
|||
|
|
- name: http
|
|||
|
|
port: 8080 # 调用方连的端口
|
|||
|
|
targetPort: 8080 # 容器里实际监听的端口</pre>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="应用并查看"># env: all
|
|||
|
|
kubectl apply -f deploy/base/shop-api-svc.yaml
|
|||
|
|
kubectl get svc -n shop -o wide</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
service/shop-api created
|
|||
|
|
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
|
|||
|
|
shop-api ClusterIP 10.96.45.12 <none> 8080/TCP 9s app.kubernetes.io/instance=shop,app.kubernetes.io/name=shop-api</pre>
|
|||
|
|
|
|||
|
|
<p><code>SELECTOR</code> 里没有 <code>version</code>:带上它,滚动更新期间新旧 ReplicaSet 的 Pod 标签不同,
|
|||
|
|
Service 只选中其中一批。</p>
|
|||
|
|
|
|||
|
|
<div class="box warn">
|
|||
|
|
<span class="bt">易错点 · port 与 targetPort 只写对一个</span>
|
|||
|
|
<p><b>现象</b>:<code>get svc</code> 正常,<code>ENDPOINTS</code> 里地址齐全,<code>curl</code> 却报
|
|||
|
|
<code>connection refused</code>,换个 Pod 重试也一样。</p>
|
|||
|
|
<p><b>原因</b>:<code>targetPort</code> 决定转发到容器哪个端口。写成 80 而进程监听 8080,规则指向没人监听的端口,
|
|||
|
|
连接被内核直接拒绝——是拒绝,不是超时,也不是解析失败。</p>
|
|||
|
|
<p><b>怎么验证</b>:<code>kubectl get svc shop-api -n shop -o jsonpath='{.spec.ports[0].targetPort}'</code>
|
|||
|
|
与 Pod 的 <code>containerPort</code> 必须相等。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h4>Step 3 · 用 EndpointSlice 看清后端</h4>
|
|||
|
|
<p><span class="tag">意图</span>把「Service 后面的名单」变成可读对象,这是本章排障的第一个命令。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="查名单"># env: all
|
|||
|
|
kubectl get endpointslices -n shop -l kubernetes.io/service-name=shop-api
|
|||
|
|
kubectl get endpoints -n shop shop-api</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
|
|||
|
|
shop-api-9x2kd IPv4 8080 10.244.1.7,10.244.2.4,10.244.3.6 41s
|
|||
|
|
|
|||
|
|
Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice
|
|||
|
|
NAME ENDPOINTS AGE
|
|||
|
|
shop-api 10.244.1.7:8080,10.244.2.4:8080,10.244.3.6:8080 41s</pre>
|
|||
|
|
|
|||
|
|
<p>名单里三个地址就是 <code>get pods -o wide</code> 里那三个;<code>endpoints</code> 还能读,
|
|||
|
|
但每次都在提醒你换 EndpointSlice。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="看单个切片的细节"># env: all
|
|||
|
|
kubectl describe endpointslice -n shop -l kubernetes.io/service-name=shop-api</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
Ports:
|
|||
|
|
Name Port Protocol
|
|||
|
|
---- ---- --------
|
|||
|
|
http 8080 TCP
|
|||
|
|
|
|||
|
|
Endpoints:
|
|||
|
|
- Addresses: 10.244.1.7
|
|||
|
|
Conditions:
|
|||
|
|
Ready: true</pre>
|
|||
|
|
|
|||
|
|
<h4>Step 4 · 建一个真实调用方</h4>
|
|||
|
|
<p><span class="tag">意图</span>起 <code>shop-web</code>,它的 nginx 配置写的是 <code>http://shop-api:8080</code>,
|
|||
|
|
没有任何 IP,所以必须建在 Service 之后。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="yaml" data-name="deploy/base/shop-web.yaml"># file: deploy/base/shop-web.yaml
|
|||
|
|
apiVersion: apps/v1
|
|||
|
|
kind: Deployment
|
|||
|
|
metadata:
|
|||
|
|
name: shop-web
|
|||
|
|
namespace: shop
|
|||
|
|
labels:
|
|||
|
|
app.kubernetes.io/name: shop-web
|
|||
|
|
app.kubernetes.io/instance: shop
|
|||
|
|
app.kubernetes.io/part-of: aurora-shop
|
|||
|
|
spec:
|
|||
|
|
replicas: 2
|
|||
|
|
selector:
|
|||
|
|
matchLabels:
|
|||
|
|
app.kubernetes.io/name: shop-web
|
|||
|
|
app.kubernetes.io/instance: shop
|
|||
|
|
template:
|
|||
|
|
metadata:
|
|||
|
|
labels:
|
|||
|
|
app.kubernetes.io/name: shop-web
|
|||
|
|
app.kubernetes.io/instance: shop
|
|||
|
|
app.kubernetes.io/part-of: aurora-shop
|
|||
|
|
app.kubernetes.io/version: v1.2.3
|
|||
|
|
spec:
|
|||
|
|
containers:
|
|||
|
|
- name: shop-web
|
|||
|
|
image: registry.local/aurora/shop-web:v1.2.3
|
|||
|
|
imagePullPolicy: IfNotPresent # 镜像已用 kind load 灌进节点
|
|||
|
|
ports:
|
|||
|
|
- name: http
|
|||
|
|
containerPort: 8080</pre>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="应用并确认两个副本"># env: all
|
|||
|
|
kubectl apply -f deploy/base/shop-web.yaml
|
|||
|
|
kubectl get pods -n shop -l app.kubernetes.io/name=shop-web -o wide</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
deployment.apps/shop-web created
|
|||
|
|
NAME READY STATUS RESTARTS AGE IP NODE
|
|||
|
|
shop-web-6c8f9d7b5c-4m2qp 1/1 Running 0 22s 10.244.1.9 aurora-worker
|
|||
|
|
shop-web-6c8f9d7b5c-r7ktn 1/1 Running 0 22s 10.244.3.5 aurora-worker2</pre>
|
|||
|
|
|
|||
|
|
<p>清单里没有探针与 <code>resources</code>,第 7 章补齐。</p>
|
|||
|
|
|
|||
|
|
<h4>Step 5 · 从 shop-web 里调 shop-api</h4>
|
|||
|
|
<p><span class="tag">意图</span>用短名打通一次,并确认流量真的分散到三个副本。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="健康检查与三次调用"># env: all
|
|||
|
|
kubectl exec -n shop deploy/shop-web -- curl -s http://shop-api:8080/healthz
|
|||
|
|
kubectl exec -n shop deploy/shop-web -- sh -c 'for i in 1 2 3; do curl -s http://shop-api:8080/ | grep -o "\"hostname\":\"[^\"]*\""; done'</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
ok
|
|||
|
|
"hostname":"shop-api-7d9f8c6b5d-2xk4l"
|
|||
|
|
"hostname":"shop-api-7d9f8c6b5d-h8bq9"
|
|||
|
|
"hostname":"shop-api-7d9f8c6b5d-v5trl"</pre>
|
|||
|
|
|
|||
|
|
<p>三次落到三个 Pod,说明规则按连接做负载均衡。都相同就先看名单里有几个地址,再看 <code>curl</code> 是否复用了连接。</p>
|
|||
|
|
|
|||
|
|
<p>页面能不能打开,还要从你的电脑看一眼。</p>
|
|||
|
|
|
|||
|
|
<div class="grid2">
|
|||
|
|
<div class="box">
|
|||
|
|
<h4>Windows 11 + WSL2</h4>
|
|||
|
|
<pre data-lang="bash" data-name="转发 shop-web"># env: wsl
|
|||
|
|
kubectl port-forward -n shop deploy/shop-web 18080:8080</pre>
|
|||
|
|
<p>浏览器打开 <code>http://127.0.0.1:18080</code>,页面顶部应显示后端 hostname 与 <code>THEME=light</code>。</p>
|
|||
|
|
</div>
|
|||
|
|
<div class="box">
|
|||
|
|
<h4>Linux (Ubuntu 24.04)</h4>
|
|||
|
|
<pre data-lang="bash" data-name="转发并直接验证反代"># env: linux
|
|||
|
|
kubectl port-forward -n shop deploy/shop-web 18080:8080 &
|
|||
|
|
curl -s http://127.0.0.1:18080/api/healthz; echo</pre>
|
|||
|
|
<p>本机有 <code>curl</code>,不看页面也能验证:打反代路径 <code>/api/healthz</code>,返回 <code>ok</code> 即通。</p>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
<p><b>差异结论</b>:命令一样,差别在验证手段。Windows 侧只能看渲染结果,Linux 侧能直接打反代路径,
|
|||
|
|
一眼分清是页面问题还是反代问题。</p>
|
|||
|
|
|
|||
|
|
<h4>Step 6 · 反向验证:把 selector 写错一个字母</h4>
|
|||
|
|
<p><span class="tag">意图</span>亲手制造一次「Service 没有后端」,记住它的两条报错。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="改错标签并观察"># env: all
|
|||
|
|
sed -i 's#app.kubernetes.io/instance: shop#app.kubernetes.io/instance: shp#' deploy/base/shop-api-svc.yaml
|
|||
|
|
kubectl apply -f deploy/base/shop-api-svc.yaml
|
|||
|
|
kubectl get endpointslices -n shop -l kubernetes.io/service-name=shop-api
|
|||
|
|
kubectl exec -n shop deploy/shop-web -- curl -s -m 3 http://shop-api:8080/healthz</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
service/shop-api configured
|
|||
|
|
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
|
|||
|
|
shop-api-9x2kd IPv4 8080 <unset> 3m12s
|
|||
|
|
curl: (7) Failed to connect to shop-api port 8080 after 0 ms: Connection refused</pre>
|
|||
|
|
|
|||
|
|
<p><code>ENDPOINTS</code> 变 <code><unset></code>,说明名单空了;报 <code>Connection refused</code>
|
|||
|
|
而不是解析失败,说明名字与 VIP 都在,只是后面没人。改回来,等两三秒再看:</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="改回标签"># env: all
|
|||
|
|
sed -i 's#app.kubernetes.io/instance: shp#app.kubernetes.io/instance: shop#' deploy/base/shop-api-svc.yaml
|
|||
|
|
kubectl apply -f deploy/base/shop-api-svc.yaml
|
|||
|
|
kubectl get endpointslices -n shop -l kubernetes.io/service-name=shop-api</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
service/shop-api configured
|
|||
|
|
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
|
|||
|
|
shop-api-9x2kd IPv4 8080 10.244.1.7,10.244.2.4,10.244.3.6 4m40s</pre>
|
|||
|
|
|
|||
|
|
<p>整个过程 <code>get svc</code> 一直显示 <code>ClusterIP 10.96.45.12</code>,看不出后端是空的。
|
|||
|
|
排障第一步永远是 EndpointSlice。</p>
|
|||
|
|
|
|||
|
|
<h4>Step 7 · 建 headless Service,对比两次解析</h4>
|
|||
|
|
<p><span class="tag">意图</span>看清「一个名字对一个 VIP」与「一个名字对三个地址」的差别。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="yaml" data-name="deploy/base/shop-api-headless.yaml"># file: deploy/base/shop-api-headless.yaml
|
|||
|
|
apiVersion: v1
|
|||
|
|
kind: Service
|
|||
|
|
metadata:
|
|||
|
|
name: shop-api-headless
|
|||
|
|
namespace: shop
|
|||
|
|
labels:
|
|||
|
|
app.kubernetes.io/name: shop-api
|
|||
|
|
app.kubernetes.io/instance: shop
|
|||
|
|
app.kubernetes.io/part-of: aurora-shop
|
|||
|
|
spec:
|
|||
|
|
clusterIP: None # 不要 VIP:DNS 直接返回 Pod 地址
|
|||
|
|
selector:
|
|||
|
|
app.kubernetes.io/name: shop-api
|
|||
|
|
app.kubernetes.io/instance: shop
|
|||
|
|
ports:
|
|||
|
|
- name: http
|
|||
|
|
port: 8080
|
|||
|
|
targetPort: 8080</pre>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="应用并对比两个 Service"># env: all
|
|||
|
|
kubectl apply -f deploy/base/shop-api-headless.yaml
|
|||
|
|
kubectl get svc -n shop</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
service/shop-api-headless created
|
|||
|
|
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
|
|||
|
|
shop-api ClusterIP 10.96.45.12 <none> 8080/TCP 6m
|
|||
|
|
shop-api-headless ClusterIP None <none> 8080/TCP 4s</pre>
|
|||
|
|
|
|||
|
|
<p>两个 Service 选同一批 Pod,行为却不同:普通 Service 返回一条 A 记录指向 VIP,headless 返回三条,
|
|||
|
|
每条对应一个就绪 Pod。所以 headless 没有负载均衡,客户端总用第一个地址。</p>
|
|||
|
|
|
|||
|
|
<p><code>None</code> 是声明「不要 VIP」,不是分配失败。名字的完整形态是
|
|||
|
|
<code><service>.<namespace>.svc.cluster.local</code>,短名靠 <code>search</code> 域补全,见图 2。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="两次 nslookup"># env: all
|
|||
|
|
kubectl exec -n shop deploy/shop-web -- nslookup shop-api
|
|||
|
|
kubectl exec -n shop deploy/shop-web -- nslookup shop-api-headless</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
Server: 10.96.0.10
|
|||
|
|
Address: 10.96.0.10:53
|
|||
|
|
|
|||
|
|
Name: shop-api.shop.svc.cluster.local
|
|||
|
|
Address: 10.96.45.12
|
|||
|
|
|
|||
|
|
Server: 10.96.0.10
|
|||
|
|
Address: 10.96.0.10:53
|
|||
|
|
|
|||
|
|
Name: shop-api-headless.shop.svc.cluster.local
|
|||
|
|
Address 1: 10.244.1.7 shop-api-7d9f8c6b5d-2xk4l.shop-api-headless.shop.svc.cluster.local
|
|||
|
|
Address 2: 10.244.2.4 shop-api-7d9f8c6b5d-h8bq9.shop-api-headless.shop.svc.cluster.local
|
|||
|
|
Address 3: 10.244.3.6 shop-api-7d9f8c6b5d-v5trl.shop-api-headless.shop.svc.cluster.local</pre>
|
|||
|
|
|
|||
|
|
<figure class="figure">
|
|||
|
|
<svg viewBox="0 0 720 280" role="img" aria-label="短名 shop-api 在 ndots:5 下按 search 域补全为四条候选,第一条命中">
|
|||
|
|
<defs>
|
|||
|
|
<marker id="a5b" 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="10" y="110" width="146" height="58" rx="9" style="fill:var(--card);stroke:var(--line)"></rect>
|
|||
|
|
<text x="83" y="134" text-anchor="middle" font-size="12.5" style="fill:var(--ink)">curl shop-api:8080</text>
|
|||
|
|
<text x="83" y="154" text-anchor="middle" font-size="11" style="fill:var(--muted)">名字里一个点都没有</text>
|
|||
|
|
|
|||
|
|
<rect x="186" y="10" width="250" height="104" rx="9" style="fill:var(--soft);stroke:var(--line)"></rect>
|
|||
|
|
<text x="196" y="32" font-size="12" style="fill:var(--ink)">/etc/resolv.conf</text>
|
|||
|
|
<text x="196" y="56" font-size="11" style="fill:var(--muted)">nameserver 10.96.0.10</text>
|
|||
|
|
<text x="196" y="78" font-size="11" style="fill:var(--muted)">search shop.svc.cluster.local</text>
|
|||
|
|
<text x="196" y="100" font-size="11" style="fill:var(--muted)">options ndots:5</text>
|
|||
|
|
|
|||
|
|
<rect x="186" y="140" width="250" height="112" rx="9" style="fill:var(--card);stroke:var(--line)"></rect>
|
|||
|
|
<text x="196" y="162" font-size="11.5" style="fill:var(--ink)">依次尝试,命中即停</text>
|
|||
|
|
<text x="196" y="184" font-size="11" style="fill:var(--muted)">1 shop-api.shop.svc.cluster.local</text>
|
|||
|
|
<text x="196" y="204" font-size="11" style="fill:var(--muted)">2 shop-api.svc.cluster.local</text>
|
|||
|
|
<text x="196" y="224" font-size="11" style="fill:var(--muted)">3 shop-api.cluster.local</text>
|
|||
|
|
<text x="196" y="244" font-size="11" style="fill:var(--muted)">4 shop-api.(当作绝对域名)</text>
|
|||
|
|
|
|||
|
|
<rect x="486" y="166" width="222" height="60" rx="9" style="fill:var(--brand-soft);stroke:var(--brand)"></rect>
|
|||
|
|
<text x="597" y="190" text-anchor="middle" font-size="12" style="fill:var(--brand)">第 1 条就命中</text>
|
|||
|
|
<text x="597" y="210" text-anchor="middle" font-size="12" style="fill:var(--brand)">A 10.96.45.12</text>
|
|||
|
|
|
|||
|
|
<path d="M156,132 L182,102" style="stroke:var(--muted);fill:none" marker-end="url(#a5b)"></path>
|
|||
|
|
<path d="M156,146 L182,152" style="stroke:var(--muted);fill:none" marker-end="url(#a5b)"></path>
|
|||
|
|
<path d="M436,196 L482,196" style="stroke:var(--muted);fill:none" marker-end="url(#a5b)"></path>
|
|||
|
|
</svg>
|
|||
|
|
<div class="figcap"><b>图 2 ·</b><code>ndots:5</code> 决定补全顺序:点数不足 5 个的名字先补 <code>search</code> 后缀</div>
|
|||
|
|
</figure>
|
|||
|
|
|
|||
|
|
<p>点数少于 5 个的名字先依次补 <code>search</code> 里的三个后缀,全部失败才当绝对域名发出去——
|
|||
|
|
短名能用,代价是最多四次查询。</p>
|
|||
|
|
|
|||
|
|
<details>
|
|||
|
|
<summary>可跳过 · 为什么这里用 <code>nslookup</code> 而不是 <code>dig</code></summary>
|
|||
|
|
<p><code>nginx:1.27-alpine</code> 里只有 busybox 的 <code>nslookup</code>,<code>dig</code> 属于
|
|||
|
|
<code>bind-tools</code> 包。想在 Pod 里用它得先 <code>apk add --no-cache bind-tools</code>,
|
|||
|
|
不想动镜像就用宿主机,见下面双栏。</p>
|
|||
|
|
</details>
|
|||
|
|
|
|||
|
|
<p>同一件事还有第二种验证方式:直接问 CoreDNS。</p>
|
|||
|
|
|
|||
|
|
<div class="grid2">
|
|||
|
|
<div class="box">
|
|||
|
|
<h4>Windows 11 + WSL2</h4>
|
|||
|
|
<pre data-lang="bash" data-name="借 Pod 代问"># env: wsl
|
|||
|
|
kubectl exec -n shop deploy/shop-web -- nslookup shop-api-headless.shop.svc.cluster.local</pre>
|
|||
|
|
<p>Docker Desktop 把节点放在自己的虚拟机里,WSL 侧加不出到 <code>10.96.0.0/16</code> 的路由,只能借 Pod 代问。</p>
|
|||
|
|
</div>
|
|||
|
|
<div class="box">
|
|||
|
|
<h4>Linux (Ubuntu 24.04)</h4>
|
|||
|
|
<pre data-lang="bash" data-name="加路由后直接 dig"># env: linux
|
|||
|
|
NODE_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' aurora-control-plane)
|
|||
|
|
sudo ip route add 10.96.0.0/16 via $NODE_IP
|
|||
|
|
dig +short @10.96.0.10 shop-api-headless.shop.svc.cluster.local
|
|||
|
|
sudo ip route del 10.96.0.0/16</pre>
|
|||
|
|
<p>返回三行 Pod 地址。路由只在这一段命令里存在,最后一行删掉,不留残留。</p>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
<p><b>差异结论</b>:差别在能不能直接问 DNS。Linux 上加一条到 service 网段的路由就能 <code>dig @10.96.0.10</code>;
|
|||
|
|
WSL2 里节点在 Docker Desktop 的虚拟机内,这条路走不通。</p>
|
|||
|
|
|
|||
|
|
<div class="box warn">
|
|||
|
|
<span class="bt">易错点 · 短名只在同一个命名空间有效</span>
|
|||
|
|
<p><b>现象</b>:把同一份脚本复制到别的命名空间的 Pod 里,<code>curl http://shop-api:8080</code> 报
|
|||
|
|
<code>Could not resolve host: shop-api</code>。</p>
|
|||
|
|
<p><b>原因</b>:<code>search</code> 域只含当前 Pod 所在命名空间,补全出来的都是
|
|||
|
|
<code>shop-api.<当前 ns>.svc.cluster.local</code>,跨命名空间必须写 <code>shop-api.shop</code> 或全名。</p>
|
|||
|
|
<p><b>怎么验证</b>:<code>kubectl exec -n shop deploy/shop-web -- cat /etc/resolv.conf</code> 看
|
|||
|
|
<code>search</code> 那行的命名空间;再试 <code>curl -s http://shop-api.shop:8080/healthz</code>,应返回 <code>ok</code>。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h4>Step 8 · 提交本章产物</h4>
|
|||
|
|
<p><span class="tag">意图</span>三份清单与 <code>Makefile</code> 的断言一起入库,第 6 章从这里接着做。</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="提交"># env: all
|
|||
|
|
git add deploy/base/shop-api-svc.yaml deploy/base/shop-api-headless.yaml deploy/base/shop-web.yaml Makefile
|
|||
|
|
git commit -m "ch05: 给 shop-api 固定入口,拆出 shop-web"</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
[main 4f2c1a9] ch05: 给 shop-api 固定入口,拆出 shop-web
|
|||
|
|
4 files changed, 168 insertions(+)</pre>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-7">5.7 验收清单</h3>
|
|||
|
|
|
|||
|
|
<ol class="steps">
|
|||
|
|
<li><code>kubectl get svc -n shop</code> → 两个 Service,<code>shop-api</code> 有 VIP,<code>shop-api-headless</code> 是 <code>None</code></li>
|
|||
|
|
<li><code>kubectl get endpointslices -n shop</code> → 两条,<code>ENDPOINTS</code> 各三个地址</li>
|
|||
|
|
<li>从 <code>shop-web</code> 里 <code>curl -s http://shop-api:8080/healthz</code> → <code>ok</code></li>
|
|||
|
|
<li>反向验证:<code>kubectl delete pod</code> 掉一个副本,名单在 10 秒内换成新地址,期间 <code>curl</code> 不断</li>
|
|||
|
|
<li><code>nslookup shop-api-headless</code> → 三条 A 记录;<code>nslookup shop-api</code> → 一条,指向 VIP</li>
|
|||
|
|
<li><code>make verify-ch05</code> 全绿</li>
|
|||
|
|
</ol>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="一次跑完"># env: all
|
|||
|
|
make verify-ch05</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
[ok] service shop-api clusterip 8080
|
|||
|
|
[ok] endpointslice shop-api has 3 ready addresses
|
|||
|
|
[ok] service shop-api-headless has no vip
|
|||
|
|
[ok] headless returns 3 A records
|
|||
|
|
[ok] deployment shop-web ready 2/2
|
|||
|
|
[ok] curl shop-api:8080/healthz from shop-web is ok</pre>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-8">5.8 故障实验室</h3>
|
|||
|
|
|
|||
|
|
<p>两个故障在同一条链上:一个断在解析,一个断在转发,见图 3。</p>
|
|||
|
|
|
|||
|
|
<figure class="figure">
|
|||
|
|
<svg viewBox="0 0 720 300" role="img" aria-label="从 curl 报错出发的三层判断:能否解析、名单是否有地址、端口是否对得上">
|
|||
|
|
<defs>
|
|||
|
|
<marker id="a5c" 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="280" y="8" width="160" height="44" rx="9" style="fill:var(--soft);stroke:var(--line)"></rect>
|
|||
|
|
<text x="360" y="36" text-anchor="middle" font-size="12.5" style="fill:var(--ink)">curl shop-api:8080</text>
|
|||
|
|
|
|||
|
|
<polygon points="360,72 458,110 360,148 262,110" style="fill:var(--card);stroke:var(--line)"></polygon>
|
|||
|
|
<text x="360" y="114" text-anchor="middle" font-size="11.5" style="fill:var(--ink)">能解析出 IP 吗</text>
|
|||
|
|
|
|||
|
|
<rect x="14" y="86" width="208" height="50" rx="9" style="fill:var(--danger-bg);stroke:var(--danger-line)"></rect>
|
|||
|
|
<text x="118" y="107" text-anchor="middle" font-size="11.5" style="fill:var(--danger)">否 · 解析失败</text>
|
|||
|
|
<text x="118" y="125" text-anchor="middle" font-size="11" style="fill:var(--muted)">查 CoreDNS 副本与 search 域</text>
|
|||
|
|
|
|||
|
|
<polygon points="360,196 470,232 360,268 250,232" style="fill:var(--card);stroke:var(--line)"></polygon>
|
|||
|
|
<text x="360" y="236" text-anchor="middle" font-size="11.5" style="fill:var(--ink)">名单里有就绪地址吗</text>
|
|||
|
|
|
|||
|
|
<rect x="14" y="206" width="208" height="50" rx="9" style="fill:var(--warn-bg);stroke:var(--warn-line)"></rect>
|
|||
|
|
<text x="118" y="227" text-anchor="middle" font-size="11.5" style="fill:var(--warn)">否 · 名单为空</text>
|
|||
|
|
<text x="118" y="245" text-anchor="middle" font-size="11" style="fill:var(--muted)">查 selector 与 Pod 就绪</text>
|
|||
|
|
|
|||
|
|
<rect x="502" y="206" width="206" height="50" rx="9" style="fill:var(--warn-bg);stroke:var(--warn-line)"></rect>
|
|||
|
|
<text x="605" y="227" text-anchor="middle" font-size="11.5" style="fill:var(--warn)">是 · 地址齐但连不上</text>
|
|||
|
|
<text x="605" y="245" text-anchor="middle" font-size="11" style="fill:var(--muted)">查 targetPort 与 kube-proxy</text>
|
|||
|
|
|
|||
|
|
<path d="M360,52 L360,68" style="stroke:var(--muted);fill:none" marker-end="url(#a5c)"></path>
|
|||
|
|
<path d="M262,110 L226,110" style="stroke:var(--muted);fill:none" marker-end="url(#a5c)"></path>
|
|||
|
|
<text x="240" y="100" text-anchor="middle" font-size="10.5" style="fill:var(--muted)">否</text>
|
|||
|
|
<path d="M360,148 L360,192" style="stroke:var(--muted);fill:none" marker-end="url(#a5c)"></path>
|
|||
|
|
<text x="372" y="172" text-anchor="middle" font-size="10.5" style="fill:var(--muted)">是</text>
|
|||
|
|
<path d="M250,232 L226,232" style="stroke:var(--muted);fill:none" marker-end="url(#a5c)"></path>
|
|||
|
|
<text x="238" y="222" text-anchor="middle" font-size="10.5" style="fill:var(--muted)">否</text>
|
|||
|
|
<path d="M470,232 L498,232" style="stroke:var(--muted);fill:none" marker-end="url(#a5c)"></path>
|
|||
|
|
<text x="484" y="222" text-anchor="middle" font-size="10.5" style="fill:var(--muted)">是</text>
|
|||
|
|
</svg>
|
|||
|
|
<div class="figcap"><b>图 3 ·</b>报错先分三层:解析不到、名单为空、地址齐全但连不上</div>
|
|||
|
|
</figure>
|
|||
|
|
|
|||
|
|
<div class="box danger">
|
|||
|
|
<span class="bt">故障 1 · 所有 Service 的名字都解析不了</span>
|
|||
|
|
<p><b>注入</b>:<code>kubectl -n kube-system scale deploy/coredns --replicas=0</code>,再从
|
|||
|
|
<code>shop-web</code> 里发起新请求。</p>
|
|||
|
|
<p><b>症状</b>:已建立的连接还在,新请求全部失败。<code>curl http://shop-api:8080/healthz</code> 报
|
|||
|
|
<code>Could not resolve host: shop-api</code>;改成 <code>curl http://10.96.45.12:8080/healthz</code>
|
|||
|
|
却返回 <code>ok</code>。</p>
|
|||
|
|
<p><b>诊断</b>:<code>kubectl exec -n shop deploy/shop-web -- cat /etc/resolv.conf</code> 看
|
|||
|
|
<code>nameserver</code> 是 <code>10.96.0.10</code>;
|
|||
|
|
<code>kubectl get pods -n kube-system -l k8s-app=kube-dns</code> 一个都没有;
|
|||
|
|
<code>kubectl get endpointslices -n kube-system -l kubernetes.io/service-name=kube-dns</code> 的
|
|||
|
|
<code>ENDPOINTS</code> 是 <code><unset></code>。</p>
|
|||
|
|
<p><b>根因</b>:<code>10.96.0.10</code> 是 <code>kube-dns</code> 的 VIP,后面一个就绪端点都没有,
|
|||
|
|
UDP 53 的查询被送到没有进程的地方,客户端等到超时。DNS 故障只断在「解析」这一环,不影响直连 IP。</p>
|
|||
|
|
<p><b>修复</b>:<code>kubectl -n kube-system scale deploy/coredns --replicas=2</code>,约 20 秒恢复;
|
|||
|
|
已缓存的名字在 TTL 内还会命中,别拿「立刻通」当验收标准。</p>
|
|||
|
|
<p><b>预防</b>:给 CoreDNS 配 PodDisruptionBudget,并把「解析失败」与「连接失败」拆成两条独立告警,见第 13 章。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="box danger">
|
|||
|
|
<span class="bt">故障 2 · 名单是满的,请求却一直超时</span>
|
|||
|
|
<p><b>注入</b>:把 <code>kube-proxy</code> 的 <code>nodeSelector</code> 改成一个没有节点带的标签,再删掉现有 Pod——DaemonSet 没有 <code>replicas</code> 字段,<code>kubectl scale</code> 对它无效。</p>
|
|||
|
|
<pre data-lang="bash" data-name="让 kube-proxy 无法调度"><code># env: all
|
|||
|
|
kubectl -n kube-system patch ds kube-proxy --type=merge \
|
|||
|
|
-p '{"spec":{"template":{"spec":{"nodeSelector":{"kube-proxy-disabled":"true"}}}}}'
|
|||
|
|
kubectl -n kube-system delete pod -l k8s-app=kube-proxy</code></pre>
|
|||
|
|
<p><b>症状</b>:老 Service 正常,新建的连不通,<code>curl -m 3</code> 三秒后返回
|
|||
|
|
<code>Connection timed out</code>——注意是超时,不是 <code>refused</code>。<code>EndpointSlice</code> 里地址齐全。</p>
|
|||
|
|
<p><b>诊断</b>:<code>kubectl -n kube-system get ds kube-proxy</code> 显示 <code>DESIRED 0</code>;
|
|||
|
|
<code>kubectl get pods -n kube-system -l k8s-app=kube-proxy</code> 为空;
|
|||
|
|
进节点看规则,<code>docker exec aurora-control-plane iptables -t nat -L KUBE-SERVICES | head</code>
|
|||
|
|
里只有老 Service 的 VIP,没有新建的那一个。</p>
|
|||
|
|
<p><b>根因</b>:VIP 本身不响应任何东西,它只是规则匹配出来的目的地址。规则由 <code>kube-proxy</code> 写进内核,
|
|||
|
|
进程不在就没人写新规则,报文到达一个没有监听者、也没有规则的地址,只能等超时。老 Service 还通,
|
|||
|
|
是因为旧规则留在节点上没被清掉。</p>
|
|||
|
|
<p><b>修复</b>:把那个标签去掉,DaemonSet 会在三台节点上重新建 Pod,新规则随之写入。</p>
|
|||
|
|
<pre data-lang="bash" data-name="恢复 kube-proxy"><code># env: all
|
|||
|
|
kubectl -n kube-system patch ds kube-proxy --type=json \
|
|||
|
|
-p '[{"op":"remove","path":"/spec/template/spec/nodeSelector/kube-proxy-disabled"}]'
|
|||
|
|
kubectl -n kube-system rollout status ds/kube-proxy</code></pre>
|
|||
|
|
<p><b>预防</b>:把 <code>kube-proxy</code> 的就绪数纳入巡检;升级后验证一个新建的 Service,而不只是已有的那几个。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-9">5.9 生产对照</h3>
|
|||
|
|
|
|||
|
|
<div class="tablewrap">
|
|||
|
|
<table>
|
|||
|
|
<thead><tr><th>维度</th><th>学习环境</th><th>生产环境</th><th>为什么不同</th><th>迁移动作</th></tr></thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td>Service 类型</td><td>只用 ClusterIP</td><td>内网 LB + Ingress,公网流量不进 Pod 网段</td><td>集群外客户端没有到 Pod 网段的路由</td><td>入口交给第 9 章,Service 仍保持 ClusterIP</td></tr>
|
|||
|
|
<tr><td>Service 个数</td><td>每个工作负载一个,另加一个 headless 做对比</td><td>普通 Service 按调用方划分;headless 只给有状态集</td><td>headless 没有负载均衡,普通业务用它会放大单点</td><td>本章的 headless 只做对比,第 8 章再给 StatefulSet 用</td></tr>
|
|||
|
|
<tr><td>代理模式</td><td>kind 默认 <code>iptables</code></td><td><code>nftables</code>(v1.33 GA),或继续 iptables</td><td>几千个 Service 时 iptables 规则链会成为匹配负担</td><td>新集群在 kube-proxy 配置里显式选 <code>nftables</code></td></tr>
|
|||
|
|
<tr><td>端点健康</td><td>没有探针,<code>Running</code> 就算就绪</td><td>readinessProbe + 优雅退出 + PDB</td><td>没有探针的 Pod 会把流量接走却处理不了</td><td>第 7 章补齐探针与 <code>preStop</code></td></tr>
|
|||
|
|
<tr><td>DNS</td><td>CoreDNS 2 副本,kind 默认</td><td>2 副本以上 + 反亲和 + NodeLocal DNSCache</td><td>上万 Pod 时 53 端口的并发与跨节点延迟不可忽略</td><td>第 18 章随节点规模一起规划</td></tr>
|
|||
|
|
<tr><td>变更方式</td><td><code>kubectl apply</code> 直接改</td><td>GitOps 提交 + 审计 + 回滚</td><td>手工 apply 没有记录,出事无法定位是谁改的</td><td>第 20 章</td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-10">5.10 清理与残留检查</h3>
|
|||
|
|
|
|||
|
|
<p>本章产物留给第 6 章,只清验证过程留下的临时对象。先看一遍现场:</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="现场检查"># env: all
|
|||
|
|
kubectl get svc,deploy -n shop
|
|||
|
|
kubectl get endpointslices -n shop
|
|||
|
|
git status --short</pre>
|
|||
|
|
<pre data-lang="text" data-name="预期输出"># 预期输出
|
|||
|
|
NAME TYPE CLUSTER-IP PORT(S)
|
|||
|
|
service/shop-api ClusterIP 10.96.45.12 8080/TCP
|
|||
|
|
service/shop-api-headless ClusterIP None 8080/TCP
|
|||
|
|
NAME READY UP-TO-DATE AVAILABLE
|
|||
|
|
deployment.apps/shop-api 3/3 3 3
|
|||
|
|
deployment.apps/shop-web 2/2 2 2
|
|||
|
|
|
|||
|
|
NAME ADDRESSTYPE ENDPOINTS
|
|||
|
|
shop-api-9x2kd IPv4 10.244.1.7,10.244.2.4,10.244.3.6
|
|||
|
|
shop-api-headless-j4m8p IPv4 10.244.1.7,10.244.2.4,10.244.3.6</pre>
|
|||
|
|
|
|||
|
|
<p>残留检查三条:<code>shop</code> 里不应有临时 Pod,<code>port-forward</code> 进程应已退出,
|
|||
|
|
<code>git status</code> 应为空。</p>
|
|||
|
|
|
|||
|
|
<p>回滚本章:</p>
|
|||
|
|
|
|||
|
|
<pre data-lang="bash" data-name="回滚本章"># env: all
|
|||
|
|
kubectl delete svc shop-api shop-api-headless -n shop
|
|||
|
|
kubectl delete deploy shop-web -n shop
|
|||
|
|
git revert --no-edit HEAD</pre>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-11">5.11 小结与自测</h3>
|
|||
|
|
|
|||
|
|
<p class="take"><b>一句话:</b>Pod IP 属于某一次运行,ClusterIP 属于这个服务。</p>
|
|||
|
|
<p class="take"><b>一句话:</b>名单为空报 <code>refused</code>,名单有而规则没写报 <code>timeout</code>。</p>
|
|||
|
|
<p class="take"><b>一句话:</b><code>could not resolve</code> 先查 CoreDNS,其余先查 EndpointSlice。</p>
|
|||
|
|
|
|||
|
|
<details>
|
|||
|
|
<summary>自测 3 题(点击展开答案)</summary>
|
|||
|
|
<p><b>1. selector 里为什么不能写 <code>version</code>?</b>
|
|||
|
|
滚动更新期间新旧 ReplicaSet 的 Pod 带着不同的 <code>version</code> 值,Service 只会选中其中一批,
|
|||
|
|
表现是「副本都在,流量只进一半」。</p>
|
|||
|
|
<p><b>2. headless 的 ClusterIP 是 <code>None</code>,为什么还能 <code>curl</code> 通?</b>
|
|||
|
|
因为调用方拿到的是三个 Pod 地址而不是 VIP,自己挑一个连过去,全程没有 VIP 参与——所以它也没有负载均衡。</p>
|
|||
|
|
<p><b>3. <code>ndots:5</code> 对 <code>shop-api.shop</code> 这种带一个点的名字有什么影响?</b>
|
|||
|
|
点数 1 小于 5,仍会先补 <code>search</code> 后缀,试一个无效名字后才按绝对域名解析,多出一次无效查询。</p>
|
|||
|
|
</details>
|
|||
|
|
|
|||
|
|
<div class="box tip">
|
|||
|
|
<span class="bt">延伸挑战(≤30 分钟)</span>
|
|||
|
|
<p>用一行 <code>-o jsonpath</code> 算出每个 Service 的就绪地址数,加进 <code>make verify-ch05</code>。
|
|||
|
|
再把 <code>shop-api</code> 扩到 4 个副本,看断言立刻变红——这就是把「三个」写死的代价。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-12">5.12 衔接</h3>
|
|||
|
|
|
|||
|
|
<p>那次 502 的根因是地址被写死;现在调用方只写 <code>http://shop-api:8080</code>,
|
|||
|
|
滚动更新、扩缩容、Pod 重建都不需要改配置。第 4 章留下的缺口,到这里补上了。</p>
|
|||
|
|
|
|||
|
|
<p>还有一处没解决:<code>shop-web</code> 页面顶部的 <code>THEME</code> 写在镜像构建参数里,
|
|||
|
|
改一次主题就要重新构建、推送、发布。</p>
|
|||
|
|
|
|||
|
|
<p>第 6 章把 <code>THEME</code> 与数据库口令搬进 ConfigMap 和 Secret,做到改配置不用改镜像,
|
|||
|
|
顺带解释 Secret 只是 base64。</p>
|
|||
|
|
|
|||
|
|
<h3 id="sec-5-13">5.13 参考与验证版本</h3>
|
|||
|
|
|
|||
|
|
<ul>
|
|||
|
|
<li><a href="https://kubernetes.io/docs/concepts/services-networking/service/">Service</a></li>
|
|||
|
|
<li><a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/">DNS for Services and Pods</a></li>
|
|||
|
|
<li><a href="https://kubernetes.io/docs/concepts/services-networking/endpoint-slices/">EndpointSlices</a></li>
|
|||
|
|
<li><a href="https://kubernetes.io/docs/reference/networking/virtual-ips/">Virtual IPs and Service Proxies</a></li>
|
|||
|
|
<li><a href="https://kubernetes.io/blog/2025/04/24/endpoints-deprecation/">Endpoints 废弃说明</a></li>
|
|||
|
|
</ul>
|
|||
|
|
|
|||
|
|
<p>验证版本:kind v0.32.0 / kindest/node v1.36.1 / 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="04-deployment.html"><span class="k">← 上一章</span><span class="v">第 4 章 · Deployment:副本与滚动更新</span></a>
|
|||
|
|
<a class="next" href="06-config.html"><span class="k">下一章 →</span><span class="v">第 6 章 · 配置与密钥:ConfigMap 与 Secret</span></a>
|
|||
|
|
</nav>
|
|||
|
|
|
|||
|
|
<p class="foot">
|
|||
|
|
Kubernetes 专题 · S1 第 5 章 Service 与集群 DNS<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>
|