All Products
Search
Document Center

Container Service for Kubernetes:Use a Deployment to create ephemeral volumes

最終更新日:Mar 15, 2024

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.

Note

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.

  1. 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.

    Click to view the YAML content of the Deployment

    allowVolumeExpansion: true
    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      creationTimestamp: "2023-07-19T06:54:25Z"
      name: alicloud-disk-topology-alltype
      resourceVersion: "773847"
      uid: 5a9f8e3b-d22e-483d-a3bf-90ff8d466492
    parameters:
      type: cloud_essd,cloud_ssd,cloud_efficiency
    provisioner: diskplugin.csi.alibabacloud.com
    reclaimPolicy: Delete
    volumeBindingMode: WaitForFirstConsumer
    ---
    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: busybox-container
              image: busybox
              resources:
                requests:
                  cpu: 500m
                  memory: 2Gi
                  ephemeral-storage: 2Gi
              volumeMounts:
              - mountPath: "/scratch"
                name: scratch-volume
              command: [ "sleep", "1000000" ]
          volumes:
            - name: scratch-volume
              ephemeral:
                volumeClaimTemplate:
                  metadata:
                    labels:
                      type: scratch-volume
                  spec:
                    accessModes: [ "ReadWriteOnce" ]
                    storageClassName: alicloud-disk-topology-alltype
                    resources:
                      requests:
                        storage: 30Gi

    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.

  2. Run the following command to deploy the Deployment:

    kubectl create -f ephemeral-example.yaml
  3. 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
  4. 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

  1. Run the following command to scale the number of pods to 1:

    kubectl scale deploy ephemeral-example --replicas=1
    deployment.apps/ephemeral-example scaled
  2. 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
  3. 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