Generic Ephemeral Volumes

What if you want scratch space that behaves like emptyDir but has a fixed size on a real StorageClass? A generic ephemeral volume is an inline volumeClaimTemplate written on the Pod, and it gets a real PVC, provisioned and mounted like any other claim. Its lifetime is still the Pod: the PVC carries an ownerReference and is collected when the Pod goes, taking the disk with it unless its reclaim policy is Retain. It bridges scratch and the real machinery.

Play the animation 5 steps, animated, best on a wide screen

Generic Ephemeral Volumes, Kubernetes diagram

Generic Ephemeral Volumes, step by step

  1. When the Pod is created, the ephemeral volume controller turns that inline template 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. Nothing about this path is a shortcut.

    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. Delete the Pod and 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. The scratch data lived as long as the Pod. A class set to Retain would keep the disk.

    Play from step 5

Sources