In nan previous tutorial, we deployed Nginx Gateway Fabric and configured HTTP routing to expose soul services done nan Kubernetes Gateway API. While functional for development, accumulation deployments require TLS encryption to unafraid postulation betwixt clients and nan gateway.
This tutorial extends our existing setup by adding TLS termination astatine nan gateway and implementing automatic HTTP to HTTPS redirection. By nan end, you’ll person a production-ready configuration that encrypts each customer traffic.
Prerequisites
This tutorial assumes you person completed Part 1 and person nan pursuing resources running:
- Nginx Gateway Fabric deployed successful nan nginx-gateway namespace.
- A Gateway assets named demo-gateway pinch an HTTP listener connected larboard 80.
- Sample exertion (demo-web and demo-api) moving successful nan demo namespace.
- HTTPRoute configured to way postulation to nan backend services.
- OpenSSL installed locally (for certificate generation).
Verify your existing setup:
kubectl get gateway -n nginx-gateway kubectl get httproute -n demo kubectl get pods -n demo |
Step 1: Generate TLS Certificates
For this tutorial, we’ll create a self-signed certificate. In production, you would usage cert-manager pinch Let’s Encrypt aliases certificates from your organization’s CA.`12
Create a directory and make nan certificate:
mkdir -p ~/gateway-certs && cd ~/gateway-certs # Generate backstage key openssl genrsa -out tls.key 2048 # Generate self-signed certificate pinch SAN openssl req -new -x509 -key tls.key -out tls.crt -days 365 \ -subj "/CN=demo.example.com" \ -addext "subjectAltName=DNS:demo.example.com,DNS:*.demo.example.com" |
Important: Replace demo.example.com pinch your existent domain. The Subject Alternative Name (SAN) is required for modern browsers and clients to judge nan certificate.
Verify nan certificate:
openssl x509 -in tls.crt -text -noout | grep -A1 "Subject Alternative Name" |
Step 2: Create nan Kubernetes TLS Secret
Store nan certificate and cardinal successful a Kubernetes secret. The concealed must reside successful nan aforesaid namespace arsenic nan Gateway (nginx-gateway).
kubectl create secret tls demo-tls-secret \ --cert=tls.crt \ --key=tls.key \ -n nginx-gateway |
Verify nan secret:
kubectl describe secret demo-tls-secret -n nginx-gateway |
You should spot tls.crt and tls.key listed successful nan Data section.
Step 3: Update nan Gateway for HTTPS
Modify nan Gateway to adhd an HTTPS listener alongside nan existing HTTP listener. Create a record named gateway-tls.yaml:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: demo-gateway namespace: nginx-gateway spec: gatewayClassName: nginx listeners: # Existing HTTP listener - kept for redirect - name: http port: 80 protocol: HTTP hostname: demo.example.com allowedRoutes: namespaces: from: All # New HTTPS listener pinch TLS termination - name: https port: 443 protocol: HTTPS hostname: demo.example.com tls: mode: Terminate certificateRefs: - kind: Secret name: demo-tls-secret allowedRoutes: namespaces: from: All |
Key configuration elements: The tls.mode: Terminate mounting performs TLS termination astatine nan gateway, decrypting postulation earlier forwarding to backend services complete plain HTTP. The certificateRefs points to our TLS secret.
Apply nan updated Gateway:
kubectl apply -f gateway-tls.yaml |
Verify some listeners are configured:
kubectl get gateway demo-gateway -n nginx-gateway -o yaml | grep -A5 "listeners:" |
Step 4: Create nan HTTPS Route
Create an HTTPRoute that binds to nan HTTPS listener utilizing nan sectionName field. Save arsenic route-https.yaml:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: demo-route-https namespace: demo spec: parentRefs: - name: demo-gateway namespace: nginx-gateway sectionName: https hostnames: - demo.example.com rules: - matches: - path: type: PathPrefix value: /api backendRefs: - name: demo-api port: 80 - matches: - path: type: PathPrefix value: / backendRefs: - name: demo-web port: 80 |
Apply nan HTTPS route:
kubectl apply -f route-https.yaml |
Step 5: Configure HTTP to HTTPS Redirect
To guarantee each postulation uses HTTPS, create a redirect way that captures HTTP requests and returns a 301 redirect. Save arsenic http-redirect.yaml:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: http-to-https-redirect namespace: demo spec: parentRefs: - name: demo-gateway namespace: nginx-gateway sectionName: http hostnames: - demo.example.com rules: - filters: - type: RequestRedirect requestRedirect: scheme: https statusCode: 301 |
This way binds to nan HTTP listener (sectionName: http) and applies a RequestRedirect select that changes nan strategy to HTTPS and returns a 301 imperishable redirect.
Apply nan redirect route:
kubectl apply -f http-redirect.yaml |
You tin now delete nan original HTTP way from Part 1 since each HTTP postulation will beryllium redirected:
kubectl delete httproute demo-route -n demo |
Step 6: Test nan TLS Configuration
First, get nan NodePort assignments for HTTP and HTTPS:
kubectl get svc -n nginx-gateway |
Note nan ports mapped to 80 (HTTP) and 443 (HTTPS). For this example, we’ll presume HTTP is connected larboard 31080 and HTTPS is connected larboard 31443.
Test HTTPS Access
Use curl pinch --resolve to representation nan hostname to your node IP. The -k emblem skips certificate verification for self-signed certs:
# Test web endpoint complete HTTPS curl -k --resolve demo.example.com:31443:<NODE_IP> \ https://demo.example.com:31443/web # Test API endpoint complete HTTPS curl -k --resolve demo.example.com:31443:<NODE_IP> \ https://demo.example.com:31443/api |
Test HTTP Redirect
Verify that HTTP requests person a 301 redirect to HTTPS:
curl -I --resolve demo.example.com:31080:<NODE_IP> \ http://demo.example.com:31080/ |
Expected response:
HTTP/1.1 301 Moved Permanently
Location: https://demo.example.com/
Verify Certificate Details
Inspect nan certificate being served by nan gateway:
echo | openssl s_client -connect <NODE_IP>:31443 \ -servername demo.example.com 2>/dev/null | \ openssl x509 -noout -subject -dates |
Summary
You person successfully added TLS support to your Nginx Gateway Fabric deployment. The last configuration includes:
- A Gateway pinch some HTTP (port 80) and HTTPS (port 443) listeners.
- TLS termination astatine nan gateway utilizing a Kubernetes secret.
- An HTTPRoute for HTTPS postulation bound to nan https listener.
- Automatic HTTP to HTTPS redirect utilizing nan Gateway API’s autochthonal RequestRedirect filter.
Resources created successful this tutorial:
Production Recommendations
For accumulation deployments, switch self-signed certificates pinch automated certificate guidance utilizing cert-manager:
# Install cert-manager kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.0/cert-manager.yaml |
Create a ClusterIssuer for Let’s Encrypt and annotate your Gateway to automatically proviso certificates. Refer to nan cert-manager archiving for Gateway API integration details.
Additional information hardening:
- Monitor certificate expiration pinch Prometheus metrics.
- Implement HSTS headers utilizing Nginx Gateway Fabric’s argumentation resources.
- Use abstracted certificates for different domains aliases environments.
- Rotate certificates astatine slightest annually, preferably much often pinch automation.
Looking Ahead
This tutorial demonstrated really to unafraid your Gateway API implementation pinch TLS termination and automatic HTTP redirects. The Gateway API’s autochthonal support for these patterns — done tls.mode: Terminate and RequestRedirect filters — eliminates nan request for vendor-specific annotations that plagued Ingress configurations.
In consequent tutorials, we’ll research precocious postulation guidance patterns including canary deployments pinch postulation splitting, header-based routing for A/B testing, complaint limiting and petition throttling, and cross-namespace routing for multi-tenant clusters.
The unafraid instauration you’ve built present — pinch due TLS termination and redirect handling — is basal groundwork for these accumulation scenarios. Stay tuned!
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube channel to watercourse each our podcasts, interviews, demos, and more.
Group Created pinch Sketch.
English (US) ·
Indonesian (ID) ·