UDP routing
Gateway API is designed to work with multiple protocols and UDPRoute is one such route which allows for managing UDP traffic.
In this example, we have one Gateway resource and two UDPRoute resources that distribute the traffic with the following rules:
- All UDP datagrams on port 8080 of the Gateway are forwarded to port 6000 of
my-foo-serviceKubernetes Service. - All UDP datagrams on port 8090 of the Gateway are forwarded to port 6000 of
my-bar-serviceKubernetes Service.
In this example two UDP listeners will be applied to the Gateway
in order to route traffic to two separate backend UDPRoutes, note that the
protocol set for the listeners on the Gateway is UDP:
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: my-udp-gateway
spec:
gatewayClassName: my-udp-gateway-class
listeners:
- name: foo
protocol: UDP
port: 8080
allowedRoutes:
kinds:
- kind: UDPRoute
- name: bar
protocol: UDP
port: 8090
allowedRoutes:
kinds:
- kind: UDPRoute
---
apiVersion: gateway.networking.k8s.io/v1
kind: UDPRoute
metadata:
name: udp-app-1
spec:
parentRefs:
- name: my-udp-gateway
sectionName: foo
rules:
- backendRefs:
- name: my-foo-service
port: 6000
---
apiVersion: gateway.networking.k8s.io/v1
kind: UDPRoute
metadata:
name: udp-app-2
spec:
parentRefs:
- name: my-udp-gateway
sectionName: bar
rules:
- backendRefs:
- name: my-bar-service
port: 6000
In the above example we separate the traffic for the two separate backend UDP
Services by using the sectionName field in the parentRefs to refer to a specific named listener:
spec:
parentRefs:
- name: my-udp-gateway
sectionName: foo
This corresponds directly with the name in the listeners in the Gateway:
listeners:
- name: foo
protocol: UDP
port: 8080
- name: bar
protocol: UDP
port: 8090
In this way each UDPRoute “attaches” itself to a different port on the
Gateway so that the service my-foo-service receives traffic for port 8080
and my-bar-service receives traffic for port 8090.
Note that you can achieve this same result by binding the Routes to the Gateway
listeners using the port field in the parentRefs:
spec:
parentRefs:
- name: my-udp-gateway
port: 8080
Using the port field instead of sectionName for the attachment has the
downside of more tightly coupling the relationship between the Gateway and
its associated Routes. Refer to Attaching to Gateways for more
details.
Note
UDPRoute to an HTTP or HTTPS listener. A UDPRoute
can only attach to listeners using the UDP protocol. Attempting to attach a
UDPRoute to an HTTP or HTTPS listener will result in the route not being
accepted.