Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get the currently used and maximum available memory for a Docker container running in Kubernetes
# These commands demonstrate how to determine how much memory is availble on a k8s worker node and how much memory is
# available to the k8s _container_ hosted on that node.
# Source:
# https://shuheikagawa.com/blog/2017/05/27/memory-usage/
#
# Other interesting articles:
# https://docs.docker.com/engine/docker-overview/#the-underlying-technology
# https://docs.docker.com/config/containers/resource_constraints/
#
# These values are approximations, because according to the first article, calculating/determining available memory
# is difficult.
#
# In this example, the k8s deployment file is set with the following resource requests and limits values:
cat my-deployment-file.yml
spec:
# ...
template:
# ...
spec:
containers:
- name: [name of container]
image: [image]
resources:
requests:
cpu: 200m
memory: 1024M
limits:
cpu: 400m
memory: 2048M
# Get a shell onto the pod
kubectl exec -it [valid pod name in your k8s cluster] -- /bin/sh
# Get the memory available on the node:
/ # free -m
total used free shared buffers cached
Mem: 7986 6940 1045 0 552 3978
-/+ buffers/cache: 2409 5577
Swap: 0 0 0
# Get the memory usage of the container (in bytes)
/ # cat /sys/fs/cgroup/memory/memory.usage_in_bytes
443748352 (443748352/1024^2 ~= 423.19MB)
# Get the memory limit of the container (in bytes)
/ # cat /sys/fs/cgroup/memory/memory.limit_in_bytes
2048000000 (2048000000/1024^3 ~= 1953.13MB)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.