This project is in the process of being donated to the CNCF and is not affiliated with the Kubernetes project.

Host redirect

Redirect requests to a different host.

For more information, see the Kubernetes Gateway API documentation.

Before you begin

  1. Follow the Get started guide to install K8sGateway, set up a gateway resource, and deploy the httpbin sample app.

  2. Get the external address of the gateway and save it in an environment variable.

    export INGRESS_GW_ADDRESS=$(kubectl get svc -n gloo-system gloo-proxy-http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}")
    echo $INGRESS_GW_ADDRESS  
    kubectl port-forward deployment/gloo-proxy-http -n gloo-system 8080:8080

Set up host redirects

  1. Create an HTTP route for the httpbin app that you created as part of the Get started guide. In the following example, requests for the host.redirect.example domain are redirected to the www.example.com hostname, and a 302 HTTP response code is returned to the user.

    kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1beta1
    kind: HTTPRoute
    metadata:
      name: httpbin-redirect
      namespace: httpbin
    spec:
      parentRefs:
        - name: http
          namespace: gloo-system
      hostnames:
        - host.redirect.example
      rules:
        - filters:
          - type: RequestRedirect
            requestRedirect:
              hostname: "www.example.com"
              statusCode: 302
    EOF
  2. Send a request to the httpbin app on the host.redirect.example domain and verify that you get back a 302 HTTP response code and the redirect location www.example.com/headers.

    curl -vik http://$INGRESS_GW_ADDRESS:8080/headers -H "host: host.redirect.example:8080"
    curl -vik localhost:8080/headers -H "host: host.redirect.example"

    Example output:

    * Mark bundle as not supporting multiuse
    < HTTP/1.1 302 Found
    HTTP/1.1 302 Found
    < location: http://www.example.com/headers
    location: http://www.example.com/headers
    < date: Sat, 04 Nov 2023 01:40:06 GMT
    date: Sat, 04 Nov 2023 01:40:06 GMT
    < server: envoy
    server: envoy
    < content-length: 0
    content-length: 0

Cleanup

You can remove the resources that you created in this guide.
kubectl delete httproute httpbin-redirect -n httpbin