登录
推荐 文章 Go 技术 课程 下载 专题 AI
首页 >  科技周边 >  业界新闻

Kubernetes Gateway API HTTPRoute 怎样用 parentRefs 绑定 Gateway

来源:17golang原创

时间:2026-11-20 10:14:11 314浏览 收藏

我第一次把 Gateway API 的路由拆到不同命名空间时,最容易混淆的不是匹配规则,而是“这条 HTTPRoute 到底挂到了哪个 Gateway”。答案集中在 spec.parentRefs:它声明 Route 想绑定的父资源;同命名空间时通常只写 Gateway 名称,跨命名空间时还要写目标命名空间,并且 Gateway 的 Listener 必须允许这类 Route 接入。

官方地址:https://gateway-api.sigs.k8s.io/guides/user-guides/http-routing/

最小绑定看 HTTPRoute 的 parentRefs 是否指向正确的 Gateway;共享 Gateway 时,还要同时满足 Route 一侧的引用和 Gateway 一侧的 allowedRoutes 约束。只写了 parentRefs,不代表一定会被接受。

parentRefs 到底绑定了什么

HTTPRoute 描述请求如何匹配并转发,Gateway 提供监听入口,parentRefs 则把两者关联起来。默认情况下,Route 和 Gateway 在同一个 Namespace,name 就足够表达目标。

如果 Gateway 有多个 Listener,可以在 parentRef 中用 sectionName 指定某个 Listener;用 port 则是按端口选择监听器。我的经验是:监听器名称稳定时优先用 sectionName,因为管理员以后调整端口,不必跟着改 Route。

HTTPRoute parentRefs Gateway Listener Service 的静态关系说明图
图1:HTTPRoute.parentRefs 的结构说明图,查看路由对象、网关监听器和后端服务之间的静态关系;这不是运行截图。

同命名空间的最小写法

下面把 Gateway 和 HTTPRoute 都放在 edge Namespace。注释只解释关键字段,示例没有绑定具体实现厂商,读者可以替换自己的 GatewayClass 和 Service。

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: web-gateway
  namespace: edge
spec:
  gatewayClassName: example-class # 由集群中的 GatewayClass 实现接管
  listeners:
  - name: http
    protocol: HTTP
    port: 80
    allowedRoutes:
      namespaces:
        from: Same # 默认只接收同一 Namespace 的 Route
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: web-route
  namespace: edge
spec:
  parentRefs:
  - name: web-gateway # 省略 namespace,目标在当前 Namespace
    sectionName: http # 按 Listener 名称绑定,避免依赖端口变化
  rules:
  - matches:
    - path:
        type: PathPrefix # 让 /docs 及其子路径进入这条规则
        value: /docs
    backendRefs:
    - name: docs-service # 将匹配请求转给同 Namespace 的 Service
      port: 8080

这里的 parentRefs 没有写 namespace,含义不是“任意 Gateway”,而是当前 Route Namespace 下名为 web-gateway 的 Gateway。若不指定 sectionName,实现会根据监听器和其它匹配条件判断是否能接受;指定后意图更清楚。

跨命名空间共享 Gateway 时,谁有决定权

当应用团队在 site-ns 发布 Route、基础设施团队在 infra-ns 管理 Gateway,Route 要在 parentRef 里明确写 namespace: infra-ns。与此同时,Gateway Listener 通过 allowedRoutes.namespaces 决定哪些 Namespace 可以接入。两边缺一不可,这是一种双向握手。

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: shared-gateway
  namespace: infra-ns
spec:
  gatewayClassName: shared-class # 基础设施侧选择 Gateway 实现
  listeners:
  - name: https
    protocol: HTTPS
    port: 443
    allowedRoutes:
      namespaces:
        from: Selector # 只接收带有指定标签的 Namespace
        selector:
          matchLabels:
            gateway-access: "true"
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: shop-route
  namespace: site-ns
spec:
  parentRefs:
  - name: shared-gateway
    namespace: infra-ns # 跨 Namespace 指向 Gateway
    sectionName: https # 只申请接入 HTTPS Listener
  rules:
  - matches:
    - path:
        type: PathPrefix # 只声明 /shop 这一段路由
        value: /shop
    backendRefs:
    - name: shop-service # 后端仍按集群资源权限单独管理
      port: 8080

上例中的 site-ns 还需要满足 Gateway 的 Namespace selector。若只把 parentRefs.namespace 写对、却没有匹配标签,Route 仍可能无法附着。反过来,Gateway 放宽了接入范围,也不等于 Route 会自动选择它,因为 Route 仍然必须明确引用目标 Gateway。

跨命名空间 HTTPRoute parentRefs namespace 与 Gateway allowedRoutes 的边界说明图
图2:跨命名空间 Route Attachment 的边界说明图,查看 parentRefs.namespace 与 allowedRoutes 如何共同决定 Accepted 状态;这不是运行截图。

绑定失败时先看哪一个状态

我排查这类问题时,先看 Route 的 status.parents,再回到 Gateway 的 Listener 配置。重点不是“YAML 能不能提交”,而是父资源是否报告了接受状态。

status:
  parents:
  - parentRef:
      name: shared-gateway # 对应 spec.parentRefs 中的目标
      namespace: infra-ns # 对应跨 Namespace 目标
    conditions:
    - type: Accepted
      status: "True" # True 才表示该父资源接受了 Route

如果 Accepted 不是 True,按这个顺序检查:Gateway 名称与 Namespace 是否正确;sectionName 是否真的存在;Listener 的 allowedRoutes 是否允许 Route 所在 Namespace;最后再看匹配的 hostname、路径和后端 Service。不要把“Route 已创建”当成“Route 已生效”。

几个容易混在一起的问题

  • parentRefs 和 backendRefs 不同:前者选择父 Gateway,后者选择匹配请求的后端 API 对象。
  • 省略 namespace 不等于跨命名空间:它通常表示使用 Route 自己所在的 Namespace。
  • sectionName 和 port 是选择方式:名称适合稳定绑定某个 Listener,端口适合明确声明端口级关系,但会受端口调整影响。

如果只需要单团队、单 Namespace 的入口,先用最小写法;如果要让多个团队共享同一个地址和证书,再引入跨命名空间的 parentRefs.namespace 与 Listener 约束。这个选择比一开始堆更多匹配条件更容易维护。

相关问题

HTTPRoute 不写 parentRefs 会怎样?

它没有声明想附着的父资源,通常不会获得 Gateway 的路由入口;应先确认目标 Gateway 名称和 Namespace,再补充 parentRefs。

为什么 parentRefs 写对了仍没有流量?

先区分“未被 Gateway 接受”和“已接受但规则未匹配”。前者看 status.parents 的条件,后者再检查 Listener、hostname、path 和 backendRefs。

声明:本文转载于:17golang原创 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>