0

I need to measure and show to the user current k8s resource usage of the k8s pods. We gather metrics by using prometheus. I tried to compare the data which I get by using kubectl top command and prometheus query but get the different results, why?

To get metrics by using kubectl I'm using the following command:

kubectl top pod -n your-namespace --no-headers | awk '{cpu+=$2; memory+=$3} END {print "CPU: " cpu "m, Memory: " memory "Mi"}'

basically it get results from the all pods which related to specific namespace, sum it and return. When I run it for specific namespace I get the following value:

CPU: 3m, Memory: 1137Mi

But when I run the following query in Prometheus:

sum (container_memory_working_set_bytes{namespace="<same_namespace_as_above>"}) by (pod_name)

I get 2.33Gb

According to conversion calculator:

1137 MiB = 1192.230912 MB

The difference is huge and there's no correlation at all. What am I doing wrong?

Anatoly
  • 5,056
  • 9
  • 62
  • 136
  • Have you seen [this question](https://stackoverflow.com/questions/65428558/what-is-the-difference-between-container-memory-working-set-bytes-and-contain)? – markalex Jul 05 '23 at 15:20
  • @markalex not yet, will look at it, thank you. – Anatoly Jul 05 '23 at 16:46

0 Answers0