W4-D23 Ingress 与 Gateway API:从公网域名到 Pod 的六跳(精讲 + 实操)

6830 字
34 分钟
W4-D23 Ingress 与 Gateway API:从公网域名到 Pod 的六跳(精讲 + 实操)
  1. 1K8s 四周课程总表
  2. 2第 1 周:架构与核心对象
  3. 3W1-D1 架构总览(精讲 + 实操)
  4. 4W1-D2 容器运行时(精讲 + 实操)
  5. 5W1-D3 apiserver 准入链(精讲 + 实操)
  6. 6W1-D4 etcd:强一致从哪来(精讲 + 实操)
  7. 7W1-D5 Pod 与资源管理:一个 Pod 起不来,到底该查哪一层(精讲 + 实操)
  8. 8W1-D6 Deployment 与工作负载:一次发布到底发生了什么(精讲 + 实操)
  9. 9W1-D7 Service 与网络入口:为什么"有端点却不通"(精讲 + 实操)
  10. 10第 1 周面试题库
  11. 11W2 调度与网络(题干稿)
  12. 12W3 权限、存储与排障(题干稿)
  13. 13W4 交付、扩展与实战(题干稿)
  14. 14第 2 周:调度与网络
  15. 15W2-D8 调度器:为什么"节点明明很空,Pod 就是起不来"(精讲 + 实操)
  16. 16W2-D9 调度约束:为什么"想摊开副本"反而让全组 Pending(精讲 + 实操)
  17. 17W2-D10 探针与生命周期:为什么"探针全绿"还是半夜反复重启(精讲 + 实操)
  18. 18面试题库(总) W1 + W2 + W3 + 每日场景 · 226 题
  19. 19W1 知识点总结(架构与核心对象)
  20. 20W2-D11 优雅终止与滚动:为什么"参数都调对了"发布还是丢请求(精讲 + 实操)
  21. 21W2-D12 CNI 与 Pod 网络:一个包从 eth0 到 eth0 到底走了几跳(精讲 + 实操)
  22. 22W2-D12 急救版:只看三个画面(20 分钟)
  23. 23W2-D13 Service 与 kube-proxy:那个"不存在的 IP"怎么把流量送到 Pod(精讲 + 实操)
  24. 24W2-D14 DNS 与连接:三个"对象全正常"的隐形杀手(精讲 + 实操)
  25. 25W2 知识点总结(调度与网络)
  26. 26W3-D15 ServiceAccount:集群里的"工作负载身份"是怎么发出去的(精讲 + 实操)
  27. 27W3-D16 RBAC 工程实践:为什么"给了 admin"还是做不了(精讲 + 实操)
  28. 28W3-D17 配额、LimitRange 与 PodSecurity:三种"拒绝",三本账(精讲 + 实操)
  29. 29W3-D18 存储:PVC 为什么永远 Pending(精讲 + 实操)
  30. 30W3-D19 ConfigMap 与 Secret:改了配置为什么应用没变(精讲 + 实操)
  31. 31W3-D20 排障方法论:把"猜"换成"按状态机走"(精讲 + 实操)
  32. 32W3-D21 综合故障日:四层模型与五条串烧(精讲 + 实操)
  33. 33第 3 周:权限、存储与排障
  34. 34W3 知识点总结(权限、存储与排障)
  35. 35W4-D22 Helm:从"会装"到"会改"(精讲 + 实操)
  36. 36W4-D23 Ingress 与 Gateway API:从公网域名到 Pod 的六跳(精讲 + 实操)本文
  37. 37W4-D24 CRD 与 Operator:把运维知识写成代码(精讲 + 实操)
  38. 38W4-D25 高可用、升级与证书:你的集群有多脆(精讲 + 实操)
  39. 39第 4 周:交付、扩展与实战
  40. 40W5-D29 控制面故障:三个"看起来不严重"的事故
  41. 41第 5 周:故障案例复盘(35 个完整案例)

一天一个文件:白天读第一部分,晚上做第二部分。W4 第 2 天(全局编号 D23)。

时段做什么时长
早通勤翻总题库 W4 Day 1 的 3 道题(Q151–Q156 里抽 3),出声讲 30 秒版15 min
上班读本文件第一部分(纯阅读,不开终端)70 min
晚上做本文件第二部分,6 个实验60 min
睡前背总题库 W4 Day 2(Q157–Q162)6 题 + 场景题 D23S1–S440 min

环境:1 master + 2 worker,Calico。今晚用 NodePort 模式装 ingress-nginx(不依赖云 LB),访问地址是 http://<任一节点IP>:30080。

今天讲什么

一个请求从公网到 Pod,中间有六跳。今天的目标不是”会写 Ingress yaml”,而是每一跳都有一个命令能单独验证:

  1. Ingress 只是规则,Controller 才是执行者。只建 Ingress 没有 Controller,等于写了一张没人看的纸条。
  2. 六跳链路:DNS → 四层入口(NodePort/LB)→ Controller Pod → Host/Path 匹配 → Service → EndpointSlice → Pod。
  3. 状态码就是层级:404 是规则没匹配上、503 是后端没端点、504 是后端慢、413 是 body 太大。看码就能定位到层,不用猜。

主线事故:Ingress 建好了,curl 一直 404。查了 Service、查了 Pod、查了网络,全正常。真因是集群里有两个 Ingress Controller,ingressClassName 没写,请求进了另一个空的那个。

先认几个词

词白话意思中文谐音
Ingress一份”域名/路径 → Service”的规则声明因格瑞斯
Ingress Controller真正监听端口、执行规则的组件因格瑞斯 康吹勒
ingressClassName指定这份规则交给哪个 Controller 执行因格瑞斯 克拉斯 内姆
pathType路径匹配方式:精确 / 前缀 / 由实现决定帕斯 泰普
default backend匹配不上时兜底的那个后端迪佛特 拜肯德
annotationController 私有的扩展参数安诺泰申
TLS 终止在 Controller 把 HTTPS 解开,后端走 HTTP提 艾尔 艾斯
Gateway APIIngress 的下一代标准(角色拆分)盖特威 艾皮艾
HTTPRouteGateway API 里”路由规则”那个对象艾奇提提皮 如特

更全的在附录 A。


第一部分 白天精讲(上班阅读 70 min)#

第 0 章 404 查了两小时,因为集群里有两个 Controller#

需求:把 order.example.com 指到 order 服务。yaml 写得很标准:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: order
namespace: prod
spec:
rules:
- host: order.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: order
port: {number: 80}

kubectl get ingress 显示 ADDRESS 是空的,curl -H "Host: order.example.com" http://<node>:30080 返回 404。

查了一圈:Service 有端点、Pod 是 Ready、网络通。最后发现:

Terminal window
$ kubectl get ingressclass
NAME CONTROLLER PARAMETERS AGE
nginx k8s.io/ingress-nginx <none> 30d
traefik traefik.io/ingress-controller <none> 5d
$ kubectl get ingressclass nginx -o jsonpath='{.metadata.annotations}'
{"ingressclass.kubernetes.io/is-default-class":"false"}

两个 Controller,没有默认 class。而他的 Ingress 没写 ingressClassName——两个 Controller 都认为”这不是给我的”,于是请求落到了默认后端(404)。

这件事推出两条:

  1. Ingress 必须有 ingressClassName(或者集群里明确设了一个默认 class)。
  2. ADDRESS 为空是个信号:正常工作时 Controller 会回填它的地址。空着说明”没人认领”或”Controller 没就绪”。

第 1 章 Ingress 是规则,不是服务#

对象是什么谁创建没有它会怎样
Ingress一份声明:域名/路径 → 哪个 Service你(应用方)没有任何路由规则
IngressClass“这份规则交给哪类 Controller”装 Controller 时创建多个 Controller 时无法区分
Ingress Controller一个 Pod(Deployment/DaemonSet),监听 80/443平台方Ingress 完全无效

关键认知:Ingress 对象本身不监听任何端口。 它只是 etcd 里的一条记录,Controller 通过 watch 拿到它,然后生成 nginx 配置(或 traefik 的 router)。

所以:

  • kubectl get ingress 显示规则,但不代表它在生效。
  • 判断”真的生效了”要看三样:ADDRESS 有值、Controller 日志里没有 error、curl 能通。

为什么这么设计? 因为它把”路由规则”和”实现方式”解耦了:应用只写 Ingress,平台可以换 nginx / traefik / APISIX / 云厂商的 ALB,应用的 yaml 不用改。


第 2 章 六跳链路:每一跳都能单独验证#

① DNS 解析:order.example.com → 1.2.3.4
② 四层入口:1.2.3.4:443 → NodePort 30443 / 云 LB → Controller Pod
③ Controller:按 Host + Path 匹配 Ingress 规则
④ 转发到 Service:order.prod.svc:80(ClusterIP)
⑤ Service → EndpointSlice → 某个 Pod IP:8080
⑥ Pod 处理,原路返回

每一跳的验证命令:

跳验证失败表现
① DNSdig +short order.example.com解析不到 / 解析到错的 IP
② 入口curl -v http://<node>:30080连不上(防火墙/NodePort 没开)
③ 匹配kubectl get ingress -o wide 看 ADDRESS;Controller 日志404(规则没匹配上)
④ Servicekubectl get svc order;kubectl exec 里 curl ClusterIP503(无端点)
⑤ 端点kubectl get endpointslices -l kubernetes.io/service-name=order503
⑥ Podkubectl exec <pod> -- curl localhost:8080502/504

这条链的价值:出问题时不要”从前往后猜”,而是找出第一个失败的跳。四条命令就能定位:

Terminal window
dig +short order.example.com
curl -sv -H "Host: order.example.com" http://<node>:30080 | head -20
kubectl get ingress order -n prod -o wide
kubectl get endpointslices -l kubernetes.io/service-name=order -n prod

第 3 章 ingressClassName 与默认 class#

三种写法,优先级从高到低:

# ① 新写法(推荐)
spec:
ingressClassName: nginx
# ② 老写法(1.18 之前,现在仍兼容)
metadata:
annotations:
kubernetes.io/ingress.class: nginx

设默认 class:

Terminal window
kubectl annotate ingressclass nginx ingressclass.kubernetes.io/is-default-class=true

设了之后,不写 ingressClassName 的 Ingress 会归它管。但集群里只应该有一个默认 class,两个默认会导致行为不确定。

排查顺序:

Terminal window
kubectl get ingressclass # 有哪几个
kubectl get ingress order -n prod -o jsonpath='{.spec.ingressClassName}' # 这份规则给谁
kubectl get pods -A | grep -E "nginx|traefik" # 对应的 Controller 在不在

一个容易忽略的点:ingressClassName 写了一个不存在的 class,Ingress 不会报错,只是永远处于”没人处理”的状态(ADDRESS 空)。这又是”静默失败”。


第 4 章 pathType 三种:Prefix 的真实含义#

pathType匹配规则例:/api 能匹配什么
Exact完全相等(含大小写)只匹配 /api
Prefix按路径段匹配/api、/api/、/api/v1、/api/users/1
ImplementationSpecific由 Controller 决定(nginx 里等价于前缀,但可以配正则)看实现

重点:Prefix 是按 / 分段匹配,不是字符串前缀。

所以:

  • /api 不会匹配 /apiserver(分段不同)
  • /api 会匹配 /api/v1/users
  • / 匹配所有路径(因为它是空前缀)

这是最容易被问倒的一题,因为很多人以为 Prefix 就是 strings.HasPrefix。

优先级:最长路径优先。/api/v1 和 /api 同时存在时,/api/v1/x 走前者。

ImplementationSpecific 什么时候用? 需要正则(nginx.ingress.kubernetes.io/use-regex: "true" + path: /api/.*)的时候。但它不可移植——换 Controller 就失效。


第 5 章 TLS 终止在 Controller#

spec:
tls:
- hosts: [order.example.com]
secretName: order-tls # 类型必须是 kubernetes.io/tls
rules:
- host: order.example.com
...

Secret 里两个 key:tls.crt(证书链)和 tls.key(私钥)。

Terminal window
kubectl create secret tls order-tls -n prod --cert=tls.crt --key=tls.key

关键认知:TLS 在 Controller 终止。 Controller 解开 HTTPS,然后用 HTTP 转发给 Service。

客户端 --HTTPS--> Controller --HTTP--> Service --HTTP--> Pod
↑
证书在这里

所以:

  • 后端 Pod 不需要配证书(除非你要 mTLS,那是另一套:service mesh)。
  • X-Forwarded-Proto: https 是 Controller 加的,应用靠它判断”用户是不是走的 HTTPS”。
  • 如果应用自己生成绝对 URL(比如重定向),不读这个 header 就会生成 http:// 的地址,导致混合内容。

HTTP → HTTPS 跳转(nginx):

annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true"
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"

第 6 章 annotation:Controller 的私有扩展#

annotation 是 Ingress 规范之外的东西,每个 Controller 自己定义。常用的几个(nginx):

annotation作用默认
rewrite-target改写转发路径不改
proxy-body-size请求体大小上限1m(上传大文件必炸)
proxy-read-timeout / proxy-send-timeout后端超时60s
canary / canary-weight金丝雀分流关闭
ssl-redirect强制 HTTPStrue
use-regex路径按正则解释false

rewrite-target 的坑:它会改写所有匹配的路径,需要配合 use-regex 和捕获组才能精确控制。写错的表现是”后端收到的路径不对”(404 或 500)。

为什么 Gateway API 会出现? 因为 annotation 不可移植:nginx 的 proxy-body-size 在 traefik 里叫 buffering.maxRequestBodyBytes,换个 Controller 全部要重写。而且 annotation 没有 schema 校验,写错了不报错,只是静默不生效。


第 7 章 Gateway API:把”谁管什么”拆开#

三个对象,对应三种角色:

对象谁创建管什么
GatewayClass平台方(集群级)“我们有哪类网关”(对应某个 Controller 实现)
Gateway平台方“这个网关监听哪些端口、哪些域名、用什么证书”
HTTPRoute应用方“这个域名/路径 → 哪个 Service”

对比 Ingress:

# Ingress:一个对象包办所有事,靠 annotation 扩展
kind: Ingress
spec:
tls: [...]
rules: [...]
# Gateway API:拆成三层
kind: Gateway # 平台管:监听 443,证书在哪
kind: HTTPRoute # 应用管:host + path → backend

它解决的三个真问题:

  1. 权限边界:应用方只能建 HTTPRoute,碰不到证书和监听端口。
  2. 可移植:HTTPRoute 的 matches / filters 是规范字段,不依赖 annotation。
  3. 表达力:原生支持 header 匹配、权重分流、请求改写(filters),不用靠 Controller 私有扩展。

现实:Gateway API 需要先装 CRD + 支持它的 Controller(nginx、traefik、istio、cilium 都支持)。小集群、单团队用 Ingress 就够,Gateway API 是给”平台团队 + 多租户”准备的。


第 8 章 四个状态码,四层#

码含义谁返回的判据
404规则没匹配上Controller(默认后端)kubectl get ingress 的 host/path 和请求对不上;ingressClassName 不对
503后端没有可用端点Controllerkubectl get endpointslices 为空
504后端处理超时Controller后端慢于 proxy-read-timeout;后端在启动
413请求体太大Controllerproxy-body-size 太小(默认 1m)
502后端返回了非法响应 / 连接被重置Controller后端进程崩了、或者协议不对(HTTP 打到 HTTPS 端口)

最有用的一条:404 一定是”规则/入口”层,503 一定是”端点”层。 这两个码就能把范围砍掉一半。

Terminal window
kubectl get ingress -A -o wide # ADDRESS 空?
kubectl get endpointslices -A | grep <svc> # 空?
kubectl logs -n ingress-nginx deploy/ingress-nginx-controller --tail=50

第 9 章 故障表:Ingress 类十行#

现象直接原因深层机制怎么防
一直 404没写/写错 ingressClassName请求落到另一个 Controller 或默认后端显式写 class;只保留一个默认
一直 404host 不匹配(带不带 www、大小写)七层按 Host 精确匹配(支持通配符)curl -H "Host: ..." 逐步试
503Service 无端点selector 不匹配 / Pod 未 Readyget endpointslices
503Service 的 port 和 targetPort 写错转发到没人监听的端口用 kubectl exec 直接 curl Pod
504后端超过 proxy-read-timeout默认 60s调大或优化后端
413proxy-body-size 默认 1mnginx 层限制按需调大
ADDRESS 一直空Controller 没就绪 / 没认领Controller 只能回填自己的地址get pod -n ingress-nginx
TLS 报错 certificate not foundSecret 不存在或类型不对必须是 kubernetes.io/tls 且 key 是 tls.crt/key用 create secret tls
改了 annotation 没生效拼错 / 该 annotation 不属于这个 Controllerannotation 无 schema 校验对照官方文档,看 Controller 日志
灰度流量不按比例只有一个 Ingress 配了 canarycanary 需要”主规则 + 金丝雀规则”成对存在两条 Ingress 同 host/path

第 10 章 五层追问链:从”Ingress 404”往下问#

第 1 层:Ingress 建了但 404,先查什么?

先查 ingressClassName 和 ADDRESS。ADDRESS 空说明没有 Controller 认领;有值但还 404,就去比对 host/path。

第 2 层:怎么确认请求真的到了 Controller?

kubectl logs -n ingress-nginx deploy/ingress-nginx-controller 能看到每条请求的日志(含 host、path、状态码、后端)。没日志说明没到 Controller。

第 3 层:Controller 收到了,但 503?

那是后端没端点。get endpointslices -l kubernetes.io/service-name=<svc>,空就查 selector 和 readiness。

第 4 层:端点有,还 503/502?

查 Service 的 targetPort 和 Pod 实际监听端口是否一致;kubectl exec 进 Pod 直接 curl 本地端口。

第 5 层:为什么 TLS 配了还是 502?

很可能是”后端是 HTTPS 但 Controller 用 HTTP 转发”,或者 Secret 的证书 CN 和 host 不匹配。看 Controller 日志里的 SSL_do_handshake 错误。


第 11 章 生产现场:四件只有真配过 Ingress 才知道的事#

① 集群里有两个 Controller 是常态。 装东西时随手 helm install,很容易留下两套。排查第一步永远是 kubectl get ingressclass。

② annotation 是隐形的技术债。 一套 Ingress 上挂十几个 annotation,换 Controller 时全部失效,而且不会报错——只是行为变了。

③ proxy-body-size: 1m 会打死文件上传。 默认值很小,上传接口必须显式调大,否则用户看到的是 413,而你的应用日志里什么都没有。

④ 504 经常不是”后端慢”,是”后端在启动”。 滚动更新时新 Pod 还没就绪就被转发(readiness 没配好),Controller 等 60 秒超时。这时候调 timeout 只是掩盖问题。


第 12 章 十二条判断题#

  1. 只建 Ingress 对象,不装 Controller,也能访问。→ 错,Ingress 不监听端口。
  2. ingressClassName 写错会报错。→ 错,静默不生效。
  3. pathType: Prefix 的 /api 能匹配 /apiserver。→ 错,按路径段。
  4. pathType: Exact 区分大小写。→ 对。
  5. TLS 证书要放在 ConfigMap 里。→ 错,Secret(kubernetes.io/tls)。
  6. TLS 终止后,后端也必须用 HTTPS。→ 错,默认是 HTTP。
  7. 503 一般是 Service 没有端点。→ 对。
  8. 404 可能是 host 写错。→ 对。
  9. annotation 是 K8s 标准字段,换 Controller 也通用。→ 错,是私有扩展。
  10. Gateway API 里,HTTPRoute 由应用方创建。→ 对。
  11. proxy-body-size 默认是 10m。→ 错,1m。
  12. 同一个 host/path 只能有一个 Ingress 管。→ 错,canary 就是两条共存。

第 2、3、5 题是分水岭。


第 13 章 真实面试题 10 道(按现场口吻,出声答)#

  1. 「Ingress 和 Ingress Controller 是什么关系?只装 Ingress 能用吗?」
  2. 「一条请求从公网到 Pod,中间经过哪些组件?每跳怎么验证?」
  3. 「pathType 三种有什么区别?/api 会不会匹配 /apiserver?」
  4. 「Ingress 报 404 和 503,分别是什么原因?」
  5. 「TLS 在哪一层终止?后端还需要 HTTPS 吗?」
  6. 「Gateway API 相比 Ingress 解决了什么问题?」
  7. 「集群里有两个 Controller 会怎样?怎么排查?」
  8. 「上传大文件报 413,怎么改?」
  9. 「怎么做金丝雀发布?不改应用能做到吗?」
  10. 「你们线上入口是怎么做的?为什么这么选?」

第 14 章 白话总结#

Ingress 是纸条,Controller 是执行的人。纸条写得再对,没人看就是废纸。

六跳链路的价值在于”每一跳都能单独验证”:DNS、入口、匹配、Service、端点、Pod。404 砍掉后三跳,503 砍掉前三跳——这两个码就能把范围减半。

TLS 在 Controller 终止,后端是 HTTP;annotation 是私有扩展,换 Controller 就失效——这就是 Gateway API 存在的全部理由。

明天讲”给 apiserver 加一种新资源”:CRD 和 Operator。


附录 A 白话小词典#

词白话谐音记忆点
Ingress路由规则声明因格瑞斯不监听端口
Ingress Controller执行规则的组件因格瑞斯 康吹勒没有它全废
ingressClassName交给哪个 Controller因格瑞斯 克拉斯 内姆写错静默失效
pathType路径匹配方式帕斯 泰普Prefix 按路径段
default backend兜底后端迪佛特 拜肯德404 就是它
annotation私有扩展参数安诺泰申换 Controller 失效
TLS 终止在入口解 HTTPS提艾尔艾斯后端走 HTTP
GatewayClass网关”类型”盖特威 克拉斯平台方建
Gateway网关实例盖特威平台方建
HTTPRoute路由规则艾奇提提皮 如特应用方建

附录 B Ingress 排障 Runbook#

① curl -sv -H "Host: <域名>" http://<node>:<nodeport>/
├─ 连不上 → 入口层:NodePort/防火墙/Controller 没起
└─ 有响应 → 看状态码,往下
② 状态码分诊
├─ 404 → kubectl get ingress -A -o wide(ADDRESS 空?)
│ kubectl get ingressclass
│ kubectl logs -n ingress-nginx deploy/ingress-nginx-controller | grep <host>
├─ 503 → kubectl get endpointslices -l kubernetes.io/service-name=<svc>
│ kubectl get svc <svc> -o yaml(targetPort)
├─ 504 → 后端超时:看 proxy-read-timeout、看 Pod 日志耗时
└─ 413 → proxy-body-size
③ 直连后端确认
kubectl exec -it <any-pod> -- curl -sv http://<svc>.<ns>.svc.cluster.local:<port>/

附录 C 报错文本词典#

原文含义下一步
404 Not Found + nginx 页脚规则没匹配查 class / host / path
503 Service Temporarily Unavailable无端点查 endpointslices
504 Gateway Time-out后端超时查 timeout 与后端耗时
413 Request Entity Too Largebody 超限proxy-body-size
default backend - 404落到兜底后端class 不对/没规则
ssl certificate ... not foundSecret 缺失或类型错create secret tls

附录 D 今天能带走的两份产出#

产出 1:一份可直接用的 Ingress 模板

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: order
namespace: prod
annotations:
nginx.ingress.kubernetes.io/proxy-body-size: "20m"
nginx.ingress.kubernetes.io/proxy-read-timeout: "120"
spec:
ingressClassName: nginx
tls:
- hosts: [order.example.com]
secretName: order-tls
rules:
- host: order.example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: order-api
port: {number: 80}
- path: /
pathType: Prefix
backend:
service:
name: order-web
port: {number: 80}

产出 2:六跳验证清单(出问题时按顺序打勾)

Terminal window
dig +short order.example.com
curl -sv -H "Host: order.example.com" http://<node>:30080/ | head -5
kubectl get ingress order -n prod -o wide
kubectl get endpointslices -l kubernetes.io/service-name=order -n prod
kubectl exec -it <pod> -- curl -s localhost:8080/healthz

今晚的 6 题(睡前 40 min,总题库 W4 Day 2:Q157–Q162)#

  1. Ingress 和 Ingress Controller 是什么关系?只装 Ingress 能用吗?
  2. 一条请求从公网到 Pod,中间经过哪些组件?每跳怎么验证?
  3. pathType 三种有什么区别?/api 会不会匹配 /apiserver?
  4. Ingress 报 404 和 503,分别是什么原因?
  5. TLS 在哪一层终止?后端还需要 HTTPS 吗?
  6. Gateway API 相比 Ingress 解决了什么问题?

第 15 章 场景题四道(给现象,你给排查顺序)#

Ingress 的题几乎都能用”看状态码”开头。四道题分别对应 404、503、504 和”改了不生效”。


S1 · Ingress 一直 404,Service 和 Pod 都正常#

面试官怎么问

Ingress 建好了,curl 一直 404,Service 有端点、Pod 也 Ready。查哪?

现象

Terminal window
$ kubectl get ingress -n prod
NAME CLASS HOSTS ADDRESS PORTS
order <none> order.example.com 80
$ curl -H "Host: order.example.com" http://192.168.10.51:30080/
<html><head><title>404 Not Found</title></head>...

30 秒版

CLASS 是空的、ADDRESS 也是空的——没有 Controller 认领这份规则。集群里可能有多个 Controller 且没有默认 class。第一动作是 kubectl get ingressclass。

展开

  1. kubectl get ingressclass,看有几个、哪个是默认(看 annotation)
  2. kubectl get ingress order -n prod -o jsonpath='{.spec.ingressClassName}',空就是没指定
  3. 修:加上 ingressClassName: nginx(按你实际的 class 名)
  4. 再看 ADDRESS 是否被回填,等 10 秒
  5. 还 404 → 比对 host:curl -H "Host: ..." 里的 host 必须和 spec.rules[].host 完全一致
  6. 看 Controller 日志:kubectl logs -n ingress-nginx deploy/ingress-nginx-controller --tail=50,能看到它实际收到的 Host

追问:为什么 ADDRESS 为空不报错?

因为 Ingress 只是一个声明,没有 Controller 处理时它不会”失败”,只是永远处于未实现状态。这属于 K8s 里最常见的”静默失败”——和 W3-D18 的 PVC 没 SC 一样。

易错

去查 Service 和 Pod。404 是七层的”我不认识这个 host/path”,跟后端没关系。


S2 · 503,端点明明是空的#

面试官怎么问

Ingress 返回 503,get endpointslices 是空的。先修 Service 还是先修 Pod?

现象

Terminal window
$ kubectl get ingress -n prod
order nginx order.example.com 192.168.10.51 80
$ curl -H "Host: order.example.com" http://192.168.10.51:30080/
<html>...503 Service Temporarily Unavailable...</html>
$ kubectl get endpointslices -l kubernetes.io/service-name=order -n prod
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
order-abc12 IPv4 8080 <unset> 3d

30 秒版

503 = 有规则、没后端。端点空只有三个原因:selector 不匹配、Pod 不 Ready、端口名/号对不上。第一动作是 kubectl get svc -o yaml 看 selector,再和 Pod 的 label 逐个对。

展开

  1. kubectl get svc order -n prod -o yaml | grep -A3 selector
  2. kubectl get pod -n prod --show-labels | grep order
  3. 对不上 → 改 selector 或改 Pod label(注意 Deployment 的模板)
  4. 对得上但端点还空 → kubectl get pod -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}'
  5. Ready=False → 看 readiness 探针(W1-D7 第 3 章)
  6. 端点有但端口不对 → 看 targetPort 是数字还是名字,和容器的 ports[].name 对得上

追问:端点为空时,Service 的 ClusterIP 还能通吗?

不能。Service 只是个 DNAT 规则,没有后端就没有 DNAT 目标,包会被丢弃(表现为超时,不是拒绝)。所以”Service 内部也不通”和”Ingress 503”是同一件事的两种表现。

易错

先去改 Ingress 配置。503 和 Ingress 规则无关,规则能匹配上才会返回 503。


S3 · 504,代码明明没改#

面试官怎么问

昨天还好好的,今天所有走 Ingress 的接口都 504,后端日志看不出慢。什么情况?

现象

Terminal window
$ curl -sv -H "Host: order.example.com" http://192.168.10.51:30080/api/slow
< HTTP/1.1 504 Gateway Time-out

后端日志里请求耗时只有 200ms。但 curl 等了 60 秒。

30 秒版

504 是 Controller 等后端超时,不是后端处理慢。可能后端压根没收到请求(连接建立不了),或者响应被中间某层卡住。第一动作是进 Pod 直接访问,看是不是”去掉 Ingress 就好了”。

展开

  1. kubectl exec -it <pod> -- curl -sv http://order.prod.svc:80/api/slow —— 通不通、快不快
  2. 直连快、走 Ingress 慢 → 问题在 Controller 到后端这一段
  3. 看 Pod 的实际监听端口和 Service 的 targetPort 是否一致(不一致时连接会一直重试)
  4. 看 NetworkPolicy:kubectl get networkpolicy -n prod,有没有拦 ingress-nginx 的流量
  5. 看后端是不是 HTTPS 而你用 HTTP 转发(表现为连上但不响应)
  6. kubectl logs -n ingress-nginx deploy/ingress-nginx-controller | grep "upstream timed out" 能看到真实的 upstream 地址

追问:为什么后端日志只有 200ms?

因为它处理的是”另一个请求”。504 的那个请求可能根本没到后端,或者到了但响应写不回去(比如 NetworkPolicy 单向放行)。日志里没有 = 请求没到,这是最关键的推理。

易错

直接调大 proxy-read-timeout。如果请求根本没到后端,调多大都没用,只是让你多等。


S4 · 金丝雀配了,流量一点没过去#

面试官怎么问

按文档配了 canary 的 annotation,canary-weight: 20,但新版本一点流量都没有。为什么?

现象

Terminal window
$ kubectl get ingress -n prod
order nginx order.example.com ... 80
order-canary nginx order.example.com ... 80
$ for i in {1..20}; do curl -s -H "Host: order.example.com" http://192.168.10.51:30080/ver; done | sort | uniq -c
20 v1

30 秒版

canary 规则要求”主规则 + 金丝雀规则”成对存在,且两者的 host/path 完全一致,只有后端不同。另外 canary-by-header 之类的条件如果配了,权重就不生效(header 优先)。第一动作是对比两条 Ingress 的 host、path、annotation。

展开

  1. kubectl get ingress order order-canary -n prod -o yaml | grep -E "host:|path:|canary"
  2. host/path 必须完全一致,差一个字母就不成对
  3. 检查有没有同时配 canary-by-header / canary-by-cookie——它们优先级高于 weight
  4. 确认 canary 那条的 canary: "true" 是字符串(yaml 里不加引号会被当布尔)
  5. 看 Controller 日志里有没有 reload 失败(配置错了会整体不生效)
  6. 用 curl -H "Host: ..." -H "X-Canary: always" 验证 header 规则是否生效

追问:为什么 canary Ingress 的 ADDRESS 也是同一个?

因为它不是独立的入口,只是主规则的一个”附加分支”。Controller 把两条合并成一份 nginx 配置(split_clients 或 map),所以对外是同一个地址。

易错

给 canary 单独写一个不同的 host,然后发现”新域名能通但不是灰度”。那不是灰度,是两条独立的路由。


第二部分 晚上实操(60 min,只敲命令)#

6 个实验。全部用 NodePort,不依赖云 LB。

开工前的 60 秒#

Terminal window
kubectl create ns ingress-demo
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
kubectl get ingressclass # 装之前应该是空的

实验 1|装 Controller,验证”Ingress 本身不监听端口”(12 min)#

Terminal window
helm install ingress-nginx ingress-nginx/ingress-nginx -n ingress-nginx --create-namespace \
--set controller.service.type=NodePort \
--set controller.service.nodePorts.http=30080 \
--set controller.service.nodePorts.https=30443
kubectl get pod -n ingress-nginx
kubectl get svc -n ingress-nginx ingress-nginx-controller
kubectl get ingressclass

预期:Controller Pod Running,Service 是 NodePort 且 80:30080/TCP,ingressclass 有 nginx。

然后建一个 Ingress 但先不写 class:

Terminal window
cat <<'EOF' | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata: {name: web1, namespace: ingress-demo}
spec:
replicas: 1
selector: {matchLabels: {app: web1}}
template:
metadata: {labels: {app: web1}}
spec:
containers:
- name: c
image: hashicorp/http-echo:1.0
args: ["-text=hello-from-web1", "-listen=:8080"]
ports: [{containerPort: 8080}]
---
apiVersion: v1
kind: Service
metadata: {name: web1, namespace: ingress-demo}
spec:
selector: {app: web1}
ports: [{port: 80, targetPort: 8080}]
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata: {name: web1, namespace: ingress-demo}
spec:
rules:
- host: a.local
http:
paths:
- path: /
pathType: Prefix
backend: {service: {name: web1, port: {number: 80}}}
EOF
sleep 15
kubectl get ingress -n ingress-demo -o wide
curl -s -H "Host: a.local" http://192.168.10.51:30080/ ; echo

预期:ADDRESS 可能空着,curl 返回 404(因为没写 class,且 ingress-nginx 通常会被设为默认——如果它是默认,就能通)。

这说明了什么:Ingress 有没有生效,看 ADDRESS 和 curl,不看 kubectl get 的 EXISTS。


实验 2|Host 分流:同一个 IP 两个域名(10 min)#

Terminal window
# 再建一个 web2 和它的 Ingress
sed 's/web1/web2/g; s/hello-from-web1/hello-from-web2/; s/a\.local/b.local/' <<'EOF' | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata: {name: web1, namespace: ingress-demo}
spec:
replicas: 1
selector: {matchLabels: {app: web1}}
template:
metadata: {labels: {app: web1}}
spec:
containers:
- name: c
image: hashicorp/http-echo:1.0
args: ["-text=hello-from-web1", "-listen=:8080"]
ports: [{containerPort: 8080}]
---
apiVersion: v1
kind: Service
metadata: {name: web1, namespace: ingress-demo}
spec:
selector: {app: web1}
ports: [{port: 80, targetPort: 8080}]
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata: {name: web1, namespace: ingress-demo}
spec:
ingressClassName: nginx
rules:
- host: a.local
http:
paths:
- path: /
pathType: Prefix
backend: {service: {name: web1, port: {number: 80}}}
EOF
# 给 web1 补上 class
kubectl patch ingress web1 -n ingress-demo --type=merge -p '{"spec":{"ingressClassName":"nginx"}}'
sleep 10
curl -s -H "Host: a.local" http://192.168.10.51:30080/ ; echo
curl -s -H "Host: b.local" http://192.168.10.51:30080/ ; echo

预期:返回 hello-from-web1 和 hello-from-web2。

这说明了什么:Host 匹配发生在七层,同一个 IP 靠 Host 头分流。


实验 3|造 404 / 503 / 504(12 min,核心)#

Terminal window
# 404:改一个不存在的 host
curl -s -o /dev/null -w "%{http_code}\n" -H "Host: nope.local" http://192.168.10.51:30080/
# 503:把 Service 的 selector 改错
kubectl patch svc web2 -n ingress-demo -p '{"spec":{"selector":{"app":"nope"}}}'
sleep 5
curl -s -o /dev/null -w "%{http_code}\n" -H "Host: b.local" http://192.168.10.51:30080/
kubectl get endpointslices -l kubernetes.io/service-name=web2 -n ingress-demo
kubectl patch svc web2 -n ingress-demo -p '{"spec":{"selector":{"app":"web2"}}}'
# 504:把后端换成一个"接受连接但不响应"的
kubectl run blackhole -n ingress-demo --image=busybox:1.36 --restart=Never -- \
sh -c 'while true; do nc -l -p 8080; done' 2>/dev/null

预期:404 / 503 各一次;504 需要一点耐心(等 60 秒超时)。

这说明了什么:状态码就是层级。404 改规则、503 改端点、504 改超时或后端。


实验 4|TLS:自签证书 + 强制跳转(10 min)#

Terminal window
openssl req -x509 -nodes -days 1 -newkey rsa:2048 \
-keyout /tmp/tls.key -out /tmp/tls.crt \
-subj "/CN=a.local" 2>/dev/null
kubectl create secret tls a-local-tls -n ingress-demo --cert=/tmp/tls.crt --key=/tmp/tls.key
kubectl patch ingress web1 -n ingress-demo --type=merge -p '{
"spec":{"tls":[{"hosts":["a.local"],"secretName":"a-local-tls"}]}}'
sleep 10
curl -sk -o /dev/null -w "%{http_code}\n" --resolve a.local:30443:192.168.10.51 https://a.local:30443/

预期:200。

再看强制跳转:

Terminal window
kubectl annotate ingress web1 -n ingress-demo \
nginx.ingress.kubernetes.io/ssl-redirect=true --overwrite
sleep 8
curl -s -o /dev/null -w "%{http_code} -> %{redirect_url}\n" -H "Host: a.local" http://192.168.10.51:30080/

预期:308 -> https://a.local/...。

这说明了什么:TLS 在 Controller 终止,X-Forwarded-Proto 由它加。


实验 5|annotation 金丝雀(10 min)#

Terminal window
# web1-canary:同一个 host,不同后端
cat <<'EOF' | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata: {name: web1-canary, namespace: ingress-demo}
spec:
replicas: 1
selector: {matchLabels: {app: web1-canary}}
template:
metadata: {labels: {app: web1-canary}}
spec:
containers:
- name: c
image: hashicorp/http-echo:1.0
args: ["-text=hello-from-CANARY", "-listen=:8080"]
---
apiVersion: v1
kind: Service
metadata: {name: web1-canary, namespace: ingress-demo}
spec:
selector: {app: web1-canary}
ports: [{port: 80, targetPort: 8080}]
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web1-canary
namespace: ingress-demo
annotations:
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "50"
spec:
ingressClassName: nginx
rules:
- host: a.local
http:
paths:
- path: /
pathType: Prefix
backend: {service: {name: web1-canary, port: {number: 80}}}
EOF
sleep 10
for i in $(seq 1 10); do curl -s -H "Host: a.local" http://192.168.10.51:30080/; done | sort | uniq -c

预期:大约一半 web1 一半 CANARY(权重 50)。

这说明了什么:灰度完全可以在 Ingress 层做,不用改应用。


实验 6|对照一份 HTTPRoute(8 min,可选)#

Terminal window
kubectl get crd | grep gateway # 有没有装 Gateway API CRD
# 没装就跳过读代码部分,只看 yaml 结构
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata: {name: order, namespace: prod}
spec:
parentRefs:
- {name: my-gateway, namespace: gateway-system}
rules:
- matches:
- path: {type: PathPrefix, value: /api}
headers:
- {name: X-Canary, value: always}
backendRefs:
- {name: order-api, port: 80}

对比:matches 和 filters 是规范字段(有 schema 校验),而 Ingress 的 header 匹配只能靠 annotation(canary-by-header,写错不报错)。

这说明了什么:Gateway API 的核心不是”新语法”,是”把路由能力变成规范字段”。


收尾:写一条排障记录(5 min)#

现象:Ingress 404,Service/Pod 正常
第一条命令:kubectl get ingress -n prod -o wide
输出:CLASS 空,ADDRESS 空
结论:没有 Controller 认领(集群有两个 class,没默认)
修法:spec.ingressClassName: nginx

六个必撞的坑#

  1. NodePort 范围默认 30000–32767,写 80 会失败。
  2. helm install 到 ingress-nginx ns 时,-n 和 --create-namespace 都要写。
  3. 改完 Ingress 等 5–10 秒,Controller 要 reload 配置。
  4. curl 必须带 -H "Host: ...",否则打的是 IP 的默认后端。
  5. 自签证书的 CN 必须和 host 一致,否则 curl -k 能过但浏览器报警。
  6. canary 的两条 Ingress 不能写不同 class,必须是同一个 Controller。

今晚的一句话#

Ingress 是纸条,Controller 是执行的人;404 找纸条,503 找后端,504 找时间。

明天:给 apiserver 注册一种新资源,再写一个 20 行的控制器。


睡前(40 min)#

  1. 总题库 W4 Day 2 的 6 道问答(Q157–Q162)。
  2. 场景题 D23S1–D23S4:只看标题,说”看哪个状态码、第一条命令”。
  3. 把”六跳验证清单”抄进本子,下次出问题直接照着打勾。

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

W4-D23 Ingress 与 Gateway API:从公网域名到 Pod 的六跳(精讲 + 实操)
https://aiyw.top/posts/k8s-w4-d23/
作者
LiPeng
发布于
2025-11-12
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
LiPeng
运维工程师 · 可观测性 / 系统迁移 / 容器编排
公告
记录可观测体系、系统迁移、容器编排的实战复盘;「学习」里是 K8s 四周课程笔记。
分类
标签
站点统计
文章
31
分类
5
标签
58
总字数
59,760
运行时长
0 天
最后活动
0 天前
1
第一部分 白天精讲(上班阅读 70 min)
第 0 章 404 查了两小时,因为集群里有两个 Controller
第 1 章 Ingress 是规则,不是服务
第 2 章 六跳链路:每一跳都能单独验证
第 3 章 ingressClassName 与默认 class
第 4 章 pathType 三种:Prefix 的真实含义
第 5 章 TLS 终止在 Controller
第 6 章 annotation:Controller 的私有扩展
第 7 章 Gateway API:把”谁管什么”拆开
第 8 章 四个状态码,四层
第 9 章 故障表:Ingress 类十行
第 10 章 五层追问链:从”Ingress 404”往下问
第 11 章 生产现场:四件只有真配过 Ingress 才知道的事
第 12 章 十二条判断题
第 13 章 真实面试题 10 道(按现场口吻,出声答)
第 14 章 白话总结
附录 A 白话小词典
附录 B Ingress 排障 Runbook
附录 C 报错文本词典
附录 D 今天能带走的两份产出
今晚的 6 题(睡前 40 min,总题库 W4 Day 2:Q157–Q162)
第 15 章 场景题四道(给现象,你给排查顺序)
2
第二部分 晚上实操(60 min,只敲命令)
开工前的 60 秒
实验 1|装 Controller,验证”Ingress 本身不监听端口”(12 min)
实验 2|Host 分流:同一个 IP 两个域名(10 min)
实验 3|造 404 / 503 / 504(12 min,核心)
实验 4|TLS:自签证书 + 强制跳转(10 min)
实验 5|annotation 金丝雀(10 min)
实验 6|对照一份 HTTPRoute(8 min,可选)
收尾:写一条排障记录(5 min)
六个必撞的坑
今晚的一句话
3
睡前(40 min)
文章目录
1
第一部分 白天精讲(上班阅读 70 min)
第 0 章 404 查了两小时,因为集群里有两个 Controller
第 1 章 Ingress 是规则,不是服务
第 2 章 六跳链路:每一跳都能单独验证
第 3 章 ingressClassName 与默认 class
第 4 章 pathType 三种:Prefix 的真实含义
第 5 章 TLS 终止在 Controller
第 6 章 annotation:Controller 的私有扩展
第 7 章 Gateway API:把”谁管什么”拆开
第 8 章 四个状态码,四层
第 9 章 故障表:Ingress 类十行
第 10 章 五层追问链:从”Ingress 404”往下问
第 11 章 生产现场:四件只有真配过 Ingress 才知道的事
第 12 章 十二条判断题
第 13 章 真实面试题 10 道(按现场口吻,出声答)
第 14 章 白话总结
附录 A 白话小词典
附录 B Ingress 排障 Runbook
附录 C 报错文本词典
附录 D 今天能带走的两份产出
今晚的 6 题(睡前 40 min,总题库 W4 Day 2:Q157–Q162)
第 15 章 场景题四道(给现象,你给排查顺序)
2
第二部分 晚上实操(60 min,只敲命令)
开工前的 60 秒
实验 1|装 Controller,验证”Ingress 本身不监听端口”(12 min)
实验 2|Host 分流:同一个 IP 两个域名(10 min)
实验 3|造 404 / 503 / 504(12 min,核心)
实验 4|TLS:自签证书 + 强制跳转(10 min)
实验 5|annotation 金丝雀(10 min)
实验 6|对照一份 HTTPRoute(8 min,可选)
收尾:写一条排障记录(5 min)
六个必撞的坑
今晚的一句话
3
睡前(40 min)