<p>One of my kubernetes Node, I see my kubelet listening on port 38641. How to map this port number to actual POD its pointing too.</p>

<pre><code># netstat -alpn |grep 38641
tcp        0      0 127.0.0.1:38641         0.0.0.0:*               LISTEN      9832/kubelet
tcp        0      0 127.0.0.1:45230         127.0.0.1:38641         CLOSE_WAIT  9832/kubelet
tcp        0      0 127.0.0.1:39922         127.0.0.1:38641         CLOSE_WAIT  9832/kubelet
tcp        0      0 127.0.0.1:39238         127.0.0.1:38641         ESTABLISHED 9832/kubelet
</code></pre>

<p>I assume you have exposed a service in kubernetes using the NodePort type which would account for the high port number. If this is the case, you can simply get a list of all services running in the cluster and search then for the port you want. ie. </p>

<p><code>kubectl get svc --all-namespaces | grep 38641</code></p>

<p>Once you have the service which exposes this port, you can then inspect the service and see which pods are being selected by the service</p>

<pre><code>PORT=38641
SERVICE_OUTPUT=$(kubectl get svc --all-namespaces | grep $PORT)
NAMESPACE=$(echo $SERVICE_OUTPUT | awk '{ print $1 }')
SERVICE=$(echo $SERVICE_OUTPUT | awk '{ print $2 }')
kubectl describe -n $NAMESPACE svc $SERVICE
</code></pre>

<p>Now let's get the selector:</p>

<pre><code>SELECTOR=$(kubectl describe -n $NAMESPACE svc $SERVICE | grep Selector | awk '{ print $2}')
</code></pre>

<p>Ok, now that we know what pods are being selected we can find these pods easily:</p>

<p><code>kubectl get po --selector $SELECTOR -n $NAMESPACE</code></p>

<p>We can capture the pod names for further inspection, such as for determining the replicaset they are from:</p>

<pre><code>PODS=$(kubectl get po --selector $SELECTOR -n $NAMESPACE --no-headers=true | awk '{ print $1}')
echo $PODS
</code></pre>

<p>If you need to you can trace the pod origin:</p>

<pre><code>POD=full-pod-name
RS=$(kubectl -n $NAMESPACE describe po $POD | grep -hoe ReplicaSet.*); 
echo rs: $RS;

DEPLOYMENT=$(kubectl -n $NAMESPACE describe $RS | grep -hoe Deployment.*); 
echo deployment: $DEPLOYMENT;
</code></pre>
