Request timeouts
Set the route-level timeout with an HTTPRoute or AgentgatewayPolicy. To ensure that your apps are available even if they are temporarily unavailable, you can use timeouts alongside Retries.
Before you begin
- Set up an agentgateway proxy.
- Install the httpbin sample app.
Set up timeouts
Specify timeouts for a specific route.
-
Configure a timeout for specific routes by using the Kubernetes Gateway API-native configuration in an HTTPRoute or by using an AgentgatewayPolicy.
-
Configure the HTTPRoute. In the following example, you set a timeout of 2 seconds for the
/delaypath of the httpbin app.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-timeout namespace: httpbin spec: hostnames: - timeout.example parentRefs: - name: agentgateway-proxy namespace: agentgateway-system rules: - matches: - path: type: PathPrefix value: /delay backendRefs: - name: httpbin port: 8000 namespace: httpbin timeouts: request: 2s EOF -
Verify that the gateway proxy is configured to apply the timeout to a request.
-
Port-forward the gateway proxy on port 15000.
kubectl port-forward deploy/agentgateway-proxy -n agentgateway-system 15000 -
Find the route configuration for the cluster in the config dump. Verify that the timeout policy is set as you configured it. In the output for the following
jqcommand, thedelayprefix is included because it has a timeout set.Example
jqcommand:curl -s http://localhost:15000/config_dump | jq '[.binds[].listeners | to_entries[] | .value.routes | to_entries[] | select(.value.inlinePolicies[]? | has("timeout")) | .value] | .[0]'Example output:
http://localhost:15000/config_dump1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31{ "key": "httpbin/httpbin-timeout.0.0.http", "name": "httpbin-timeout", "namespace": "httpbin", "hostnames": [ "timeout.example" ], "matches": [ { "path": { "pathPrefix": "/delay" } } ], "backends": [ { "weight": 1, "service": { "name": "httpbin/httpbin.httpbin.svc.cluster.local", "port": 8000 } } ], "inlinePolicies": [ { "timeout": { "requestTimeout": "2s" } } ] }
-
-
Configure the HTTPRoute. In the following example, you set a timeout of 2 seconds for the
/delaypath of the httpbin app and add an HTTPRoute rule name to the path. You use the rule name later to apply the timeout to a particular route.kubectl apply -n httpbin -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-timeout namespace: httpbin spec: hostnames: - timeout.example parentRefs: - name: agentgateway-proxy namespace: agentgateway-system rules: - matches: - path: type: PathPrefix value: /delay backendRefs: - kind: Service name: httpbin port: 8000 name: timeout EOF -
Create an AgentgatewayPolicy with your timeout settings and use the
targetRefs.sectionNameto apply the timeout to a specific HTTPRoute rule. In this example, you apply the policy to thetimeoutrule that points to the/delaypath in your HTTPRoute resource.kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayPolicy metadata: name: timeout namespace: httpbin spec: targetRefs: - kind: HTTPRoute group: gateway.networking.k8s.io name: httpbin-timeout sectionName: timeout traffic: timeouts: request: 2s EOF -
Find the route configuration for the cluster in the config dump. Verify that the timeout policy is set as you configured it.
Example
jqcommand:curl -s http://localhost:15000/config_dump | jq '[.policies[] | select(.policy.traffic.timeout?)] | .[0]'Example output:
http://localhost:15000/config_dump1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23{ "key": "traffic/httpbin/timeout:timeout:httpbin/httpbin-timeout/timeout", "name": { "kind": "AgentgatewayPolicy", "name": "timeout", "namespace": "httpbin" }, "target": { "route": { "name": "httpbin-timeout", "namespace": "httpbin", "ruleName": "timeout" } }, "policy": { "traffic": { "phase": "route", "timeout": { "requestTimeout": "2s" } } } }
-
Create an HTTPRoute that configures a route to the
/delaypath of the httpbin app.kubectl apply -n httpbin -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-timeout namespace: httpbin spec: hostnames: - timeout.example parentRefs: - name: agentgateway-proxy namespace: agentgateway-system rules: - matches: - path: type: PathPrefix value: /delay backendRefs: - kind: Service name: httpbin port: 8000 EOF -
Create an AgentgatewayPolicy with your timeout settings and use the
targetRefs.sectionNameto apply the timeout to a Gateway listener.kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayPolicy metadata: name: timeout namespace: agentgateway-system spec: targetRefs: - kind: Gateway group: gateway.networking.k8s.io name: agentgateway-proxy sectionName: http traffic: timeouts: request: 2s EOF -
Find the route configuration for the cluster in the config dump. Verify that the timeout policy is set as you configured it.
Example
jqcommand:curl -s http://localhost:15000/config_dump | jq '[.policies[] | select(.policy.traffic.timeout?)] | .[0]'Example output:
http://localhost:15000/config_dump1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23{ "key": "traffic/agentgateway-system/timeout:timeout:agentgateway-system/agentgateway-proxy/http", "name": { "kind": "AgentgatewayPolicy", "name": "timeout", "namespace": "agentgateway-system" }, "target": { "gateway": { "gatewayName": "agentgateway-proxy", "gatewayNamespace": "agentgateway-system", "listenerName": "http" } }, "policy": { "traffic": { "phase": "route", "timeout": { "requestTimeout": "2s" } } } }
-
-
Send a request along the
/delaypath of the httpbin. This path delays requests for the number of seconds that you specify. In this example, you delay the request by 1 second. Because the delay is shorter than the timeout that you configured, the request succeeds and a 200 HTTP response code is returned.curl -vi http://$INGRESS_GW_ADDRESS:80/delay/1 -H "host: timeout.example:80"curl -vi localhost:8080/delay/1 -H "host: timeout.example"Example output:
... < HTTP/1.1 200 OK ... { "args": {}, "headers": { "Accept": [ "*/*" ], "Host": [ "timeout.example" ], "User-Agent": [ "curl/8.7.1" ] }, "origin": "10.244.0.2:37150", "url": "http://timeout.example/delay/1", "data": "", "files": null, "form": null, "json": null } -
Review the logs of the httpbin app and verify that you can see the successful request.
kubectl logs -n agentgateway-system \ -l gateway.networking.k8s.io/gateway-name=agentgateway-proxy \ --tail=1 | grep -E 'timeout.example'Example output:
info request gateway=agentgateway-system/agentgateway-proxy listener=http route=httpbin/timeout endpoint=10.244.0.13:8080 src.addr=127.0.0.1:34300 http.method=GET http.host=timeout.example http.path=/delay/1 http.version=HTTP/1.1 http.status=200 protocol=http duration=0ms -
Repeat the request along the
/delaypath. This time, you use a delay that is longer than the request timeout that you previously specified and therefore simulates an app that is slow to respond. Verify that the request times out and that you get back a 504 HTTP response code.curl -vi http://$INGRESS_GW_ADDRESS:80/delay/5 -H "host: timeout.example:80"curl -vi localhost:8080/delay/5 -H "host: timeout.example"Example output:
... < HTTP/1.1 504 Gateway Timeout ... request timeout% -
In the httpbin logs, verify that the request took longer than 2 seconds, which triggered the request timeout.
kubectl logs -n agentgateway-system \ -l gateway.networking.k8s.io/gateway-name=agentgateway-proxy \ --tail=1 | grep -E 'timeout.example'Example output:
info request gateway=agentgateway-system/agentgateway-proxy listener=http route=httpbin/httpbin-timeout endpoint=10.244.0.21:8080 src.addr=127.0.0.1:43640 http.method=GET http.host=timeout.example http.path=/delay/5 http.version=HTTP/1.1 http.status=504 protocol=http error="upstream call timeout" duration=2001ms
Cleanup
You can remove the resources that you created in this guide. Run the following commands.
- Delete the HTTPRoute resource.
kubectl delete httproute httpbin-timeout -n httpbin- If you created an AgentgatewayPolicy, delete it from the namespace you created it in.
kubectl delete AgentgatewayPolicy timeout -n httpbin
kubectl delete AgentgatewayPolicy timeout -n agentgateway-system