Skip to main content
In this lesson you will learn how to terminate TLS at a Gateway using a Kubernetes Secret, configure an HTTP→HTTPS redirect, and allow cross-namespace Secret references using a ReferenceGrant. We use a self-signed certificate for the lab so you can test HTTPS locally.
A presentation slide titled "Configure TLS with Auto-Redirect" with a large blue teardrop-shaped area on the right containing the word "Demo." The slide has a clean white background and a small copyright notice for KodeKloud.

Environment status

At the start of the lab only the coffee application and its Service are running — no Gateways or HTTPRoutes are present.

What we’ll deploy

  • A Gateway (in default) with:
    • HTTP listener on port 80
    • HTTPS listener on port 443 with TLS termination using a Secret
  • Two HTTPRoutes:
    • One on the HTTP listener that redirects requests to HTTPS (301)
    • One on the HTTPS listener that forwards /coffee to the coffee Service
  • A certificate namespace with a TLS Secret cafe-secret (self-signed)
  • A ReferenceGrant that allows the Gateway in default to reference the Secret in certificate
Files in the working directory:

Quick reference — manifests and purpose

Relevant docs:

Gateway manifest (gateway.yaml)

This Gateway uses the nginx GatewayClass and terminates TLS using a Secret in the certificate namespace.

Notes on TLS mode

  • Terminate means TLS is terminated at the Gateway; traffic to backend pods will be plain HTTP.
  • Because the Secret is in a different namespace (certificate) than the Gateway (default), a ReferenceGrant is required to allow cross-namespace references.

Namespace and Secret (ns.yaml & cert.yaml)

  • ns.yaml creates the certificate namespace.
  • cert.yaml contains the kube TLS Secret cafe-secret with base64-encoded tls.crt and tls.key (self-signed for lab/testing).
Example namespace:
Example Secret (cert.yaml excerpt):
Apply namespace and Secret:

ReferenceGrant (ref-grant.yaml)

A ReferenceGrant in the certificate namespace grants permission for Gateways in default to use the Secret.
Apply the ReferenceGrant:

Create the Gateway

HTTPRoutes (routes.yaml)

We create two HTTPRoute resources:
  1. cafe-tls-redirect — attached to the Gateway’s HTTP listener; issues a 301 redirect to HTTPS.
  2. coffee — attached to the Gateway’s HTTPS listener; matches path prefix /coffee and forwards requests to the coffee Service on port 80.
Apply the routes:

Verify resources

Check the Secret, ReferenceGrant, and HTTPRoutes:

Testing behavior with curl

In this lab the Gateway ports are exposed on localhost using mapped ports (example: 8080 for HTTP and 8443 for HTTPS). We use --resolve so cafe.example.com resolves to 127.0.0.1 at the chosen port.
  1. Access HTTP (expect a 301 redirect to HTTPS)
  1. Access HTTPS (Gateway terminates TLS; the cert is self-signed so --insecure is used here)
The HTTPS curl uses --insecure because this lab uses a self-signed certificate. In production, use certificates signed by a trusted CA (for example via Let’s Encrypt or an internal PKI) and proper DNS; you won’t need --resolve or --insecure.

Summary

  • The Gateway is configured to terminate TLS with a Secret stored in a separate namespace.
  • A ReferenceGrant allows the Gateway to reference that cross-namespace Secret.
  • An HTTPRoute on the HTTP listener performs a 301 redirect to HTTPS.
  • The HTTPS listener forwards /coffee to the coffee backend Service.
  • For production deployments use a trusted certificate and real DNS so clients can validate TLS without bypassing checks.

Watch Video

Practice Lab