Path, header, and query string routing with Application Gateway for Containers - Gateway API
Article
This document helps you set up an example application that uses the resources from Gateway API to demonstrate traffic routing based on URL path, query string, and header. Steps are provided to:
Create a Gateway resource with one HTTPS listener.
Create an HTTPRoute resource that references a backend service.
Use HTTPRouteMatch to perform matches that route based on path, header, and query string.
Background
Application Gateway for Containers enables traffic routing based on URL path, query string, and header. See the following example scenario:
Prerequisites
If following the BYO deployment strategy, ensure you have set up your Application Gateway for Containers resources and ALB Controller
Deploy sample HTTP application
Apply the following deployment.yaml file on your cluster to create a sample web application to demonstrate path, query, and header based routing.
When the ALB Controller creates the Application Gateway for Containers resources in ARM, it'll use the following naming convention for a frontend resource: fe-<8 randomly generated characters>
RESOURCE_GROUP='<resource group name of the Application Gateway For Containers resource>'
RESOURCE_NAME='alb-test'
RESOURCE_ID=$(az network alb show --resource-group $RESOURCE_GROUP --name $RESOURCE_NAME --query id -o tsv)
FRONTEND_NAME='frontend'
Once the gateway has been created, create an HTTPRoute to define two different matches and a default service to route traffic to.
The way the following rules read are as follows:
If the path is /bar, traffic is routed to backend-v2 service on port 8080 OR
If the request contains an HTTP header with the name magic and the value foo, the URL contains a query string defining the name great with a value of example, AND the path is /some/thing, the request is sent to backend-v2 on port 8080.
Otherwise, all other requests are routed to backend-v1 service on port 8080.
Once the HTTPRoute resource has been created, ensure the route has been Accepted and the Application Gateway for Containers resource has been Programmed.
Bash
kubectl get httproute http-route -n test-infra -o yaml
Verify the status of the Application Gateway for Containers resource has been successfully updated.
Now we're ready to send some traffic to our sample application, via the FQDN assigned to the frontend. Use the following command to get the FQDN.
Bash
fqdn=$(kubectl get gateway gateway-01 -n test-infra -o jsonpath='{.status.addresses[0].value}')
By using the curl command, we can validate three different scenarios:
Path based routing
In this scenario, the client request sent to http://frontend-fqdn/bar is routed to backend-v2 service.
Run the following command:
Bash
curl http://$fqdn/bar
Notice the container serving the request is backend-v2.
Query string + header + path routing
In this scenario, the client request sent to http://frontend-fqdn/some/thing?great=example with a header key/value part of "magic: foo" is routed to backend-v2 service.
Notice the container serving the request is backend-v2.
Default route
If neither of the first two scenarios are satisfied, Application Gateway for Containers routes all other requests to the backend-v1 service.
Run the following command:
Bash
curl http://$fqdn/
Notice the container serving the request is backend-v1.
Congratulations, you have installed ALB Controller, deployed a backend application and routed traffic to the application via Gateway API on Application Gateway for Containers.
In this module, you'll learn to improve application resilience by distributing load across multiple servers and use path-based routing to direct web traffic.
Overview of Azure Application Load Balancer Application Gateway for Containers features, resources, architecture, and implementation. Learn how Application Gateway for Containers works and how to use Application Gateway for Containers resources in Azure.