Skip to content

Kubernetes Volume mounting configuration

Kubernetes Volume mounting configuration defines how storage volumes are defined within a Pod specification and mapped into the container's filesystem. This process allows data to be shared between containers or survive beyond the container's lifecycle.^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]

Definition and Volumes

A volume is configured in two stages within a Pod definition: defining the volume itself, and then mounting it into a specific container.

Volumes are defined under the spec.volumes field in the Pod configuration^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]. This section acts as a declaration of the storage resources available to the Pod. For example, an emptyDir volume is defined here to create a temporary directory that shares the Pod's lifetime^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md].

Container Mounts

Once a volume is defined, it is attached to a specific container using the volumeMounts field within the spec.containers array^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md].

The volumeMounts configuration requires: * name: The name of the volume to mount, which must match a name defined in spec.volumes^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]. * mountPath: The path within the container's filesystem where the volume will be accessed (e.g., /usr/share/nginx/html or /html)^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md].

EmptyDir Configuration

The emptyDir volume type creates a temporary directory that is created when a Pod is assigned to a node and exists as long as that Pod runs^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md].

It accepts specific configuration fields: * medium: Specifies the storage medium. * default (default): Uses the node's default storage medium (e.g., disk). * Memory: Uses a RAM-backed filesystem (tmpfs). This offers high performance but storage is constrained by available memory^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]. * sizeLimit: Sets a quota for the storage usage (e.g., 256Mi). This is particularly recommended when using the Memory medium to prevent the container from exhausting node memory^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md].

Multi-Container Sharing

A key feature of mounting volumes is the ability to share data between containers running in the same Pod^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]. By referencing the same volume name in the volumeMounts section of different containers, those containers can read and write to the same storage location^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md].

For example, an alpine container can write logs to a mounted directory, while an nginx container serving that directory via its own mount displays the updated content in real-time^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md].

Verification

To verify mount configurations, you can inspect a running Pod using kubectl describe [Pod](<./pod.md>)/<pod-name>^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]. The output lists the Mounts for each container, showing the mapping between the host path and the container path^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md].

Sources

^[400-devops-06-kubernetes-k8s-ithelp-day17-readme.md]