Setting size of /dev/shm for k8s container
Recently I needed to set the shared memory size for containers running in a kubernetes instance.
Unfortunately there is no explicit setting like docker or podman provide with the --shm-size
command line option.
But this can be done in a comprehensible approach by mounting a memory drive with specific size to /dev/shm
.
apiVersion: apps/v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-pod-ctr
image: <image>
volumeMounts:
- name: shmem # mount the volumed named `shmem` (see below)
mountPath: /dev/shm # as /dev/shm
volumes:
- name: shmem # create a volumed named `shmem`
emptyDir:
medium: Memory # specify it as memory drive
sizeLimit: 2Gi # and set its size
In this example the POD mounts a volume shmem
to /dev/shm
and defines this volume shmem
as memory drive with a size of 2 gigabytes.
That is the same as running the image directly with something like docker run --shm-size=2g <image>
.
By the way - what is the difference between 2G
and 2Gi
?
2Gi
is the traditional way of interpreting the prefix (by power of 2):
1 megabyte = 1,024 kilobyte = 1,048,576 byte
.
2G
is the new approach and conform with how the prefixes are interpreted in science (by power of 10):
1 megabyte = 1,000 kilobyte = 1,000,000 byte
.
It is also possibly to define the value in fractions.
1.5Mi
means 1.5 * 1,024 kilobyte = 1,536 kilobyte
and 2.1G
is 2.1 * 1,000 megabyte = 2,100 megabyte
.
See also the kubernetes specifications about quantity.