API Gateways, Traffic Management & the Edge›04 · Lab: your first gateway

Lesson 04 of 13 · Hands-on with Envoy Gateway

Lab: your first gateway

Hands-on: install Envoy Gateway on a local kind cluster with Helm, create a shared Gateway owned by the platform team, deploy two versions of a service, route by host and path, let testers reach version 2 with a header, split the rest 90/10, and add an HTTPS listener with your own certificate. Every command and output was run for real (Kubernetes 1.36, Envoy Gateway 1.10.0, Gateway API 1.6.1). The cluster is reused in lessons 5 to 7 and 12.

Intermediate
Key wordslabkindEnvoy GatewayHelmgateway-helmGatewayClassEnvoyProxyClusterIPport-forwardHTTPRouteheader matchingweighted traffic splitcanaryecho serverHTTPS listenerTLS secretopensslcurl --resolve

What you'll build

your laptop ──port-forward──► Envoy (gateway pods, namespace envoy-gateway-system)
                                │  Gateway "edge" (namespace infra, platform team)
                                │  HTTPRoute "orders" (namespace shop, app team)
                                ├── x-version: v2 ──────────► orders-v2
                                └── everyone else: 90% ─────► orders-v1
                                                   10% ─────► orders-v2

You need Docker, kind, kubectl and Helm. Expect about 30 minutes, most of it downloading images.

Your laptop curl Envoy Gateway controller Envoy proxy gateway pods Gateway edge namespace shop HTTPRoute orders x-version: v2 → v2 others: 90 / 10 orders-v1 echo pod orders-v2 echo pod watches config (xDS) port-forward 90 % 10 % + header v2
The lab: Envoy Gateway's controller turns the Gateway and HTTPRoute into Envoy configuration; requests are routed by header and weight to two versions of a service.

You're the caretaker of a new building entrance. First you hire the security company (install Envoy Gateway), then open a door (the Gateway), then put up signs inside (the route): "visitors with a green badge go to the new room; everyone else, nine in ten to the old room, one in ten to the new one".

Step 1: cluster and Envoy Gateway

kind create cluster --name gw-lab
helm install eg oci://docker.io/envoyproxy/gateway-helm --version v1.10.0 \
  -n envoy-gateway-system --create-namespace
kubectl wait --timeout=5m -n envoy-gateway-system deployment/envoy-gateway --for=condition=Available

The chart installs the controller and the Gateway API CRDs (version 1.6.1, experimental channel) plus Envoy Gateway's own policy CRDs:

$ kubectl get crd | grep gateway | awk '{print $1}'
backendtrafficpolicies.gateway.envoyproxy.io
clienttrafficpolicies.gateway.envoyproxy.io
envoyproxies.gateway.envoyproxy.io
gatewayclasses.gateway.networking.k8s.io
gateways.gateway.networking.k8s.io
grpcroutes.gateway.networking.k8s.io
httproutes.gateway.networking.k8s.io
referencegrants.gateway.networking.k8s.io
securitypolicies.gateway.envoyproxy.io
...

Step 2: the Gateway (platform team)

apiVersion: v1
kind: Namespace
metadata:
  name: infra
---
# kind has no cloud load balancer: give gateways a ClusterIP Service and use port-forward
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: EnvoyProxy
metadata:
  name: kind-proxy
  namespace: envoy-gateway-system
spec:
  provider:
    type: Kubernetes
    kubernetes:
      envoyService:
        type: ClusterIP
---
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
  name: envoy
spec:
  controllerName: gateway.envoyproxy.io/gatewayclass-controller
  parametersRef:
    group: gateway.envoyproxy.io
    kind: EnvoyProxy
    name: kind-proxy
    namespace: envoy-gateway-system
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: edge
  namespace: infra
spec:
  gatewayClassName: envoy
  listeners:
  - name: http
    protocol: HTTP
    port: 80
    allowedRoutes:
      namespaces:
        from: Selector
        selector:
          matchLabels:
            edge-access: "true"

Without the EnvoyProxy part, the gateway Service is type LoadBalancer, kind never assigns it an address, and you see this (real output):

$ kubectl get gateway -n infra
NAME   CLASS   ADDRESS   PROGRAMMED   AGE
edge   envoy             False        3m
Programmed=False AddressNotAssigned: No addresses have been assigned to the Gateway

With it:

$ kubectl wait --for=condition=Programmed gateway/edge -n infra --timeout=120s
$ kubectl get gateway -n infra
NAME   CLASS   ADDRESS        PROGRAMMED   AGE
edge   envoy   10.96.39.127   True         3m19s

Step 3: two versions of a service (app team)

The echo server replies with JSON describing the request and the pod that answered.

apiVersion: v1
kind: Namespace
metadata:
  name: shop
  labels:
    edge-access: "true"          # allowed to attach routes to the shared Gateway
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: orders-v1
  namespace: shop
spec:
  replicas: 1
  selector:
    matchLabels: { app: orders, version: v1 }
  template:
    metadata:
      labels: { app: orders, version: v1 }
    spec:
      containers:
      - name: echo
        image: gcr.io/k8s-staging-gateway-api/echo-basic:v20231214-v1.0.0-140-gf544a46e
        env:
        - name: POD_NAME
          valueFrom: { fieldRef: { fieldPath: metadata.name } }
        - name: NAMESPACE
          valueFrom: { fieldRef: { fieldPath: metadata.namespace } }
---
apiVersion: v1
kind: Service
metadata:
  name: orders-v1
  namespace: shop
spec:
  selector: { app: orders, version: v1 }
  ports:
  - { port: 80, targetPort: 3000 }

Create orders-v2 the same way (replace v1 with v2 in the names and labels).

Step 4: the route

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: orders
  namespace: shop
spec:
  parentRefs:
  - { name: edge, namespace: infra }
  hostnames: ["api.example.test"]
  rules:
  - matches:                                   # testers opt in to v2 with a header
    - path: { type: PathPrefix, value: /orders }
      headers:
      - { name: x-version, value: v2 }
    backendRefs:
    - { name: orders-v2, port: 80 }
  - matches:                                   # everyone else: 90% v1, 10% v2
    - path: { type: PathPrefix, value: /orders }
    backendRefs:
    - { name: orders-v1, port: 80, weight: 90 }
    - { name: orders-v2, port: 80, weight: 10 }

Step 5: test it

SVC=$(kubectl get svc -n envoy-gateway-system \
  -l gateway.envoyproxy.io/owning-gateway-name=edge -o jsonpath='{.items[0].metadata.name}')
kubectl port-forward -n envoy-gateway-system svc/$SVC 8080:80 &

Real output:

$ curl -s -H 'Host: api.example.test' localhost:8080/orders
{
 "path": "/orders",
 "host": "api.example.test",
 "method": "GET",
 "headers": {
  "X-Envoy-External-Address": ["127.0.0.1"],
  "X-Forwarded-For": ["10.244.0.10"],
  "X-Forwarded-Proto": ["http"],
  "X-Request-Id": ["a0d5eca7-a39f-4814-bf60-508e9d45d802"],
  ...
 },
 "namespace": "shop",
 "pod": "orders-v1-5d7b78997b-gkhjs"
}

$ curl -s -H 'Host: api.example.test' -H 'x-version: v2' localhost:8080/orders | grep pod
 "pod": "orders-v2-67959546ff-2ck7l"

$ for i in $(seq 200); do curl -s -H 'Host: api.example.test' localhost:8080/orders | grep -o 'orders-v[12]'; done | sort | uniq -c
 183 orders-v1
  17 orders-v2

$ curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: other.example.test' localhost:8080/orders
404
$ curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: api.example.test' localhost:8080/payments
404

The gateway added X-Forwarded-For, X-Forwarded-Proto and X-Request-Id (lesson 1). The 90/10 split is random per request, so 200 requests give roughly, not exactly, 180/20.

Step 6: HTTPS

Create a certificate for api.example.test (self-signed; in production use cert-manager and a real CA), store it as a Secret next to the Gateway, and add a listener:

openssl req -x509 -newkey rsa:2048 -nodes -days 30 -keyout tls.key -out tls.crt \
  -subj "/CN=api.example.test" -addext "subjectAltName=DNS:api.example.test"
kubectl create secret tls api-tls -n infra --cert=tls.crt --key=tls.key
# Add this listener to the Gateway's listeners list
- name: https
  protocol: HTTPS
  port: 443
  hostname: api.example.test
  tls:
    mode: Terminate
    certificateRefs:
    - { kind: Secret, name: api-tls }
  allowedRoutes:
    namespaces:
      from: Selector
      selector:
        matchLabels:
          edge-access: "true"
$ kubectl get gateway edge -n infra -o jsonpath='{range .status.listeners[*]}{.name}: attachedRoutes={.attachedRoutes}{"\n"}{end}'
http: attachedRoutes=1
https: attachedRoutes=1

$ kubectl port-forward -n envoy-gateway-system svc/$SVC 8443:443 &
$ curl -s -o /dev/null -w '%{http_code} tls=%{ssl_verify_result}\n' --cacert tls.crt \
    --resolve api.example.test:8443:127.0.0.1 https://api.example.test:8443/orders
200 tls=0
$ curl -s --resolve api.example.test:8443:127.0.0.1 https://api.example.test:8443/orders; echo "exit=$?"
exit=60                                  # certificate not trusted
$ echo | openssl s_client -connect 127.0.0.1:8443 -servername api.example.test 2>/dev/null \
    | openssl x509 -noout -subject -enddate
subject= /CN=api.example.test
notAfter=Dec  8 18:10:33 2026 GMT

--resolve sends the right hostname (SNI and Host) to 127.0.0.1 without editing /etc/hosts.

Try it: more routing

  1. Change the weights to 50/50, apply, and count again. Then to 0/100: that's a blue/green switch.
  2. Add a rule that redirects http://api.example.test/* to HTTPS (RequestRedirect with scheme: https, statusCode: 301) on the http listener only (sectionName: http).
  3. Add a third backend for /inventory and route it only for GET.
  4. Keep the cluster: lessons 5, 6, 7 and 12 build on it. When you're finished with them: kind delete cluster --name gw-lab.

Going deeper

  • In a real cloud, leave the Service as LoadBalancer; the cloud controller gives it an address and PROGRAMMED becomes True by itself.
  • EnvoyProxy also sets replicas, resources, autoscaling, pod disruption budgets and access-log format for the gateway pods.
  • For canaries driven by metrics instead of fixed weights, a rollout controller changes the weights for you (see the multi-tenant SaaS course's canary analysis lesson).

Recap

  • Envoy Gateway installs with one Helm chart and implements the Gateway API with Envoy proxies.
  • The platform team owns the GatewayClass, the Gateway and which namespaces may attach; the app team owns the HTTPRoute.
  • Header matches send testers to a new version; weights split everyone else.
  • HTTPS is a listener with a certificate Secret; test it with curl --resolve and --cacert.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.