简体   繁体   中英

Is there a way to log Kubernetes Pod memory usage, in a container running an application built on Node.js

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM