How to Filter Pods by Node Name Using Kubernetes API

I’m trying to figure out how to get all pods running on a particular node without having to add labels to every single pod. I know you can use labels to select pods but that seems like extra work when I just want to filter by the node they’re running on.

Is there a way to use the node name directly as a filter? I tried looking at the kubectl output and saw there’s a NODE column:

kubectl get pods --output=wide --namespace=$NAMESPACE

# Output shows:
NAME   READY   STATUS    RESTARTS   AGE   NODE

Can I use this NODE field somehow to filter the results? What’s the right way to do this with kubectl commands or through the Kubernetes API directly?

While the field selector approach is effective, you can also apply the --field-selector flag alongside namespace filtering, which is especially useful in production. For example, using kubectl get pods --all-namespaces --field-selector spec.nodeName=node-name can be beneficial for troubleshooting across various namespaces on a specific node. Keep in mind that this will only reflect currently scheduled pods; for historical details, consulting events or logs is necessary. The API endpoint method is also reliable for programmatic use, though remember to manage pagination in environments with numerous pods per node.

You can definitely filter pods by node name without needing custom labels. The field selector approach works well for this - use kubectl get pods --field-selector spec.nodeName=your-node-name to get all pods scheduled on a specific node. This queries the pod spec directly where Kubernetes stores the node assignment.

If you’re working with the API directly, you can make a GET request to /api/v1/pods with the same field selector as a query parameter: ?fieldSelector=spec.nodeName=your-node-name. This is much cleaner than parsing the wide output format and gives you structured data to work with. I’ve used this approach in several automation scripts and it’s been reliable across different Kubernetes versions.

another option is using jsonpath if you perfer that route. try kubectl get pods -o jsonpath='{range .items[?(@.spec.nodeName=="your-node")]}{.metadata.name}{"\n"}{end}' - bit messy but works when you need just pod names. i usually stick with field selector tho since its simpler