Sub topic 5
Listeners and TLS termination
This chapter assumes you know what an Ingress tls block does and how a Kubernetes Secret of type kubernetes.io/tls is structured. It covers how the Gateway API moves TLS configuration from the Ingress into the Gateway's listeners.
What a listener is
A listener is a single entry point on a Gateway. Each listener has a name, a protocol, a port, and optionally a hostname. In an Ingress, ports 80 and 443 were implied by the controller. In the Gateway API you declare them, so the platform team can see every port the Gateway exposes.
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: shared-gateway
namespace: gateway-system
spec:
gatewayClassName: example-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All
- name: https
protocol: HTTPS
port: 443
hostname: app.example.com
tls:
mode: Terminate
certificateRefs:
- name: app-example-com-tls
allowedRoutes:
namespaces:
from: AllReplace example-class with the class your chosen implementation provides.
Mapping from Ingress
| Ingress | Gateway |
|---|---|
| Implicit port 80 | Listener with `protocol: HTTP`, `port: 80` |
| `spec.tls[].hosts` | Listener `hostname` |
| `spec.tls[].secretName` | `tls.certificateRefs[].name` |
| TLS handled by the controller | `tls.mode: Terminate` |
Listener names must be unique within a Gateway. Routes use the name later through sectionName.
Terminate and Passthrough
mode: Terminate decrypts traffic at the Gateway, which matches what Ingress NGINX did by default. mode: Passthrough forwards encrypted bytes to the backend untouched. Passthrough requires protocol TLS rather than HTTPS, and it is routed with TLSRoute instead of HTTPRoute, so HTTP rules cannot apply to it.
Where the certificate lives
The Secret named in certificateRefs is looked up in the Gateway's own namespace. An Ingress read its Secret from the Ingress's namespace, which was usually the application's. After migration you have two choices:
- Copy or issue the certificate into
gateway-system. For cert-manager, point the Certificate resource at that namespace. - Keep the Secret where it is and add a ReferenceGrant. A later chapter covers this.
Common errors
- Hostname mismatch. A route whose hostnames do not intersect the listener's
hostnameis not accepted by that listener. - Missing
hostnameon a shared listener. Without it, the listener matches any host and presents one certificate for all of them. Use one listener per certificate and sethostnameon each. - Wrong Secret type. The Secret must be
kubernetes.io/tlswithtls.crtandtls.key. - Expecting the redirect here. The listener does not redirect HTTP to HTTPS. In Ingress NGINX the
ssl-redirectannotation did that. Here a route on the HTTP listener does it, which a later chapter covers.
Exercise
An Ingress has tls with hosts: [shop.example.com] and secretName: shop-tls in namespace shop. The Gateway is in gateway-system. Name the listener fields you set, and say where shop-tls must exist if you add no ReferenceGrant.
Show answer
Set protocol: HTTPS, port: 443, hostname: shop.example.com, tls.mode: Terminate, and tls.certificateRefs with the name shop-tls. Without a ReferenceGrant the Secret must exist in gateway-system, the Gateway's namespace.