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

  1. Set up an agentgateway proxy.
  2. 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

  1. 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
    EOF
    SettingDescription
    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.
  2. Port-forward the gateway proxy on port 15000.

    kubectl port-forward deployment/agentgateway-proxy -n agentgateway-system 15000
  3. Get the config dump and verify that the keepalive policy is set as you configured it.

    Example jq command:

    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_dump
     1
     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 httpbin

HTTP 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

  1. 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
    EOF
    SettingDescription
    http2KeepaliveIntervalThe number of seconds between keep-alive probes.
    http2KeepaliveTimeoutThe number of seconds a connection is idle before the first keep-alive probe is sent.
  2. Port-forward the gateway proxy on port 15000.

    kubectl port-forward deployment/agentgateway-proxy -n agentgateway-system 15000
  3. Get the config dump and verify that the keepalive policy is set as you configured it.

    Example jq command:

    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_dump
     1
     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
Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

Tip: one topic per conversation gives the best results. Use the + button in the chat header to start a new conversation.

Switching topics? Starting a new conversation improves accuracy.
↑↓ navigate select esc dismiss

What could be improved?

Your feedback helps us improve assistant answers and identify docs gaps we should fix.

Need more help? Join us on Discord: https://discord.gg/y9efgEmppm

Want to use your own agent? Add the Solo MCP server to query our docs directly. Get started here: https://search.solo.io/.