How Kubernetes TCP Probe handle the fact that TCP handshake is manage by the OS?

Viewed 249

I'm a bit confuse on how are handle TCP probe with Kubernetes the documentation says:

A third type of liveness probe uses a TCP socket. With this configuration, the kubelet will attempt to open a socket to your container on the specified port. If it can establish a connection, the container is considered healthy, if it can't it is considered a failure.

source

But has far as I known, socket client is connected before the server perform accept on the socket. This TCP handshake is managed by the OS... So how Kubernetes "known" the state of the socket?

To give a little be of context I'm trying to write a unit test in my application (C++) and I cannot figure how K8s handle this, but in k8s it does work as expected (I mean that if I do not accept the connection it will declare my container as not alive).

Thank you for your time and consideration!

Edit 1

Sorry @Steffen Ullrich it take me some time but here a sample of code: https://github.com/quentingodeau/k8s-probe

And then the traces that I get:

$ kubectl logs -f $(kubectl get pods | egrep -o 'sample-deployment-[^ ]*')
[2021-07-10 18:46:22.837] [info] Server acccept the client...
[2021-07-10 18:46:23.838] [info] Server acccept the client...
[2021-07-10 18:46:24.840] [info] Server acccept the client...
[2021-07-10 18:46:25.837] [info] Server acccept the client...
[2021-07-10 18:46:26.836] [info] Server acccept the client...
[2021-07-10 18:46:27.839] [info] Server acccept the client...
[2021-07-10 18:46:28.840] [info] Server acccept the client...
[2021-07-10 18:46:29.836] [info] Server acccept the client...
[2021-07-10 18:46:30.843] [info] Server acccept the client...
[2021-07-10 18:46:31.028] [info] Send SIGUSR1
[2021-07-10 18:46:31.836] [info] Server acccept the client...
[2021-07-10 18:46:31.836] [info] Start to not procssing incoming connection
[2021-07-10 18:46:35.855] [info] End of application (signal=15)

Edit 2

https://github.com/kubernetes/kubernetes/issues/103632

1 Answers

But has far as I known, socket client is connected before the server perform accept on the socket

While it is true that the connection might be established in the OS before accept is called, it is only established after listen is called on the socket. If the application is not running (failed to start, crashed) then there is no listening socket so any connection to it will fail. If the listen queue is full since the application fails to handle new connections in time, then the connection will fail too.

This kind of cheap probe is sufficient in many cases but it surely does not handle every case, like making sure that the application responds correctly and responds within the expected time. If such checks are needed more elaborate probes and maybe even application specific probes need to be done.

Related