Disks are block-level storage products provided by Alibaba Cloud. Disks feature low latency, high performance, high durability, and high reliability. After you install an Alibaba Cloud Container Storage Interface (CSI) plugin, you can mount disks on self-managed Kubernetes clusters as volumes. You can use a PersistentVolumeClaim (PVC) to mount disks as statically or dynamically provisioned volumes. This topic describes how to use a PVC to mount a disk to an Elastic Container Instance pod.
Prerequisites
A VNode is deployed in a self-managed Kubernetes cluster.
The version of the self-managed Kubernetes cluster is v1.16 or later, and the CSI-Provisioner component is deployed on the self-managed Kubernetes cluster.
ImportantFor information about how to deploy the CSI-Provisioner component, see alibaba-cloud-csi-driver. If an issue occurs when you deploy the component, submit the issue on GitHub.
If the self-managed Kubernetes cluster is deployed in a data center, the data center is connected to Alibaba Cloud.
Precautions
Before you mount a disk, take note of the following items:
Disks cannot be shared. You can mount a disk to only one pod.
You can mount a disk only to an Elastic Container Instance pod on a VNode that resides in the same zone as the disk.
Mount a disk as a statically provisioned volume
Create a disk.
Log on to the Elastic Compute Service (ECS) console.
In the region and zone in which the VNode resides, create a pay-as-you-go disk.
For information about how to create a disk, see Create a disk. After the disk is created, record the ID of the disk.
NoteIf you use an existing disk, make sure that the disk resides in the same region and zone as the VNode and that the disk is not partitioned and formatted.
Create a PersistentVolume (PV).
Create a file named static-disk-pv.yaml and copy the following template content into the file. Modify the parameters in the template as required.
apiVersion: v1 kind: PersistentVolume metadata: name: static-disk-pv labels: alicloud-pvname: static-disk-pv spec: capacity: storage: 25Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain csi: driver: diskplugin.csi.alibabacloud.com volumeHandle: "<your disk-id>" # Replace <your disk-id> with the ID of your disk. nodeAffinity: required: nodeSelectorTerms: - matchExpressions: - key: topology.diskplugin.csi.alibabacloud.com/zone operator: In values: - "<your-node-zone-id>" # Replace with the ID of the zone where the VNode resides.
The following table describes the parameters in the template.
Parameter
Description
driver
The type of the driver used. In this example, the parameter is set to
diskplugin.csi.alibabacloud.com
. This indicates that the CSI plug-in provided by Alibaba Cloud for disks is used.volumeHandle
The ID of the disk.
Run the following command to create a PV:
kubectl create -f static-disk-pv.yaml
Create a PVC.
Create a file named static-disk-pvc.yaml and copy the following template content into the file.
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: static-disk-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 25Gi selector: matchLabels: alicloud-pvname: static-disk-pv
Run the following command to create a PVC:
kubectl create -f static-disk-pvc.yaml
Mount the disk to an Elastic Container Instance pod.
Create a file named static-disk-test.yaml and copy the following template content into the file.
apiVersion: v1 kind: Pod metadata: name: static-disk-test labels: alibabacloud.com/eci: "true" spec: nodeSelector: k8s.aliyun.com/vnode: "true" tolerations: - key: k8s.aliyun.com/vnode operator: "Equal" value: "true" effect: "NoSchedule" containers: - name: nginx image: registry-vpc.cn-beijing.aliyuncs.com/eci_open/nginx:1.14.2 ports: - containerPort: 80 name: web volumeMounts: - name: pvc-disk mountPath: /data volumes: - name: pvc-disk persistentVolumeClaim: claimName: static-disk-pvc
Run the following command to create a pod:
kubectl create -f static-disk-test.yaml
View the result.
kubectl get pods -o wide
Expected output:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES static-disk-test 1/1 Running 0 44s 172.16.XX.XX cn-beijing.vnd-2ze8nd8xcl33t4pa**** <none> <none>
Check the file directories in the pod and verify that the
/data
mount directory is generated for the disk.
Mount a disk as a dynamically provisioned volume
Create a StorageClass.
Create a file named disk-sc.yaml and copy the following template content into the file. Modify the parameters in the template as required.
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: alicloud-disk-essd provisioner: diskplugin.csi.alibabacloud.com parameters: type: cloud_essd regionId: cn-beijing zoneId: cn-beijing-l fstype: ext4 readonly: "true" mkfsOptions: "-O project,quota" diskTags: "key1:value1,key2:value2" encrypted: "false" performanceLevel: PL1 volumeBindingMode: Immediate reclaimPolicy: Delete allowVolumeExpansion: true
The following table describes the parameters in the template.
Parameter
Description
provisioner
The type of the driver used. In this example, the parameter is set to
diskplugin.csi.alibabacloud.com
. This indicates that the CSI plug-in provided by Alibaba Cloud for disks is used.type
The category of the disk. Valid values:
cloud_essd: enhanced SSD (ESSD).
cloud_ssd: standard SSD.
cloud_efficiency: ultra disk.
available: The system first tries to create a standard SSD. If no standard SSD is available in the zone, the system creates an ultra disk.
NoteYou can set this parameter to any combination of cloud_essd, cloud_ssd, and cloud_efficiency. For example, you can set this parameter to
type: cloud_efficiency, cloud_ssd, cloud_essd
. In this case, the system tries to create a disk of a specified type in sequence. The system stops trying to create a disk after a disk is created.regionId and zoneId
The region and zone in which the disk resides. The disk must reside in the same region and zone as the VNode.
fstype
The file system of the disk. Default value: ext4.
readonly
Specifies whether the automatically created disk is read-only. Default value: false.
true: The disk is read-only.
false: The disk is readable and writable.
mkfsOptions
The options that are specified to format the disk. Example:
mkfsOptions: "-O project,quota"
.diskTags
The tags of the disk. Format:
key1:value1,key2:value2
.encrypted
Specifies whether to encrypt the disk. Default value: false.
performanceLevel
The performance level (PL) of the disk. This parameter is valid only if the category of the disk is ESSD. Valid values: PL0, PL1, PL2, and PL3. For more information, see ESSDs.
volumeBindingMode
The binding mode of the disk. Default value: Immediate, which indicates that the system creates a disk before it creates a pod.
reclaimPolicy
The reclaim policy of the disk. Default value: Delete. Valid values:
Delete: When the PVC is deleted, the related PV and disk are also deleted.
Retain: When the PVC is deleted, the related PV and disk are retained and can only be manually deleted.
If you have high requirements for data security, we recommend that you apply the Retain policy to the disk to prevent data loss caused by errors.
allowVolumeExpansion
Specifies whether to automatically scale up the disk.
Run the following command to create a StorageClass:
kubectl create -f disk-sc.yaml
Create a PVC.
Create a file named disk-pvc.yaml and copy the following template content into the file.
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: disk-pvc spec: accessModes: - ReadWriteOnce volumeMode: Filesystem resources: requests: storage: 25Gi storageClassName: alicloud-disk-essd
Run the following command to create a PVC:
kubectl create -f disk-pvc.yaml
The system automatically creates a disk and the related PV based on the StorageClass configurations.
Mount the disk to an Elastic Container Instance pod.
Create a file named disk-test.yaml and copy the following template content into the file.
NoteWe recommend that you create a StatefulSet instead of a Deployment to mount a disk. Deployments are used to manage stateless applications. When a pod is restarted, the start time of the next start may be earlier than the end time of the previous start. If multiple pods are created for a Deployment, no dedicated volume is provisioned for each pod.
apiVersion: apps/v1 kind: StatefulSet metadata: name: disk-test spec: selector: matchLabels: app: nginx serviceName: "nginx" template: metadata: labels: app: nginx spec: nodeSelector: k8s.aliyun.com/vnode: "true" tolerations: - key: k8s.aliyun.com/vnode operator: "Equal" value: "true" effect: "NoSchedule" containers: - name: nginx image: registry-vpc.cn-beijing.aliyuncs.com/eci_open/nginx:1.14.2 ports: - containerPort: 80 name: web volumeMounts: - name: pvc-disk mountPath: /data volumes: - name: pvc-disk persistentVolumeClaim: claimName: disk-pvc
Run the following command to create a StatefulSet:
kubectl create -f disk-test.yaml
View the result.
kubectl get pods -o wide
Expected output:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES disk-test-0 1/1 Running 0 3m58s 172.16.XX.XX cn-beijing.vnd-2ze8nd8xcl33t4pa**** <none> <none>
Check the file directories in the pod and verify that the
/data
mount directory is generated for the disk.