Going to AWS Summit London? 🇬🇧🇬🇧

Meet us there →


What is Kubectl Get Nodes and How to Use It

What is Kubectl Get Nodes

Spacelift and Kubernetes

Manage the challenges of Kubernetes with a GitOps flow, policies, and the ability to communicate between stacks from your choice of IaC tools.

Book a demo

In Kubernetes, every administrator needs a trusty tool to navigate the complex network of nodes. Enter kubectl get nodes, the ultimate command that unveils the secrets of your cluster. 

The introduction of the kubectl get nodes command in the Kubernetes command-line ecosystem was driven by the need for administrators and developers to retrieve information about the cluster’s nodes. 

In this blog, we’ll walk you through the basics of this command, helping you gain the confidence and expertise to navigate and manage your Kubernetes environment like a pro. Get ready to take control of your clusters with ease!

Let’s dive in and unlock the potential of this indispensable command!

We will cover:

  1. What is kubectl get nodes?
  2. How to run kubectl get nodes?
  3. When to use kubectl to get nodes?
  4. Kubectl get node labels
  5. Kubectl get node for pods
  6. Use cases of kubectl get nodes

What is kubectl get nodes?

kubectl get nodes is a powerful command that provides essential information about the nodes in your Kubernetes cluster. When you execute this command, it communicates with the Kubernetes API server, retrieving the necessary information from the cluster’s control plane. 

The kubectl get nodes command fetches the current state of all nodes in the cluster and displays the information in a tabular format, as shown below.

$ kubectl get nodes

node-1     Ready    <none>   7d      v1.21.3
node-2     Ready    <none>   7d      v1.21.3
node-3     Ready    <none>   7d      v1.21.3

The output columns that are fetched are:

  • NAME: The name of the node within the cluster.
  • STATUS: The current status of the node, indicating whether it is ready, not ready, or unreachable.
  • ROLES: Any roles assigned to the node, such as master, worker, etcd.
  • AGE: The duration since the node was created or added to the cluster.
  • VERSION: The Kubernetes version is running on the node.
  • INTERNAL-IP: The internal IP address assigned to the node.
  • EXTERNAL-IP: The external IP address assigned to the node, if applicable.

This command provides a quick overview of the cluster’s nodes, providing us with the information to monitor the infrastructure of Kubernetes furthermore. It is often used for troubleshooting, scaling, capacity planning, and general cluster management tasks.

How to run kubectl get nodes?

This command has some prerequisites to satisfy.

Step 1: Install kubectl

You’ll need to install the kubectl command-line tool if you haven’t already. You can find installation instructions specific to your operating system in the official Kubernetes documentation: https://kubernetes.io/docs/tasks/tools/

Step 2: Configure kubectl

Once kubectl is installed, you must configure it to connect to your cluster. This involves specifying the address and authentication credentials of your cluster. 

If you’re using a Kubernetes cluster managed by a cloud provider, you might have a configuration file such as kubeconfig available. 

Step 3: Run kubectl get nodes

In the terminal or command prompt, type kubectl get nodes and press Enter. This will execute the command and retrieve the node information from the cluster.

Step 4: Output

The command will display the node information in a tabular format, including the earlier columns (NAME, STATUS, ROLES, AGE, VERSION, INTERNAL-IP, EXTERNAL-IP). The output will show the current state of all the nodes in your cluster.

Note: Some columns mentioned above only use flags like -o wide to the command.

$ kubectl get nodes -o wide

node-1     Ready    master   7d      v1.21.3    <none>        Ubuntu 18.04.5 LTS      4.15.0-142-generic   docker://20.10.6
node-2     Ready    worker   7d      v1.21.3    <none>        Ubuntu 18.04.5 LTS      4.15.0-142-generic   docker://20.10.6
node-3     Ready    worker   7d      v1.21.3    <none>        Ubuntu 18.04.5 LTS      4.15.0-142-generic   docker://20.10.6

When to use kubectl to get nodes?

It helps in retrieving information in several scenarios, such as:

  • Cluster Overview: kubectl get nodes provides a quick overview of the nodes present in your Kubernetes cluster. It allows you to see each node’s current status, availability, and resource allocation. This information is useful for understanding your cluster’s overall health and capacity.
  • Kubernetes is a container orchestration tool that maintains a cluster of nodes. Nodes are similar to virtual machines that run our applications in a containerized form. Administrators require a command-line tool to effectively manage the cluster to obtain the node’s crucial information.
  • Troubleshooting: kubectl get nodes can help you identify the problem that is specific to a particular node. You can check the health of the node better and analyze root causes. 
  • Node Capacity Planning: If you need to plan resource allocation or capacity scaling, kubectl get nodes provides insights into the available resources on each node. 

You can clarify how CPU and memory are allocated and how it is utilized for the current workloads. This gives you visibility and an understanding of how you can plan your future infrastructures.

  • The kubectl get nodes command provides an overview of the nodes’ operational state, which includes information based on their readiness, schedulability, etc.
  • Upgrades and Maintenance: it is important to have an understanding of the current state of the nodes. This command helps us confirm whether the nodes are ready and available before initiating maintenance tasks.

Kubectl get node labels

Consider an environment with multiple nodes and different applications with several environments that need deployment. How will you distinguish and segregate your environments and applications in such a scenario? 

This is where the concepts of labels come into the picture. It enables logical and physical segregation of workloads based on different environments. 

For example, let’s say we have two nodes and two different environments of an application (Production and Staging) in a cluster. All you have to do is apply labels to the nodes using a command called kubectl label node key=value.

Once labeled, you can use this in your pod configurations to distribute workloads equally.

To view the labels created in nodes, you can use the command, kubectl get nodes --show-labels=true.

Following is an example output of how labels are displayed. Observe the last column.

$ kubectl get nodes --show-labels=true

node-1     Ready    <none>      1d      v1.21.3         disk=ssd,environment=production
node-2     Ready    <none>      1d      v1.21.3         disk=hdd,environment=staging

Kubectl get node for pods

To retrieve the pods scheduled on each node in a Kubernetes cluster, you can use the kubectl get pods --all-namespaces -o wide command. 

This command fetches information about all the pods across all namespaces and includes additional details like the node on which each pod is running. Similarly, you can view this information for specific namespaces as well.

Here’s how you can use the command:

  1. Open a terminal and ensure that kubectl is installed and configured to connect to your Kubernetes cluster.
  2. Run the command: Enter the following command: kubectl get pods --all-namespaces -o wide

The -o wide option is used to display the entire information of pods (such as kubectl detele pod), including the node name, namespace, and IP addresses.

  1. Display the output: The command will display a tabular output with different columns. The NODE column indicates the node on which each pod is running.

Here’s an example of the output for the command 

$ kubectl get pods -o wide

default    my-pod-1      1/1    Running        0     1d     node-1
default    my-pod-2      1/1    Running        0     1d     node-2
kube-system kube-proxy-abcde   1/1   Running    0          1d     node-1
kube-system kube-proxy-fghij   1/1   Running    0          1d     node-2

In the example above, pods from different namespaces are listed, along with their statuses, IP addresses, and the nodes they are running on.

Use cases of kubectl get nodes

There are multiple use cases for using this command.

Monitor the status and availability of nodes in the cluster.

This displays the number of nodes and its status if it is ready or not. A simple kubectl get nodes would fetch this for you. It can be used for troubleshooting purposes as well.

Understand the available resources on each node for capacity planning.

This is useful when you need a node’s CPU and memory details. Run the following command for this information.

kubectl get nodes -o custom-columns=NAME:.metadata.name,CPU:.status.capacity.cpu,MEMORY:.status.capacity.memory

Filter nodes based on specific labels assigned to them.

If we recollect, after we enabled some labels on the nodes for better management of workloads, this is one of the most widely used use cases. 

Let’s say there is an issue in the production environment, and you need to troubleshoot the node which has pods belonging to only production. This information is just one command away!

kubectl get nodes -l environment=production

Note: -l and –labels do the same operation

Key Points

kubectl get nodes is another one amongst kubectl commands (like kubectl config set-context) that are used to retrieve the details of nodes running in the Kubernetes cluster and their status. 

Whether you need an overview of your cluster, troubleshooting assistance, capacity planning insights, or the ability to filter nodes based on specific labels, kubectl get nodes is your go-to tool.

We encourage you to also check out how Spacelift helps you manage the complexities and compliance challenges of using Kubernetes. Anything that can be run via kubectl can be run within a Spacelift stack. Find out more about how Spacelift works with Kubernetes, and get started on your journey by creating a free trial account.

The Most Flexible CI/CD Automation Tool

Spacelift is an alternative to using homegrown solutions on top of a generic CI. It helps overcome common state management issues and adds several must-have capabilities for infrastructure management.

Start free trial