2026-11-11 12:33:25 +08:00
<!DOCTYPE html>
< html lang = "zh-CN" data-theme = "light" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< title > 第 9 章 · 入口与网关: Ingress 与 Gateway API · Kubernetes 专题< / title >
< meta name = "description" content = "在 kind 集群上装 Envoy Gateway, 用 Gateway 与 HTTPRoute 把 shop.example.com 接到 shop-web, 把 Envoy 的 Service 钉到 30080 与 30443, 用自签证书打通 HTTPS, 并用一个没人处理的 Ingress 说明资源与控制器是两回事。" >
< meta name = "keywords" content = "Gateway API, Envoy Gateway, HTTPRoute, Ingress, NodePort, TLS 终止" >
< 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 > 第 9 章< / b >
< / div >
< div class = "spacer" > < / div >
< button class = "btn" id = "tocToggle" > ☰ 导航< / button >
< button class = "btn" id = "themeBtn" title = "切换到深色主题" > 深色< / button >
< / div >
< / header >
< div class = "layout" id = "layout" >
< aside class = "sidebar" >
< h2 > 章节目录< / h2 >
2026-11-11 12:57:24 +08:00
< nav id = "chapters" > < div class = "toc-stage" > < b > S0< / b > < span class = "h" > 起步:环境与心智< / span > < / div > < a class = "toc-a" href = "00-why.html" > < i class = "cn" > 0< / i > 从一次线上事故理解 K8s< / a > < a class = "toc-a" href = "01-env.html" > < i class = "cn" > 1< / i > 装第一个集群: Windows 11 + WSL2 与 Linux 双轨< / a > < a class = "toc-a" href = "02-kubectl.html" > < i class = "cn" > 2< / i > kubectl 与集群解剖:把黑盒变成可观测对象< / a > < div class = "toc-stage" > < b > S1< / b > < span class = "h" > 跑起来:单服务最小闭环< / span > < / div > < a class = "toc-a" href = "03-pod.html" > < i class = "cn" > 3< / i > Pod: 最小调度单元< / a > < a class = "toc-a" href = "04-deployment.html" > < i class = "cn" > 4< / i > Deployment: 副本与滚动更新< / a > < a class = "toc-a" href = "05-service.html" > < i class = "cn" > 5< / i > Service 与集群 DNS< / a > < a class = "toc-a" href = "06-config.html" > < i class = "cn" > 6< / i > 配置与密钥: ConfigMap 与 Secret< / a > < a class = "toc-a" href = "07-probe.html" > < i class = "cn" > 7< / i > 探针与资源:让它活得像生产< / a > < div class = "toc-stage" > < b > S2< / b > < span class = "h" > 撑得住:状态、入口与多服务< / span > < / div > < a class = "toc-a" href = "08-storage.html" > < i class = "cn" > 8< / i > 存储:从 emptyDir 到 StatefulSet< / a > < span class = "toc-a active" aria-current = "page" > < i class = "cn" > 9< / i > 入口与网关: Ingress 与 Gateway API< / span > < div class = "subnav current" > < a class = "toc-a lv3" href = "#sec-9-1" > 9.1 本章在哪:主线之前与之后< / a > < a class = "toc-a lv3" href = "#sec-9-2" > 9.2 学习目标< / a > < a class = "toc-a lv3" href = "#sec-9-3" > 9.3 场景:域名解析到了,页面还是 502< / a > < a class = "toc-a lv3" href = "#sec-9-4" > 9.4 心智模型:一栋楼的门与指示牌< / a > < a class = "toc-a lv3" href = "#sec-9-5" > 9.5 概念与字段速查< / a > < a class = "toc-a lv3" href = "#sec-9-6" > 9.6 动手:把 shop.example.com 接进来< / a > < a class = "toc-a lv3" href = "#sec-9-7" > 9.7 验收清单< / a > < a class = "toc-a lv3" href = "#sec-9-8" > 9.8 故障实验室< / a > < a class = "toc-a lv3" href = "#sec-9-9" > 9.9 生产对照< / a > < a class = "toc-a lv3" href = "#sec-9-10" > 9.10 清理与残留检查< / a > < a class = "toc-a lv3" href = "#sec-9-11" > 9.11 小结与自测< / a > < a class = "toc-a lv3" href = "#sec-9-12" > 9.12 衔接< / a > < a class = "toc-a lv3" href = "#sec-9-13" > 9.13 参考与验证版本< / a > < / div > < a class = "toc-a" href = "10-job.html" > < i class = "cn" > 10< / i > Job 与 CronJob: 迁移与定时任务< / a > < a class = "toc-a" href = "11-multisvc.html" > < i class = "cn" > 11< / i > 多服务与网络策略< / a > < div class = "toc-stage" > < b > S3< / b > < span class = "h" > 看得见:可观测、发布与排障< / span > < / div > < a class = "toc-a" href = "12-logging.html" > < i class = "cn" > 12< / i > 日志:从 kubectl logs 到可检索< / a > < a class = "toc-a" href = "13-metrics.html" > < i class = "cn" > 13< / i > 指标、扩缩与告警< / a > < a class = "toc-a" href = "14-release.html" > < i class = "cn" > 14< / i > 发布策略:滚动、蓝绿、金丝雀< / a > < a class = "toc-a" href = "15-supply.html" > < i class = "cn" > 15< / i > 密钥与镜像供应链< / a > < a class = "toc-a" href = "16-debug.html" > < i class = "cn" > 16< / i > 故障诊断方法论:六类故障,一张诊断树< / a > < div class = "toc-stage" > < b > S4< / b > < span class = "h" > 上生产:集群、调度、安全、交付< / span > < / div > < 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 >
2026-11-11 12:33:25 +08:00
< div class = "side-foot" >
< a class = "btn" href = "index.html" > ← 总目录< / a >
< / div >
< / aside >
< div class = "mask" id = "mask" > < / div >
< main class = "main-col" >
< article >
< p class = "chapnav" >
< a href = "index.html" > Kubernetes 专题< / a >
< span > ·< / span >
< span > S2 撑得住:状态、入口与多服务< / span >
< span > ·< / span >
< span > 第 < b class = "pos" > 9< / b > / 26 章< / span >
< / p >
< h2 id = "ch9" > < span class = "n" > 9< / span > 入口与网关: Ingress 与 Gateway API< / h2 >
< div class = "lead" >
< b > 30 秒速览:< / b > 到第 8 章为止,集群里的一切都只能用 < code > port-forward< / code > 看。本章装 Envoy Gateway v1.10.0,
用 < code > Gateway< / code > 与 < code > HTTPRoute< / code > 把 < code > shop.example.com< / code > 接到 < code > shop-web< / code > ,
再把 Envoy 的 Service 钉到 kind 已经映射好的 30080/30443, 最后用自签证书让 HTTPS 走通。
约 150 分钟,其中 20 分钟在等 Envoy 的镜像。
< / div >
< h3 id = "sec-9-1" > 9.1 本章在哪:主线之前与之后< / h3 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 项< / th > < th > 内容< / th > < / tr > < / thead >
< tbody >
< tr > < td > 主线之前< / td > < td > 页面只能靠 < code > kubectl port-forward< / code > 打开;< code > shop-web< / code > 只有集群内地址,< code > http://shop.example.com< / code > 没有任何东西应答< / td > < / tr >
< tr > < td > 主线之后< / td > < td > 浏览器打开 < code > http://shop.example.com< / code > 是 shop-web 的页面;< code > https://shop.example.com< / code > 有证书告警但能通;< code > /api< / code > 仍由 nginx 转发< / td > < / tr >
< tr > < td > 本章产物< / td > < td > < code > deploy/base/shop-gw.yaml< / code > 、< code > deploy/base/shop-route.yaml< / code > 、< code > deploy/base/envoy-proxy.yaml< / code > 、< code > deploy/base/ingress-legacy.yaml< / code > 、< code > Makefile< / code > 的 < code > verify-ch09< / code > < / td > < / tr >
< tr > < td > 适用环境< / td > < td > ENV-A( Win11 + WSL2 + kind) 必做; ENV-C( Linux 上的 kind) 同样适用, 差异只在改 hosts 与生成证书两处< / td > < / tr >
< / tbody >
< / table >
< / div >
< h3 id = "sec-9-2" > 9.2 学习目标< / h3 >
< div class = "box note" >
< span class = "bt" > 学完你能< / span >
< ul >
< li > 能用 < code > Gateway< / code > 加 < code > HTTPRoute< / code > 把域名接到 Service, 判定标准是 < code > curl -H 'Host: shop.example.com'< / code > 拿到 shop-web 首页。< / li >
< li > 能解释 kind 上 < code > EXTERNAL-IP< / code > 为什么是 < code > < pending> < / code > ,判定标准是说出 Envoy 的 Service 是 < code > LoadBalancer< / code > 而 kind 没实现,并把它改成 < code > NodePort< / code > 30080。< / li >
< li > 能用 < code > status.conditions< / code > 判断路由卡在哪一步,判定标准是从 < code > ResolvedRefs< / code > 直接指到写错的 < code > backendRef< / code > 。< / li >
< li > 能说清资源与控制器的分工,判定标准是 apply 一个没人处理的 < code > Ingress< / code > 后 < code > ADDRESS< / code > 与 Events 都为空,并且能解释为什么。< / li >
< / ul >
< / div >
< h3 id = "sec-9-3" > 9.3 场景:域名解析到了,页面还是 502< / h3 >
< p > 周一 09:20, 市场部把 < code > shop.example.com< / code > 的 A 记录指向公司出口 IP, 活动链接同时发了出去。十分钟后客服转来一张截图: < b > 502 Bad Gateway< / b > 。域名是通的,解析没问题。< / p >
< p > 值班的人先怀疑 < code > shop-web< / code > 挂了,< code > get pods< / code > 两个副本都 < code > Running< / code > ;又在集群里 < code > curl< / code > 了它的 ClusterIP, 200 和整页 HTML 都拿到了。最后在出口 nginx 的日志里看到原因:< / p >
< pre data-lang = "text" data-name = "出口 nginx 的 error.log 片段" > # 预期输出
2026-11-09 09:26:41 [error] 812#812: *4711 connect() failed (111: Connection refused)
while connecting to upstream, client: 203.0.113.24, request: "GET / HTTP/1.1",
upstream: "http://10.0.0.11:30080/", host: "shop.example.com"< / pre >
< div class = "box danger" >
< span class = "bt" > 现象< / span >
< p > 502 不是「网站挂了」,是「有人接住了连接,但连不上后面」。出口 nginx 把请求转给 < code > 10.0.0.11:30080< / code > ,而集群里没有进程在那个端口上。从域名到 Pod 之间缺的那一环,谁都没配过。< / p >
< / div >
< p > 本章补的就是这一环:把域名变成一个 listener, 再把 listener 变成一条通往 Service 的路由。< / p >
< h3 id = "sec-9-4" > 9.4 心智模型:一栋楼的门与指示牌< / h3 >
< figure class = "figure" >
< svg viewBox = "0 0 720 440" role = "img" aria-label = "从浏览器到 shop-api 的六跳路径" >
< defs >
< marker id = "ar1" markerWidth = "9" markerHeight = "9" refX = "8" refY = "4.5" orient = "auto" >
< path d = "M0,0 L9,4.5 L0,9 z" style = "fill:var(--muted)" > < / path >
< / marker >
< / defs >
< text x = "40" y = "40" font-size = "12" style = "fill:var(--muted)" > 1< / text >
< rect x = "60" y = "14" width = "300" height = "44" rx = "9" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "210" y = "41" text-anchor = "middle" font-size = "13" style = "fill:var(--ink)" > 浏览器 / curl< / text >
< text x = "376" y = "40" font-size = "11.5" style = "fill:var(--muted)" > hosts 与 DNS 只把名字变成 IP< / text >
< path d = "M210,58 L210,84" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar1)" > < / path >
< text x = "40" y = "114" font-size = "12" style = "fill:var(--muted)" > 2< / text >
< rect x = "60" y = "88" width = "300" height = "44" rx = "9" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "210" y = "115" text-anchor = "middle" font-size = "13" style = "fill:var(--ink)" > 宿主 :80 / :443< / text >
< text x = "376" y = "114" font-size = "11.5" style = "fill:var(--muted)" > kind 的 extraPortMappings, 建集群时写死< / text >
< path d = "M210,132 L210,158" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar1)" > < / path >
< text x = "40" y = "188" font-size = "12" style = "fill:var(--muted)" > 3< / text >
< rect x = "60" y = "162" width = "300" height = "44" rx = "9" style = "fill:var(--brand-soft);stroke:var(--brand)" > < / rect >
< text x = "210" y = "189" text-anchor = "middle" font-size = "13" style = "fill:var(--brand)" > kind 节点 :30080 / :30443< / text >
< text x = "376" y = "188" font-size = "11.5" style = "fill:var(--muted)" > NodePort, 由 EnvoyProxy 的 patch 钉住< / text >
< path d = "M210,206 L210,232" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar1)" > < / path >
< text x = "40" y = "262" font-size = "12" style = "fill:var(--muted)" > 4< / text >
< rect x = "60" y = "236" width = "300" height = "44" rx = "9" style = "fill:var(--brand-soft);stroke:var(--brand)" > < / rect >
< text x = "210" y = "263" text-anchor = "middle" font-size = "13" style = "fill:var(--brand)" > Envoy Pod: listener :80 / :443< / text >
< text x = "376" y = "262" font-size = "11.5" style = "fill:var(--muted)" > TLS 在这里终止,证书来自 secret/shop-tls< / text >
< path d = "M210,280 L210,306" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar1)" > < / path >
< text x = "40" y = "336" font-size = "12" style = "fill:var(--muted)" > 5< / text >
< rect x = "60" y = "310" width = "300" height = "44" rx = "9" style = "fill:var(--brand-soft);stroke:var(--brand)" > < / rect >
< text x = "210" y = "337" text-anchor = "middle" font-size = "13" style = "fill:var(--brand)" > HTTPRoute: host + path 匹配< / text >
< text x = "376" y = "336" font-size = "11.5" style = "fill:var(--muted)" > hostnames 选域名, pathType 选路径< / text >
< path d = "M210,354 L210,372 L150,372 L150,382" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar1)" > < / path >
< path d = "M210,372 L345,372 L345,382" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar1)" > < / path >
< path d = "M210,372 L535,372 L535,382" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar1)" > < / path >
< text x = "40" y = "410" font-size = "12" style = "fill:var(--muted)" > 6< / text >
< rect x = "60" y = "386" width = "180" height = "40" rx = "8" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "150" y = "411" text-anchor = "middle" font-size = "12" style = "fill:var(--ink)" > svc/shop-web:80< / text >
< rect x = "285" y = "386" width = "120" height = "40" rx = "8" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "345" y = "411" text-anchor = "middle" font-size = "12" style = "fill:var(--ink)" > nginx< / text >
< rect x = "450" y = "386" width = "170" height = "40" rx = "8" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "535" y = "411" text-anchor = "middle" font-size = "12" style = "fill:var(--ink)" > shop-api:8080< / text >
< / svg >
< div class = "figcap" > < b > 图 1 ·< / b > 一个外部请求要穿过六跳才到容器,任何一跳没配上,报错都不一样。< / div >
< / figure >
< p > 见图 1: 前两跳由第 1 章的 < code > kind-config.yaml< / code > 决定,中间三跳由本章的三个对象决定,最后一跳是 Service 与 < code > shop-web< / code > 里的 nginx。断在哪一跳, 症状就不同: 第 2 跳 < code > ERR_CONNECTION_REFUSED< / code > ,第 5 跳 404, 最后一跳 502。< / p >
< p > 换个说法:< code > GatewayClass< / code > 是物业公司的资质,< code > Gateway< / code > 是传达室, listener 是具体的门(开 80 还是 443) , < code > HTTPRoute< / code > 是门里那块「几楼几号找谁」的指示牌,< code > backendRefs< / code > 才是房间号。牌子挂错门,人就走错楼。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 常见误解< / th > < th > 事实< / th > < / tr > < / thead >
< tbody >
< tr > < td > 「< code > Ingress< / code > 就是一种反向代理」< / td > < td > < code > Ingress< / code > 只是一组规则;真正监听端口的是某个 controller 起的代理进程,没有 controller 它什么也不做< / td > < / tr >
< tr > < td > 「< code > Gateway< / code > 自己会监听 80」< / td > < td > < code > Gateway< / code > 是声明;监听的是它派生出来的 Envoy Pod, 端口先落在 Service 上,再落到节点上< / td > < / tr >
< tr > < td > 「域名解析到集群 IP 就能访问」< / td > < td > Pod 与 Service 的地址在集群外不可路由,外部流量必须经过节点端口或负载均衡器< / td > < / tr >
< / tbody >
< / table >
< / div >
< p class = "take" > < b > 一句话:< / b > 网关不是一台机器,是一组声明;接流量的是它派生出来的代理。< / p >
< h3 id = "sec-9-5" > 9.5 概念与字段速查< / h3 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 对象 / 字段< / th > < th > 作用< / th > < th > 默认值< / th > < th > 生产建议< / th > < / tr > < / thead >
< tbody >
< tr > < td > < code > GatewayClass< / code > < / td > < td > 声明由哪个控制器实现网关,集群级< / td > < td > 无,< code > controllerName< / code > 必填< / td > < td > 一个集群只留一个默认类< / td > < / tr >
< tr > < td > < code > Gateway< / code > < / td > < td > 一组 listener 的集合,代表一个网关实例< / td > < td > 无,< code > gatewayClassName< / code > 必填< / td > < td > 每个域名或团队一个 Gateway< / td > < / tr >
< tr > < td > < code > listeners[]< / code > < / td > < td > 开哪个端口、什么协议、接哪个域名< / td > < td > < code > hostname< / code > 空表示所有域名< / td > < td > 显式写 hostname, 避免两条路由抢同一域名< / td > < / tr >
< tr > < td > < code > listeners[].tls< / code > < / td > < td > 是否终止 TLS、引用哪张证书< / td > < td > 不写就是明文< / td > < td > < code > mode: Terminate< / code > 加 < code > certificateRefs< / code > < / td > < / tr >
< tr > < td > < code > allowedRoutes< / code > < / td > < td > 允许哪些命名空间的路由挂到这个 listener< / td > < td > < code > namespaces.from: Same< / code > < / td > < td > 平台网关注入 < code > Selector< / code > 加标签< / td > < / tr >
< tr > < td > < code > HTTPRoute< / code > < / td > < td > 一条 HTTP 路由规则,属于某个命名空间< / td > < td > 无< / td > < td > 与它指向的 Service 同命名空间< / td > < / tr >
< tr > < td > < code > parentRefs< / code > < / td > < td > 这条路由挂到哪个 Gateway、哪个 listener< / td > < td > < code > kind: Gateway< / code > 、< code > group: gateway.networking.k8s.io< / code > 、< code > sectionName< / code > 空表示所有 listener< / td > < td > 只挂 https 时才写 < code > sectionName< / code > < / td > < / tr >
< tr > < td > < code > hostnames< / code > < / td > < td > 这条路由匹配哪些域名< / td > < td > 空表示都匹配,但仍受 listener 的 hostname 限制< / td > < td > 与 listener 的 hostname 写成同一个值< / td > < / tr >
< tr > < td > < code > pathType< / code > / < code > path.value< / code > < / td > < td > 路径怎么匹配< / td > < td > 无,必填:< code > Exact< / code > 、< code > PathPrefix< / code > 、< code > RegularExpression< / code > < / td > < td > 一律 < code > PathPrefix< / code > 加 < code > /< / code > ,细分交给后端< / td > < / tr >
< tr > < td > < code > backendRefs[]< / code > < / td > < td > 转发到哪个 Service 的哪个端口< / td > < td > < code > kind: Service< / code > 、< code > group: ""< / code > 、< code > weight: 1< / code > (权重按比例)< / td > < td > 端口填 Service 端口,不是容器端口< / td > < / tr >
< / tbody >
< / table >
< / div >
< p > 十个字段里只有 < code > pathType< / code > 必填,其余默认值都倾向于「更宽」:不写 hostname 就是所有域名,不写 < code > sectionName< / code > 就是所有 listener, 不写 < code > matches< / code > 就是 < code > /< / code > 。越宽越容易被别人的路由抢走请求。< / p >
< details >
< summary > 可跳过 · Gateway API 与 Ingress 的对象对照< / summary >
< p > 两套 API 解决同一个问题,对象能一一对上:< code > IngressClass< / code > ↔ < code > GatewayClass< / code > , < code > Ingress< / code > ↔ < code > Gateway< / code > 加若干 < code > HTTPRoute< / code > 。区别是职责被拆开:监听与证书归 < code > Gateway< / code > (平台管),路径与后端归 < code > HTTPRoute< / code > ( 业务管) 。Ingress 把两件事塞进一个对象,只能靠注解,这是它被替代的原因之一。< / p >
< / details >
< h3 id = "sec-9-6" > 9.6 动手:把 shop.example.com 接进来< / h3 >
< p > 八个步骤,工作目录 < code > ~/work/aurora-shop< / code > 。第 6 步前浏览器打不开是正常的:入口建好了,还没接到宿主上。< / p >
< h4 > Step 1 · 校验上一章状态< / h4 >
< p > < span class = "tag" > 意图< / span > 确认两个 Deployment 与三个 Service 都在,且第 8 章的 Postgres 已按 8.10 清理——这不是故障,是本章的起点。< / p >
< pre data-lang = "bash" data-name = "校验起点" > # env: all
cd ~/work/aurora-shop
kubectl get deploy,svc -n shop
kubectl get pvc -n shop< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/shop-api 3/3 3 3 3d
deployment.apps/shop-web 2/2 2 2 3d
NAME TYPE CLUSTER-IP PORT(S) AGE
service/shop-api ClusterIP 10.96.45.12 8080/TCP 3d
service/shop-api-headless ClusterIP None 8080/TCP 3d
service/shop-web ClusterIP 10.96.88.30 80/TCP 3d
No resources found in shop namespace.< / pre >
< p > 这里不跑 < code > make verify-ch08< / code > :它第一条断言是 < code > postgres< / code > 1/1, 库已被 8.10 删了。
一条 verify 锁的是状态,不是进度:状态变了,它就该红。< / p >
< p > < b > 不一致怎么办< / b > :还有 < code > postgres< / code > 或 < code > pvc/data-postgres-0< / code > 就是 8.10 没清完,先删;本章不加数据库,第 10 章会把它装回来。< / p >
< h4 > Step 2 · 装 Envoy Gateway v1.10.0< / h4 >
< p > < span class = "tag" > 意图< / span > 一条命令装完 Gateway API 的 CRD、控制器和 GatewayClass < code > eg< / code > 。不用 Helm: 第 14 章才引入。< / p >
< pre data-lang = "bash" data-name = "安装并等待就绪" > # env: all
kubectl apply --server-side -f https://github.com/envoyproxy/gateway/releases/download/v1.10.0/install.yaml
kubectl wait --for=condition=Available -n envoy-gateway-system deployment/envoy-gateway --timeout=5m
kubectl get gatewayclass eg< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
customresourcedefinition.apiextensions.k8s.io/gateways.gateway.networking.k8s.io serverside-applied
namespace/envoy-gateway-system serverside-applied
deployment.apps/envoy-gateway serverside-applied
gatewayclass.gateway.networking.k8s.io/eg serverside-applied
deployment.apps/envoy-gateway condition met
NAME CONTROLLER ACCEPTED AGE
eg gateway.envoyproxy.io/gatewayclass-controller True 41s< / pre >
< p > 必须带 < code > --server-side< / code > : CRD schema 太大,客户端 apply 会把整份定义塞进 < code > last-applied-configuration< / code > 注解,超 256KB 就被 API 拒绝。< / p >
< p > < b > 不一致怎么办< / b > : wait 超时先看 < code > get pods< / code > ,多半在拉镜像;< code > gatewayclass< / code > 不存在就是 CRD 没装成,重跑第一条看报错。< / p >
< h4 > Step 3 · 生成自签证书并建 Secret< / h4 >
< p > < span class = "tag" > 意图< / span > 443 那条 listener 要一个 < code > kubernetes.io/tls< / code > 的 Secret。自签够用: 浏览器告警一次, 点继续即可, 本章不引 cert-manager。< / p >
< div class = "grid2" >
< div class = "box" >
< h4 > Windows 11 + WSL2< / h4 >
< pre data-lang = "powershell" data-name = "PowerShell 原生生成的证书" > # env: windows-ps
New-SelfSignedCertificate -DnsName shop.example.com -CertStoreLocation Cert:\CurrentUser\My< / pre >
< p > 产物在证书存储里,不是文件,还得 < code > Export-PfxCertificate< / code > 转 PEM, 多两步。< / p >
< / div >
< div class = "box" >
< h4 > Linux (Ubuntu 24.04)< / h4 >
< pre data-lang = "bash" data-name = "openssl 直接出 PEM" > # env: linux
openssl req -x509 -newkey rsa:2048 -nodes -days 365 \
-subj "/CN=shop.example.com" \
-addext "subjectAltName=DNS:shop.example.com" \
-keyout shop.key -out shop.crt< / pre >
< p > 产出 < code > shop.crt< / code > 与 < code > shop.key< / code > ,格式正是 Secret 要的。< / p >
< / div >
< / div >
< p > < b > 差异结论< / b > : K8s 的 TLS Secret 要 PEM 文本, PowerShell 那条给的是证书存储里的对象。所以统一用 openssl: Windows 上在 WSL 里跑,自带它。< / p >
< pre data-lang = "bash" data-name = "建 Secret" > # env: all
kubectl create secret tls shop-tls -n shop --cert=shop.crt --key=shop.key
kubectl get secret shop-tls -n shop< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
secret/shop-tls created
NAME TYPE DATA AGE
shop-tls kubernetes.io/tls 2 3s< / pre >
< p > < b > 不一致怎么办< / b > : < code > failed to find any PEM data< / code > 说明文件不是 PEM; < code > private key does not match< / code > 说明不是一对,重跑 openssl。SAN 必须含 < code > shop.example.com< / code > ,否则浏览器连告警都过不去。< / p >
< h4 > Step 4 · 建 Gateway 与 HTTPRoute< / h4 >
< p > < span class = "tag" > 意图< / span > 先写「门」,再写「指示牌」。两条 listener 一模一样,只是协议和端口不同。< / p >
< pre data-lang = "yaml" data-name = "deploy/base/shop-gw.yaml" > # file: deploy/base/shop-gw.yaml
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: shop-gw
namespace: shop
labels:
app.kubernetes.io/name: shop-gw
app.kubernetes.io/instance: shop
app.kubernetes.io/part-of: aurora-shop
spec:
gatewayClassName: eg
listeners:
- name: http
protocol: HTTP
port: 80
hostname: shop.example.com
allowedRoutes:
namespaces:
from: Same # 只允许 shop 里的 HTTPRoute 挂上来
- name: https
protocol: HTTPS
port: 443
hostname: shop.example.com
tls:
mode: Terminate # 在网关这一层解密,后端继续走明文 HTTP
certificateRefs:
- kind: Secret
name: shop-tls
allowedRoutes:
namespaces:
from: Same< / pre >
< pre data-lang = "yaml" data-name = "deploy/base/shop-route.yaml" > # file: deploy/base/shop-route.yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
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:
parentRefs:
- name: shop-gw # 不写 sectionName: 两条 listener 都挂
hostnames:
- shop.example.com
rules:
- matches:
- path:
type: PathPrefix
value: / # /api 的细分由 shop-web 里的 nginx 做,不在网关拆
backendRefs:
- name: shop-web
port: 80< / pre >
< pre data-lang = "bash" data-name = "应用并看状态" > # env: all
kubectl apply -f deploy/base/shop-gw.yaml
kubectl apply -f deploy/base/shop-route.yaml
kubectl get gateway shop-gw -n shop
kubectl get httproute shop-web -n shop
kubectl -n envoy-gateway-system get svc< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
gateway.gateway.networking.k8s.io/shop-gw created
httproute.gateway.networking.k8s.io/shop-web created
NAME CLASS ADDRESS PROGRAMMED AGE
shop-gw eg True 12s
NAME HOSTNAMES AGE
shop-web ["shop.example.com"] 9s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
envoy-gateway ClusterIP 10.96.192.76 < none> 18000/TCP 3m
envoy-shop-shop-gw-8f5c9d7b6 LoadBalancer 10.96.141.23 < pending> 80:31234/TCP,443:31567/TCP 40s< / pre >
< p > 最后一行是伏笔: Envoy 的 Service 是 < code > LoadBalancer< / code > , kind 里没人实现,< code > EXTERNAL-IP< / code > 停在 < code > < pending> < / code > ; < code > ADDRESS< / code > 空同理。< / p >
< p > < b > 不一致怎么办< / b > : < code > PROGRAMMED=False< / code > 时 < code > describe gateway< / code > 看 Conditions, < code > Invalid< / code > 多半是 listener 同名或端口冲突;刚 apply 完等 20 秒再看。< / p >
< div class = "box warn" >
< span class = "bt" > 易错点 · 忘了 < code > -n shop< / code > , Gateway 建到了 default< / span >
< p > < b > 现象< / b > : < code > kubectl apply -f shop-gw.yaml< / code > 报 created, 但 < code > kubectl get httproute shop-web -n shop< / code > 的 < code > Accepted< / code > 是 < code > False< / code > ,原因 < code > NoMatchingParent< / code > 。< / p >
< p > < b > 原因< / b > :文件里设了 < code > namespace: shop< / code > 就不会错,但用 < code > -n< / code > 覆盖或在命令行 < code > create< / code > 时漏掉, Gateway 就落在 < code > default< / code > ; < code > allowedRoutes< / code > 只允许同命名空间的路由挂上来。< / p >
< p > < b > 怎么验证< / b > : < code > kubectl get gateway -A< / code > 看 < code > NAMESPACE< / code > 应为 < code > shop< / code > ;不对就删掉重建,对象不能改归属。< / p >
< / div >
< h4 > Step 5 · 把 30080/30443 钉到 Envoy 上< / h4 >
< p > < span class = "tag" > 意图< / span > 让第 1 章的端口映射生效。Envoy 给每个 Gateway 生成一个 Service, 默认 < code > LoadBalancer< / code > ;改 NodePort 后端口随机(如 31718) , 与 < code > kind-config.yaml< / code > 的 30080 对不上,所以必须钉死 < code > nodePort< / code > 。< / p >
< pre data-lang = "yaml" data-name = "deploy/base/envoy-proxy.yaml" > # file: deploy/base/envoy-proxy.yaml
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: EnvoyProxy
metadata:
name: shop-proxy
namespace: shop
labels:
app.kubernetes.io/name: shop-proxy
app.kubernetes.io/instance: shop
app.kubernetes.io/part-of: aurora-shop
spec:
provider:
type: Kubernetes
kubernetes:
envoyService:
type: NodePort
patch: # 直接在生成的 Service 上做战略合并
type: StrategicMerge
value:
spec:
ports:
- port: 80 # 战略合并的键是 port, 别写 name:
nodePort: 30080 # 与第 1 章的 extraPortMappings 对齐
- port: 443
nodePort: 30443< / pre >
< p > 不写 < code > name< / code > 是故意的:战略合并按 < code > port< / code > 匹配,写了可能撞上 Envoy 起的名字,报 < code > may not change< / code > 。再在 < code > shop-gw.yaml< / code > 的 < code > spec< / code > 下补四行,
把 Gateway 绑到这个 EnvoyProxy: < / p >
< pre data-lang = "yaml" data-name = "shop-gw.yaml 的 spec 下新增" > # file: deploy/base/shop-gw.yaml( spec 下新增这一段)
infrastructure:
parametersRef:
group: gateway.envoyproxy.io
kind: EnvoyProxy
name: shop-proxy< / pre >
< pre data-lang = "bash" data-name = "应用并验证端口" > # env: all
kubectl apply -f deploy/base/envoy-proxy.yaml
kubectl apply -f deploy/base/shop-gw.yaml
kubectl -n envoy-gateway-system get svc -l gateway.envoyproxy.io/owning-gateway-name=shop-gw< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
envoyproxy.config.gateway.envoyproxy.io/shop-proxy created
gateway.gateway.networking.k8s.io/shop-gw configured
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
envoy-shop-shop-gw-4b7d6f8c9a NodePort 10.96.141.31 < none> 80:30080/TCP,443:30443/TCP 14s< / pre >
< p > 改配置时 Envoy 会重建 Service, 名字后缀会变, 别写死, 一律用 < code > -l< / code > 查。端口是 < code > 80:30080/TCP,443:30443/TCP< / code > 才算接到宿主上。< / p >
< div class = "grid2" >
< div class = "box" >
< h4 > Windows 11 + WSL2< / h4 >
< pre data-lang = "powershell" data-name = "查 Service 并确认宿主 80 在监听" > # env: windows-ps
kubectl -n envoy-gateway-system get svc -l gateway.envoyproxy.io/owning-gateway-name=shop-gw
Get-NetTCPConnection -LocalPort 80 -State Listen |
Select-Object LocalAddress, LocalPort, OwningProcess< / pre >
< p > 确认宿主 80 真有人在听: Docker Desktop 用转发进程占住它,打不开时先看这条。< / p >
< / div >
< div class = "box" >
< h4 > Linux (Ubuntu 24.04)< / h4 >
< pre data-lang = "bash" data-name = "查 nodePort 并确认宿主 80 在监听" > # env: linux
kubectl -n envoy-gateway-system get svc -l gateway.envoyproxy.io/owning-gateway-name=shop-gw \
-o jsonpath='{.items[0].spec.ports[*].nodePort}'
ss -lntp | grep ':80 '< / pre >
< p > < code > nodePort< / code > 应输出 < code > 30080 30443< / code > , < code > ss< / code > 里能看到它。< / p >
< / div >
< / div >
< p > < b > 差异结论< / b > :名字与端口两边一样,差别在谁在宿主 80 上监听: Windows 是 Docker Desktop 转发进程, Linux 是 < code > docker-proxy< / code > 。这一跳决定 502 还是 < code > ERR_CONNECTION_REFUSED< / code > 。< / p >
< div class = "box warn" >
< span class = "bt" > 易错点 · 只改 type 不固定 nodePort, 映射等于没写< / span >
< p > < b > 现象< / b > : Service 变成 < code > NodePort< / code > 了,但 < code > PORT(S)< / code > 是 < code > 80:31718/TCP< / code > ,浏览器打不开(< code > ERR_CONNECTION_REFUSED< / code > )。< / p >
< p > < b > 原因< / b > :不写 < code > nodePort< / code > 时 apiserver 从 30000– 32767 随机分配,而 kind 的 < code > extraPortMappings< / code > 在创建时已把宿主 80 钉在节点 30080 上,两个数字对不上。< / p >
< p > < b > 怎么验证< / b > : < code > get svc -l gateway.envoyproxy.io/owning-gateway-name=shop-gw< / code > 看 < code > PORT(S)< / code > ;不是 30080/30443 就是 < code > patch< / code > 没生效,回去查缩进。< / p >
< / div >
< p class = "take" > < b > 一句话:< / b > 30080 不是 K8s 的约定,是第 1 章写进 kind 配置里的那三行。< / p >
< h4 > Step 6 · 从浏览器打开 http 与 https< / h4 >
< p > < span class = "tag" > 意图< / span > 让域名解析到 127.0.0.1,再用 curl 验明文与 TLS。Windows 浏览器只认 Windows 的 hosts, WSL 里的它读不到——本章最容易卡在这。< / p >
< div class = "grid2" >
< div class = "box" >
< h4 > Windows 11 + WSL2< / h4 >
< pre data-lang = "powershell" data-name = "以管理员身份改 hosts" > # env: windows-ps
Add-Content -Path "$env:SystemRoot\System32\drivers\etc\hosts" -Value "127.0.0.1 shop.example.com"
Resolve-DnsName shop.example.com | Select-Object Name, IPAddress< / pre >
< p > 要管理员权限。改完直接开浏览器,自签证书提示不安全时选「继续访问」。< / p >
< / div >
< div class = "box" >
< h4 > Linux (Ubuntu 24.04)< / h4 >
< pre data-lang = "bash" data-name = "用 --resolve 绕过 hosts" > # env: linux
curl -s --resolve shop.example.com:80:127.0.0.1 http://shop.example.com/ | head -n 4
curl -sk --resolve shop.example.com:443:127.0.0.1 https://shop.example.com/ | head -n 4< / pre >
< p > 不改 < code > /etc/hosts< / code > : < code > --resolve< / code > 只对这一条命令生效,测试完不留痕迹。< / p >
< / div >
< / div >
< p > < b > 差异结论< / b > :作用域不同:改 hosts 全局生效,< code > --resolve< / code > 只骗过这一次 curl。所以「curl 通、浏览器不通」几乎一定是 hosts 改错了位置(改到 WSL 的 < code > /etc/hosts< / code > ,或存成 < code > hosts.txt< / code > )。< / p >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
< !DOCTYPE html>
< html lang="zh-CN">
< head>
< title> Aurora Shop< /title> < / pre >
< p > < b > 不一致怎么办< / b > : < code > (7) Failed to connect< / code > 说明宿主 80 没人监听,回 Step 5 看 nodePort; 连接成功但 404 说明到了 Envoy 没匹配上,见 9.8 故障 3; 502 则是 < code > shop-web< / code > 的 Endpoints 为空。< / p >
< h4 > Step 7 · 负向: apply 一个没人处理的 Ingress< / h4 >
< p > < span class = "tag" > 意图< / span > 看「资源存在」与「行为发生」的区别。本章唯一一次用 Ingress, 只为证明没有 controller 它什么也不做。< / p >
< pre data-lang = "yaml" data-name = "deploy/base/ingress-legacy.yaml" > # file: deploy/base/ingress-legacy.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: shop-web-legacy
namespace: shop
spec:
ingressClassName: nginx # 指向一个本集群里不存在的 controller
rules:
- host: shop.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: shop-web
port:
number: 80< / pre >
< pre data-lang = "bash" data-name = "应用并观察" > # env: all
kubectl apply -f deploy/base/ingress-legacy.yaml
kubectl get ingress shop-web-legacy -n shop
kubectl get ingressclass
kubectl describe ingress shop-web-legacy -n shop | tail -n 5< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
ingress.networking.k8s.io/shop-web-legacy created
NAME CLASS HOSTS ADDRESS PORTS AGE
shop-web-legacy nginx shop.example.com 80 41s
No resources found
Events: < none> < / pre >
< p > 三处证据:< code > ADDRESS< / code > 空、< code > ingressclass< / code > 一个没有、Events 一条都没有。API 收下了规则,没有进程去读它。< b > Ingress 只是资源,行为在 controller 里。< / b > < / p >
< div class = "box warn" >
< span class = "bt" > 易错点 · 以为 ADDRESS 为空是「还没生效,等一会儿就好」< / span >
< p > < b > 现象< / b > : < code > kubectl get ingress< / code > 的 < code > ADDRESS< / code > 一直是空的,< code > kubectl describe< / code > 的 Events 也是空的,
等一小时还是这样。< / p >
< p > < b > 原因< / b > : < code > ADDRESS< / code > 由 controller 回写,没有 controller 就永远没人写;< code > ingressClassName< / code > 只是给 controller 看的选择器,写成什么都不会触发行为。< / p >
< p > < b > 怎么验证< / b > : < code > kubectl get ingressclass< / code > 应为空;< code > kubectl get pods -A< / code > 里也找不到任何 ingress controller。两个都空就别等了。< / p >
< / div >
< p > < b > 不一致怎么办< / b > : < code > ADDRESS< / code > 有值说明装过某个 controller, 用 < code > get pods -A | grep -i ingress< / code > 找出来; 本章不需要它, ingress-nginx 已退役。< / p >
< details >
< summary > 可跳过 · ingress-nginx 的退役时间线与替代路径< / summary >
< p > 2025-11-11 官方公告: ingress-nginx 2026 年 3 月退役,之后不再发安全补丁——维护者覆盖不住攻击面,职责又被 Gateway API 拆走了。替代路径: Envoy Gateway( 本章) 、Cilium 与 Traefik 的 Gateway 实现、云厂商托管网关,都实现同一套 API。< / p >
< / details >
< h4 > Step 8 · 提交本章产物< / h4 >
< p > < span class = "tag" > 意图< / span > 四个清单与 Makefile 的验收目标一起入库;下一章开头要用它们确认入口还通着。< / p >
< pre data-lang = "bash" data-name = "提交" > # env: all
git add deploy/base/shop-gw.yaml deploy/base/shop-route.yaml \
deploy/base/envoy-proxy.yaml deploy/base/ingress-legacy.yaml Makefile
git commit -m "ch09: Envoy Gateway 接入 shop.example.com"< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
[main 9f3c1d2] ch09: Envoy Gateway 接入 shop.example.com
5 files changed, 164 insertions(+)< / pre >
< p > < b > 不一致怎么办< / b > : < code > nothing to commit< / code > 说明某一步是对着集群直接 apply 的,没落到文件里。< / p >
< h3 id = "sec-9-7" > 9.7 验收清单< / h3 >
< ol class = "steps" >
< li > < code > kubectl get gatewayclass eg< / code > → < code > ACCEPTED< / code > 为 < code > True< / code > < / li >
< li > < code > kubectl get gateway shop-gw -n shop< / code > → < code > PROGRAMMED< / code > 为 < code > True< / code > ,两条 listener 都在< / li >
< li > < code > kubectl get httproute shop-web -n shop< / code > → < code > Accepted< / code > 为 < code > True< / code > , < code > backendRefs< / code > 只有 1 个< / li >
< li > < code > kubectl -n envoy-gateway-system get svc -l gateway.envoyproxy.io/owning-gateway-name=shop-gw< / code > → < code > 80:30080/TCP,443:30443/TCP< / code > < / li >
< li > 反向验证:< code > curl -s --resolve shop.example.com:80:127.0.0.1 http://shop.example.com/< / code > 返回 shop-web 的 HTML, 而不是 404 或 502< / li >
< li > < code > kubectl get ingress shop-web-legacy -n shop< / code > 的 < code > ADDRESS< / code > 仍为空——负向证据要留着< / li >
< li > < code > make verify-ch09< / code > 全绿< / li >
< / ol >
< pre data-lang = "bash" data-name = "一次跑完" > # env: all
make verify-ch09< / pre >
< pre data-lang = "text" data-name = "预期输出" > # 预期输出
[ok] gatewayclass eg accepted
[ok] gateway shop-gw programmed, 2 listeners
[ok] httproute shop-web accepted, 1 backendRef
[ok] envoy service nodePort 30080/30443
[ok] https listener terminates with secret/shop-tls
[ok] host header reaches shop-web< / pre >
< h3 id = "sec-9-8" > 9.8 故障实验室< / h3 >
< p > 三个故障都来自本章的几个对象,按「从外往里」的顺序排。第三个的判断路径见图 2。< / p >
< div class = "box danger" >
< span class = "bt" > 故障 1 · gatewayclass/eg 不是 Accepted< / span >
< p > < b > 现象< / b > : < code > gatewayclass eg< / code > 的 < code > ACCEPTED< / code > 为 < code > False< / code > , < code > gateway shop-gw< / code > 的 < code > PROGRAMMED< / code > 也一直是 < code > False< / code > 。< / p >
< p > < b > 注入< / b > : < code > kubectl -n envoy-gateway-system scale deploy/envoy-gateway --replicas=0< / code > ,模拟控制器没起来;恢复用 < code > --replicas=1< / code > 。< / p >
< p > < b > 诊断< / b > :① 看条件原文,别猜;② 看控制器在不在;③ 看 CRD 有没有装全。< / p >
< pre data-lang = "bash" data-name = "三步定位" > # env: all
kubectl get gatewayclass eg -o yaml | grep -A6 'status:'
kubectl -n envoy-gateway-system get deploy,pods
kubectl get crd | grep gateway.networking.k8s.io< / pre >
< p > < b > 根因< / b > : < code > Accepted< / code > 由 < code > controllerName< / code > 指向的控制器回写;控制器不在,条件就停住,< code > message< / code > 通常是 < code > Waiting for controller< / code > 。< / p >
< p > < b > 修复< / b > :把 < code > deploy/envoy-gateway< / code > 拉回 1 个副本,等 30 秒再查; CRD 缺失就重跑 9.6 Step 2。< / p >
< p > < b > 预防< / b > : < code > verify-ch09< / code > 第一条就断言 < code > gatewayclass eg< / code > ;控制器副本用 PDB 保住,别让人 scale 到 0。< / p >
< / div >
< div class = "box danger" >
< span class = "bt" > 故障 2 · 路由 Accepted, 但 ResolvedRefs=False< / span >
< p > < b > 现象< / b > : < code > get httproute< / code > 看着正常,浏览器是 503; < code > describe< / code > 的 < code > status.parents< / code > 里有 < code > ResolvedRefs=False< / code > ,原因 < code > BackendNotFound< / code > 。< / p >
< p > < b > 注入< / b > :把 < code > backendRefs< / code > 的 < code > port< / code > 从 < code > 80< / code > 改成 < code > 8080< / code > ,再 < code > apply< / code > 。< / p >
< p > < b > 诊断< / b > :① 看 < code > status.parents< / code > 的两个条件;② 对比 Service 的真实端口;③ 确认 Endpoints 非空。< / p >
< pre data-lang = "bash" data-name = "定位写错的 backendRef" > # env: all
kubectl describe httproute shop-web -n shop | grep -A3 'Conditions:'
kubectl get svc shop-web -n shop -o jsonpath='{.spec.ports[*].port}'
kubectl get endpointslice -n shop -l kubernetes.io/service-name=shop-web< / pre >
< p > < b > 根因< / b > : < code > backendRef.port< / code > 指 Service 端口,不是容器端口(< code > shop-web< / code > 是 80 → 8080) 。注意 < code > Accepted< / code > 仍是 < code > True< / code > :它只说路由挂上了,不代表后端可用。< / p >
< p > < b > 修复< / b > :改回 < code > port: 80< / code > 并 < code > apply< / code > , 10 秒内条件转成 < code > True< / code > 。< / p >
< p > < b > 预防< / b > : < code > Accepted< / code > 与 < code > ResolvedRefs< / code > 分开断言,两个都绿才算通;端口用同一处变量(第 20 章)。< / p >
< / div >
< div class = "box danger" >
< span class = "bt" > 故障 3 · 浏览器 ERR_CONNECTION_REFUSED 或 404< / span >
< p > < b > 现象< / b > : < code > curl -H 'Host: shop.example.com' http://127.0.0.1/< / code > 能拿到页面,浏览器却是 < code > ERR_CONNECTION_REFUSED< / code > ;或都能连,浏览器 404。< / p >
< p > < b > 注入< / b > :把 < code > http< / code > listener 的 < code > hostname< / code > 改成 < code > shop.example.com< / code > 之外的值(比如加一个前缀),再 < code > apply< / code > ;此时浏览器用 < code > shop.example.com< / code > 访问就是 404。< / p >
< p > < b > 诊断< / b > :按下图的顺序,先分清「连不上」与「连上了但没匹配」,两者的排查方向完全相反。< / p >
< pre data-lang = "bash" data-name = "先分清哪一种" > # env: all
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: shop.example.com' http://127.0.0.1/
kubectl get gateway shop-gw -n shop -o jsonpath='{.spec.listeners[*].hostname}'
kubectl get httproute shop-web -n shop -o jsonpath='{.spec.hostnames}'< / pre >
< p > < b > 根因< / b > :连接被拒是宿主 80 → 节点 30080 这一跳没通( 映射没写、nodePort 随机、Docker 没转发) ; 404 是请求到了 Envoy, 但 listener 的 < code > hostname< / code > 与 < code > Host< / code > 头不匹配。< / p >
< p > < b > 修复< / b > :连接类回 9.6 Step 5 对齐 nodePort; 匹配类把 listener 与 < code > hostnames< / code > 改成同一个值,再 < code > apply< / code > 。< / p >
< p > < b > 预防< / b > :把 < code > curl -H 'Host: …'< / code > 写进 < code > verify-ch09< / code > ——它比浏览器更早暴露 host 不匹配,还能贴进工单。< / p >
< / div >
< figure class = "figure" >
< svg viewBox = "0 0 720 272" role = "img" aria-label = "浏览器打不开时的三段判断路径" >
< defs >
< marker id = "ar3" 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 = "100" height = "44" rx = "8" style = "fill:var(--card);stroke:var(--line)" > < / rect >
< text x = "60" y = "137" text-anchor = "middle" font-size = "13" style = "fill:var(--ink)" > 打不开< / text >
< path d = "M110,132 L118,132" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar3)" > < / path >
< polygon points = "185,84 250,132 185,180 120,132" style = "fill:var(--soft);stroke:var(--line)" > < / polygon >
< text x = "185" y = "137" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > 解析对吗< / text >
< path d = "M185,180 L185,212" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar3)" > < / path >
< text x = "192" y = "200" font-size = "11.5" style = "fill:var(--muted)" > 否< / text >
< rect x = "60" y = "216" width = "200" height = "44" rx = "8" style = "fill:var(--danger-bg);stroke:var(--danger)" > < / rect >
< text x = "160" y = "243" text-anchor = "middle" font-size = "12.5" style = "fill:var(--danger)" > 查 hosts / DNS< / text >
< path d = "M250,132 L262,132" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar3)" > < / path >
< text x = "252" y = "122" font-size = "11.5" style = "fill:var(--muted)" > 是< / text >
< polygon points = "330,84 395,132 330,180 265,132" style = "fill:var(--soft);stroke:var(--line)" > < / polygon >
< text x = "330" y = "137" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > TCP 通吗< / text >
< path d = "M330,180 L330,212" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar3)" > < / path >
< text x = "337" y = "200" font-size = "11.5" style = "fill:var(--muted)" > 否< / text >
< rect x = "250" y = "216" width = "200" height = "44" rx = "8" style = "fill:var(--danger-bg);stroke:var(--danger)" > < / rect >
< text x = "350" y = "243" text-anchor = "middle" font-size = "12.5" style = "fill:var(--danger)" > 30080 没对上:改 patch< / text >
< path d = "M395,132 L420,132" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar3)" > < / path >
< text x = "400" y = "122" font-size = "11.5" style = "fill:var(--muted)" > 是< / text >
< rect x = "424" y = "108" width = "150" height = "48" rx = "8" style = "fill:var(--brand-soft);stroke:var(--brand)" > < / rect >
< text x = "499" y = "137" text-anchor = "middle" font-size = "12.5" style = "fill:var(--brand)" > 看状态码< / text >
< path d = "M574,132 L586,132 L586,84 L592,84" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar3)" > < / path >
< path d = "M574,132 L586,132 L586,182 L592,182" style = "stroke:var(--muted);fill:none" marker-end = "url(#ar3)" > < / path >
< rect x = "596" y = "60" width = "116" height = "48" rx = "8" style = "fill:var(--warn-bg);stroke:var(--warn)" > < / rect >
< text x = "654" y = "80" text-anchor = "middle" font-size = "12" style = "fill:var(--warn)" > 404< / text >
< text x = "654" y = "97" text-anchor = "middle" font-size = "11.5" style = "fill:var(--warn)" > host 对不上< / text >
< rect x = "596" y = "158" width = "116" height = "48" rx = "8" style = "fill:var(--warn-bg);stroke:var(--warn)" > < / rect >
< text x = "654" y = "178" text-anchor = "middle" font-size = "12" style = "fill:var(--warn)" > 502< / text >
< text x = "654" y = "195" text-anchor = "middle" font-size = "11.5" style = "fill:var(--warn)" > 后端不可达< / text >
< / svg >
< div class = "figcap" > < b > 图 2 ·< / b > 打不开时先分层,别在同一个方向上猜三次。< / div >
< / figure >
< p > 见图 2: 这张图的价值在于「先分层, 再查细节」。绝大多数人卡住, 是因为把 404 当成连接问题去查端口,或者把连接被拒当成路由问题去改 hostname——两个方向恰好相反, 试三次就浪费半小时。< / p >
< h3 id = "sec-9-9" > 9.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 > Envoy 的 Service 是 < code > NodePort< / code > ,端口钉在 30080< / td > < td > 云负载均衡器或 Anycast 入口< / td > < td > kind 里没有 < code > LoadBalancer< / code > 的实现,< code > EXTERNAL-IP< / code > 只会停在 < code > < pending> < / code > ,不钉 < code > nodePort< / code > 就只能用随机端口< / td > < td > 把 < code > envoyService.type< / code > 换回 < code > LoadBalancer< / code > ,加云厂商的注解< / td > < / tr >
< tr > < td > 证书< / td > < td > openssl 自签, 365 天,手动建 Secret< / td > < td > cert-manager 加 ACME 自动签发与轮换< / td > < td > 自签证书浏览器告警,到期也不会自动续,只适合本地验证< / td > < td > 装 cert-manager, < code > certificateRefs< / code > 指向它签发的 Secret< / td > < / tr >
< tr > < td > 网关归属< / td > < td > < code > Gateway< / code > 与路由都在 < code > shop< / code > < / td > < td > 平台团队维护 < code > Gateway< / code > ,业务只提交 < code > HTTPRoute< / code > < / td > < td > < code > allowedRoutes.from: Same< / code > 把路由锁在网关所在命名空间,避免任何命名空间都能抢域名< / td > < td > 网关挪到 < code > platform< / code > , < code > from< / code > 改成 < code > Selector< / code > 加标签< / td > < / tr >
< tr > < td > 路由粒度< / td > < td > 一条 < code > /< / code > 全量转发< / td > < td > 按路径、Header、权重拆多条< / td > < td > 单条规则无法灰度,也没法把 < code > /api< / code > 与静态页分给不同后端< / td > < td > 加 < code > matches< / code > 与 < code > weight< / code > ,或接 Argo Rollouts< / td > < / tr >
< tr > < td > 观测< / td > < td > 不看 Envoy 的日志< / td > < td > access log、指标、超时与重试策略< / td > < td > 网关是唯一知道「谁在 502」的位置, 而 Envoy 默认不打印访问日志< / td > < td > 在 < code > EnvoyProxy< / code > 的 telemetry 里开 access log, 接进第 12 章< / td > < / tr >
< / tbody >
< / table >
< / div >
< p class = "take" > < b > 一句话:< / b > 生产与学习的差别不在网关软件,在谁来发 IP、谁来续证书。< / p >
< h3 id = "sec-9-10" > 9.10 清理与残留检查< / h3 >
< p > 负向步骤那个 < code > Ingress< / code > 必须删掉,否则后面每章的 < code > get ingress< / code > 都会看到一个 < code > ADDRESS< / code > 为空的对象, 容易被误判成「入口坏了」。Gateway 本章不删:第 10 章要用它验证订单写入后页面可见。< / p >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 对象< / th > < th > 默认处理< / th > < th > 影响< / th > < / tr > < / thead >
< tbody >
< tr > < td > < code > ingress/shop-web-legacy< / code > < / td > < td > 删< / td > < td > 无影响,它只是一条负向证据< / td > < / tr >
< tr > < td > < code > secret/shop-tls< / code > < / td > < td > 保留< / td > < td > https listener 依赖它;删了 verify-ch09 立刻红< / td > < / tr >
< tr > < td > < code > gateway/shop-gw< / code > < / td > < td > 保留< / td > < td > 删掉会连带回收 Envoy 的 Deployment 与 Service, 30080 释放< / td > < / tr >
< tr > < td > < code > envoyproxy/shop-proxy< / code > < / td > < td > 保留< / td > < td > 删掉后 Gateway 的 < code > infrastructure< / code > 指向空对象, Service 退回随机端口< / td > < / tr >
< tr > < td > < code > gatewayclass/eg< / code > < / td > < td > 保留< / td > < td > 删它会带走 Envoy Gateway 的全部 CRD, 第 10 章还得重装< / td > < / tr >
< / tbody >
< / table >
< / div >
< pre data-lang = "bash" data-name = "删掉负向步骤的对象" > # env: all
kubectl delete -f deploy/base/ingress-legacy.yaml
# 影响:只删这个 Ingress, 不影响已经生效的 Gateway 与路由
# 证书留着: verify-ch09 的 https 断言要用它< / pre >
< p > 如果打算停在这里、彻底回滚到第 8 章的状态:< / p >
< pre data-lang = "bash" data-name = "彻底回滚(可选)" > # env: all
kubectl delete -f deploy/base/shop-route.yaml
kubectl delete -f deploy/base/shop-gw.yaml
kubectl delete -f deploy/base/envoy-proxy.yaml
# 影响:删掉 Gateway 后 Envoy 的 Deployment 与 Service 会被回收, 30080 端口释放
kubectl delete -f https://github.com/envoyproxy/gateway/releases/download/v1.10.0/install.yaml
# 影响:连同 Gateway API 的 CRD 一起删除,集群里所有 Gateway/HTTPRoute 对象消失< / pre >
< pre data-lang = "bash" data-name = "残留检查" > # env: all
kubectl get gateway,httproute,ingress,svc,secret -n shop
kubectl get gatewayclass< / pre >
< pre data-lang = "text" data-name = "预期输出(保留入口时)" > # 预期输出
NAME CLASS ADDRESS PROGRAMMED AGE
gateway.gateway.networking.k8s.io/shop-gw eg True 46m
NAME HOSTNAMES AGE
httproute.gateway.networking.k8s.io/shop-web ["shop.example.com"] 46m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
service/shop-web ClusterIP 10.96.88.14 < none> 80/TCP
NAME CONTROLLER ACCEPTED AGE
gatewayclass/eg gateway.envoyproxy.io/gatewayclass-controller True 52m< / pre >
< p > 回滚到上一章:< code > git checkout HEAD~1 -- deploy/base/ Makefile< / code > ,再跑 < code > make verify-ch08< / code > 。注意 8.10 已删掉 Postgres, 所以它会在 < code > pvc< / code > 那一条上失败——这是预期的,第 10 章会把库装回来。< / p >
< h3 id = "sec-9-11" > 9.11 小结与自测< / h3 >
< p class = "take" > < b > 一句话:< / b > Ingress 只是规则, Gateway 才是那个真的在监听的东西。< / p >
< p class = "take" > < b > 一句话:< / b > kind 没有负载均衡器,< code > < pending> < / code > 不是故障,是没人给你发 IP。< / p >
< p class = "take" > < b > 一句话:< / b > < code > Accepted< / code > 只说明挂上了,< code > ResolvedRefs< / code > 才说明后端找得到。< / p >
< p > 三句对应三层:资源层(对象是什么)、网络层(流量从哪进来)、状态层(怎么判断它真的通了)。< / p >
< details >
< summary > 自测 3 题(点击展开答案)< / summary >
< p > < b > 1( 概念) < / b > < code > Gateway< / code > 与 < code > GatewayClass< / code > 的关系,和 < code > Pod< / code > 与 < code > Deployment< / code > 的关系一样吗?< / p >
< p > 方向相反。< code > Deployment< / code > 创建 < code > Pod< / code > ; < code > GatewayClass< / code > 不创建 < code > Gateway< / code > ,它只声明这类网关由哪个控制器实现,< code > Gateway< / code > 用 < code > gatewayClassName< / code > 引用它。< / p >
< p > < b > 2( 排错) < / b > < code > HTTPRoute< / code > 的 < code > Accepted=True< / code > ,浏览器却 503, 下一步? < / p >
< p > 看 < code > ResolvedRefs< / code > : < code > BackendNotFound< / code > 就是名字或端口写错了;两个条件都 True 还 503, 去查 EndpointSlice 是不是空的(第 7 章)。< / p >
< p > < b > 3( 决策) < / b > 团队要上新项目,还装一个 ingress-nginx 行不行?< / p >
< p > 不行。它已在 2026 年 3 月退役,之后没有安全补丁。本地用 Envoy Gateway( 一条 < code > install.yaml< / code > ,无 Helm) , 已用 Cilium 做 CNI 的直接开 Cilium Gateway, 云上优先托管网关。< / p >
< / details >
< div class = "box tip" >
< span class = "bt" > 延伸挑战( ≤30 分钟)< / span >
< p > 再加一条 < code > HTTPRoute< / code > ,把 < code > shop.example.com/api< / code > 指到 < code > shop-api:8080< / code > ( < code > pathType: PathPrefix< / code > + < code > value: /api< / code > ),再想清楚:< code > shop-web< / code > 里的 nginx 要不要改?两条路由谁优先?< / p >
< / div >
< h3 id = "sec-9-12" > 9.12 衔接< / h3 >
< p > < b > 回收< / b > :第 8 章结尾问「数据只在集群内可达,产品经理想从公网用域名打开怎么办」,本章用 < code > Gateway< / code > 加 < code > HTTPRoute< / code > 回答了;第 5 章的边界也没破:< code > /api< / code > 的转发仍由 nginx 做,网关只认域名和路径前缀。< / p >
< p > < b > 钩子< / b > :入口通了,用户点「下单」会真的写进数据库。可 < code > orders< / code > 表还不存在,而建表不该由某个 Pod 启动时顺手做——它要能重跑、能失败、能定时。< a href = "10-job.html" > 第 10 章< / a > 从这两件事开始。< / p >
< h3 id = "sec-9-13" > 9.13 参考与验证版本< / h3 >
< ul >
< li > < a href = "https://gateway-api.sigs.k8s.io/" > Gateway API 官方文档< / a > < / li >
< li > < a href = "https://gateway-api.sigs.k8s.io/api-types/httproute/" > HTTPRoute< / a > < / li >
< li > < a href = "https://gateway.envoyproxy.io/docs/tasks/quickstart/" > Envoy Gateway Quickstart< / a > < / li >
< li > < a href = "https://gateway.envoyproxy.io/docs/tasks/operations/customize-envoyproxy/" > Customize EnvoyProxy( envoyService 与 patch) < / a > < / li >
< li > < a href = "https://kubernetes.io/docs/concepts/services-networking/ingress/" > Ingress( 含退役说明) < / a > < / li >
< / ul >
< p > 验证版本: k8s v1.36.1 / kubectl v1.36.1 / kind v0.32.0 / Envoy Gateway v1.10.0 · 最后验证: 2026-11-11< / p >
< / article >
< nav class = "pager" >
< a class = "prev" href = "08-storage.html" > < span class = "k" > ← 上一章< / span > < span class = "v" > 第 8 章 · 存储:从 emptyDir 到 StatefulSet< / span > < / a >
< a class = "next" href = "10-job.html" > < span class = "k" > 下一章 →< / span > < span class = "v" > 第 10 章 · Job 与 CronJob: 迁移与定时任务< / span > < / a >
< / nav >
< p class = "foot" >
Kubernetes 专题 · S2 第 9 章 入口与网关: Ingress 与 Gateway API< 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 >