Determine CPU and memory usages by a pod

3/9/2021

I'm looking for a way to find:

  1. The current usage of CPU and RAM of each pod running.
  2. The configured CPU and RAM of each pod.

One side is to identify the resource usage, and the other is to identify if it was patched manually or via the deploy YAML.

-- Frank N Stein
kubectl
kubernetes

2 Answers

3/9/2021

You can deploy a metrics-server on your cluster to get resources usage:

Metrics Server is a scalable, efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines ... Metrics API can also be accessed by kubectl top ...

Then you can use kubectl top to view current resources usage. e.g.:

$ kubectl top pods --all-namespaces
NAMESPACE     NAME                                        CPU(cores)   MEMORY(bytes)   
kube-system   coredns-74ff55c5b-vgfzw                     5m           13Mi            
kube-system   etcd-minikube                               32m          46Mi            
kube-system   ingress-nginx-controller-65cf89dc4f-crrr9   6m           204Mi           
kube-system   kube-apiserver-minikube                     99m          295Mi           
kube-system   kube-controller-manager-minikube            32m          53Mi            
kube-system   kube-proxy-9mfb9                            0m           23Mi            
kube-system   kube-scheduler-minikube                     4m           17Mi            
kube-system   metrics-server-56c4f8c9d6-48rdd             1m           12Mi            
kube-system   storage-provisioner                         2m           9Mi

You can kubectl describe nodes to get an overview of requests/limits configurations for pods running on each node. e.g.:

Non-terminated Pods:          (13 in total)
  Namespace                   Name                                         CPU Requests  CPU Limits  Memory Requests  Memory Limits  AGE
  ---------                   ----                                         ------------  ----------  ---------------  -------------  ---
  default                     my-nginx-5b56ccd65f-txkfg                    0 (0%)        0 (0%)      0 (0%)           0 (0%)         4m48s
  default                     my-nginx-5b56ccd65f-wkhms                    0 (0%)        0 (0%)      0 (0%)           0 (0%)         4m48s
  kube-system                 coredns-74ff55c5b-vgfzw                      100m (0%)     0 (0%)      70Mi (0%)        170Mi (1%)     4d
  kube-system                 etcd-minikube                                100m (0%)     0 (0%)      100Mi (0%)       0 (0%)         17h
  kube-system                 ingress-nginx-controller-65cf89dc4f-crrr9    100m (0%)     0 (0%)      90Mi (0%)        0 (0%)         3d23h
  kube-system                 kube-apiserver-minikube                      250m (2%)     0 (0%)      0 (0%)           0 (0%)         17h
  kube-system                 kube-controller-manager-minikube             200m (1%)     0 (0%)      0 (0%)           0 (0%)         4d
  kube-system                 kube-proxy-9mfb9                             0 (0%)        0 (0%)      0 (0%)           0 (0%)         4d
  kube-system                 kube-scheduler-minikube                      100m (0%)     0 (0%)      0 (0%)           0 (0%)         4d
  kube-system                 metrics-server-56c4f8c9d6-48rdd              0 (0%)        0 (0%)      0 (0%)           0 (0%)         4m18s
  kube-system                 my-nginx-5b56ccd65f-96n7v                    0 (0%)        0 (0%)      0 (0%)           0 (0%)         3d23h
  kube-system                 my-nginx-5b56ccd65f-sm7w5                    0 (0%)        0 (0%)      0 (0%)           0 (0%)         3d23h
  kube-system                 storage-provisioner                          0 (0%)        0 (0%)      0 (0%)           0 (0%)         4d

0 means no request/limits defined.

-- Eduardo Baitello
Source: StackOverflow

3/11/2021

The first part of your question is answered with the kubectl top command. The second part is here

You specify the initial cpu and memory and the max cpu and memory in the pod spec.

spec:
  containers:
  - name: cpu-demo-ctr
    image: vish/stress
    resources:
      limits:
        cpu: "1"
        memory: "400Mi"
      requests:
        cpu: "0.5"
        memory: "200Mi"

There is a guide in the Kubernetes documentation here : enter link description here

-- Henrik Hoegh
Source: StackOverflow