Kubernetes 专题 · S2 撑得住:状态、入口与多服务 · 第 9 / 26 章
9入口与网关:Ingress 与 Gateway API
port-forward 看。本章装 Envoy Gateway v1.10.0,
用 Gateway 与 HTTPRoute 把 shop.example.com 接到 shop-web,
再把 Envoy 的 Service 钉到 kind 已经映射好的 30080/30443,最后用自签证书让 HTTPS 走通。
约 150 分钟,其中 20 分钟在等 Envoy 的镜像。
9.1 本章在哪:主线之前与之后
| 项 | 内容 |
|---|---|
| 主线之前 | 页面只能靠 kubectl port-forward 打开;shop-web 只有集群内地址,http://shop.example.com 没有任何东西应答 |
| 主线之后 | 浏览器打开 http://shop.example.com 是 shop-web 的页面;https://shop.example.com 有证书告警但能通;/api 仍由 nginx 转发 |
| 本章产物 | deploy/base/shop-gw.yaml、deploy/base/shop-route.yaml、deploy/base/envoy-proxy.yaml、deploy/base/ingress-legacy.yaml、Makefile 的 verify-ch09 |
| 适用环境 | ENV-A(Win11 + WSL2 + kind)必做;ENV-C(Linux 上的 kind)同样适用,差异只在改 hosts 与生成证书两处 |
9.2 学习目标
- 能用
Gateway加HTTPRoute把域名接到 Service,判定标准是curl -H 'Host: shop.example.com'拿到 shop-web 首页。 - 能解释 kind 上
EXTERNAL-IP为什么是<pending>,判定标准是说出 Envoy 的 Service 是LoadBalancer而 kind 没实现,并把它改成NodePort30080。 - 能用
status.conditions判断路由卡在哪一步,判定标准是从ResolvedRefs直接指到写错的backendRef。 - 能说清资源与控制器的分工,判定标准是 apply 一个没人处理的
Ingress后ADDRESS与 Events 都为空,并且能解释为什么。
9.3 场景:域名解析到了,页面还是 502
周一 09:20,市场部把 shop.example.com 的 A 记录指向公司出口 IP,活动链接同时发了出去。十分钟后客服转来一张截图:502 Bad Gateway。域名是通的,解析没问题。
值班的人先怀疑 shop-web 挂了,get pods 两个副本都 Running;又在集群里 curl 了它的 ClusterIP,200 和整页 HTML 都拿到了。最后在出口 nginx 的日志里看到原因:
# 预期输出 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"
502 不是「网站挂了」,是「有人接住了连接,但连不上后面」。出口 nginx 把请求转给 10.0.0.11:30080,而集群里没有进程在那个端口上。从域名到 Pod 之间缺的那一环,谁都没配过。
本章补的就是这一环:把域名变成一个 listener,再把 listener 变成一条通往 Service 的路由。
9.4 心智模型:一栋楼的门与指示牌
见图 1:前两跳由第 1 章的 kind-config.yaml 决定,中间三跳由本章的三个对象决定,最后一跳是 Service 与 shop-web 里的 nginx。断在哪一跳,症状就不同:第 2 跳 ERR_CONNECTION_REFUSED,第 5 跳 404,最后一跳 502。
换个说法:GatewayClass 是物业公司的资质,Gateway 是传达室,listener 是具体的门(开 80 还是 443),HTTPRoute 是门里那块「几楼几号找谁」的指示牌,backendRefs 才是房间号。牌子挂错门,人就走错楼。
| 常见误解 | 事实 |
|---|---|
「Ingress 就是一种反向代理」 | Ingress 只是一组规则;真正监听端口的是某个 controller 起的代理进程,没有 controller 它什么也不做 |
「Gateway 自己会监听 80」 | Gateway 是声明;监听的是它派生出来的 Envoy Pod,端口先落在 Service 上,再落到节点上 |
| 「域名解析到集群 IP 就能访问」 | Pod 与 Service 的地址在集群外不可路由,外部流量必须经过节点端口或负载均衡器 |
一句话:网关不是一台机器,是一组声明;接流量的是它派生出来的代理。
9.5 概念与字段速查
| 对象 / 字段 | 作用 | 默认值 | 生产建议 |
|---|---|---|---|
GatewayClass | 声明由哪个控制器实现网关,集群级 | 无,controllerName 必填 | 一个集群只留一个默认类 |
Gateway | 一组 listener 的集合,代表一个网关实例 | 无,gatewayClassName 必填 | 每个域名或团队一个 Gateway |
listeners[] | 开哪个端口、什么协议、接哪个域名 | hostname 空表示所有域名 | 显式写 hostname,避免两条路由抢同一域名 |
listeners[].tls | 是否终止 TLS、引用哪张证书 | 不写就是明文 | mode: Terminate 加 certificateRefs |
allowedRoutes | 允许哪些命名空间的路由挂到这个 listener | namespaces.from: Same | 平台网关注入 Selector 加标签 |
HTTPRoute | 一条 HTTP 路由规则,属于某个命名空间 | 无 | 与它指向的 Service 同命名空间 |
parentRefs | 这条路由挂到哪个 Gateway、哪个 listener | kind: Gateway、group: gateway.networking.k8s.io、sectionName 空表示所有 listener | 只挂 https 时才写 sectionName |
hostnames | 这条路由匹配哪些域名 | 空表示都匹配,但仍受 listener 的 hostname 限制 | 与 listener 的 hostname 写成同一个值 |
pathType / path.value | 路径怎么匹配 | 无,必填:Exact、PathPrefix、RegularExpression | 一律 PathPrefix 加 /,细分交给后端 |
backendRefs[] | 转发到哪个 Service 的哪个端口 | kind: Service、group: ""、weight: 1(权重按比例) | 端口填 Service 端口,不是容器端口 |
十个字段里只有 pathType 必填,其余默认值都倾向于「更宽」:不写 hostname 就是所有域名,不写 sectionName 就是所有 listener,不写 matches 就是 /。越宽越容易被别人的路由抢走请求。
可跳过 · Gateway API 与 Ingress 的对象对照
两套 API 解决同一个问题,对象能一一对上:IngressClass ↔ GatewayClass,Ingress ↔ Gateway 加若干 HTTPRoute。区别是职责被拆开:监听与证书归 Gateway(平台管),路径与后端归 HTTPRoute(业务管)。Ingress 把两件事塞进一个对象,只能靠注解,这是它被替代的原因之一。
9.6 动手:把 shop.example.com 接进来
八个步骤,工作目录 ~/work/aurora-shop。第 6 步前浏览器打不开是正常的:入口建好了,还没接到宿主上。
Step 1 · 校验上一章状态
意图确认两个 Deployment 与三个 Service 都在,且第 8 章的 Postgres 已按 8.10 清理——这不是故障,是本章的起点。
# env: all cd ~/work/aurora-shop kubectl get deploy,svc -n shop kubectl get pvc -n shop
# 预期输出 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.
这里不跑 make verify-ch08:它第一条断言是 postgres 1/1,库已被 8.10 删了。
一条 verify 锁的是状态,不是进度:状态变了,它就该红。
不一致怎么办:还有 postgres 或 pvc/data-postgres-0 就是 8.10 没清完,先删;本章不加数据库,第 10 章会把它装回来。
Step 2 · 装 Envoy Gateway v1.10.0
意图一条命令装完 Gateway API 的 CRD、控制器和 GatewayClass eg。不用 Helm:第 14 章才引入。
# 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
# 预期输出 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
必须带 --server-side:CRD schema 太大,客户端 apply 会把整份定义塞进 last-applied-configuration 注解,超 256KB 就被 API 拒绝。
不一致怎么办:wait 超时先看 get pods,多半在拉镜像;gatewayclass 不存在就是 CRD 没装成,重跑第一条看报错。
Step 3 · 生成自签证书并建 Secret
意图443 那条 listener 要一个 kubernetes.io/tls 的 Secret。自签够用:浏览器告警一次,点继续即可,本章不引 cert-manager。
Windows 11 + WSL2
# env: windows-ps New-SelfSignedCertificate -DnsName shop.example.com -CertStoreLocation Cert:\CurrentUser\My
产物在证书存储里,不是文件,还得 Export-PfxCertificate 转 PEM,多两步。
Linux (Ubuntu 24.04)
# 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
产出 shop.crt 与 shop.key,格式正是 Secret 要的。
差异结论:K8s 的 TLS Secret 要 PEM 文本,PowerShell 那条给的是证书存储里的对象。所以统一用 openssl:Windows 上在 WSL 里跑,自带它。
# env: all kubectl create secret tls shop-tls -n shop --cert=shop.crt --key=shop.key kubectl get secret shop-tls -n shop
# 预期输出 secret/shop-tls created NAME TYPE DATA AGE shop-tls kubernetes.io/tls 2 3s
不一致怎么办:failed to find any PEM data 说明文件不是 PEM;private key does not match 说明不是一对,重跑 openssl。SAN 必须含 shop.example.com,否则浏览器连告警都过不去。
Step 4 · 建 Gateway 与 HTTPRoute
意图先写「门」,再写「指示牌」。两条 listener 一模一样,只是协议和端口不同。
# 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
# 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
# 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
# 预期输出 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
最后一行是伏笔:Envoy 的 Service 是 LoadBalancer,kind 里没人实现,EXTERNAL-IP 停在 <pending>;ADDRESS 空同理。
不一致怎么办:PROGRAMMED=False 时 describe gateway 看 Conditions,Invalid 多半是 listener 同名或端口冲突;刚 apply 完等 20 秒再看。
-n shop,Gateway 建到了 default
现象:kubectl apply -f shop-gw.yaml 报 created,但 kubectl get httproute shop-web -n shop 的 Accepted 是 False,原因 NoMatchingParent。
原因:文件里设了 namespace: shop 就不会错,但用 -n 覆盖或在命令行 create 时漏掉,Gateway 就落在 default;allowedRoutes 只允许同命名空间的路由挂上来。
怎么验证:kubectl get gateway -A 看 NAMESPACE 应为 shop;不对就删掉重建,对象不能改归属。
Step 5 · 把 30080/30443 钉到 Envoy 上
意图让第 1 章的端口映射生效。Envoy 给每个 Gateway 生成一个 Service,默认 LoadBalancer;改 NodePort 后端口随机(如 31718),与 kind-config.yaml 的 30080 对不上,所以必须钉死 nodePort。
# 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
不写 name 是故意的:战略合并按 port 匹配,写了可能撞上 Envoy 起的名字,报 may not change。再在 shop-gw.yaml 的 spec 下补四行,
把 Gateway 绑到这个 EnvoyProxy:
# file: deploy/base/shop-gw.yaml(spec 下新增这一段)
infrastructure:
parametersRef:
group: gateway.envoyproxy.io
kind: EnvoyProxy
name: shop-proxy
# 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
# 预期输出 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
改配置时 Envoy 会重建 Service,名字后缀会变,别写死,一律用 -l 查。端口是 80:30080/TCP,443:30443/TCP 才算接到宿主上。
Windows 11 + WSL2
# 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
确认宿主 80 真有人在听:Docker Desktop 用转发进程占住它,打不开时先看这条。
Linux (Ubuntu 24.04)
# 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 '
nodePort 应输出 30080 30443,ss 里能看到它。
差异结论:名字与端口两边一样,差别在谁在宿主 80 上监听:Windows 是 Docker Desktop 转发进程,Linux 是 docker-proxy。这一跳决定 502 还是 ERR_CONNECTION_REFUSED。
现象:Service 变成 NodePort 了,但 PORT(S) 是 80:31718/TCP,浏览器打不开(ERR_CONNECTION_REFUSED)。
原因:不写 nodePort 时 apiserver 从 30000–32767 随机分配,而 kind 的 extraPortMappings 在创建时已把宿主 80 钉在节点 30080 上,两个数字对不上。
怎么验证:get svc -l gateway.envoyproxy.io/owning-gateway-name=shop-gw 看 PORT(S);不是 30080/30443 就是 patch 没生效,回去查缩进。
一句话:30080 不是 K8s 的约定,是第 1 章写进 kind 配置里的那三行。
Step 6 · 从浏览器打开 http 与 https
意图让域名解析到 127.0.0.1,再用 curl 验明文与 TLS。Windows 浏览器只认 Windows 的 hosts,WSL 里的它读不到——本章最容易卡在这。
Windows 11 + WSL2
# 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
要管理员权限。改完直接开浏览器,自签证书提示不安全时选「继续访问」。
Linux (Ubuntu 24.04)
# 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
不改 /etc/hosts:--resolve 只对这一条命令生效,测试完不留痕迹。
差异结论:作用域不同:改 hosts 全局生效,--resolve 只骗过这一次 curl。所以「curl 通、浏览器不通」几乎一定是 hosts 改错了位置(改到 WSL 的 /etc/hosts,或存成 hosts.txt)。
# 预期输出 <!DOCTYPE html> <html lang="zh-CN"> <head> <title>Aurora Shop</title>
不一致怎么办:(7) Failed to connect 说明宿主 80 没人监听,回 Step 5 看 nodePort;连接成功但 404 说明到了 Envoy 没匹配上,见 9.8 故障 3;502 则是 shop-web 的 Endpoints 为空。
Step 7 · 负向:apply 一个没人处理的 Ingress
意图看「资源存在」与「行为发生」的区别。本章唯一一次用 Ingress,只为证明没有 controller 它什么也不做。
# 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
# 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
# 预期输出 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>
三处证据:ADDRESS 空、ingressclass 一个没有、Events 一条都没有。API 收下了规则,没有进程去读它。Ingress 只是资源,行为在 controller 里。
现象:kubectl get ingress 的 ADDRESS 一直是空的,kubectl describe 的 Events 也是空的,
等一小时还是这样。
原因:ADDRESS 由 controller 回写,没有 controller 就永远没人写;ingressClassName 只是给 controller 看的选择器,写成什么都不会触发行为。
怎么验证:kubectl get ingressclass 应为空;kubectl get pods -A 里也找不到任何 ingress controller。两个都空就别等了。
不一致怎么办:ADDRESS 有值说明装过某个 controller,用 get pods -A | grep -i ingress 找出来;本章不需要它,ingress-nginx 已退役。
可跳过 · ingress-nginx 的退役时间线与替代路径
2025-11-11 官方公告:ingress-nginx 2026 年 3 月退役,之后不再发安全补丁——维护者覆盖不住攻击面,职责又被 Gateway API 拆走了。替代路径:Envoy Gateway(本章)、Cilium 与 Traefik 的 Gateway 实现、云厂商托管网关,都实现同一套 API。
Step 8 · 提交本章产物
意图四个清单与 Makefile 的验收目标一起入库;下一章开头要用它们确认入口还通着。
# 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"
# 预期输出 [main 9f3c1d2] ch09: Envoy Gateway 接入 shop.example.com 5 files changed, 164 insertions(+)
不一致怎么办:nothing to commit 说明某一步是对着集群直接 apply 的,没落到文件里。
9.7 验收清单
kubectl get gatewayclass eg→ACCEPTED为Truekubectl get gateway shop-gw -n shop→PROGRAMMED为True,两条 listener 都在kubectl get httproute shop-web -n shop→Accepted为True,backendRefs只有 1 个kubectl -n envoy-gateway-system get svc -l gateway.envoyproxy.io/owning-gateway-name=shop-gw→80:30080/TCP,443:30443/TCP- 反向验证:
curl -s --resolve shop.example.com:80:127.0.0.1 http://shop.example.com/返回 shop-web 的 HTML,而不是 404 或 502 kubectl get ingress shop-web-legacy -n shop的ADDRESS仍为空——负向证据要留着make verify-ch09全绿
# env: all make verify-ch09
# 预期输出 [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
9.8 故障实验室
三个故障都来自本章的几个对象,按「从外往里」的顺序排。第三个的判断路径见图 2。
现象:gatewayclass eg 的 ACCEPTED 为 False,gateway shop-gw 的 PROGRAMMED 也一直是 False。
注入:kubectl -n envoy-gateway-system scale deploy/envoy-gateway --replicas=0,模拟控制器没起来;恢复用 --replicas=1。
诊断:① 看条件原文,别猜;② 看控制器在不在;③ 看 CRD 有没有装全。
# 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
根因:Accepted 由 controllerName 指向的控制器回写;控制器不在,条件就停住,message 通常是 Waiting for controller。
修复:把 deploy/envoy-gateway 拉回 1 个副本,等 30 秒再查;CRD 缺失就重跑 9.6 Step 2。
预防:verify-ch09 第一条就断言 gatewayclass eg;控制器副本用 PDB 保住,别让人 scale 到 0。
现象:get httproute 看着正常,浏览器是 503;describe 的 status.parents 里有 ResolvedRefs=False,原因 BackendNotFound。
注入:把 backendRefs 的 port 从 80 改成 8080,再 apply。
诊断:① 看 status.parents 的两个条件;② 对比 Service 的真实端口;③ 确认 Endpoints 非空。
# 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
根因:backendRef.port 指 Service 端口,不是容器端口(shop-web 是 80 → 8080)。注意 Accepted 仍是 True:它只说路由挂上了,不代表后端可用。
修复:改回 port: 80 并 apply,10 秒内条件转成 True。
预防:Accepted 与 ResolvedRefs 分开断言,两个都绿才算通;端口用同一处变量(第 20 章)。
现象:curl -H 'Host: shop.example.com' http://127.0.0.1/ 能拿到页面,浏览器却是 ERR_CONNECTION_REFUSED;或都能连,浏览器 404。
注入:把 http listener 的 hostname 改成 shop.example.com 之外的值(比如加一个前缀),再 apply;此时浏览器用 shop.example.com 访问就是 404。
诊断:按下图的顺序,先分清「连不上」与「连上了但没匹配」,两者的排查方向完全相反。
# 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}'
根因:连接被拒是宿主 80 → 节点 30080 这一跳没通(映射没写、nodePort 随机、Docker 没转发);404 是请求到了 Envoy,但 listener 的 hostname 与 Host 头不匹配。
修复:连接类回 9.6 Step 5 对齐 nodePort;匹配类把 listener 与 hostnames 改成同一个值,再 apply。
预防:把 curl -H 'Host: …' 写进 verify-ch09——它比浏览器更早暴露 host 不匹配,还能贴进工单。
见图 2:这张图的价值在于「先分层,再查细节」。绝大多数人卡住,是因为把 404 当成连接问题去查端口,或者把连接被拒当成路由问题去改 hostname——两个方向恰好相反,试三次就浪费半小时。
9.9 生产对照
| 维度 | 学习环境(kind) | 生产环境 | 为什么 | 迁移动作 |
|---|---|---|---|---|
| 入口类型 | Envoy 的 Service 是 NodePort,端口钉在 30080 | 云负载均衡器或 Anycast 入口 | kind 里没有 LoadBalancer 的实现,EXTERNAL-IP 只会停在 <pending>,不钉 nodePort 就只能用随机端口 | 把 envoyService.type 换回 LoadBalancer,加云厂商的注解 |
| 证书 | openssl 自签,365 天,手动建 Secret | cert-manager 加 ACME 自动签发与轮换 | 自签证书浏览器告警,到期也不会自动续,只适合本地验证 | 装 cert-manager,certificateRefs 指向它签发的 Secret |
| 网关归属 | Gateway 与路由都在 shop | 平台团队维护 Gateway,业务只提交 HTTPRoute | allowedRoutes.from: Same 把路由锁在网关所在命名空间,避免任何命名空间都能抢域名 | 网关挪到 platform,from 改成 Selector 加标签 |
| 路由粒度 | 一条 / 全量转发 | 按路径、Header、权重拆多条 | 单条规则无法灰度,也没法把 /api 与静态页分给不同后端 | 加 matches 与 weight,或接 Argo Rollouts |
| 观测 | 不看 Envoy 的日志 | access log、指标、超时与重试策略 | 网关是唯一知道「谁在 502」的位置,而 Envoy 默认不打印访问日志 | 在 EnvoyProxy 的 telemetry 里开 access log,接进第 12 章 |
一句话:生产与学习的差别不在网关软件,在谁来发 IP、谁来续证书。
9.10 清理与残留检查
负向步骤那个 Ingress 必须删掉,否则后面每章的 get ingress 都会看到一个 ADDRESS 为空的对象,容易被误判成「入口坏了」。Gateway 本章不删:第 10 章要用它验证订单写入后页面可见。
| 对象 | 默认处理 | 影响 |
|---|---|---|
ingress/shop-web-legacy | 删 | 无影响,它只是一条负向证据 |
secret/shop-tls | 保留 | https listener 依赖它;删了 verify-ch09 立刻红 |
gateway/shop-gw | 保留 | 删掉会连带回收 Envoy 的 Deployment 与 Service,30080 释放 |
envoyproxy/shop-proxy | 保留 | 删掉后 Gateway 的 infrastructure 指向空对象,Service 退回随机端口 |
gatewayclass/eg | 保留 | 删它会带走 Envoy Gateway 的全部 CRD,第 10 章还得重装 |
# env: all kubectl delete -f deploy/base/ingress-legacy.yaml # 影响:只删这个 Ingress,不影响已经生效的 Gateway 与路由 # 证书留着:verify-ch09 的 https 断言要用它
如果打算停在这里、彻底回滚到第 8 章的状态:
# 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 对象消失
# env: all kubectl get gateway,httproute,ingress,svc,secret -n shop kubectl get gatewayclass
# 预期输出 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
回滚到上一章:git checkout HEAD~1 -- deploy/base/ Makefile,再跑 make verify-ch08。注意 8.10 已删掉 Postgres,所以它会在 pvc 那一条上失败——这是预期的,第 10 章会把库装回来。
9.11 小结与自测
一句话:Ingress 只是规则,Gateway 才是那个真的在监听的东西。
一句话:kind 没有负载均衡器,<pending> 不是故障,是没人给你发 IP。
一句话:Accepted 只说明挂上了,ResolvedRefs 才说明后端找得到。
三句对应三层:资源层(对象是什么)、网络层(流量从哪进来)、状态层(怎么判断它真的通了)。
自测 3 题(点击展开答案)
1(概念)Gateway 与 GatewayClass 的关系,和 Pod 与 Deployment 的关系一样吗?
方向相反。Deployment 创建 Pod;GatewayClass 不创建 Gateway,它只声明这类网关由哪个控制器实现,Gateway 用 gatewayClassName 引用它。
2(排错)HTTPRoute 的 Accepted=True,浏览器却 503,下一步?
看 ResolvedRefs:BackendNotFound 就是名字或端口写错了;两个条件都 True 还 503,去查 EndpointSlice 是不是空的(第 7 章)。
3(决策)团队要上新项目,还装一个 ingress-nginx 行不行?
不行。它已在 2026 年 3 月退役,之后没有安全补丁。本地用 Envoy Gateway(一条 install.yaml,无 Helm),已用 Cilium 做 CNI 的直接开 Cilium Gateway,云上优先托管网关。
再加一条 HTTPRoute,把 shop.example.com/api 指到 shop-api:8080(pathType: PathPrefix + value: /api),再想清楚:shop-web 里的 nginx 要不要改?两条路由谁优先?
9.12 衔接
回收:第 8 章结尾问「数据只在集群内可达,产品经理想从公网用域名打开怎么办」,本章用 Gateway 加 HTTPRoute 回答了;第 5 章的边界也没破:/api 的转发仍由 nginx 做,网关只认域名和路径前缀。
钩子:入口通了,用户点「下单」会真的写进数据库。可 orders 表还不存在,而建表不该由某个 Pod 启动时顺手做——它要能重跑、能失败、能定时。第 10 章从这两件事开始。
9.13 参考与验证版本
- Gateway API 官方文档
- HTTPRoute
- Envoy Gateway Quickstart
- Customize EnvoyProxy(envoyService 与 patch)
- Ingress(含退役说明)
验证版本:k8s v1.36.1 / kubectl v1.36.1 / kind v0.32.0 / Envoy Gateway v1.10.0 · 最后验证:2026-11-11