Reputation: 91
]$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
ip<IP>.ec2.internal Ready master 300d v1.15.3
ip<IP>.ec2.internal Ready node 180d v1.15.3
ip<IP>.ec2.internal Ready master 300d v1.15.3
ip<IP>.ec2.internal Ready node 300d v1.15.3
ip<IP>.ec2.internal Ready node 300d v1.15.3
ip<IP>.ec2.internal Ready,SchedulingDisabled node 180d v1.15.3
ip<IP>.ec2.internal Ready node 180d v1.15.3
ip<IP>.ec2.internal Ready master 300d v1.15.3
ip<IP>.ec2.internal Ready node 300d v1.15.3
What I want is the output should have only list of node name showing which is first column and which are master only. I tried the script way:
#!/bin/bash
kubectl get nodes --selector=node-role.kubernetes.io/master > nodelist.txt
cat nodelist.txt
while IFS=" " read -r f1
do
echo $f1
done < nodelist.txt
, But I want any method using kubectl --custom-column or json filtering plz suggest.
Upvotes: 1
Views: 3227
Reputation: 53
kubectl get nodes -o 'json' | jq -r '.items[] | [.metadata.name] | @tsv'
Upvotes: 0
Reputation: 8451
Also you can use labels and jsonpath to select anything you need from kubectl get nodes -o json
output
kubectl get nodes -l node-role.kubernetes.io/master -o 'jsonpath={.items[*].metadata.name}'
btw, you could you kubernetes kubectl Cheat Sheet if you lost at any point. It has most frequently used commands
Upvotes: 5
Reputation: 128857
But I want any method using kubectl --custom-column or json filtering plz suggest.
Yes, you can use a --custom-columns
to only show the name
kubectl get nodes -o custom-columns=NAME:.metadata.name
NAME
my-node
In addition, you can omit the headers using --no-headers
kubectl get nodes -o custom-columns=NAME:.metadata.name --no-headers
my-node
Using the selector you provided, to only show master nodes, the full command is this:
kubectl get nodes --selector=node-role.kubernetes.io/master -o custom-columns=NAME:.metadata.name --no-headers
Upvotes: 0
Reputation: 538
I did not try this but it should give you the output you want.
kubectl get nodes | grep master | awk 'print {$1 $3}'
Upvotes: 0