How to dump the resource (CPU, memory) usage per namespace in k8s?

1/24/2019

I have a list of namespaces created under the same k8s cluster and I'd like to find out the resource (CPU, memory) usage per namespace. Is there any command I can use?

-- injoy
kubectl
kubernetes

3 Answers

1/25/2019

Thanks Rico, the answer is good but just as an addition:

You can specify resource quotas and then view them as specified here. Other than that, there are external monitoring tools like Prometheus. Also, there is a Resource Explorer which can:

Display historical statistical resource usage from StackDriver.

https://github.com/kubernetes/kubernetes/issues/55046

List resource QoS allocation to pods in a cluster. Inspired by:

https://github.com/kubernetes/kubernetes/issues/1751

The case is still open on GitHub, but it seems there should be some changes eventually as one of the contributors states there is a plan to remove kubectl top and using some native solutions so I advise to follow this thread.

-- aurelius
Source: StackOverflow

1/27/2019

Write a shell script to get all namespaces in the cluster. Iterate through each namespace. Run kubectl top pod. Add up the cpu and memory of all pods in the name space.

-- P Ekambaram
Source: StackOverflow

1/25/2019

Yes. You can use

$ kubectl -n <nampespace> top pod

For example:

$ kubectl top pod -n kube-system
NAME                                                                 CPU(cores)   MEMORY(bytes)
calico-node-xxxxx                                                    17m          166Mi
coredns-xxxxxxxxxx-xxxxx                                             2m           11Mi
coredns-xxxxxxxxxx-xxxxx                                             3m           11Mi
etcd-ip-x-x-x-x.us-west-2.compute.internal                           19m          149Mi
kube-apiserver-ip-x-x-x-x.us-west-2.compute.internal                 39m          754Mi
kube-controller-manager-ip-x-x-x-x.us-west-2.compute.internal        20m          138Mi
kube-proxy-xxxxx                                                     5m           12Mi
kube-scheduler-ip-x-x-x-x.us-west-2.compute.internal                 6m           17Mi
metrics-server-xxxxxxxxxx-xxxxx                                      0m           15Mi

You need to add up all the entries on the CPU and MEMORY columns if you want the total.

Note that for kubectl top to work you need to have the metrics-server set up and configured appropriately. (Older clusters use the heapster)

-- Rico
Source: StackOverflow