What we are discussing in this blog?
Though seemingly minor, leveraging the kubectl CLI can be a game-changer for troubleshooting or comprehending worker configurations on AWS without the need to log into the console. This powerful tool allows you to glean intricate details about worker nodes effortlessly.
I recently found myself on the quest for a command to list all worker nodes along with their AMI IDs using kubectl. It took a bit of time to nail down the exact command that met my requirements. While describing nodes provides the necessary details, imagine the complexity when dealing with hundreds of nodes in a cluster. This is where the power of kubectl shines – it goes beyond the UI or eksctl/awscli, offering a versatile tool for in-depth analysis and insights into your Kubernetes environment.”
While my initial need was to list all nodes along with their AMI IDs using kubectl, I soon discovered its versatility in providing a wealth of information. This powerful tool can assist in various scenarios, such as listing Kubernetes worker nodes based on specific instance types, identifying those running on SPOT or On-Demand instances, pinpointing nodes in a particular region or zone, filtering by launch template or version, and even narrowing down results based on custom labels assigned during instance creation. It’s a comprehensive solution for diverse analysis and node management within your Kubernetes cluster.
This is a small article, but it can save some time for you. Initially, I resorted to using a for loop with kubectl describe
to gather the necessary data. However, I eventually discovered a more straightforward approach to retrieve the details I needed.
My requirement: Fetch nodes with its AMI details.
Way 1: Initially I was doing the describe on a for loop and fetching the details. For example; I made a list of nodes in a file and passed that to find the AMI details.
Save nodes to a text file
kubectl get nodes | grep -v NAME| awk {'print $1'} > nodes.txt
Then get the AMI details by doing kubectl describe in a loop
for i in $(cat nodes.txt); do echo "Node: $i and its AMI is: $(kubectl describe nodes $i | grep "eks.amazonaws.com/nodegroup-image"| cut -d "=" -f 2) "; done
This will meet you requirement. However, we have some better way to do this.
Way 2: By using kubectl directly
kubectl get nodes -o custom-columns=NAME:.metadata.name,AMI:'{.metadata.labels.eks\.amazonaws\.com/nodegroup-image}'
We can use these labels to understand the worker nodes details from CLI. EKS by default add few labels and also it includes the labels that we pass when we create the Node Groups. So we can use these labels for analysing the worker nodes from the CLI. Adding few other examples below:
Example 1: List out worker nodes which uses SPOT / ON DEMAND instances by using kubectl
kubectl get nodes -o custom-columns=NAME:.metadata.name,TYPE:'{.metadata.labels.eks\.amazonaws\.com/capacityType}'
Example 2: List out worker nodes with EC2 instance type by using kubectl
kubectl get nodes -o custom-columns=NAME:.metadata.name,TYPE:'{.metadata.labels.node\.kubernetes\.io/instance-type}'
Example 3: List out worker nodes running based on Availability Zone by using kubectl
kubectl get nodes -o custom-columns=NAME:.metadata.name,TYPE:'{.metadata.labels.failure-domain\.beta\.kubernetes\.io/zone}'
Yeah, not adding more. You can describe one worker node and check the available label and print based on your specific requirement. You can pipe and do further analysis grep/awk/sort for your requirement.
This will help you to create quick inventory of your EKS worker with these information.
Thanks for reading this. Share your suggestions and feedback.