Kubectl Get Nodes with Instance Type and Arch - K8s | Devops Junction

In this post, we are going to give a quick command to list your worker nodes with their instance types and architecture.

I have posted this for the Amazon EKS Kubernetes cluster but with slight modification, you can use the same approach for any other managed Kubernetes cluster too.

While there are some hard ways to get to know the machine information by logging in to the worker node and getting the info.

The easy way is to make use of the metadata provided by the Kubernetes itself.  This metadata would be available for each node

Before we move on to the commands, take a look at the pre-requisites real quick.

Prerequisites

 

As we have already mentioned we are using JQ and some Linux commands to get the desired output

here are the kubectl get nodes example.

Kubectl Get Nodes sorted by machine Architecture

In this, we are trying to list the node names, and their instance type followed by the architecture (arm/amd)

We are using a sort -k3 -r to reverse sort based on the value of 3rd column which is the architecture

kubectl get nodes -o json|jq -Cjr '.items[] | .metadata.name," ",.metadata.labels."beta.kubernetes.io/instance-type"," ",.metadata.labels."beta.kubernetes.io/arch", "\n"'|sort -k3 -r

Here is the output, when this command was executed on our end

kubectl instance type

 

Kubectl Get Nodes sorted by Node Name or the Instance type

As we have already seen, we are using sort -k -r  to perform a reverse sort based on the column number

To reverse sort based on the Node Name, All you have to do is to update the value after the -k in sort command like this

kubectl get nodes -o json|jq -Cjr '.items[] | .metadata.name," ",.metadata.labels."beta.kubernetes.io/instance-type"," ",.metadata.labels."beta.kubernetes.io/arch", "\n"'|sort -k1 -r

 

To reverse sort based on the instance or machine type, you need to update the column value in the sort command to k2 like this

kubectl get nodes -o json|jq -Cjr '.items[] | .metadata.name," ",.metadata.labels."beta.kubernetes.io/instance-type"," ",.metadata.labels."beta.kubernetes.io/arch", "\n"'|sort -k2 -r

 

Formatting the output with the Column command

In the preceding screenshot, you might have noticed that the output is not well formatted and columns were overlapping.

To avoid that and to print the output in a nice format.  we can use the column -t command at the end like this

kubectl get nodes -o json|jq -Cjr '.items[] | .metadata.name," ",.metadata.labels."beta.kubernetes.io/instance-type"," ",.metadata.labels."beta.kubernetes.io/arch", "\n"'|sort -k2 -r|column -t

here is the output of this command when executed on our end

kubectl sort machine type

 

Creating a CSV file report with the same Output

To create a CSV file report of this output all you have to do is to change the space to , on the JQ command

kubectl get nodes -o json|jq -Cjr '.items[] | .metadata.name,",",.metadata.labels."beta.kubernetes.io/instance-type",",",.metadata.labels."beta.kubernetes.io/arch", "\n"'|sort -k2 -r

this would replace the space between the columns with comma which can be saved into a file to make a CSV report that's all.

kubectl get nodes -o json|jq -Cjr '.items[] | .metadata.name,",",.metadata.labels."beta.kubernetes.io/instance-type",",",.metadata.labels."beta.kubernetes.io/arch", "\n"'|sort -k2 -r > k8sreport.csv

 

Conclusion

Hope this article helps and in this article hope you have learnt some tricks with kubectl to list the nodes by their arch type and their machine type especially for EKS.

These same commands can be tweaked to use for other managed clusters like AKS and GKE. we will try to add examples for other clusters soon.

Are you interested in DevOps/Cloud Automation solutions like this? please do reach out to us [email protected]

 

Cheers
Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading