Common Recipes
This page collects the most frequently requested configurations in one place. Copy, paste, and adapt these recipes to your environment.
HTTPS with cert-manager
Section titled “HTTPS with cert-manager”End-to-end TLS termination using Let’s Encrypt.
# 1. Install cert-managerkubectl apply -f https://github.com/cert-manager/cert-manager/releases/latest/download/cert-manager.yaml
# 2. Create a ClusterIssuerkubectl apply -f - <<EOFapiVersion: cert-manager.io/v1kind: ClusterIssuermetadata: name: letsencrypt-prodspec: acme: server: https://acme-v02.api.letsencrypt.org/directory email: [email protected] privateKeySecretRef: name: letsencrypt-prod solvers: - http01: ingress: class: nginxEOF
# 3. Create a Certificatekubectl apply -n nantian-gw -f - <<EOFapiVersion: cert-manager.io/v1kind: Certificatemetadata: name: gateway-tlsspec: secretName: gateway-tls dnsNames: - api.example.com issuerRef: name: letsencrypt-prod kind: ClusterIssuerEOF
# 4. Create Gateway with TLSkubectl apply -f - <<EOFapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata: name: tls-gatewayspec: gatewayClassName: nantian-gw listeners: - name: https port: 443 protocol: HTTPS tls: mode: Terminate certificateRefs: - name: gateway-tls allowedRoutes: namespaces: from: AllEOFVerify: curl -v https://api.example.com/ should show a valid certificate.
Path-Based Routing
Section titled “Path-Based Routing”Route different URL paths to different backends.
apiVersion: gateway.networking.k8s.io/v1kind: HTTPRoutemetadata: name: path-routingspec: parentRefs: - name: my-gateway rules: - matches: - path: type: PathPrefix value: /api backendRefs: - name: api-service port: 8080 - matches: - path: type: PathPrefix value: /web backendRefs: - name: web-service port: 3000 - matches: - path: type: Exact value: /health backendRefs: - name: health-check port: 9090Rules are evaluated from top to bottom. The first matching rule wins.
Traffic Splitting (Canary Deployment)
Section titled “Traffic Splitting (Canary Deployment)”Send 90% of traffic to v1 and 10% to v2.
apiVersion: gateway.networking.k8s.io/v1kind: HTTPRoutemetadata: name: canaryspec: parentRefs: - name: my-gateway rules: - matches: - path: type: PathPrefix value: / backendRefs: - name: app-v1 port: 8080 weight: 90 - name: app-v2 port: 8080 weight: 10Weights are relative. 90 and 10 is the same as 9 and 1. Omit weight for equal distribution.
gRPC Routing
Section titled “gRPC Routing”Route gRPC traffic by service and method name.
apiVersion: gateway.networking.k8s.io/v1kind: GRPCRoutemetadata: name: grpc-routingspec: parentRefs: - name: my-gateway rules: - matches: - method: service: echo.Echo method: UnaryEcho backendRefs: - name: grpc-echo port: 9090 - matches: - method: service: echo.Echo backendRefs: - name: grpc-echo port: 9090# Gateway must allow gRPC on the listenerapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata: name: my-gatewayspec: gatewayClassName: nantian-gw listeners: - name: http port: 80 protocol: HTTP allowedRoutes: namespaces: from: All kinds: - kind: GRPCRoute - kind: HTTPRouteCORS Configuration
Section titled “CORS Configuration”Allow cross-origin requests from a specific domain.
apiVersion: gateway.networking.k8s.io/v1kind: HTTPRoutemetadata: name: cors-apispec: parentRefs: - name: my-gateway rules: - matches: - path: type: PathPrefix value: /api filters: - type: CORS cors: allowOrigins: - "https://app.example.com" allowMethods: - GET - POST - PUT - DELETE allowHeaders: - Content-Type - Authorization exposeHeaders: - X-Request-Id allowCredentials: true maxAge: 3600 backendRefs: - name: api-service port: 8080The CORS filter handles preflight OPTIONS requests automatically — no backend changes needed.
Header-Based Routing
Section titled “Header-Based Routing”Route requests to different backends based on request headers.
apiVersion: gateway.networking.k8s.io/v1kind: HTTPRoutemetadata: name: header-routingspec: parentRefs: - name: my-gateway rules: - matches: - headers: - name: X-Version value: v2 backendRefs: - name: app-v2 port: 8080 - matches: - path: type: PathPrefix value: / backendRefs: - name: app-v1 port: 8080Use this pattern for A/B testing, feature flags, or canary releases based on header values.
URL Rewrite
Section titled “URL Rewrite”Rewrite the request path before forwarding to the backend.
apiVersion: gateway.networking.k8s.io/v1kind: HTTPRoutemetadata: name: rewritespec: parentRefs: - name: my-gateway rules: - matches: - path: type: PathPrefix value: /public/api filters: - type: URLRewrite urlRewrite: path: type: ReplacePrefixMatch replacePrefixMatch: /api backendRefs: - name: internal-api port: 8080With this configuration, GET /public/api/users becomes GET /api/users when forwarded to the backend.
Redirect
Section titled “Redirect”Redirect HTTP to HTTPS, or permanent redirects for changed paths.
# HTTP → HTTPS redirectapiVersion: gateway.networking.k8s.io/v1kind: HTTPRoutemetadata: name: http-to-httpsspec: parentRefs: - name: my-gateway rules: - filters: - type: RequestRedirect requestRedirect: scheme: https statusCode: 301# Path migration redirectapiVersion: gateway.networking.k8s.io/v1kind: HTTPRoutemetadata: name: path-redirectspec: parentRefs: - name: my-gateway rules: - matches: - path: type: PathPrefix value: /old-path filters: - type: RequestRedirect requestRedirect: path: type: ReplacePrefixMatch replacePrefixMatch: /new-path statusCode: 301Backend TLS with Custom CA
Section titled “Backend TLS with Custom CA”Verify the backend’s certificate against a custom CA stored in a ConfigMap.
# 1. Store the CA certificatekubectl create configmap backend-ca -n default --from-file=ca.crt=/path/to/ca.pem
# 2. Create BackendTLSPolicykubectl apply -f - <<EOFapiVersion: gateway.networking.k8s.io/v1alpha3kind: BackendTLSPolicymetadata: name: backend-tls namespace: defaultspec: targetRefs: - group: "" kind: Service name: secure-backend validation: hostname: secure-backend.default.svc caCertificateRefs: - name: backend-ca group: "" kind: ConfigMap subjectAltNames: - DNS: secure-backend.default.svc - DNS: secure-backendEOFThe gateway will now verify the backend’s certificate against the specified CA and hostname before establishing a connection.
Request Mirroring
Section titled “Request Mirroring”Send a copy of production traffic to a staging backend for testing.
apiVersion: gateway.networking.k8s.io/v1kind: HTTPRoutemetadata: name: mirrorspec: parentRefs: - name: my-gateway rules: - matches: - path: type: PathPrefix value: / filters: - type: RequestMirror requestMirror: backendRef: name: staging-backend port: 8080 fraction: numerator: 10 denominator: 100 backendRefs: - name: production-backend port: 8080This sends 10% of requests to the staging backend. The primary backend still receives all requests. Mirror responses are discarded.
What’s Next
Section titled “What’s Next”- Your First Route — step-by-step walkthrough from zero to routing
- Traffic Management — deeper dive into routing patterns
- Security Best Practices — TLS, authentication, and network security
- Troubleshooting — diagnostic commands and common issues