user1595858
user1595858

Reputation: 3878

kubectl command disconnects after few minutes of idle time

We noticed when we exec -it to connect into a pod, after certain idle time the connection get destroyed. Is there any option to leave the connection open longer?

I see there is an open PR, but wondering if there is any workaround for this issue.

Upvotes: 4

Views: 3900

Answers (3)

Sida Zhou
Sida Zhou

Reputation: 3705

Upgraded kubectl from 1.11 to 1.25, solved my disconnection problem..

Upvotes: 0

Oliver
Oliver

Reputation: 29483

I have not found a way to change the timeout but one workaround might be to re-attach:

$ kubectl run psql --image postgres \
    --env PGHOST=<HOST> \
    --env PGUSER=<USER>  
    --env PGPASSWORD=<PWD> \
    --restart Never -- \
    bash -c 'while true; do echo -n "."; sleep 1; done'
pod/psql created

$ kubectl attach psql
Defaulting container name to psql.
Use 'kubectl describe pod/psql -n default' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
............

Upvotes: 0

VAS
VAS

Reputation: 9031

The short answer is no. And that's why:

Enabling TCP keepalive for console connections

TCP keepalive is a TCP option that causes packets to be exchanged over a connection even if there is no traffic to transport. It should be enabled on both ends of the connection. TCP keepalive must be enabled at the operating-system level and by the application/program opening TCP connections.

On Linux, edit the "/etc/sysctl.conf" file and add these lines:

net.ipv4.tcp_keepalive_time = 200
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_intvl = 50

(feel free to adapt the values as you see fit). When done editing, you must make the new values known to the kernel:

# sysctl --load=/etc/sysctl.conf

Custom Configuration of TCP Socket Keep-Alive Timeouts

Default values for these properties is:

tcp_keepalive_time = 7200 seconds
tcp_keepalive_probes = 9
tcp_keepalive_intvl = 75 seconds

The other possible way is to start some kind of proxy server on the client side and connect to Kubernetes apiserver through it. I haven’t tested it myself and it could be tricky, but here is an example of how to enable keepalives to backend for Nginx.

Upvotes: 2

Related Questions