Skip to content

TLS 路由

原文:https://gateway-api.sigs.k8s.io/guides/user-guides/tls-routing/

TLSRoute 资源 让你可以基于 TLS 元数据进行匹配,并把请求转发到 Kubernetes 后端。本指南展示 TLSRoute 如何基于主机名对流量进行匹配,并把它转发到不同的 Kubernetes Service —— 既可以在 Gateway 上使用 Passthrough(透传)模式,也可以使用 Terminate(终止)模式。

要接收来自 Gateway 的流量,TLSRoute 资源必须配置 ParentRefs,指明它应该被附加到哪些父 Gateway 上。下面的示例展示了如何把 Gateway 与 TLSRoute 组合起来,在 Gateway 实现支持的前提下,同时通过 PassthroughTerminate 模式为 TLS 流量提供服务:

yaml
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: example-gateway
spec:
  gatewayClassName: example-gateway-class
  listeners:
  - name: tls
    protocol: TLS
    port: 443
    tls:
      mode: Passthrough
  - name: tls-terminate
    protocol: TLS
    port: 8443
    tls:
      mode: Terminate
      certificateRefs:
      - name: tls-terminate-certificate

一个 TLSRoute 可以匹配一组 hostnames。关于主机名与 Listener 之间的交集以及路由行为,详见 Hostnames in Gateway API。由于 foo.example.combar.example.com 是主机名不同、路由需求不同的两个主机,所以它们各自被部署为独立的 TLSRoute —— foo-routebar-route

下面的 foo-route TLSRoute 会匹配 foo.example.com 的所有流量,并应用其路由规则把流量转发到所配置的后端。由于它被附加到的是 Passthrough 模式的 listener,Gateway 会把加密的 TCP 流直接转发给后端:

yaml
apiVersion: gateway.networking.k8s.io/v1
kind: TLSRoute
metadata:
  name: foo-route
spec:
  parentRefs:
  - name: example-gateway
    sectionName: tls
  hostnames:
  - "foo.example.com"
  rules:
  - backendRefs:
    - name: foo-svc
      port: 443

类似地,bar-route TLSRoute 匹配 bar.example.com 的流量。但是,由于它被附加到的是 Terminate 模式的 listener,Gateway 会用 listener 上指定的证书终止 TLS 流,把解密后的 TCP 流转发给后端:

yaml
apiVersion: gateway.networking.k8s.io/v1
kind: TLSRoute
metadata:
  name: bar-route
spec:
  parentRefs:
  - name: example-gateway
    sectionName: tls-terminate
  hostnames:
  - "bar.example.com"
  rules:
  - backendRefs:
    - name: bar-svc
      port: 8080

基于 MIT 协议发布