Kubernetes API - Get Pods on Specific Nodes
Introduction
Retrieving Pods running on specific nodes in a Kubernetes cluster can be useful for debugging and resource management. In this guide, you'll learn how to use the Kubernetes API to filter Pods by node.
Prerequisites
Before proceeding, make sure:
- You have
kubectlinstalled and configured. - You have access to the Kubernetes API.
Using the Kubernetes API
List Pods with Node Information
To list Pods along with their node information, use:
kubectl get pods -o wide
This command displays additional details, including the node where each Pod is running.
Filter Pods by Node
To filter Pods running on a specific node, use:
kubectl get pods --field-selector spec.nodeName=<node-name>
Replace <node-name> with the name of the node you want to filter by.
Example
Suppose you want to retrieve Pods running on a node named worker-node-1. Run the following command:
kubectl get pods --field-selector spec.nodeName=worker-node-1
This lists all Pods running on worker-node-1.
Using the Kubernetes API Directly
You can also use the Kubernetes API directly to query Pods by node. Here's an example using curl:
curl -k -H "Authorization: Bearer <token>" \
https://<api-server>/api/v1/pods?fieldSelector=spec.nodeName=<node-name>
Replace <token> with your API token, <api-server> with the API server URL, and <node-name> with the node name.
Best Practices
- Use Labels: Label nodes and Pods for easier filtering.
- Monitor Resources: Use tools like
kubectl topto monitor resource usage on nodes. - Automate Queries: Use scripts or tools like
kubectlplugins for frequent queries.
Conclusion
Using the Kubernetes API to retrieve Pods on specific nodes is a powerful way to manage and debug your cluster. By following these steps, you can efficiently filter and monitor Pods.
Related Resources
We earn commissions when you shop through the links below.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
How to List All Pods and Their Nodes in Kubernetes
Learn how to list all pods in your Kubernetes cluster along with the nodes they're running on, using kubectl and command-line tools for clear, actionable output.
Kubernetes Kubelet
What is the role of the kubelet in a Kubernetes cluster? How does it interact with the control plane?
mid
Helm Charts and Kubernetes Package Management
Learn Kubernetes application deployment and management using Helm charts with templates, values, and lifecycle management.
90 minutes