Sub topic 5
Listeners: ports, protocols, and hostnames
This chapter assumes you can read Kubernetes YAML, know what a Service is, and understand HTTP hosts and paths. It also assumes you have seen a GatewayClass and a Gateway from the earlier chapters.
What a listener is
A listener is one entry in a Gateway's spec.listeners list. Each listener describes one way traffic can enter: a port, a protocol, and optionally a hostname. The Gateway is the object that asks for a traffic entry point. The listeners say exactly which doors that entry point opens.
Here is the Gateway from the topic plan, with one listener:
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: web-gateway
namespace: infra
spec:
gatewayClassName: example-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: Selector
selector:
matchLabels:
shared-gateway-access: "true"The fields of a listener
| Field | Meaning |
|---|---|
| `name` | A label for the listener. It must be unique within the Gateway. Routes can target it by name. |
| `port` | The port the Gateway accepts traffic on. |
| `protocol` | What the listener speaks, for example `HTTP`, `HTTPS`, `TLS`, `TCP`, or `UDP`. |
| `hostname` | Optional. Limits the listener to requests for this host. |
| `tls` | TLS settings, used by `HTTPS` and `TLS` listeners. |
| `allowedRoutes` | Which routes may attach to this listener. |
The protocol decides which route kinds can attach. An HTTP or HTTPS listener accepts HTTPRoute. Other route kinds, covered in a later chapter, need other protocols.
Hostnames on listeners
If you omit hostname, the listener accepts any host. If you set it, the listener only serves matching requests. A hostname can start with a wildcard label, such as *.example.com.
A listener hostname and a route hostname both apply. A route that lists shop.example.com can attach to a listener with hostname: "*.example.com". The effective hostnames for the route are the ones that satisfy both. If the two do not overlap at all, the route does not bind to that listener.
Multiple listeners on one Gateway
A Gateway can have several listeners. This one serves plain HTTP on port 80 and HTTPS on port 443:
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: multi-gateway
namespace: infra
spec:
gatewayClassName: example-class
listeners:
- name: http
protocol: HTTP
port: 80
hostname: shop.example.com
- name: https
protocol: HTTPS
port: 443
hostname: shop.example.com
tls:
mode: Terminate
certificateRefs:
- kind: Secret
name: shop-certTwo listeners may share a port only if they differ in hostname, and they must use compatible protocols. If two listeners on the same port have the same protocol and the same hostname, the Gateway reports a conflict in its status, and the controller does not accept the conflicting listener.
Common errors
- Duplicate listener names. Names must be unique inside one Gateway. The API server rejects the manifest.
- Conflicting listeners. Same port, protocol, and hostname on two listeners. The status shows a
Conflictedcondition. - Protocol and route mismatch. An HTTPRoute cannot attach to a
TCPlistener. The route status reports that it was not accepted. - Hostname mismatch. The route's hostnames do not overlap the listener's hostname, so no traffic reaches the route.
- Missing certificate. An
HTTPSlistener without a validcertificateRefsentry is not ready.
Check how the controller reads each listener in the Gateway status:
kubectl get gateway -n infra multi-gateway -o yamlLook under status.listeners. Each listener has its own conditions and a count of attached routes.
Exercise
A Gateway has a listener with hostname *.example.com. An HTTPRoute attached to it lists only the hostname api.other.org. Will the route receive traffic through that listener, and why?
Show answer
No. The route hostname and the listener hostname do not overlap, so the route does not bind to that listener. No requests for api.other.org match the listener, and the route's parent status shows it was not accepted.
Exercise
You define two listeners on port 80, both with protocol HTTP and no hostname. What happens?
Show answer
They conflict. Two listeners with the same port, protocol, and hostname are not allowed together. The Gateway reports a conflict in its listener status, and the controller does not accept the conflicting listener. To fix it, give them different hostnames or remove one.