liveBook · Manning

Ephemeral volume

As explained in Kubernetes in Action, Second Edition

An ephemeral persistent volume is a PersistentVolume whose storage exists only for the associated workload's lifetime rather than being retained long term.

An ephemeral volume gives one pod a directory for data needed only while that pod exists. Unlike an emptyDir volume, it uses a PersistentVolumeClaim (PVC) and PersistentVolume (PV), so it can draw on persistent-storage features without keeping the volume after the pod is deleted. section 10.5.2 section 9.6

Define the volume in the pod manifest with an inline volumeClaimTemplate, then mount it in a container. Kubernetes creates a PVC exclusively for that pod and uses it to provision and bind a PV. The PVC’s lifecycle follows the pod’s lifecycle; you do not need to create a separate PVC manifest. section 10.5.1 section 9.6

Listing 10.15 Defining an ephemeral volume in a pod manifest
apiVersion: v1
kind: Pod
metadata:
  name: demo-ephemeral
spec:
  volumes:
  - name: my-volume           #1
    ephemeral:                #1
      volumeClaimTemplate:      #2
        spec:                   #2
          accessModes:          #2
          - ReadWriteOnce       #2
          resources:            #2
            requests:           #2
              storage: 1Gi      #2
  containers: #2
  - name: main
    image: busybox
    command:
    - sh
    - -c
    - | 
      echo "This is a demo of a Pod using an ephemeral volume." ;
      touch /mnt/ephemeral/file-created-by-$HOSTNAME.txt ;
      sleep infinity
    volumeMounts:
    - mountPath: /mnt/ephemeral
      name: my-volume
  terminationGracePeriodSeconds: 0

The pod manifest requests 1Gi with ReadWriteOnce access and mounts the volume at /mnt/ephemeral. With no storageClassName specified, it requests the cluster’s default StorageClass. section 10.5.1

Check the volume after creating the pod

Apply the example manifest, then run kubectl get pvc demo-ephemeral-my-volume. The PVC name combines the pod name and volume name, and its status becomes Bound. Run kubectl exec demo-ephemeral -- ls /mnt/ephemeral to see the file the container created on the mounted volume. section 10.5.1

Choose this type when scratch data should end with the pod but needs PV features. The PV can be local or network-attached; the volume can support snapshots, initialization from other data sources, a fixed size, and later resizing. For storage meant to outlive a pod, use a separately managed PVC instead. section 10.5.2 section 10.5

How Kubernetes in Action, Second Edition covers it

How deep it goes
Walks through a pod manifest, automatic claim creation, binding, and mount verification without detailing CSI driver internals.
Examples
Uses a 1Gi ReadWriteOnce pod manifest, a generated claim name, kubectl status checks, and a file visible in the mount.
What it assumes
Assumes familiarity with pods and PersistentVolumeClaims while showing how to place a claim template inside a pod manifest.
What you can do after
Build it from scratch with volumeClaimTemplate in a pod manifest, then verify the PVC and mounted file using kubectl.
How it connects
Connects ephemeral volumes to PersistentVolumeClaims, PersistentVolumes, StorageClasses, CSI drivers, emptyDir volumes, and pod lifecycles.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.