<p>I'm a bit confuse on how are handle TCP probe with Kubernetes the documentation says:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-a-tcp-liveness-probe" rel="nofollow noreferrer">source</a></p>
<p>But has far as I known, socket client is <em>connected</em> before the server perform <code>accept</code> on the socket. This TCP handshake is managed by the OS... So how Kubernetes &quot;known&quot; the state of the socket?</p>
<p>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 <em>not alive</em>).</p>
<p>Thank you for your time and consideration!</p>
<h1>Edit 1</h1>
<p>Sorry @Steffen Ullrich it take me some time but here a sample of code: <a href="https://github.com/quentingodeau/k8s-probe" rel="nofollow noreferrer">https://github.com/quentingodeau/k8s-probe</a></p>
<p>And then the traces that I get:</p>
<pre><code>$ 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)
</code></pre>
<h1>Edit 2</h1>
<p><a href="https://github.com/kubernetes/kubernetes/issues/103632" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/103632</a></p>

<blockquote>
<p>But has far as I known, socket client is connected before the server perform accept on the socket</p>
</blockquote>
<p>While it is true that the connection might be established in the OS before <code>accept</code> is called, it is only established after <code>listen</code> 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.</p>
<p>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.</p>
