TCP路由

信息 "实验频道"

The `TCPRoute` resource described below is currently only included in the
"Experimental" channel of Gateway API. For more information on release
channels, refer to the [related documentation](/concepts/versioning).

gateway API 设计用于与多种协议协同工作,TCPRoute 就是这样一种路由,它允许管理 TCP 流量。

在本例中,我们有一个 gateway 资源和两个 TCPRoute 资源,它们按照以下规则分配流量:

  • gateway 8080 端口上的所有 TCP 流都会转发到 my-foo-service Kubernetes 服务的 6000 端口。
  • gateway 8090 端口上的所有 TCP 流都会转发到 "my-bar-service "Kubernetes 服务的 6000 端口。

在此示例中,将在 gateway 上应用两个 TCP 监听器,以便将它们路由到两个独立的后端 TCPRoutes,请注意,在 Gateway 上为 监听器 设置的 protocolTCP

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: my-tcp-gateway
spec:
  gatewayClassName: my-tcp-gateway-class
  listeners:
  - name: foo
    protocol: TCP
    port: 8080
    allowedRoutes:
      kinds:
      - kind: TCPRoute
  - name: bar
    protocol: TCP
    port: 8090
    allowedRoutes:
      kinds:
      - kind: TCPRoute
---
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: TCPRoute
metadata:
  name: tcp-app-1
spec:
  parentRefs:
  - name: my-tcp-gateway
    sectionName: foo
  rules:
  - backendRefs:
    - name: my-foo-service
      port: 6000
---
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: TCPRoute
metadata:
  name: tcp-app-2
spec:
  parentRefs:
  - name: my-tcp-gateway
    sectionName: bar
  rules:
  - backendRefs:
    - name: my-bar-service
      port: 6000

在上例中,我们通过被引用parentRefs中的sectionName字段,将两个独立后端 TCP Services 的流量分开:

spec:
  parentRefs:
  - name: my-tcp-gateway
    sectionName: foo

这与 Gatewaylisteners 中的 name 直接对应:

listeners:
  - name: foo
    protocol: TCP
    port: 8080
  - name: bar
    protocol: TCP
    port: 8090

这样,每个 "TCPRoute "都会 "连接 "到 "gateway "上的不同端口,这样 "my-foo-service "服务就会接收来自集群外 "8080 "端口的流量,而 "my-bar-service "则接收 "8090 "端口的流量。