Browse topics

Gateway API and HTTPRoute

Version
1.37
Review
Draft

Summary#

A cluster-scoped GatewayClass selects a controller by spec.controllerName. A namespaced Gateway uses that class and declares listeners. An HTTPRoute attaches to a listener and sends matching requests to backends such as Services. Gateway API needs its CRDs and a controller that implements them.

Syntax#

Set Gateway.spec.gatewayClassName to the class name. On the route, parentRefs[].sectionName picks a listener by name, and backendRefs[].port is the Service port. This route attaches to the http listener on a Gateway in its own namespace.

yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: reports
spec:
  parentRefs:
    - name: edge
      sectionName: http
  hostnames: ["reports.example.test"]
  rules:
    - matches:
        - path:
            type: Exact
            value: /today
      backendRefs:
        - name: reports
          port: 80

Example#

Set KUBECONFIG to a disposable cluster's kubeconfig. Follow the Envoy Gateway quickstart's installation steps for version 1.10.0 and its Gateway API CRDs in envoy-gateway-system. The cluster pulls the Envoy images and busybox:1.37 if they are missing locally. The example uses an in-cluster client and an Envoy Service of type ClusterIP, so it needs no external load balancer.

sh
(
  set -eu
  : "${KUBECONFIG:?Set KUBECONFIG to the kubeconfig for a disposable cluster}"
  ns="ref-gateway-$$"
  class=
  kubectl create namespace "$ns" >/dev/null
  cleanup() {
    kubectl delete namespace "$ns" >/dev/null
    if [ -n "$class" ]; then
      kubectl delete gatewayclass "$class" >/dev/null
    fi
  }
  trap cleanup EXIT
  trap 'exit 1' HUP INT TERM

  kubectl create -f - >/dev/null <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
  name: $ns
spec:
  controllerName: gateway.envoyproxy.io/gatewayclass-controller
EOF
  class=$ns
  kubectl wait --for=condition=Accepted "gatewayclass/$class" --timeout=90s >/dev/null
  printf '%s\n' 'GatewayClass Accepted=True'

  kubectl create -n "$ns" -f - >/dev/null <<EOF
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: EnvoyProxy
metadata:
  name: internal
spec:
  provider:
    type: Kubernetes
    kubernetes:
      envoyService:
        type: ClusterIP
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: edge
spec:
  gatewayClassName: $class
  infrastructure:
    parametersRef:
      group: gateway.envoyproxy.io
      kind: EnvoyProxy
      name: internal
  listeners:
    - name: http
      protocol: HTTP
      port: 80
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: reports
spec:
  parentRefs:
    - name: edge
      sectionName: http
  hostnames: ["reports.example.test"]
  rules:
    - matches:
        - path:
            type: Exact
            value: /today
      backendRefs:
        - name: reports
          port: 80
---
apiVersion: v1
kind: Service
metadata:
  name: reports
spec:
  selector:
    app: reports
  ports:
    - port: 80
      targetPort: 8080
---
apiVersion: v1
kind: Pod
metadata:
  name: reports
  labels:
    app: reports
spec:
  terminationGracePeriodSeconds: 1
  containers:
    - name: web
      image: busybox:1.37
      command: ["sh", "-c", "mkdir /www && echo 'reports ready' > /www/today && exec httpd -f -p 8080 -h /www"]
      readinessProbe:
        tcpSocket:
          port: 8080
        periodSeconds: 1
EOF

  kubectl run -n "$ns" client --image=busybox:1.37 \
    --overrides='{"spec":{"terminationGracePeriodSeconds":1}}' -- sleep 3600 >/dev/null
  kubectl wait -n "$ns" --for=condition=Ready pod/reports pod/client --timeout=120s >/dev/null
  kubectl wait -n "$ns" --for=condition=Programmed gateway/edge --timeout=180s >/dev/null
  printf '%s\n' 'Gateway Programmed=True'
  for condition in Accepted ResolvedRefs; do
    kubectl wait -n "$ns" --timeout=90s httproute/reports \
      --for="jsonpath={.status.parents[*].conditions[?(@.type=='$condition')].status}=True" >/dev/null
    printf 'HTTPRoute %s=True\n' "$condition"
  done

  address=$(kubectl get svc -n envoy-gateway-system \
    -l "gateway.envoyproxy.io/owning-gateway-namespace=$ns,gateway.envoyproxy.io/owning-gateway-name=edge" \
    -o jsonpath='{.items[0].spec.clusterIP}')
  kubectl exec -n "$ns" client -- sh -c '
    for attempt in 1 2 3 4 5 6 7 8 9 10; do
      if body=$(wget -T 3 -qO- --header "Host: reports.example.test" "$1" 2>&1); then
        printf "%s\n" "$body"
        exit 0
      fi
      sleep 1
    done
    printf "%s\n" "$body" >&2
    exit 1
  ' sh "http://$address/today"
)

Expected output#

Text
GatewayClass Accepted=True
Gateway Programmed=True
HTTPRoute Accepted=True
HTTPRoute ResolvedRefs=True
reports ready

The Service's IP and the time it takes the controller to configure Envoy vary. The last line is the HTTP response body from the backend reached through Envoy.

Common mistakes#

If a route has multiple parents, read the conditions for the specific parent and controller under status.parents. Accepted=True confirms attachment. Check ResolvedRefs for bad backend names or ports, and compare observedGeneration with metadata.generation after an edit. Programmed=True tells you the Gateway configuration was sent to the data plane. Send an HTTP request to check that the backend actually answers.

Check the listener's allowedRoutes when attaching a route from another namespace. The default only allows routes in the Gateway's own namespace.

Version applicability#

Gateway API is released separately from Kubernetes. GatewayClass, Gateway and HTTPRoute graduated to gateway.networking.k8s.io/v1 in Gateway API 1.0. The example uses Envoy Gateway 1.10.0 with Gateway API 1.6.1 on Kubernetes 1.37. The EnvoyProxy resource and the generated Service labels are specific to Envoy Gateway.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0