Set Route Priority and Matching Conditions (Ingress Controller)
API7 Gateway supports configuring route priorities and advanced matching conditions to control how requests are routed to backend services.
This tutorial walks through how you can define route priority and matching rules with API7 Ingress Controller to achieve fine-grained request routing based on headers, query parameters, and more.
Prerequisites
- Install API7 Enterprise.
- Install API7 Ingress Controller and start a gateway.
Configure Route and Rules
Create a Kubernetes manifest file for an HTTPRoute as such:
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: httpbin
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: PathPrefix
value: /*
backendRefs:
- name: httpbin
port: 80
Create another Kubernetes manifest file to configure route priority and request matching conditions on the targeted HTTPRoute:
apiVersion: apisix.apache.org/v1alpha1
kind: HTTPRoutePolicy
metadata:
name: http-route-policy
spec:
targetRefs:
- group: gateway.networking.k8s.io
kind: HTTPRoute
name: httpbin
priority: 10
vars:
- - http_x_test_name
- ==
- http-route-policy
- - arg_test
- ==
- http-route-policy
❶ Configure the name
to be the target HTTPRoute metadata name.
❷ Configure the priority for the target HTTPRoute. High numeric value corresponds to a higher priority.
❸ Match requests with HTTP header X-Test-Name: http-route-policy
❹ Match requests with query parameter test=http-route-policy
.
A request will only be matched to this route if both conditions in vars
are satisfied. For more information on these expressions, see Built-In Variables and API7 Expressions.
Apply the configurations to your cluster:
kubectl apply -f httpbin-route.yaml -f route-policy.yaml
Configure Multiple Targets
If you would like to target routes in multiple HTTPRoute, you can configure more targets:
apiVersion: apisix.apache.org/v1alpha1
kind: HTTPRoutePolicy
metadata:
name: http-route-policy
spec:
targetRefs:
- group: gateway.networking.k8s.io
kind: HTTPRoute
name: httpbin
- group: gateway.networking.k8s.io
kind: HTTPRoute
name: httpbin-1
priority: 10
vars:
- - http_x_test_name
- ==
- http-route-policy
- - arg_test
- ==
- http-route-policy
Verify
Expose the gateway service port to your local machine by port forwarding:
kubectl port-forward svc/api7-ee-3-gateway-gateway 9081:9081 &
Send a request to the route matching none of the conditions:
curl "http://127.0.0.1:9080/ip"
You should see a HTTP/1.1 404 Not Found
response.
Send another request to the route matching one of the conditions:
curl "http://127.0.0.1:9080/ip?test=http-route-policy"
You should also see a HTTP/1.1 404 Not Found
response.
Finally, send a request to the route matching all conditions:
curl "http://127.0.0.1:9080/ip?test=http-route-policy" -H "X-Test-Name: http-route-policy"
You should see a response similar to the following:
{
"origin": "127.0.0.1"
}