How to map kubelet port to POD

9/27/2018

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.

# 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
-- sfgroups
kubernetes

1 Answer

9/27/2018

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.

kubectl get svc --all-namespaces | grep 38641

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

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

Now let's get the selector:

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

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

kubectl get po --selector $SELECTOR -n $NAMESPACE

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

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

If you need to you can trace the pod origin:

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;
-- yosefrow
Source: StackOverflow