Kubectl Get Nodes: Why and How to Use It
Lukas Gentele
Posted on September 7, 2022
By Mercy Kibet
When Kubernetes was first released, it was missing a critical piece of information: a way to find out which servers were running. This caused all kinds of problems in the community. You couldn’t deploy applications to specific nodes (so they couldn’t replicate), and there was no way to stop resources from spilling over onto other nodes. The Kubernetes team created a solution with the help of Google, and the core component is called “kubectl get nodes.” In this post, you’ll learn about its functionality and see some examples of how you can use it.
What Is “kubectl get nodes”?
“Kubectl get nodes” is a command to retrieve information about nodes in aKubernetes cluster. This information includes the node’s name, role, and status. The command can also get the list of nodes in a cluster and specific information about a node. You can use it like this:
kubectl get nodes
This will output all information about your Kubernetes nodes.
You can also use the command kubectl get nodes -o wideto list additional information about all the nodes in plain text format. It will give you more information and tell you which IP addresses are active in your cluster.
This is the benefit of using the --wide flag. It shows more information and provides more insight into what’s happening in your cluster. If you want to troubleshoot issues in your cluster, it might be better to use this option instead of just using -o name.
How to Run “kubectl get nodes”
To run “kubectl get nodes,” you’ll need a running Kubernetes cluster. You can do this using kubeadm.
Also, it’s best to install kubectl on your system. You can run the kubectl command to see if you already have it installed.
When running this command, you can pass either the --all flag or the --pod option. Running this command with --all will give you information about all the containers in your cluster. Running it with --pod will only provide information about containers that are part of a specific replicaset or deployment.
If you don’t have kubectl installed, you can easily install it using your favorite package manager or directly from the source.
Why and When to Use “kubectl get nodes”
Suppose you have ten pods running mywebsite.com on port 8080, but you need to scale that back to five. As far as Kubernetes is concerned, they’re all still running on the same IP address but using different hostnames.
If you query that DNS server for “mywebsite.com,” it will return ten results. This is because the ten pods are running at that IP address (and port 8080). You can see how this mismatch in addressing and discovery could cause significant headaches and nightmare scenarios if you’re not careful.
“Kubectl get nodes” also show users which pods they have. But this command is usually more helpful because it shows which services or applications each pod uses.
The command can show you the various containers running in your cluster. It can also show you which ones are consuming the most resources.
Also, it can help you identify issues related to using specific resources by showing you which ones consume the most CPU and memory. And finally, it can tell you if a process needs to be killed to reduce the number of resources the container consumes.
When to Use “kubectl get nodes” to Look Up Pod and Node Information
You can use “kubectl get nodes” to look up pod and node information. One example is when you need to find pods by label or name with a specific port number.
You should see all the nodes in your cluster. In particular, look at their IP address, hostname, and port information so you can view/query DNS servers directly for name-to-IP resolution (since k8s does not provide this itself). This is helpful if you want to access pods via hostname (as opposed to FQDN ) since pod IP addresses may change over time (e.g., due to scaling up or down), including the hostnames you used to access them.
Different Use Cases for “kubectl get nodes”
“Kubectl get nodes” will provide every knob and dial attached to all the pods in your cluster, both on your master and any you’ve added since you started running kubectl. This is incredibly useful for troubleshooting and understanding CPU usage, memory, labels, and more.
“Kubectl get nodes” also contains information about historic deployments. If, for example, you have a deployment of twenty images on your node during a rolling upgrade, you’ll get that information from the time series containing five months of data. It will also provide information about the state of your services like node selector, whether you have active endpoints and more. These are pretty helpful when debugging because they help you figure out what’s happening when things don’t work as expected.
For example, you can use it to get information about the following:
The node’s name
This is displayed as the “Name” field in a list of nodes and appears in the output when you specify the --output flag.
The node’s annotations
You can apply these to nodes to provide documentation about that node. If you’ve ever used the kubectl edit command, you should be familiar with how to apply annotations. You can use “kubectl get nodes” to see annotations applied to nodes in your cluster.
The node’s labels
Each node has a label. You can see them listed under “Labels”. Also, you can specify the nodes of a specific label by running kubectl get nodes --label=<label>.
The health of a node
It’s important to know if a node is healthy and if, for example, it has been unhealthy for longer than a certain number of minutes. To find out, run the command below. Everything is running fine if you get a “Status:” value of “Up” for the node.
kubectl get nodes -o=custom-columns=NAME:.metadata.name,STATUS:.status | grep Up
The node’s IP address
You can also use “kubectl get nodes” to determine what IP addresses are assigned to your nodes. To do so, run the following command:
kubectl get nodes -o=custom-columns=NAME:,IP
Where Can’t You Use “kubectl get nodes”?
"Kubectl get nodes" is a command you use to retrieve information from a Kubernetes cluster on your local machine. This includes all the pods, deployments, services, and ReplicationControllers. You cannot use it to view resources from remote clusters, only containers on your machine.
You also cannot use “kubectl get nodes” to pull up a certain pod in your cluster. The pods are split into sub-sections by namespace under Labels and Compute Resources in the command’s output.
Conclusion
Running a “kubectl get nodes” command can tell you what nodes are connected to your cluster. You can also see whether they’re healthy, the labels that have been applied to them, and how long they’ve been running. It lets you see all your running services at a glance, but it can also show you which services use too many resources or are redundant. This command is worth checking periodically (at least once every few weeks) to ensure that everything is working as it should.
Kubernetes’s popularity has led to the development of various powerful tools that can integrate the platform into their operations. One of these is the Loft, a control plane that you can use on top of your existing clusters to take advantage of the platform’s self-service capabilities. It helps you add multi-tenancy and manage the various aspects of your cluster.
This post was written by Mercy Kibet. Mercy is a full-stack developer with a knack for learning and writing about new and intriguing tech stacks.
Posted on September 7, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.