How to get CPU Utilization ,Memory Utilization of namespaces,pods ,services in kubernetes?

11/18/2019

For services, pods, namespaces we don't get CPU utilization, memory Utilization metrics,we get Memory request , CPU request, Memory allocated, CPU allocated using CoreV1Api. How to get Utilization for above?

For Cluster, we did average of CPU Utilization of nodes (EC2 instances Ids) , so we have cpu/memory utilization for cluster, So can we assume, CPU Utilization for pod will be utilization of node on which it is running?For namespace, on which node ,it exists?

-- Dhanraj
kubectl
kubernetes

1 Answer

11/18/2019

You can get resources request, limits or utilization for a couple of ways.

$ kubectl top pods or $ kubectl top nodes. You can check Quota or kubectl describe node/pod to check information inside. You can also specify if you need pods form only one namespace like kubectl top pod --namespace=kube-system

To do that you will need metric server which is usually installed on the beginning. To check if it is installed you should list your pods in kube-system namespaces.

$ kubectl get pods -n kube-system
NAME
...
metrics-server-v0.3.1-57c75779f-wsmfk                   2/2     Running   0          6h24m
...

Then you can check current metrics in a few ways. Check this thread. When you will list raw metrics is good to list it using jq. $ /apis/metrics.k8s.io/v1beta1/nodes | jq .

Another thing is that you could use Prometheus for metrics and alerting (depends on your needs). If you want only CPU and memory utilization, metrics server is enoug, however Prometheus also installing custom.metrics which will allow you to get metrics from all kubernetes objects.

Later you can also install some UI like Grafana.

So can we assume, CPU Utilization for pod will be utilization of node on which it is running

CPU utilization for node will display utilization of all resources assigned to this node, even if pods are in different namespaces.

I would encourage you to check this article.

-- PjoterS
Source: StackOverflow