Complete Guide to Listing All Pods and Their Nodes in Kubernetes

Nov 23, 2025 · Programming · 10 views · 7.8

Keywords: Kubernetes | Pod Query | Node Mapping | kubectl Command | Cluster Management

Abstract: This article provides a comprehensive overview of various methods to retrieve the mapping relationship between Pods and nodes in Kubernetes clusters, with a focus on the custom-columns output format of kubectl get command. Through complete code examples and in-depth technical analysis, it helps readers master the core skills for efficiently querying cluster resource distribution. The article also compares the advantages and disadvantages of different output formats, offering practical references for daily operations and troubleshooting.

Querying Pod to Node Mapping in Kubernetes

In Kubernetes cluster management practice, understanding the correspondence between Pods and nodes is a fundamental and crucial operational task. This mapping information holds significant value for resource scheduling optimization, fault troubleshooting, and capacity planning. Based on actual operational requirements, this article systematically explains the implementation principles and application scenarios of various query methods.

Detailed Explanation of Custom Columns Output Format

The kubectl command-line tool provides a powerful custom-columns output option, allowing users to customize the fields to be displayed. The advantage of this method lies in precise control over output content, avoiding interference from irrelevant information.

The syntax structure of the basic query command is as follows:

kubectl get pod -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName --all-namespaces

In this command, the -o=custom-columns parameter specifies the custom column output mode. Each column definition uses the ColumnName:FieldPath format:

The --all-namespaces flag ensures the query covers all namespaces, which is particularly important for multi-tenant environments.

Practice with Simplified Output Format

If only the correspondence between Pod names and node names is needed, a more concise command format can be used:

kubectl get pod -o=custom-columns=NODE:.spec.nodeName,NAME:.metadata.name --all-namespaces

This simplified format directly outputs two columns of data, with the first column being the node name and the second column being the Pod name, perfectly matching the required output format in the question:

NODE1 POD1
NODE1 POD2
NODE2 POD3
NODE3 POD4

The field path .spec.nodeName points to the node name field in the Pod specification, which is automatically populated by Kubernetes after the Pod is scheduled to a specific node.

Comparative Analysis of Wide Output Format

In addition to custom-columns, kubectl also provides the -o wide output option:

kubectl get pod -o wide

This format displays complete information about the Pod, including IP address, node location, readiness status, etc. Although the information is more comprehensive, for scenarios that only require Pod-node mapping relationships, the output content may be too verbose.

Comparison of the two methods:

Technical Implementation Principles

The Kubernetes API Server stores the complete state information of all resources. When executing the kubectl get command:

  1. kubectl sends a REST request to the API Server
  2. The API Server returns JSON format data of the requested resources
  3. kubectl formats the data according to the output format parameters
  4. The formatted result is finally displayed in the terminal

The custom-columns functionality essentially performs field extraction and reorganization of the JSON data returned by the API on the client side. This processing does not increase the load on the API Server.

Practical Application Scenarios

Pod-node mapping queries are particularly useful in the following scenarios:

Best Practice Recommendations

Based on production environment experience, it is recommended to:

  1. Use custom-columns format in scripts for subsequent processing
  2. Combine with --field-selector parameter to filter Pods with specific states
  3. Regularly collect Pod distribution data for trend analysis
  4. Establish automated inspection mechanisms to promptly detect abnormal distribution patterns

By mastering these query techniques, operations personnel can more efficiently manage Kubernetes clusters, ensuring high availability and performance optimization of applications.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.