Mount ephemeral volumes to pods when your application needs extra storage space but doesn't require data persistence. Ephemeral volumes are provisioned when a pod starts and deleted automatically when the pod is deleted, so you don't need to manage persistent volume claims (PVCs) manually.
This topic shows you how to add the ephemeral.volumeClaimTemplate field to a Deployment spec to provision a disk-backed ephemeral volume for each pod, and then verify that the PVC and persistent volume (PV) are automatically deleted when the pod is removed.
Why use ephemeral volumes
Generic ephemeral volumes work like emptyDir volumes — they're scoped to a single pod's lifetime and deleted when the pod is deleted. The key difference is that they're backed by a PVC, which gives you the full capabilities of a persistent storage driver:
-
Fixed capacity: Set an exact size limit per pod so the application cannot exceed its storage budget.
-
Network-attached storage: Back the volume with a cloud disk instead of node-local storage, which is often limited in size and shared across workloads.
-
StorageClass support: Configure performance tier, access mode, and other disk properties.
Use ephemeral volumes when your application needs temporary storage that is too large for node-local storage, or when you need a guaranteed capacity per pod. Common scenarios include:
-
Caching services: Applications that move infrequently accessed data from memory to disk benefit from an isolated, capacity-bounded volume per pod.
-
Log storage: Applications that generate large volumes of log data can write to a dedicated ephemeral volume instead of the shared node filesystem.
-
Scratch data: Batch-processing jobs that need high-throughput disk I/O without persisting data after the job completes.
How it works
When Kubernetes creates a pod with a volumeClaimTemplate in its ephemeral volume spec, the ephemeral volume controller creates a PVC in the same namespace as the pod. The PVC name follows a deterministic pattern: {pod-name}-{volume-name}. This predictable naming lets you look up the PVC directly without searching, as long as you know the pod name and volume name.
When the pod is deleted, the Kubernetes garbage collector deletes the PVC based on the resource ownership relationship. Because the default reclaim policy for StorageClasses is Delete, the underlying disk volume is deleted along with the PVC.
PVC naming follows the pattern{pod-name}-{volume-name}. If two pods in the same namespace have names that produce the same combined string (for example, a pod namedpod-awith a volume namedscratchand a pod namedpodwith a volume nameda-scratch), a naming conflict will occur. Use distinct pod names within a namespace to avoid this.
Prerequisites
Before you begin, ensure that you have:
-
An Alibaba Cloud Container Compute Service (ACS) cluster
-
kubectlconfigured to connect to the cluster — see Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster and Use kubectl on Cloud Shell to manage ACS clusters
Mount an ephemeral volume to a Deployment
-
Create a file named
ephemeral-example.yamlwith the following content:apiVersion: apps/v1 kind: Deployment metadata: name: ephemeral-example spec: replicas: 2 selector: matchLabels: pod: example-pod strategy: type: Recreate template: metadata: labels: pod: example-pod spec: containers: - name: nginx image: registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest resources: requests: cpu: 500m memory: 2Gi ephemeral-storage: 2Gi volumeMounts: - mountPath: "/scratch" name: scratch-volume volumes: - name: scratch-volume ephemeral: volumeClaimTemplate: metadata: labels: type: scratch-volume spec: accessModes: [ "ReadWriteOncePod" ] storageClassName: alicloud-disk-topology-alltype resources: requests: storage: 40GiThis Deployment creates two pods, each with a dedicated 40 GiB ephemeral volume mounted at
/scratch. Theephemeral.volumeClaimTemplatefield is supported on Deployments, StatefulSets, and standalone pods. The following table describes the fields insidevolumeClaimTemplate.spec:Field Description accessModesSet to ReadWriteOncePodso the volume is mounted by exactly one pod at a time.storageClassNameThe StorageClass to use. This example uses alicloud-disk-topology-alltype, the default ACS StorageClass, which provisions a performance level 1 (PL1) Enterprise SSD (ESSD). A PL1 ESSD supports 20 to 65,536 GiB. Disks are billed on a pay-as-you-go basis — see Billing of block storage devices and Prices of block storage devices.storageThe requested capacity of the ephemeral volume. -
Deploy the Deployment:
kubectl create -f ephemeral-example.yaml -
Check that both pods are running:
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE ephemeral-example-64db7b9f5c-hcpz5 1/1 Running 0 108s ephemeral-example-64db7b9f5c-qqvl5 1/1 Running 0 108s -
Check that a PVC was created for each pod:
kubectl get pvcExpected output:
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE ephemeral-example-64db7b9f5c-hcpz5-scratch-volume Bound d-uf6h6nl0yf2y1331**** 40Gi RWOP alicloud-disk-topology-alltype 3m28s ephemeral-example-64db7b9f5c-qqvl5-scratch-volume Bound d-uf6a3lk85znunou6**** 40Gi RWOP alicloud-disk-topology-alltype 3m28sThe PVC names follow the pattern
{pod-name}-scratch-volume. TheVOLUMEfield shows the disk ID of the underlying cloud disk.
Verify that ephemeral volumes are deleted with the pod
-
Scale the Deployment down to one replica:
kubectl scale deploy ephemeral-example --replicas=1 -
Confirm that one pod was deleted:
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE ephemeral-example-64db7b9f5c-qqvl5 1/1 Running 0 11m -
Check that the PV for the deleted pod was removed:
kubectl get pvExpected output (only the PV for the surviving pod remains):
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE d-uf6a3lk85znunou6**** 40Gi RWOP Delete Bound default/ephemeral-example-64db7b9f5c-qqvl5-scratch-volume alicloud-disk-topology-alltype 20m -
Check that the PVC for the deleted pod was also removed:
kubectl get pvcExpected output (only the PVC for the surviving pod remains):
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE ephemeral-example-64db7b9f5c-qqvl5-scratch-volume Bound d-uf6a3lk85znunou6**** 40Gi RWOP alicloud-disk-topology-alltype 15mThe PVC and its underlying disk were deleted together with the pod. The ephemeral volume for the remaining pod is still bound.