Ephemeral volumes can be used to store temporary data such as cache data. When you create a Deployment, you can use the persistent volume claim (PVC) template VolumeClaimTemplate
to create an ephemeral volume for each pod. The PVCs and persistent volumes (PVs) created by using this PVC template can be automatically deleted together with the pods that use them. This topic describes how to use a Deployment to create ephemeral volumes and how to verify that the ephemeral volumes can be deleted together with the pods.
Prerequisites
A Container Service for Kubernetes (ACK) cluster that runs Kubernetes 1.22 or later is created. For more information, see Create an ACK managed cluster.
You cannot use Deployments to create ephemeral volumes for ACK Serverless clusters.
Use scenarios
Applications use ephemeral volumes to store temporary data that does not need to be persisted.
Applications usually generate large amounts of log data. You can use ephemeral, exclusive volumes to store log data.
Use a Deployment to create ephemeral volumes
When you create a Deployment, you can use VolumeClaimTemplate
to automatically create PVCs and PVCs. VolumeClaimTemplate
is a PVC template. The number of PVCs that the system creates with this template is equal to the number of replicated pods specified in the Deployment. The PVCs use the same configuration but different names.
Use the following code block to create a Deployment that deploys two pods.
In addition to Deployments, you can also create ephemeral volumes by using StatefulSets and pod YAML files.
Parameter
Description
replicas
In this example, two replicated pods are created.
volumeMounts.mountPath
The container path to which the disk volume is mounted.
accessModes
The access mode.
ephemeral
Set the volume type to ephemeral.
storageClassName
In this example, the parameter is set to alicloud-disk-topology-alltype. The system attempts to create volumes based on the following disk types in sequence: cloud_essd, cloud_ssd, and cloud_efficiency.
Run the following command to deploy the Deployment:
kubectl create -f ephemeral-example.yaml
Run the following command to query pods:
kubectl get pod
Expected output:
NAME READY STATUS RESTARTS AGE ephemeral-example-65d6c5fb87-5jbjf 1/1 Running 0 4m55s ephemeral-example-65d6c5fb87-jgt8x 1/1 Running 0 4m55s
Run the following command to query PVCs:
kubectl get pvc
Expected output:
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE ephemeral-example-65d6c5fb87-5jbjf-scratch-volume Bound d-2ze7cjui6henh7z61393 30Gi RWO alicloud-disk-topology-alltype 5m42s ephemeral-example-65d6c5fb87-jgt8x-scratch-volume Bound d-2zehi4gnz4btacaf8lh2 30Gi RWO alicloud-disk-topology-alltype 5m42s
Verify that the PVCs and PVs are deleted together with the pods
Run the following command to scale the number of pods to 1:
kubectl scale deploy ephemeral-example --replicas=1 deployment.apps/ephemeral-example scaled
Run the following command to query the number of pods:
kubectl get pod
Expected output:
NAME READY STATUS RESTARTS AGE ephemeral-example-65d6c5fb87-5jbjf 1/1 Terminating 0 7m13s ephemeral-example-65d6c5fb87-jgt8x 1/1 Running 0 7m13s
Run the following command to query the number of PVCs:
kubectl get pvc
Expected output:
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE ephemeral-example-65d6c5fb87-jgt8x-scratch-volume Bound d-2zehi4gnz4btacaf8lh2 30Gi RWO alicloud-disk-topology-alltype 8m1s
The output indicates that the PVC is deleted together with the pod.
References
For more information about how to monitor the usage of ephemeral volumes, see Use storage-operator to monitor the storage resources of an ACK cluster.
For more information about how to use disks to persist data, see Disk volumes.