Fetching Pods on Specific Nodes in Kubernetes

Hey everyone, I’m working with Kubernetes and I’m trying to figure out how to get all the pods running on a specific node. I’ve been looking at the docs about labels and selectors, but it’s not clear if I can do this without labeling each pod individually.

Is there a way to use kubectl or the Kubernetes API to select pods based on which node they’re on? When I run kubectl get pods, I can see the node info in the output, but I’m not sure how to filter by that.

Here’s an example of what I’m trying to do:

# This is what I'm doing now
kubectl get pods --output=wide --namespace=my-app

# What I want to do
kubectl get pods --node=worker-1 --namespace=my-app

Is there a command like this? Or maybe a way to do it through the API? Any help would be awesome!

yo, i’ve got a trick for that! use the --field-selector option with kubectl. it’s super easy:

kubectl get pods --field-selector spec.nodeName=worker-1 -n my-app

this grabs all pods on worker-1 in ur my-app namespace. no need to mess with labels or anything. just plug in the node name and ur good to go!

Having worked extensively with Kubernetes clusters, I can confirm there’s a straightforward way to fetch pods on specific nodes. You can leverage the field selector feature in kubectl. Here’s the command that should work for your use case:

kubectl get pods --field-selector spec.nodeName=worker-1 -n my-app

This command filters pods based on the node they’re scheduled on, without needing individual pod labels. It’s efficient and works across any cluster setup.

For API users, you can make a similar request using the list pods endpoint with the fieldSelector query parameter. This approach is particularly useful for programmatic access or when building custom tooling around your Kubernetes infrastructure.

I’ve dealt with this exact situation before, and there’s definitely a way to do what you’re looking for. You can use the -o wide option with kubectl get pods to show node information, then pipe that output through grep to filter for the specific node you’re interested in.

Here’s the command I use:

kubectl get pods -o wide --all-namespaces | grep worker-1

This will show you all pods across all namespaces running on the worker-1 node. If you want to limit it to a specific namespace, you can add the -n flag before --all-namespaces.

Another approach is to use a JSONPath query:

kubectl get pods --all-namespaces -o jsonpath='{range .items[?(@.spec.nodeName=="worker-1")]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}{end}'

This gives you more control over the output format. It’s a bit more complex, but it’s powerful once you get the hang of it.

Hope this helps! Let me know if you need any clarification on these methods.