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:
-
Configure an HTTP listener on your gateway and set up basic routing for the sample apps.
-
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
-
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, replacespec.hosts
withspec.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
-
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
-
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
-
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 ...
-
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
- If you haven't already, follow the other guides in the routing section to plan your routing table setup. For example, you might check out the prefix matching guide to decide how to match the incoming requests to your service paths, the redirect guide to set up any path or prefix rewrites, or the sub-table delegation guide to nest and sort multiple route tables.
- Configure additional route settings, such as weighted routing to version subsets or adding and removing headers.
- Apply a policy to your service or route. For example, you might set a connection policy to keep alive connections that the load balancer in your infrastructure provider might otherwise periodically close.