Volumes and ephemeral storage

Beginner · 19 min

Storage that lives and dies with a Pod: emptyDir on disk or in memory, why hostPath is dangerous, image and ephemeral volumes, and ephemeral-storage limits.

Before thisPods in depth

Cloud With VarJosh · 20 min · from 0:29

Volumes and ephemeral storage

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Cloud With VarJoshReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A container's own filesystem is thrown away when the container restarts. A volume is declared on the Pod, mounted into its containers, and lives at least as long as the Pod.
  • emptyDir is scratch space that survives container restarts but not Pod deletion. Containers in a Pod share it, and medium Memory puts it in RAM, counted against memory.
  • hostPath mounts a directory from the node. It ties a Pod to one node and can expose the node itself, so Pod Security's baseline level forbids it.
  • Image volumes (GA in 1.36) mount an OCI image's content read-only. Generic ephemeral volumes create a per-Pod PVC from a template, so scratch space can come from any storage class.
  • Writable layers, logs and disk-backed emptyDirs count as ephemeral storage. Set ephemeral-storage requests and limits and emptyDir sizeLimit, or the kubelet evicts Pods when the node's disk runs low.

Files that vanish

A container starts from its image plus a thin writable layer. Anything the process writes goes into that layer, and when the container restarts (a crash, an OOM kill, a failed liveness probe) the kubelet starts a fresh container from the image again. The writes are gone. That’s a feature for stateless apps and a problem for everything else: a cache that has to rebuild after every crash, two containers in a Pod that need to hand files to each other, a sidecar that ships the app’s log files.

Kubernetes’s answer, like Docker’s, is a volume: a directory that exists outside any single container’s filesystem. The difference is where it’s declared. A volume belongs to the Pod (spec.volumes), and each container chooses where to mount it (volumeMounts). This lesson covers volumes that live and die with the Pod. The rest of the storage group is about data that has to outlive it.

emptyDir: the Pod’s scratch space

spec:
  containers:
  - name: app
    image: busybox:1.37
    volumeMounts:
    - {name: cache, mountPath: /cache}
    - {name: shm, mountPath: /dev/shm}
  volumes:
  - name: cache
    emptyDir: {sizeLimit: 500Mi}
  - name: shm
    emptyDir: {medium: Memory, sizeLimit: 256Mi}

An emptyDir is created empty when the Pod is assigned to a node and deleted when the Pod leaves it. It survives container restarts, because the Pod hasn’t gone anywhere. Every container in the Pod can mount it, which makes it the standard way to share files between an app and its sidecar, or between an init container that downloads something and the app that uses it. Think of it as a whiteboard in a meeting room: everyone in the room can write on it, it’s still there if someone steps out and comes back, and it’s wiped when the meeting ends.

By default it lives on the node’s disk, under the kubelet’s directory. medium: Memory makes it a tmpfs instead: fast, never written to disk, and charged to the Pod’s memory, so a big file written there can get the container OOM-killed. A common use is a larger /dev/shm for databases and ML frameworks, since the runtime default is only 64 MiB. sizeLimit caps either kind. For disk-backed emptyDirs the kubelet checks usage periodically and evicts the Pod when it exceeds the limit.

Kubernetes 1.37 added two alpha options, off by default: an emptyDir mode (for example 01777, so containers sharing it can’t delete each other’s files) and bindMountOptions like noexec and nosuid on volume mounts, so a writable volume can’t become a place to drop and run binaries.

hostPath: a hole in the wall

hostPath mounts a directory from the node itself. It’s how node agents (log collectors, CSI drivers, monitoring) read node files, and it’s dangerous anywhere else. It ties the Pod’s data to whichever node it happened to land on. Worse, it can hand the Pod the node: mount /var/run/containerd or / and the Pod can control the container runtime or rewrite system files. Pod Security treats it accordingly. In a namespace labelled pod-security.kubernetes.io/enforce=baseline on our lab cluster, a Pod mounting /var/run/containerd was rejected with violates PodSecurity "baseline:latest": hostPath volumes (volume "h"). If you need durable node-local disk, use a local PersistentVolume, covered next.

The other ephemeral volumes

You’ve already met ConfigMap, Secret, downward API and projected volumes: ephemeral volumes the kubelet fills from API objects. Three more are worth knowing.

Image volumes, GA since 1.36, mount the contents of an OCI image or artifact read-only, without running it. That’s handy for shipping a large model, a static site or a plugin bundle separately from the app image:

volumes:
- name: model
  image: {reference: "registry.example.com/models/llama:3", pullPolicy: IfNotPresent}

Generic ephemeral volumes give each Pod its own PersistentVolumeClaim, created from a template and deleted with the Pod. You get scratch space from any storage class (a fast local NVMe class, say, or a network disk bigger than the node’s root disk), with the same lifecycle as an emptyDir:

volumes:
- name: work
  ephemeral:
    volumeClaimTemplate:
      spec:
        accessModes: [ReadWriteOnce]
        storageClassName: fast
        resources: {requests: {storage: 10Gi}}

That’s real control-plane behaviour you can watch. When we created a Pod named scratch with that volume, the ephemeral volume controller immediately created a PVC named scratch-work (Pod name plus volume name), owned by the Pod so it’s garbage-collected with it. Since our fast class didn’t exist, the scheduler refused to place the Pod: pod has unbound immediate PersistentVolumeClaims.

CSI ephemeral volumes are inline volumes that a CSI driver creates per Pod, used by things like the Secrets Store CSI driver.

The old gitRepo volume is gone in practice: disabled by default since 1.33 and locked off since 1.36. The API still accepts it with a warning, deprecated in v1.11, and disabled by default in v1.33+, but the kubelet refuses to set it up. Use an init container that runs git clone into an emptyDir.

Ephemeral storage is a resource

Node disk is finite, and Pods share it. The kubelet counts a container’s writable layer, its logs, and disk-backed emptyDirs as ephemeral storage, and you can request and limit it like CPU and memory:

resources:
  requests: {ephemeral-storage: 1Gi}
  limits: {ephemeral-storage: 2Gi}

The scheduler uses the request to avoid packing Pods onto a node whose disk can’t hold them. The kubelet enforces the limit by evicting the Pod, not by failing writes. When the node itself runs low on disk it sets the DiskPressure condition and evicts Pods, those using the most beyond their requests first. A Pod that logs to a file inside its container and never rotates it is the classic victim. The requests and limits lesson covers the scheduling side.

About the video

The embed is a 20-minute clip, 0:29 to 20:03, of Cloud With VarJosh’s “Day 26: Kubernetes Volumes | Ephemeral Storage | emptyDir & downwardAPI DEMO” from April 2025’s CKA course. The clip covers emptyDir volumes as ephemeral storage and a demo of two containers sharing data through one. The later part of the same video is the clip in the downward API lesson. It doesn’t cover hostPath, image or generic ephemeral volumes or ephemeral-storage limits, which this lesson adds. Judged from chapters and description, since transcripts can’t be fetched here, and from its “English (US) original” audio track. CyberRavindra’s 2026 video in deeper is a shorter take on emptyDir with sidecar log shipping.

What was run: against a local v1.37.3 API server whose nodes run no containers, we applied the Pod above with emptyDir, memory-backed, image and generic ephemeral volumes and ephemeral-storage resources, watched the ephemeral volume controller create the owned scratch-work PVC and the scheduler refuse the Pod, saw Pod Security baseline reject hostPath, and saw the gitRepo deprecation warning. Nothing mounted, so eviction and tmpfs behaviour are from the docs and feature-gate history in the v1.37.3 source, checked 2026-11-28.

Where this fits

Scratch space dies with the Pod. Data that must survive Pods, nodes and reschedules needs storage the cluster manages separately. Next: PersistentVolumes and PersistentVolumeClaims.

Check yourself

  1. 01A container in a Pod crashes and restarts. Is the data in its emptyDir volume still there? What if the Pod is deleted and recreated by its Deployment?

    After a container restart, yes: the emptyDir belongs to the Pod, which is still on the same node. After Pod deletion, no: the new Pod gets a new, empty emptyDir, possibly on a different node.

  2. 02Why does a team's Pod using emptyDir with medium Memory get OOM-killed when it writes a large file there?

    A memory-backed emptyDir is tmpfs, and its pages are charged to the Pod's memory cgroup. Writing a big file there uses memory, not disk, so it counts against the container's memory limit.

  3. 03A Pod writes 5 GiB of logs to a path inside its container filesystem and gets evicted. Its ephemeral-storage limit is 2Gi. Why?

    The container's writable layer, its logs and disk-backed emptyDirs all count toward ephemeral storage. Exceeding the limit makes the kubelet evict the Pod. Log to stdout with rotation, or write to a properly sized volume.

  4. 04What's wrong with mounting /var/run/containerd from the host in an app Pod?

    It hands the Pod the container runtime's socket, which can start privileged containers on the node, a straightforward node takeover. hostPath in general breaks isolation, which is why Pod Security baseline rejects it.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark