Keepalive
Manage idle and stale connections with TCP and HTTP keepalive.
About keepalive
With keepalive, the kernel sends probe packets with only an acknowledgement flag (ACK) to the TCP or HTTP/2 socket of the destination after the connection was idle for a specific amount of time. This way, the connection does not have to be re-established repeatedly, which could otherwise lead to latency spikes. If the destination returns the packet with an acknowledgement flag (ACK), the connection is determined to be alive. If not, the probe can fail a certain number of times before the connection is considered stale. Agentgateway can then close the stale connection, which can help avoid longer timeouts and retries on broken or stale connections.
Before you begin
- Set up an agentgateway proxy.
- Install the httpbin sample app.
TCP keepalive
Keep the TCP connection alive by sending out probes after the connection has been idle for a specific amount of time.
Set up TCP keepalive
Create an AgentgatewayPolicy that applies TCP keepalive settings to the httpbin service.
kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayPolicy metadata: name: httpbin-keepalive namespace: httpbin spec: targetRefs: - kind: Gateway name: agentgateway-proxy group: gateway.networking.k8s.io frontend: tcp: keepalive: retries: 3 time: 30s interval: 5s EOFSetting Description retriesThe maximum number of keep-alive probes to send without a response before a connection is considered stale. timeThe number of seconds a connection is idle before the first keep-alive probe is sent. intervalThe number of seconds between keep-alive probes. Port-forward the gateway proxy on port 15000.
kubectl port-forward deployment/agentgateway-proxy -n agentgateway-system 15000Get the config dump and verify that the keepalive policy is set as you configured it.
Example
jqcommand:curl -s http://localhost:15000/config_dump | jq '[.policies[] | select(.policy.frontend != null and .policy.frontend.tCP != null and .policy.frontend.tCP.keepalives != null)] | .[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{ "key": "frontend/httpbin/httpbin-keepalive:frontend-tcp:httpbin/agentgateway-proxy", "name": { "kind": "AgentgatewayPolicy", "name": "httpbin-keepalive", "namespace": "httpbin" }, "target": { "gateway": { "gatewayName": "agentgateway-proxy", "gatewayNamespace": "httpbin", "listenerName": null } }, "policy": { "frontend": { "tCP": { "keepalives": { "enabled": true, "time": "30s", "interval": "5s", "retries": 3 } } } } }
Cleanup
You can remove the resources that you created in this guide.kubectl delete AgentgatewayPolicy httpbin-keepalive -n httpbinHTTP keepalive
Keep the HTTP connection alive by sending out probes after the connection has been idle for a specific amount of time.
Set up HTTP keepalive
Create an AgentgatewayPolicy that applies HTTP keepalive settings on the agentgateway proxy.
kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayPolicy metadata: name: httpbin-keepalive namespace: httpbin spec: targetRefs: - kind: Gateway name: agentgateway-proxy group: gateway.networking.k8s.io frontend: http: http2KeepaliveInterval: 5s http2KeepaliveTimeout: 30s EOFSetting Description http2KeepaliveIntervalThe number of seconds between keep-alive probes. http2KeepaliveTimeoutThe number of seconds a connection is idle before the first keep-alive probe is sent. Port-forward the gateway proxy on port 15000.
kubectl port-forward deployment/agentgateway-proxy -n agentgateway-system 15000Get the config dump and verify that the keepalive policy is set as you configured it.
Example
jqcommand:curl -s http://localhost:15000/config_dump | jq '[.policies[] | select(.policy.frontend != null and .policy.frontend.hTTP != null and .policy.frontend.hTTP.http2KeepaliveInterval != null)] | .[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{ "key": "frontend/httpbin/httpbin-keepalive:frontend-http:httpbin/agentgateway-proxy", "name": { "kind": "AgentgatewayPolicy", "name": "httpbin-keepalive", "namespace": "httpbin" }, "target": { "gateway": { "gatewayName": "agentgateway-proxy", "gatewayNamespace": "httpbin", "listenerName": null } }, "policy": { "frontend": { "hTTP": { "maxBufferSize": 2097152, "http1MaxHeaders": null, "http1IdleTimeout": "10m0s", "http2WindowSize": null, "http2ConnectionWindowSize": null, "http2FrameSize": null, "http2KeepaliveInterval": "5s", "http2KeepaliveTimeout": "30s" } } } }
Cleanup
You can remove the resources that you created in this guide.kubectl delete AgentgatewayPolicy httpbin-keepalive -n httpbin