Version note: examples use the Kubernetes v1.35 documentation snapshot. Check kind and controller compatibility. No live cluster or TLS deployment was executed for these articles.
What this guide covers
Gateway API connects incoming web requests to a Kubernetes Service. This guide adds HTTPS using a certificate stored in a TLS Secret, then routes /api requests to a web application.
Before you start
You need a working web-app Service in namespace web-demo, serving port 80 and the /api/message path. The local kind guide creates that example.
Install a Gateway API controller and its required custom resource definitions using that controller’s instructions first. Kubernetes does not install one automatically. You also need an accepted GatewayClass, a reachable gateway address, a real DNS name and a trusted certificate/private-key pair. A local kind cluster may need additional port or load-balancer configuration for external access.
Step 1: Check the Service and controller
Choose the correct cluster context and inspect the prerequisites:
read -r -p 'Intended kubectl context: ' KUBE_CONTEXT
export KUBE_CONTEXT
kubectl --context "$KUBE_CONTEXT" get gatewayclasses
kubectl --context "$KUBE_CONTEXT" -n web-demo get service web-app
kubectl --context "$KUBE_CONTEXT" -n web-demo get endpointslices -l kubernetes.io/service-name=web-app
The Service needs ready backend addresses. If using a remote cluster, publish the application image to a registry and update the Deployment; the kind-only local image cannot be pulled there.
Step 2: Create the certificate Secret
Use OpenSSL with -checkhost support. The certificate hostname must match the real hostname you will put in the YAML. Run:
set -euo pipefail
: "${KUBE_CONTEXT:?Select the intended cluster context}"
read -r -p 'Certificate full-chain file: ' TLS_CERT
read -r -p 'Protected unencrypted PEM key file: ' TLS_KEY
read -r -p 'Real web application hostname: ' TLS_HOST
openssl x509 -in "$TLS_CERT" -noout -checkhost "$TLS_HOST"
openssl x509 -in "$TLS_CERT" -noout -checkend 604800
CERT_PUB=$(openssl x509 -in "$TLS_CERT" -pubkey -noout | openssl pkey -pubin -outform DER | openssl dgst -sha256)
KEY_PUB=$(openssl pkey -in "$TLS_KEY" -pubout -outform DER | openssl dgst -sha256)
test "$CERT_PUB" = "$KEY_PUB"
kubectl --context "$KUBE_CONTEXT" -n web-demo create secret tls web-tls \
--cert="$TLS_CERT" --key="$TLS_KEY" --dry-run=client -o yaml \
| kubectl --context "$KUBE_CONTEXT" apply -f -
Enter the certificate chain and protected private-key paths. The checks verify the hostname, expiry margin and matching public keys before storing the TLS Secret. Do not commit the private key.
Step 3: Create the Gateway and route
Save this as https.yaml:
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: web-gateway
namespace: web-demo
spec:
gatewayClassName: web-class
listeners:
- name: https
hostname: app.example.com
port: 443
protocol: HTTPS
tls:
mode: Terminate
certificateRefs:
- kind: Secret
name: web-tls
allowedRoutes:
namespaces:
from: Same
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: web-app
namespace: web-demo
spec:
parentRefs:
- name: web-gateway
sectionName: https
hostnames:
- app.example.com
rules:
- matches:
- path:
type: PathPrefix
value: /api
backendRefs:
- name: web-app
port: 80
Replace web-class with the installed GatewayClass. Replace both app.example.com values with your certificate hostname. The Gateway accepts HTTPS on 443; the HTTPRoute sends /api to the Service. All references are in the same namespace.
Step 4: Apply and verify
Run:
kubectl --context "$KUBE_CONTEXT" apply --dry-run=server -f https.yaml
kubectl --context "$KUBE_CONTEXT" apply -f https.yaml
kubectl --context "$KUBE_CONTEXT" -n web-demo describe gateway web-gateway
kubectl --context "$KUBE_CONTEXT" -n web-demo describe httproute web-app
kubectl --context "$KUBE_CONTEXT" -n web-demo get gateway web-gateway -o yaml
Check Gateway conditions such as Accepted and Programmed, and route conditions such as Accepted and ResolvedRefs. Configure DNS to the reported reachable gateway address. Request https://YOUR_HOST/api/message from a client that trusts the certificate; do not use curl -k.
Step 5: Plan certificate renewal
A TLS Secret does not renew itself. Configure a certificate manager or a documented replacement process. After replacement, verify the certificate actually served by the gateway and repeat the API request.
Check the result
The hostname should present a trusted, unexpired certificate and return the API response. An unknown path should not become a successful API response. This example encrypts the client-to-gateway connection; gateway-to-application TLS is separate.
Conversation
0 commentsNo comments yet. Start the conversation below.