How to get with custom-columns resource limits and requests of all containers in namespace in kubernetes

6/8/2021

I tried this:

k get po -n myns -o=custom-columns=NAME:.spec.containers.name,MEMREQ:.spec.containers.resources.requests.memory,MEMLIM:.spec.containers.resources.limits.memory,CPUREQ:.spec.containers.resources.requests.cpu,CPULIM:.spec.containers.resources.limits.cpu

but it returned me this:

    NAME     MEMREQ   MEMLIM   CPUREQ   CPULIM
    <none>   <none>   <none>   <none>   <none>
    <none>   <none>   <none>   <none>   <none>
    <none>   <none>   <none>   <none>   <none>
    <none>   <none>   <none>   <none>   <none>
    <none>   <none>   <none>   <none>   <none>
    <none>   <none>   <none>   <none>   <none>
    <none>   <none>   <none>   <none>   <none>
-- Win32Sector
kubernetes

1 Answer

6/8/2021

Note that containers is a list, so you would have to either supply the index or * for all containers.

Here is an example from my environment, you can tweak your rest of the command.

kubectl get po -o=custom-columns=NAME:.spec.containers[*].name
NAME
nginx
nginx



kubectl get po -o=custom-columns=NAME:.spec.containers.name
NAME
<none>
<none>

To visualize the structure, here is an example with one pod, notice that a list is returned in the output of this command:

kubectl get pod my-nginx-66b6c48dd5-gq6vd  -o jsonpath='{.spec.containers}'
[{"image":"nginx:1.14.2","imagePullPolicy":"IfNotPresent","name":"nginx","ports":[{"containerPort":80,"protocol":"TCP"}],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount","name":"default-token-cbmr2","readOnly":true}]}]
-- P....
Source: StackOverflow