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
