Generic Ephemeral Volumes

A Pod needs scratch space that goes away with it, but the space must have a fixed size on real storage instead of the Node disk. How do you get a volume like that, and what cleans it up after the Pod is gone?

Play the animation 5 steps, animated, plays on a computer or a tablet

Generic Ephemeral Volumes, Kubernetes diagram

Generic Ephemeral Volumes, step by step

  1. When the Pod is created, the ephemeral volume controller turns the inline volumeClaimTemplate into a real PVC in the same namespace, named after the Pod and the volume, app-0-scratch, with an ownerReference back at the Pod. A claim of that name the Pod does not own is left alone, and the Pod cannot start.

    Play from step 1
  2. The claim names a real StorageClass, so once a Node is picked under the recommended WaitForFirstConsumer mode, the provisioner calls CreateVolume as for any claim. Unlike emptyDir, the disk has a fixed size the Pod cannot exceed, and snapshots, clones or resizing work if the driver allows.

    Play from step 2
  3. The claim binds to the new PV, then the volume is attached and mounted at /scratch inside the container over CSI, exactly as for any ordinary PVC. The Pod starts and writes to a real, dynamically provisioned volume.

    Play from step 3
  4. The ownerReference is what makes this ephemeral: a normal PVC outlives the Pods that use it, but this one belongs to its Pod. Anyone who can create a Pod can create a claim this way, even without the right to create one directly, unless a webhook rejects such Pods. PVC quota still applies.

    Play from step 4
  5. Once the Pod is deleted, the ownerReference does the rest. Garbage collection removes the PVC, and under the default Delete reclaim policy the external-provisioner calls DeleteVolume, so the disk and its PV go too, and the scratch data with them. A class set to Retain would keep the disk.

    Play from step 5

Sources