Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
919 views
in Technique[技术] by (71.8m points)

kubectl - Kubernetes question during CKA exam, how would you build this?

I was doing the CKA and stumbled upon a question I couldn't figure out. I don't remember all the details, but it goes something like this:

Get the top 1 node/pod by CPU consumption and place it in a file at {path}.

kubectl top nodes/pod --sort-by cpu <-- this orders by ascending. So you have to hardcode the last node/pod.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you need to extract out the name of the top pod and save it in the file you can do this:

Let us say you have 3 pods:

$ kubectl top pod --sort-by cpu
NAME                            CPU(cores)   MEMORY(bytes)
nats-depl-6c4b4dfb7c-tjpgv      2m           4Mi
project-depl-595bbd56db-lb6vb   8m           180Mi
auth-depl-64cccc484f-dn7w5      4m           203Mi

You can do this:

$ kubectl top pod --sort-by cpu | head -2 | tail -1 | awk {'print $1'}
chat-depl-6dd798699b-l7wcl   #### < == returns name of the pod

## you can redirect it to any file 
$ kubectl top pod --sort-by cpu | head -2 | tail -1 | awk {'print $1'} > ./file_name
$ cat ./file_name 
chat-depl-6dd798699b-l7wcl   #### < == returns name of the pod



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...