All Products
Search
Document Center

Container Compute Service:Mount ephemeral volumes

Last Updated:Dec 10, 2024

If your applications need to use additional storage but do not require data persistence, you can mount ephemeral volumes to the pods of your applications Ephemeral volumes are created and deleted together with pods, which simplifies application deployment and management. This topic describes how to mount an ephemeral volume to a pod by adding the ephemeral.volumeClaimTemplate parameter to the pod configuration. This topic also verifies whether the relevant persistent volume (PV) and persistent volume claim (PVC) are automatically deleted when the pod is deleted.

Scenarios

Ephemeral volumes are suitable for the following 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.

Procedure

  1. Connect to your ACS cluster. For more information, 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.

  2. Create a file named ephemeral-example.yaml and copy the following code block to the file. Modify the parameters in the template based on the parameter description in the table below the code block.

    Note

    The ephemeral.volumeClaimTemplate parameter is applicable to Deployments, StatefulSets, and pods. In this example, a Deployment is used.

    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: 40Gi

    The preceding YAML template specifies a Deployment that provisions two pods. An ephemeral is mounted to each pod. The following table describes the fields in the volumeClaimTemplate parameter.

    Parameter

    Description

    accessModes

    The access mode of the volume. Set the value to ReadWriteOncePod.

    storageClassName

    The name of the StorageClass that you want to use.

    In this example, the alicloud-disk-topology-alltype StorageClass is used. This is the default StorageClass provided by Alibaba Cloud Container Compute Service (ACS). This StorageClass specifies the configuration of a disk volume, which is provisioned to mount a performance level 1 (PL1) Enterprise SSD (ESSD).

    Note

    You are charged for the disks you use on a pay-as-you-go basis. For more information, see Billing of block storage devices and Prices of block storage devices.

    storage

    The capacity of the ephemeral volume.

    The StorageClass used in this example can be used to create a disk volume that is provisioned to mount a PL1 ESSD. A PL1 ESSD supports a capacity of 20 to 65,536 GiB.

  3. Deploy the ephemeral-example.yaml file to create a Deployment.

    kubectl create -f ephemeral-example.yaml
  4. Check the status of the pods created by the Deployment.

    kubectl get pod

    The following output shows that two pods are created.

    NAME                                 READY   STATUS    RESTARTS   AGE
    ephemeral-example-64db7b9f5c-hcpz5   1/1     Running   0          108s
    ephemeral-example-64db7b9f5c-qqvl5   1/1     Running   0          108s
  5. Check the PVC.

    kubectl get pvc

    The following output shows that a PVC and a PV are created for the pod.

    Note

    A disk volume is used in this example. You can obtain the disk ID from the VOLUME field.

    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   3m28s

Verify the result

After you perform the preceding steps to create a Deployment, an ephemeral volume is provisioned for each pod to mount a disk. Ephemeral volumes are automatically deleted when pods are deleted. Perform the following steps to verify the deployment result:

  1. Reduce the number of pod replicas for the Deployment.

    Run the following command to reduce the number of pod replicas for the Deployment to one:

    kubectl scale deploy ephemeral-example --replicas=1
  2. Check whether the scale-in operation is completed.

    kubectl get pod

    The following output shows that the number of pod replicas created for the Deployment is one.

    NAME                                 READY   STATUS    RESTARTS   AGE
    ephemeral-example-64db7b9f5c-qqvl5   1/1     Running   0          11m
  3. Check whether the PV and PVC are deleted.

    1. Check the PV.

      kubectl get pv

      The following output shows that the PV is deleted together with the pod.

      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
    2. Check the PVC.

      kubectl get pvc

      The following output shows that the PVC is deleted together with the pod.

      NAME                                                STATUS   VOLUME                   CAPACITY   ACCESS MODES   STORAGECLASS                     AGE
      ephemeral-example-64db7b9f5c-qqvl5-scratch-volume   Bound    d-uf6a3lk85znunou6****   40Gi       RWOP           alicloud-disk-topology-alltype   15m