Lesson 34 of 70 · 8 min read · Updated
HTTPS with Gateway API and cert-manager: Automatic Certificates and HTTP to HTTPS Redirects
On this page
So far, the PedalShare Gateway only spoke plain HTTP. In this lesson, the platform team adds an HTTPS listener, cert-manager issues and renews its certificate from an annotation on the Gateway, and the stations API moves to HTTPS with a redirect for old HTTP clients. The data plane is still NGINX Gateway Fabric.
Technical Requirements
- The
gateway-labcluster with NGINX Gateway Fabric 2.8.1 and thepublicGateway (with itsNginxProxy) from the earlier lessons - Helm, curl and openssl
- Sample repository: Fcakiroglu16/dotnetacademy, branch gateway-api-https-cert-manager
| Component | Version |
|---|---|
| cert-manager | 1.21.3 |
| NGINX Gateway Fabric | 2.8.1 |
| Gateway API | 1.6.3, Standard channel |
A kind cluster cannot get a public certificate, so the lab uses a private CA created by cert-manager. Only the issuer changes for production. The Gateway side stays the same with Let's Encrypt.
Core Concepts
TLS on a listener
An HTTPS listener terminates TLS at the Gateway. Its tls block has:
mode: Terminate(the default for HTTPS): the Gateway decrypts and routes HTTP.Passthroughis only forTLSlisteners withTLSRoute.certificateRefs: one or morekubernetes.io/tlsSecrets. By default, the Secret must be in the Gateway's namespace. A Secret in another namespace needs aReferenceGrant.
The listener's hostname limits which routes can attach and which SNI names the data plane serves. Routes choose a listener with sectionName.
cert-manager's Gateway shim
With enableGatewayAPI: true, cert-manager watches Gateways that have a cert-manager.io/issuer or cert-manager.io/cluster-issuer annotation. For each listener with TLS, it creates a Certificate:
| From the listener | In the Certificate |
|---|---|
hostname | dnsNames |
tls.certificateRefs[0].name | secretName (in the Gateway's namespace) |
| The Gateway annotation | issuerRef |
The Certificate is owned by the Gateway, so deleting the Gateway also removes it. cert-manager renews the certificate before it expires (by default when two thirds of its lifetime have passed), and NGF picks up the new Secret.
Listener status for TLS
If the Secret is missing or invalid, the listener reports it. Other listeners on the same Gateway keep working:
| Listener condition | Reason |
|---|---|
ResolvedRefs=False | InvalidCertificateRef (Secret missing or wrong type) |
ResolvedRefs=False | RefNotPermitted (Secret in another namespace without a ReferenceGrant) |
Programmed=False | Invalid |
Hands-On: Kubernetes Commands
Install cert-manager with the Gateway API integration turned on:
k8s/05-https/cert-manager-values.yaml
Commands for checking certificates:
Step-by-Step Example
Step 1: A private CA
A self-signed issuer creates a root CA certificate, and a CA issuer signs leaf certificates with it:
k8s/05-https/01-issuers.yaml
Step 2: Add the HTTPS listener
The Gateway keeps its http listener and gets an https listener for the same wildcard hostname. The Secret pedalshare-wildcard-tls does not exist yet. The annotation tells cert-manager to create it.
k8s/05-https/02-gateway-https.yaml
A few seconds later, both listeners are programmed, and NGF has added port 443 to the public-nginx Service:
Step 3: Move the stations API to HTTPS
The stations route now attaches to the https listener. A second route on the http listener answers the same host with a 301 to HTTPS:
k8s/05-https/03-routes.yaml
Forward both ports and get the CA certificate so curl can verify the chain. --resolve makes curl send the right SNI name to the local port:
Without --cacert, curl stops with exit code 60 because it does not trust the lab CA. The redirect has no port because it points to the real HTTPS port 443. Behind a load balancer, that is the port clients use.
The served certificate is the wildcard one from cert-manager:
The rides routes are still attached only to http. Over HTTPS, NGF refuses the TLS handshake for that SNI name instead of serving a 404:
Step 4: Renewal and a broken reference
If the Secret disappears, cert-manager issues it again, because the Certificate still exists:
A Gateway that references a Secret no one creates (no annotation, no Secret) shows the problem on the listener:
kubectl port-forward stays connected to one Pod. When NGF replaces the data plane Pods (for example after a configuration change), start the port-forward again.
Step 5: Production issuer
For a real domain, replace the CA issuer with an ACME issuer. cert-manager can solve HTTP-01 challenges through the same Gateway by creating a temporary HTTPRoute on the HTTP listener:
k8s/05-https/letsencrypt-issuer.example.yaml
HTTP-01 cannot issue wildcard certificates, so with this solver use one listener per hostname (or a DNS-01 solver for *.pedalshare.io).
Results
| Check | Result |
|---|---|
cert-manager 1.21.3 with enableGatewayAPI | gateway-shim controller started |
Gateway annotation cert-manager.io/cluster-issuer | Certificate pedalshare-wildcard-tls for *.pedalshare.local, owned by the Gateway |
https listener | Programmed=True, port 443 added to public-nginx |
https://stations.pedalshare.local with the lab CA | 200 from stations-api |
http://stations.pedalshare.local | 301 to https:// |
| Client without the CA | curl exit code 60 |
| HTTPS request for a host with no HTTPS route | TLS alert unrecognized name |
| Deleted TLS Secret | Reissued by cert-manager |
| Listener with a missing Secret | ResolvedRefs=False InvalidCertificateRef, Programmed=False |
Summary
- HTTPS is a listener with
tls.mode: TerminateandcertificateRefsto a TLS Secret in the Gateway's namespace. - With
enableGatewayAPI, one annotation on the Gateway makes cert-manager create, own and renew a Certificate for each TLS listener. - Routes pick a listener with
sectionName. A redirect route on the HTTP listener sends old clients to HTTPS. - Certificate problems show up on the listener's conditions, and the other listeners keep serving.
The next lesson uses backendRefs weights to run canary and blue/green releases of the PedalShare pricing API.