Container Service for Kubernetes (ACK) can automatically create and attach disks to your workloads through dynamic provisioning. Define a StorageClass and a persistent volume claim (PVC), and the system creates a persistent volume (PV) and its backing disk on demand when you deploy your application.
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.
By default, a provisioner is automatically installed in each ACK cluster.
Prerequisites
An ACK managed cluster. For more information, see Create an ACK managed cluster.
A kubectl connection to the cluster. For more information, see Step 2: Select a type of cluster credentials.
A provisioner installed in the cluster that can automatically create disks from a StorageClass.
Default StorageClasses
ACK creates four StorageClasses during cluster initialization. These StorageClasses use default settings and 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 | Ultra disk | Balanced performance and cost |
alicloud-disk-ssd | SSD | High-performance storage |
alicloud-disk-available | High availability | Tries to create an SSD first. If SSD resources are exhausted, creates an ultra disk instead. |
Create a custom StorageClass
If the default StorageClasses do not meet your requirements (for example, your cluster spans multiple zones), create a custom StorageClass.
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 | Set to alicloud/disk. The provisioner automatically creates disks. |
reclaimPolicy | Controls what happens to the disk when the PVC is deleted. 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 | Zone of the disk. Optional. For a single-zone cluster, set this to the zone ID. For a multi-zone cluster, specify comma-separated zone 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: 20GiCreate 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-ssdCreate the Deployment:
kubectl create -f nginx-dynamic.yaml
Verify persistent storage
After creating the Deployment, verify that the disk is mounted and that 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 3mVerify the disk is mounted at
/data:kubectl exec nginx-dynamic-5c74594ccb-zl9pf -- df | grep dataExpected output:
/dev/vdh 20511312 45080 20449848 1% /dataList 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/dynamicConfirm the file exists:
kubectl exec nginx-dynamic-5c74594ccb-zl9pf -- ls /dataExpected output:
dynamic lost+foundDelete the pod to trigger a replacement:
kubectl delete pod nginx-dynamic-5c74594ccb-zl9pfExpected output:
pod "nginx-dynamic-5c74594ccb-zl9pf" deletedIn 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 22sGet the name of the new pod:
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE nginx-dynamic-5c74594ccb-45sd4 2/2 Running 0 2mVerify the test file still exists in the new pod:
kubectl exec nginx-dynamic-5c74594ccb-45sd4 -- ls /dataExpected output:
dynamic lost+foundThe
dynamicfile persists after the pod is deleted and recreated. This confirms that the dynamically provisioned disk provides persistent storage.