Overview

9 Adding Volumes for Storage, Configuration, and Metadata

This chapter explains how Kubernetes volumes complement containers inside a pod to handle data persistence, file sharing, configuration, and metadata. It clarifies that containers have ephemeral filesystems, so volumes are defined at the pod level and mounted into containers to persist selected files across container restarts, share data between sidecars, or expose configuration and pod details as files. You learn when and why to use different volume types, how mounts behave, and what trade-offs exist for security, performance, and lifecycle. The chapter focuses on ephemeral volumes such as emptyDir, hostPath, configMap, secret, downwardAPI, projected, and the new image volume, while pointing to persistent storage via PersistentVolumeClaim in the next chapter.

Through hands-on examples, you add an emptyDir volume to a quiz service pod so MongoDB data survives container restarts, then initialize it with data via an init container and, later, more simply via an image volume that mounts an OCI image’s files read-only (feature-gated). You also see how one volume can be mounted in multiple containers to share files (for example, a quote writer and nginx), how mount options like readOnly and recursiveReadOnly improve safety, and how subPath mounts target a single file without hiding an existing directory. The chapter covers performance and security nuances—using tmpfs (emptyDir.medium: Memory) for speed or sensitive data, applying size limits, and remembering that emptyDir data is lost when the pod is deleted.

The chapter then shows how hostPath exposes the node’s filesystem into a pod and why it’s powerful but dangerous and typically reserved for privileged/system workloads. You project configuration and sensitive material as files with configMap and secret volumes, mark them optional, and understand their atomic, symlink-based update mechanism; moreover, you learn file permission and ownership controls (defaultMode, per-item mode, and fsGroup) to make applications like Envoy read keys securely. Pod metadata and resource values can be surfaced as files via downwardAPI, and multiple sources can be merged into a single directory with projected volumes—mirroring how every pod gets a built-in kube-api-access projected volume for API authentication. Overall, you gain practical patterns and guardrails for storage, configuration, and metadata within pod lifecycles, setting the stage for persistent storage in the following chapter.

Mounting a filesystem into the file tree
How the Quiz service fits into the architecture of the Kiada Suite
The Quiz API and the MongoDB database run in the same pod
Volumes are defined at the pod level and mounted in the pod’s containers
Volumes ensure that part of the container’s filesystem is persisted across restarts
A pod can contain multiple volumes and a container can mount multiple volumes
A volume can be mounted into more than one container
Pod volumes can also map to storage volumes that persist across pod restarts
Using volumes to share data between pods
The quiz pod with an emptyDir volume for storing MongoDB data files
The emptyDir is a normal file directory in the node’s filesystem that’s mounted into the container
Using an init container to initialize an emptyDir volume
The new version of the Quote service uses two containers and a shared volume
A hostPath volume mounts a file or directory from the worker node’s filesystem into the container.
Using subPath to mount a single file from the volume
Projecting a Secret’s entries into the container’s filesystem via a secret volume
Using a projected volume with several sources

Summary

  • Pods consist of containers and volumes. Each volume can be mounted at the desired location in the container’s filesystem.
  • Volumes are used to persist data across container restarts, share data between the containers in the pod, and even share data between the pods.
  • An emptyDir volume is used to store data for the lifetime of the pod. It starts as an empty directory that’s created just before the Pod’s containers are started and is deleted when the Pod terminates.
  • An init container can be used to add files to the emptyDir volume before the Pod’s regular containers are started. Regular containers can then add additional files or modify existing files in the volume.
  • An image volume can be used to mount an Open Container Initiative (OCI) image or artifact into a container. This is used for static, possibly large files that the container needs for its operation.
  • The hostPath volume allows a pod to access any path in the file system of the host node. This volume type is dangerous because it allows users to make changes to the configuration of the host node and run any process on the node.
  • The configMap, secret, and downwardAPI volumes are used to project ConfigMap and Secret entries as well as Pod metadata into a container. Alternatively, the same can be done with a single projected volume.
  • Many other volume types are no longer meant to be directly configured in Pods. Instead, a persistentVolumeClaim, ephemeral, or csi volume must be used, but this is explained in the next chapter.

FAQ

What problem do Kubernetes volumes solve compared to container filesystems?Containers have isolated, ephemeral filesystems that are recreated on restart, so any data written inside a container is lost when it’s replaced. Volumes are defined at the Pod level and mounted into containers, giving processes a place to read/write data that survives container restarts. Depending on the volume type, data can persist only for the Pod’s lifetime (ephemeral) or beyond it (persistent storage in the next chapter).
When should I use an emptyDir volume, and what are its key options?Use emptyDir to persist data across container restarts within the same Pod and/or to share files between containers in that Pod. It’s created empty when the Pod starts and deleted when the Pod is removed. Key options: medium (default disk or Memory for tmpfs) and sizeLimit. Memory is faster and safer for sensitive data but consumes RAM.
How do multiple containers in a Pod share files safely?Add a volume to the Pod and mount it into each container at the needed paths. You can make one mount read-only and another read/write to reduce risk (for example, a writer sidecar produces files; a web server container reads them with readOnly: true). Each container can mount the same volume at different paths to match its expected directory structure.
How can I pre-populate data in a volume when the Pod starts?Common approaches: 1) Use an init container that writes files into a shared emptyDir before main containers start; 2) For small files, create them inline with a simple shell heredoc in an init container; 3) Use the image volume type to mount files directly from an OCI image (feature-gated). All ensure the target container sees the files at startup.
What is the image volume type and when would I use it?The image volume type exposes files from an OCI image as a read-only volume for other containers in the same Pod. It’s ideal for shipping configuration, seed data, or model files without building custom runtime images or copying via init containers. Note: it requires the ImageVolume feature gate to be enabled on the cluster.
What is a hostPath volume and why is it risky?hostPath mounts a file or directory from the node’s filesystem into a container. It’s useful for system-level pods that need node logs, devices, or sockets, but it’s dangerous because it grants powerful access to the host. If misused, a pod could modify host files (e.g., via /var/run/docker.sock). You can set a type (Directory, File, …) to validate the path and optionally create it.
How do ConfigMap and Secret volumes behave, and do changes auto-refresh?Mounting a configMap or secret volume projects each key as a file. Updates to the source object are reflected automatically in the files via a symlink-based mechanism (atomic refresh), typically within a minute. Caveats: mounting a volume over a directory hides preexisting files there; using subPath mounts a single file and disables live updates for that file.
How can I control file permissions and ownership in configMap/secret volumes?Use defaultMode to set default file permissions and per-item mode for specific files (remember YAML octal needs a leading 0, e.g., 0640). Files in secret volumes live on tmpfs. To make restricted files readable by your process, set pod.spec.securityContext.fsGroup to match the group your process runs with so group-readable files are accessible.
What is a downwardAPI volume and what can it expose?A downwardAPI volume writes Pod and container metadata to files, such as metadata.name, labels, annotations, or resource requests/limits. Use fieldRef for Pod fields and resourceFieldRef for container resource values (specify containerName and optional divisor). This is useful when apps prefer reading values from files instead of env vars.
What is a projected volume, and what is the built‑in kube‑api‑access volume?A projected volume merges data from multiple sources (ConfigMaps, Secrets, Downward API, and ServiceAccount tokens) into a single directory, avoiding subPath limitations and preserving auto-updates. Kubernetes automatically adds a kube-api-access projected volume to most Pods, containing the ServiceAccount token, cluster CA cert, and namespace; you can disable it with automountServiceAccountToken: false.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Kubernetes in Action, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Kubernetes in Action, Second Edition ebook for free