<p>I am going to keep this simple and ask, is there a way to see which pod have an active connection with an endpoint like a database endpoint?</p>

<p>My cluster contains a few hundred of namespace and my database provider just told me that the maximum amount of connections is almost reached and I want to pinpoint the pod(s) that uses multiple connections to our database endpoint at the same time.</p>

<p>I can see from my database cluster that the connections come from my cluster node's IP... but it won't say which pods... and I have quite lot of pods...</p>

<p>Thanks for the help</p>

Sartigan
<p>Each container uses its own network namespace, so to check the network connection inside the container namespace you need to run command inside that namespace.  </p>

<p>Luckily all containers in a Pod share the same network namespace, so you can add small sidecar container to the pod that print to the log open connections.</p>

<p>Alternatively, you can run <code>netstat</code> command inside the pod (if the pod has it on its filesystem):</p>

<pre><code>kubectl get pods | grep Running | awk '{ print $1 }' | xargs -I % sh -c 'echo == Pod %; kubectl exec -ti % -- netstat -tunaple' &gt;netstat.txt
# or
kubectl get pods | grep Running | awk '{ print $1 }' | xargs -I % sh -c 'echo == Pod %; kubectl exec -ti % -- netstat -tunaple | grep ESTABLISHED' &gt;netstat.txt
</code></pre>

<p>After that you'll have a file on your disk (<code>netstat.txt</code>) with all information about connections in the pods.</p>

<p>The third way is most <a href="https://platform9.com/blog/container-namespaces-deep-dive-container-networking/" rel="nofollow noreferrer">complex</a>. You need to find the container ID using <code>docker ps</code> and run the following command to get PID</p>

<pre><code>$ pid = "$(docker inspect -f '{{.State.Pid}}' "container_name | Uuid")"
</code></pre>

<p>Then, you need to create named namespace: 
(you can use any name you want, or container_name/Uuid/Pod_Name as a replacement to namespace_name)</p>

<pre><code>sudo mkdir -p /var/run/netns
sudo ln -sf /proc/$pid/ns/net "/var/run/netns/namespace_name"
</code></pre>

<p>Now you can run commands in that namespace:</p>

<pre><code>sudo ip netns exec "namespace_name" netstat -tunaple | grep ESTABLISHED
</code></pre>

<p>You need to do that for each pod on each node. So, it might be useful to troubleshoot particular containers, but it needs some more automation for your task.</p>

<p>It might be helpful for you to install <a href="https://istio.io/" rel="nofollow noreferrer">Istio</a> to your cluster. It has several interesting features mentioned in this <a href="https://stackoverflow.com/a/54275422/9521610">answer</a></p>
