0

I have a Kubernetes pod which shuts down and restarts itself because it went out of memory. There are multiple child processes which run asynchronously and use memory. I want each of them to log how much memory is available/used on the Kubernetes pod. The logs should be with the other Node js logs.

I know of process.memoryUsage(), os.totalmem(), os.freemem()

Which provide the following output respectively

{"ts":"2021-04-21T10:52:50.299Z","msg":"Node Memory Stats:{\"rss\":125792256,\"heapTotal\":94720000,\"heapUsed\":48927816,\"external\":1668086}","logger":"___","level":"info"}
{"ts":"2021-04-21T10:52:50.300Z","msg":"OS/POD Total Memory:17179869184, OS/POD Free memory:87195648","logger":"___","level":"info"}

I was wondering if there is any way I could do a system call to the pod, get its resources, and log in Node?

chresse
  • 5,486
  • 3
  • 30
  • 47
Farhan Qasim
  • 990
  • 5
  • 18

1 Answers1

2

You can simply check the values from /sys/fs/cgroup/cpu/ and /sys/fs/cgroup/memory/:

For the current CPU usage do a

$ cat /sys/fs/cgroup/cpu/cpuacct.usage

For the current memory usage do a

$ cat /sys/fs/cgroup/memory/memory.usage_in_bytes

Hint: Make sure you have added the resources section (requests and limits) to the deployment so that it can calculate the usage based on cgroup.

see https://stackoverflow.com/a/62717009/1560953

chresse
  • 5,486
  • 3
  • 30
  • 47
  • Eventually, this answer worked. I was able to print out this file directly from NodeJS by using file system. There was no need of any external library. – Farhan Qasim Apr 22 '21 at 07:19