Route to external services

Route incoming requests from the gateway to an endpoint that is located outside the cluster directly by using the external endpoint's hostname or IP address.

Before you begin

Follow the getting started instructions to:

  1. Set up Gloo Gateway in a single cluster.

  2. Deploy sample apps.

  3. Configure an HTTP listener on your gateway and set up basic routing for the sample apps.

  4. Follow the other guides in this routing section to plan your routing table setup. For example, you might check out the path matching guide to decide how to match the incoming requests to your service paths, the redirect guide to set up any path or host rewrites, or the sub-table delegation guide to nest and sort multiple route tables. Note: Be sure that each route for one host is unique, such as by using prefix matching to determine which requests to the host should be forwarded to which destinations.

Route to an external service directly

  1. Create an external service resource to create a service entry for www.google.com. If you want to specify an IP address or CIDR instead, replace spec.hosts with spec.addresses. Note that you cannot specify a hostname and an IP address or CIDR in the same external service resource.

    kubectl apply -f- <<EOF
    apiVersion: networking.gloo.solo.io/v2
    kind: ExternalService
    metadata:
      name: google
      namespace: bookinfo
    spec:
      hosts:
      - "www.google.com"
      ports:
      - name: http
        number: 80
        protocol: HTTP
      selector: {}
    EOF
    
  2. Create a route table to allow routing to the external service.

    kubectl apply -f- <<EOF
    apiVersion: networking.gloo.solo.io/v2
    kind: RouteTable
    metadata:
      name: rt-google
      namespace: bookinfo
    spec:
      hosts:
        - 'www.google.com'
      virtualGateways:
        - name: istio-ingressgateway
          namespace: bookinfo
          cluster: ${CLUSTER_NAME}
      http:
        # Route for google
        - name: google
          # Prefix matching
          matchers:
          - uri:
              prefix: /google
          # Forwarding directive
          forwardTo:
            destinations:
            # Reference to the external service resource exposing your external endpoints
            - ref:
                name: google
                cluster: $CLUSTER_NAME
              kind: EXTERNAL_SERVICE
            pathRewrite: /
    EOF
    
  3. Save the external address of the ingress gateway.

    export INGRESS_GW_IP=$(kubectl get svc -n gloo-mesh-gateways istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    echo $INGRESS_GW_IP
    
    export INGRESS_GW_IP=$(kubectl get svc -n gloo-mesh-gateways istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
    echo $INGRESS_GW_IP
    

  4. Test the route to your external resource.

    curl -vik --resolve www.google.com:80:${INGRESS_GW_IP} http://www.google.com:80/google
    

    Example output:

    * Mark bundle as not supporting multiuse
    < HTTP/1.1 200 OK
    HTTP/1.1 200 OK
    < date: Tue, 16 May 2023 17:30:11 GMT
    date: Tue, 16 May 2023 17:30:11 GMT
    < expires: -1
    expires: -1
    < cache-control: private, max-age=0
    cache-control: private, max-age=0
    < content-type: text/html; charset=ISO-8859-1
    content-type: text/html; charset=ISO-8859-1
    ...
    
  5. Optional: Clean up the resources that you created as part of this guide.

    kubectl delete routetable rt-goole -n bookinfo
    kubectl delete externalservice google -n bookinfo
    

Next steps