Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage persistent connections in kubernetes

In Kubernetes services talk to each other via a service ip. With iptables or something similar each TCP connection is transparently routed to one of the pods that are available for the called service. If the calling service is not closing the TCP connection (e.g. using TCP keepalive or a connection pool) it will connect to one pod and not use the other pods that may be spawned.

What is the correct way to handle such a situation?


My own unsatisfying ideas:

Closing connection after each api call

Am I making every call slower only to be able to distribute requests to different pods? Doesn't feel right.

Minimum number of connections

I could force the caller to open multiple connections (assuming it would then distribute the requests across these connections) but how many should be open? The caller has (and probably should not have) no idea how many pods there are.

Disable bursting

I could limit the resources of the called services so it gets slow on multiple requests and the caller will open more connections (hopefully to other pods). Again I don't like the idea of arbitrarily slowing down the requests and this will only work on cpu bound services.

like image 614
deflomu Avatar asked Jul 23 '19 06:07

deflomu


People also ask

How many connections can a Kubernetes pod handle?

It's the number of pods: each pod apparently can handle 8 connections.

How does Kubernetes handle load balancing?

The Kubernetes load balancer sends connections to the first server in the pool until it is at capacity, and then sends new connections to the next available server. This algorithm is ideal where virtual machines incur a cost, such as in hosted environments.

Does Kubernetes service do load balancing?

With Kubernetes you don't need to modify your application to use an unfamiliar service discovery mechanism. Kubernetes gives Pods their own IP addresses and a single DNS name for a set of Pods, and can load-balance across them.

How Kubernetes pods communicate with each other?

In Kubernetes, each Pod has an IP address. A Pod can communicate with another Pod by directly addressing its IP address, but the recommended way is to use Services. A Service is a set of Pods, which can be reached by a single, fixed DNS name or IP address.


1 Answers

The keep-alive behavior can be tuned by options specified in the Keep-Alive general header:

E.g:

Connection: Keep-Alive
Keep-Alive: max=10, timeout=60

Thus, you could re-open a tcp connection after a specific timeout instead than at each API request or after a max number of http transactions.

Keep in mind that timeout and max are not guaranteed.

EDIT:

Note that If you use k8s service you can choose two LB mode:

  • iptables proxy mode (By default, kube-proxy in iptables mode chooses a backend at random.)

  • IPVS proxy mode where you have different load balancing options:

IPVS provides more options for balancing traffic to backend Pods; these are:

rr: round-robin lc: least connection (smallest number of open connections) dh: destination hashing sh: source hashing sed: shortest expected delay nq: never queue

check this link

like image 179
melix Avatar answered Sep 28 '22 20:09

melix