All Products
Search
Document Center

Container Service for Kubernetes:Create a snapshot of a disk volume

Last Updated:Mar 26, 2026

Use volume snapshots created from disks

Volume snapshots let you back up StatefulSet data before a risky operation—such as an upgrade or schema change—and restore it to a new volume if something goes wrong. This topic explains how to create a volume snapshot from a disk volume and how to restore data from it.

Prerequisites

Before you begin, ensure that you have:

  • An ACK managed cluster running Kubernetes 1.18 or later

  • The Elastic Compute Service (ECS) Snapshot service activated (activation is free; charges apply only for snapshots you create). For more information, see Activate ECS Snapshot

How it works

Container Service for Kubernetes (ACK) clusters use disk volumes for persistent storage on StatefulSets. Volume snapshots build on ECS disk snapshots and two Kubernetes mechanisms:

  • VolumeSnapshot: backs up data in a disk volume

  • DataSource: a PersistentVolumeClaim (PVC) field that restores data from a snapshot into a new volume

ACK implements snapshots through three CustomResourceDefinitions (CRDs):

Resource Description
VolumeSnapshotContent A disk snapshot provisioned by a cluster administrator. Not namespace-scoped. Analogous to a PersistentVolume (PV).
VolumeSnapshot A user request for a volume snapshot. Namespace-scoped. Analogous to a PVC.
VolumeSnapshotClass Defines the driver and parameters used when creating snapshots. A VolumeSnapshotClass is analogous to a StorageClass, but snapshot-specific. Because the same volume can have snapshots with different attributes (for example, different retention periods), a dedicated class is needed rather than reusing the StorageClass.

VolumeSnapshot and VolumeSnapshotContent are bound one-to-one, similar to PVC-PV binding:

  • If volumeSnapshotClassName is set to a valid VolumeSnapshotClass, ACK automatically provisions a VolumeSnapshotContent.

  • If volumeSnapshotClassName is omitted or invalid, you must manually create a VolumeSnapshotContent and bind it to the VolumeSnapshot.

Important

When you delete a VolumeSnapshotContent, the related volume snapshot is also deleted.

Billing

Volume snapshots are billed as ECS snapshots. For pricing details, see Snapshot billing.

Constraints

Before creating a snapshot, review the following constraints:

Condition Behavior
CSI plug-in version < V1.22.12-b797ad9-aliyun Snapshots can only be created from disk volumes whose PVC is attached to a pod in the Running state.
CSI plug-in version >= V1.22.12-b797ad9-aliyun Snapshots can be created from any used disk volume, regardless of pod state.

By default, instance access is enabled for dynamically provisioned snapshots from volumes backed by Enhanced SSDs (ESSDs) and ESSD AutoPL disks at performance levels PL0, PL1, PL2, and PL3.

Create a volume snapshot dynamically

Use this approach to snapshot a live disk volume managed by ACK. The cluster automatically provisions both the VolumeSnapshotContent and the underlying ECS snapshot.

snapshot

Overview: create a VolumeSnapshotClass (administrator) → deploy an app with a disk volume → create a VolumeSnapshot (ACK auto-creates VolumeSnapshotContent and the ECS snapshot) → restore data to a new volume by referencing the snapshot in a PVC.

Step 1: Create a VolumeSnapshotClass

Creating a VolumeSnapshotClass is a cluster administrator task. If your cluster already has one, skip to Step 2.
  1. Create volumesnapshotclass.yaml with the following content:

    Parameter Description
    retentionDays Snapshot retention period, in days.
    forceDelete Controls what happens when a snapshot is deleted. "true" forcefully deletes all snapshots; "false" deletes only unused snapshots. Default: "false" for csi-provisioner 1.26.5-92f859a-aliyun and later; "true" for earlier versions.
    deletionPolicy Delete: deletes the VolumeSnapshotContent and ECS snapshot when the VolumeSnapshot is deleted. Retain: keeps both when the VolumeSnapshot is deleted.
    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshotClass
    metadata:
      name: default-snapclass
    driver: diskplugin.csi.alibabacloud.com
    parameters:
      retentionDays: "5"
      forceDelete: "true"
    deletionPolicy: Delete
  2. Apply the manifest:

    kubectl apply -f volumesnapshotclass.yaml

Step 2: Deploy an app and write data

  1. Create nginx.yaml with the following content:

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: nginx
    spec:
      selector:
        matchLabels:
          app: nginx
      serviceName: "nginx"
      replicas: 1
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6
            imagePullPolicy: IfNotPresent
            volumeMounts:
            - name: disk
              mountPath: /data
      volumeClaimTemplates:
      - metadata:
          name: disk
        spec:
          accessModes: [ "ReadWriteOnce" ]
          storageClassName: "alicloud-disk-topology-alltype"
          resources:
            requests:
              storage: 20Gi
  2. Deploy the StatefulSet:

    kubectl apply -f nginx.yaml
  3. Verify the pod is running:

    kubectl get pod -l app=nginx

    Expected output:

    NAME        READY   STATUS    RESTARTS   AGE
    nginx-0     1/1     Running   0          82s
  4. Write test data to the volume:

    kubectl exec -it nginx-0 -- touch /data/test
    kubectl exec -it nginx-0 -- ls /data

    Expected output:

    lost+found test

Step 3: Create a VolumeSnapshot

  1. Create snapshot-1.yaml with the following content:

    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshot
    metadata:
      name: new-snapshot-demo
      namespace: default
    spec:
      volumeSnapshotClassName: default-snapclass   # The VolumeSnapshotClass used by the VolumeSnapshot.
      source:
        persistentVolumeClaimName: disk-nginx-0
  2. Apply the manifest:

    kubectl apply -f snapshot-1.yaml

Step 4: Verify the snapshot

  1. Check the VolumeSnapshot status:

    kubectl get volumesnapshots

    Expected output:

    NAME                READYTOUSE   SOURCEPVC      SOURCESNAPSHOTCONTENT   RESTORESIZE   SNAPSHOTCLASS       SNAPSHOTCONTENT                                    CREATIONTIME   AGE
    new-snapshot-demo   true         disk-nginx-0                           20Gi          default-snapclass   snapcontent-48b04625-679a-490f-9ef3-f04b2b8e6c57   28s            28s

    When READYTOUSE is true, the snapshot is ready for use.

  2. Check the VolumeSnapshotContent:

    kubectl get VolumeSnapshotContent

    Expected output:

    NAME                                               READYTOUSE   RESTORESIZE   DELETIONPOLICY   DRIVER                            VOLUMESNAPSHOTCLASS   VOLUMESNAPSHOT      VOLUMESNAPSHOTNAMESPACE   AGE
    snapcontent-48b04625-679a-490f-9ef3-f04b2b8e6c57   true         21474836480   Delete           diskplugin.csi.alibabacloud.com   default-snapclass     new-snapshot-demo   default                   49s
The corresponding ECS disk snapshot is also visible on the Snapshots page in the ECS console.

Step 5 (optional): Restore data to a new app

Reference the snapshot in a StatefulSet's volumeClaimTemplates to restore data into a new volume.

  1. Create nginx-restore.yaml with the following content. The dataSource block points to the snapshot you created:

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: nginx-restore
    spec:
      selector:
        matchLabels:
          app: nginx
      serviceName: "nginx"
      replicas: 1
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6
            imagePullPolicy: IfNotPresent
            volumeMounts:
            - name: disk
              mountPath: /data
      volumeClaimTemplates:
      - metadata:
          name: disk
        spec:
          accessModes: [ "ReadWriteOnce" ]
          storageClassName: "alicloud-disk-topology-alltype"
          resources:
            requests:
              storage: 20Gi
          dataSource:
            name: new-snapshot-demo      # Name of the VolumeSnapshot to restore from
            kind: VolumeSnapshot
            apiGroup: snapshot.storage.k8s.io
  2. Deploy the StatefulSet:

    kubectl apply -f nginx-restore.yaml
  3. Verify that the data was restored:

    kubectl exec -it nginx-restore-0 -- ls /data

    Expected output:

    lost+found test

Step 6 (optional): Restore data to a standalone PVC

To restore data without deploying a workload, create a PVC directly from the snapshot.

  1. Create pvc-restore.yaml with the following content:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-disk
      namespace: default
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 20Gi
      dataSource:
        name: new-snapshot-demo      # Name of the VolumeSnapshot to restore from
        kind: VolumeSnapshot
        apiGroup: snapshot.storage.k8s.io
      storageClassName: alicloud-disk-topology-alltype
      volumeMode: Filesystem
  2. Create the PVC:

    The alicloud-disk-topology-alltype StorageClass uses volumeBindingMode: WaitForFirstConsumer. The PVC stays in the Pending state until it is mounted to a pod. Do not delete the VolumeSnapshot, VolumeSnapshotContent, or the underlying ECS snapshot before the PVC is bound.
    kubectl apply -f pvc-restore.yaml

Import an existing disk snapshot

Use this approach to import a disk snapshot that already exists in the ECS console into an ACK cluster, without creating a live app first.

Step 1: Create a VolumeSnapshotContent

Creating a VolumeSnapshotContent is a cluster administrator task.
  1. Create snapshot-content.yaml. Replace <YOUR-SNAPSHOTID> with the snapshot ID from the Snapshots page in the ECS console:

    Parameter Description
    snapshotHandle The ID of the existing ECS disk snapshot.
    volumeSnapshotRef.name The name of the VolumeSnapshot to bind. Must match metadata.name in the VolumeSnapshot manifest.
    volumeSnapshotRef.namespace The namespace of the VolumeSnapshot.
    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshotContent
    metadata:
      name: new-snapshot-content-test
    spec:
      deletionPolicy: Retain
      driver: diskplugin.csi.alibabacloud.com
      source:
        snapshotHandle: <YOUR-SNAPSHOTID>   # ID of the existing ECS disk snapshot
      volumeSnapshotRef:
        name: new-snapshot-demo             # Must match the VolumeSnapshot name below
        namespace: default
  2. Apply the manifest:

    kubectl apply -f snapshot-content.yaml

Step 2: Create a VolumeSnapshot and bind it

  1. Create snapshot-2.yaml with the following content:

    Parameter Description
    metadata.name The VolumeSnapshot name. Must match the volumeSnapshotRef.name value in the VolumeSnapshotContent.
    volumeSnapshotContentName The name of the VolumeSnapshotContent to bind.
    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshot
    metadata:
      name: new-snapshot-demo       # Must match volumeSnapshotRef.name in the VolumeSnapshotContent
      namespace: default
    spec:
      source:
        volumeSnapshotContentName: new-snapshot-content-test   # Name of the VolumeSnapshotContent to bind
  2. Apply the manifest:

    kubectl apply -f snapshot-2.yaml

Step 3 (optional): Restore data

After binding is complete, restore data using the same approach as Step 5 and Step 6 in the dynamic snapshot workflow.