When containers fail, stateful application data stored in container storage can be lost or become unreliable. Persistent storage addresses this risk. This topic explains how to use dynamically provisioned disks to implement persistent storage.
How it works
-
Create a PVC that references a StorageClass.
-
Deploy your application with a volume that references the PVC.
-
The provisioner automatically creates a disk and binds it to the PVC as a PV.
Each ACK cluster includes a provisioner by default.
Prerequisites
-
An ACK managed cluster is created.
-
kubectl is connected to the cluster.
-
A disk provisioner is installed in the cluster.
Default StorageClasses
ACK creates four default StorageClasses at cluster initialization. They only work for single-zone clusters. For multi-zone clusters, create a custom StorageClass.
| StorageClass name | Disk type | Description |
|---|---|---|
alicloud-disk-common |
Basic disk | Standard baseline storage |
alicloud-disk-efficiency |
Enhanced SSD (ESSD) | Balanced performance and cost |
alicloud-disk-ssd |
SSD | High-performance storage |
alicloud-disk-available |
High availability | Creates an SSD if available; falls back to ultra disk. |
Create a custom StorageClass
Create a custom StorageClass if defaults do not meet your needs, for example in multi-zone clusters.
-
Create a file named
storageclass.yaml:kind: StorageClass apiVersion: storage.k8s.io/v1 metadata: name: alicloud-disk-ssd-hangzhou-b provisioner: alicloud/disk reclaimPolicy: Retain parameters: type: cloud_ssd regionId: cn-hangzhou zoneId: cn-hangzhou-b fstype: "ext4" readonly: "false" -
Create the StorageClass:
kubectl create -f storageclass.yaml
StorageClass parameters
| Parameter | Description |
|---|---|
provisioner |
The disk provisioner plugin. Set to alicloud/disk. |
reclaimPolicy |
Disk behavior on PVC deletion. Valid values: Delete, Retain. Default: Delete. |
type |
Disk type. Valid values: cloud, cloud_efficiency, cloud_ssd, available. |
regionId |
Region of the disk. Must match the cluster region. Optional. |
zoneId |
Disk zone. Optional. Single-zone cluster: the zone ID. Multi-zone cluster: comma-separated IDs (for example, cn-hangzhou-a,cn-hangzhou-b,cn-hangzhou-c). |
fstype |
File system type. Optional. Default: ext4. |
readonly |
Whether the disk is read-only. Optional. Valid values: true, false. Default: false. |
encrypted |
Whether to encrypt the disk. Optional. Valid values: true, false. Default: false. |
reclaimPolicy to Delete, the disk is automatically deleted when you delete the PVC. The disk data cannot be recovered.Create a PVC
-
Create a file named
pvc-ssd.yaml:kind: PersistentVolumeClaim apiVersion: v1 metadata: name: disk-ssd spec: accessModes: - ReadWriteOnce storageClassName: alicloud-disk-ssd-hangzhou-b resources: requests: storage: 20Gi -
Create the PVC:
kubectl create -f pvc-ssd.yaml
Create a Deployment
-
Create a file named
pvc-dynamic.yaml:apiVersion: apps/v1 kind: Deployment metadata: name: nginx-dynamic labels: app: nginx spec: selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx volumeMounts: - name: disk-pvc mountPath: "/data" volumes: - name: disk-pvc persistentVolumeClaim: claimName: disk-ssd -
Create the Deployment:
kubectl create -f nginx-dynamic.yaml
Verify persistent storage
Confirm the disk is mounted and data persists across pod restarts.
Confirm the disk is mounted
-
Find the pod name:
kubectl get pod | grep dynamicExpected output:
nginx-dynamic-5c74594ccb-zl9pf 2/2 Running 0 3m -
Verify the disk is mounted at
/data:kubectl exec nginx-dynamic-5c74594ccb-zl9pf -- df | grep dataExpected output:
/dev/vdh 20511312 45080 20449848 1% /data -
List files in
/data:kubectl exec nginx-dynamic-5c74594ccb-zl9pf -- ls /dataExpected output:
lost+found
Write test data and verify persistence
-
Create a test file on the disk:
kubectl exec nginx-dynamic-5c74594ccb-zl9pf -- touch /data/dynamic -
Confirm the file exists:
kubectl exec nginx-dynamic-5c74594ccb-zl9pf -- ls /dataExpected output:
dynamic lost+found -
Delete the pod to trigger a replacement:
kubectl delete pod nginx-dynamic-5c74594ccb-zl9pfExpected output:
pod "nginx-dynamic-5c74594ccb-zl9pf" deleted -
In a separate terminal, watch the pod lifecycle:
kubectl get pod -w -l app=nginxExpected output:
NAME READY STATUS RESTARTS AGE nginx-dynamic-5c74594ccb-zl9pf 2/2 Running 0 6m48s nginx-dynamic-5c74594ccb-zl9pf 2/2 Terminating 0 7m32s nginx-dynamic-5c74594ccb-45sd4 0/2 Pending 0 0s nginx-dynamic-5c74594ccb-45sd4 0/2 Pending 0 0s nginx-dynamic-5c74594ccb-45sd4 0/2 Init:0/1 0 0s nginx-dynamic-5c74594ccb-zl9pf 0/2 Terminating 0 7m32s nginx-dynamic-5c74594ccb-zl9pf 0/2 Terminating 0 7m33s nginx-dynamic-5c74594ccb-zl9pf 0/2 Terminating 0 7m33s nginx-dynamic-5c74594ccb-45sd4 0/2 PodInitializing 0 5s nginx-dynamic-5c74594ccb-45sd4 2/2 Running 0 22s -
Get the name of the new pod:
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE nginx-dynamic-5c74594ccb-45sd4 2/2 Running 0 2m -
Verify the test file still exists in the new pod:
kubectl exec nginx-dynamic-5c74594ccb-45sd4 -- ls /dataExpected output:
dynamic lost+foundThe
dynamicfile persists across pod recreation, confirming the disk provides persistent storage.