Sub topic 13
Terminating TLS on an HTTPS listener
This chapter assumes you can write a Gateway with an HTTP listener, know what a Kubernetes Secret is, and understand how HTTPRoutes attach to listeners through parentRefs. You should also know the basics of TLS certificates: a certificate, its private key, and the hostname it covers.
What termination means
TLS termination means the Gateway decrypts incoming HTTPS traffic. It holds the certificate and private key, completes the handshake with the client, and then forwards the request to a backend Service. The hop from the Gateway to the Pod is plain HTTP unless you configure something else.
You configure termination on a listener, not on a route. This puts certificate handling with the cluster operator who owns the Gateway. Application developers only write routes and never touch the key material.
Configuring the listener
An HTTPS listener needs a protocol of HTTPS, a port, and a tls block.
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: secure-gateway
namespace: infra
spec:
gatewayClassName: example-class
listeners:
- name: https
protocol: HTTPS
port: 443
hostname: shop.example.com
tls:
mode: Terminate
certificateRefs:
- kind: Secret
name: shop-certThe fields work as follows:
protocol: HTTPStells the controller to expect TLS-wrapped HTTP.hostnamelimits the listener to one name. The certificate should cover that name.tls.mode: Terminatedecrypts at the Gateway. The other standard mode isPassthrough, which forwards encrypted bytes untouched and is used with TLSRoute.certificateRefslists the Secrets that hold the certificate. By default the Secret must be in the same namespace as the Gateway.
The certificate Secret
The referenced Secret must be of type kubernetes.io/tls. It carries the certificate chain in tls.crt and the private key in tls.key. You can create one with kubectl:
kubectl create secret tls shop-cert \
--cert=shop.crt --key=shop.key -n infraMany teams let cert-manager create and renew this Secret instead. The Gateway only needs the Secret to exist with valid contents.
Attaching routes
Routes attach to an HTTPS listener the same way as to an HTTP one. A route's parentRefs can name the listener with sectionName: https. The hostnames on the route must overlap with the listener's hostname, or the route will not be accepted.
Common errors
- Secret in another namespace. If the Secret is not in the Gateway's namespace, the listener reports an unresolved reference. A ReferenceGrant in the Secret's namespace must allow it. The next chapter covers that.
- Wrong Secret type or missing keys. The listener is marked invalid when
tls.crtortls.keyis absent or unparseable. - Hostname mismatch. A certificate that does not cover the listener hostname causes browser warnings, even though the Gateway reports healthy.
- No port 80 redirect. An HTTPS listener does not answer plain HTTP. Add an HTTP listener with a route that uses a
RequestRedirectfilter if you want that.
Exercise
A Gateway in namespace infra has an HTTPS listener whose certificateRefs names a Secret that lives in namespace certs. The listener reports ResolvedRefs as False. What is the likely cause, and what resource fixes it?
Show answer
By default a certificateRef must point to a Secret in the Gateway's own namespace. Because the Secret is in certs, the reference is refused. Create a ReferenceGrant in the certs namespace that allows Gateways from the infra namespace to reference Secrets. Alternatively, move the Secret into infra.
Exercise
What is the difference between tls.mode Terminate and Passthrough?
Show answer
Terminate decrypts traffic at the Gateway using a certificate you supply, so the Gateway can inspect and route on HTTP details. Passthrough forwards the encrypted traffic unchanged to the backend, so the Gateway can route only on the TLS server name and the backend holds the certificate.