Alibaba Cloud disks are block-level data storage resources for Elastic Compute Service (ECS). Alibaba Cloud disks provide low latency, high performance, high durability, and high reliability. Alibaba Cloud Container Compute Service (ACS) allows you to use the Container Storage Interface (CSI) plug-in to statically and dynamically provision disk volumes. This topic describes how to use a dynamically provisioned disk volume and how to verify that a dynamically provisioned disk volume can be used to persist data.
Usage notes
Category | Description |
Scenarios | If no disk is available when you deploy an application in an ACK cluster, the system automatically purchases a disk. |
How to use |
|
Prerequisites
A kubectl client is connected to the cluster. For more information, see Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster.
Environment preparation
Create a custom StorageClass
ACS provides a default StorageClass named alicloud-disk-topology-alltype
. The StorageClass attempts to create the following categories of disks in sequence: cloud_essd
, cloud_ssd
, and cloud_efficiency
. If the default StorageClass does not meet your requirement, you can use kubectl to create a StorageClass.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: alibaba-cloud-custom
parameters:
type: cloud_essd,cloud_ssd
provisioner: diskplugin.csi.alibabacloud.com
reclaimPolicy: Delete
allowVolumeExpansion: true
The following table describes the parameters in the parameters
section.
Parameter | Description | Example |
| The category of the disk. The default parameter name is
You can specify one or more values. After you set this parameter, the system attempts to create disks of the specified categories in sequence until it succeeds to create a disk. | |
| The file system of the disk. Default value: |
|
| The performance level of the ESSD. Valid values: |
|
Procedure
You can use dynamically provisioned disk volumes in the following ways:
Use the console
Step 1: Create a PVC
Log on to the ACS console. In the left-side navigation pane, click Clusters.
On the Clusters page, find the cluster that you want to manage and click its ID. In the left-side navigation pane, choose Volumes > Persistent Volume Claims.
In the upper-right corner of the Persistent Volume Claims page, click Create.
In the Create PVC dialog box, configure the parameters that are described in the following table.
Parameter
Description
PVC Type
Valid values: Cloud Disk, NAS, and OSS. In this example, Cloud Disk is selected.
Name
The name of the PVC. The name must be unique in the namespace.
Allocation Mode
You can select only Use StorageClass.
Existing Storage Class
The StorageClass that is used to enable dynamic provisioning. Click Select. In the Select Storage Class dialog box, find the StorageClass that you want to use and click Select in the Actions column.
Capacity
The capacity of the PV.
Access Mode
You can select only ReadWriteOncePod.
Click Create.
After the PVC is created, you can find the PVC in the Persistent Volume Claims list. The PVC is in the Pending state, which means that no volume is bound to the PVC. This is because the volumeBindingMode
parameter of the PVC is set to WaitForFirstConsumer
. This means that a volume is created and bound to the PVC only after the PVC is consumed by a workload.
Step 2: Create an application
In the left-side navigation pane of the details page, choose Workloads > StatefulSets.
On the StatefulSets page, click Create from Image.
Configure the application parameters.
Parameter
Value
Name
mysql
Replicas
1
Type
StatefulSet
Instance Type
General-purpose
QoS Type
default
Image Name
mysql:5.7
Port
80
Environment Variable
Type: Secrets
Variable Key:
MYSQL_ROOT_PASSWORD
Value/ValueFrom:
mysql-pass password
Volume
Add PVC:
Mount Source:
disk-pvc
Container Path:
/data
This example shows how to set the volume parameters. For more information about other parameters, see Use a StatefulSet to create a stateful application.
You can create volumes that use local storage or cloud storage in ACS cluster. In this example, cloud storage is used. In the following figure, the PV type is set to Cloud Storage and the mount source is set to the PVC that you created. The disk is mounted to the
/tmp
path of the container. All container data generated in the path is saved to the disk.Set other parameters and click Create.
In the left-side navigation pane of the cluster management page, choose Volumes > Persistent Volumes. You can find that a PV is created and bound to the PVC that you created. In the left-side navigation pane, choose Volumes > Persistent Volume Claims. You can find that the status of the PVC changes to Bound
.
Use kubectl
Step 1: Create a PVC
Create a file named pvc-disk.yaml and copy the following content to the file:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: disk-pvc spec: accessModes: - ReadWriteOncePod volumeMode: Filesystem resources: requests: storage: 25Gi storageClassName: alicloud-disk-topology-alltype
Parameter
Description
name
The name of the PVC.
accessModes
The access mode of the PVC.
volumeMode
The volume mode of the disk. Valid values: Filesystem and Block. This parameter is optional. Default value: Filesystem.
storageClassName
The name of the StorageClass that you want to associate with the PVC.
storage
The capacity of the disk.
Run the following command to create the PVC:
kubectl create -f pvc-disk.yaml
View the PVC.
In the left-side navigation pane of the cluster management page, choose
. On the Persistent Volume Claims page, you can find that the PVC is in the Pending state.
Step 2: Create an application
Create a file named mysql.yaml and copy the following content to the file:
apiVersion: apps/v1 kind: StatefulSet metadata: name: mysql spec: selector: matchLabels: app: mysql serviceName: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql:5.7 env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-pass key: password ports: - containerPort: 80 name: mysql volumeMounts: - name: pvc-disk mountPath: /data volumes: - name: pvc-disk persistentVolumeClaim: claimName: disk-pvc --- apiVersion: v1 kind: Secret metadata: name: mysql-pass type: Opaque data: username: dGVz**** password: dGVzdDEt****
Parameter
Description
mountPath
The path to which the disk is mounted.
claimName
The name of the PVC that is mounted to the application.
Run the following command to deploy the application and mount the PVC to the application:
kubectl create -f mysql.yaml
View the created application.
In the left-side navigation pane of the cluster management page, choose
. You can view the application on the StatefulSets page. In the left-side navigation pane, choose . You can find that the status of the PVC changes to Bound.
Verify that the dynamically provisioned disk volume can be used to persist data
Data is persisted in the disk. After a pod is deleted and recreated, the disk data is the same as before the pod is deleted.
Perform the following steps to verify that data is persisted to the disk:
View the pod that runs the MySQL application and the files in the disk.
Run the following command to query the pod that runs the MySQL application:
kubectl get pod | grep mysql
Expected output:
mysql-0 1/1 Running 0 3m
Run the following command to check whether a new disk is mounted to the /data path of the pod:
kubectl exec mysql-0 -- df | grep data
Expected output:
/dev/vdb 25626852 24 25610444 1% /data
Run the following command to query files in the /data path of one pod:
kubectl exec mysql-0 -- ls /data
Expected output:
lost+found
Create a file in the disk.
Run the following command to create a file named MySQL in the /data path:
kubectl exec mysql-0 -- touch /data/mysql
Run the following command to query files in the /data path of the pod:
kubectl exec mysql-0 -- ls /data
Expected output:
lost+found mysql
Run the following command to delete the pod named
mysql-0
and wait for the system to recreate the pod:kubectl delete pod mysql-0
Expected output:
pod "mysql-0" deleted
Verify that the file still exists after the pod is deleted.
Run the following command and wait until the mysql-0 pod is created and enters the Running state:
kubectl get pod
Expected output:
NAME READY STATUS RESTARTS AGE mysql-0 1/1 Running 0 1m
Run the following command to query files in the /data path of the pod:
kubectl exec mysql-0 -- ls /data
Expected output:
lost+found mysql
The MySQL file still exists in the /data path. This indicates that data is persisted to the dynamically provisioned disk volume.