Alibaba Cloud disks are block-level storage products that offer low latency, high performance, durability, and high reliability. After you deploy the Alibaba Cloud CSI plugin, you can use Alibaba Cloud disks as volumes in your self-managed Kubernetes cluster. You can mount a disk by using a PersistentVolumeClaim (PVC) with either static or dynamic provisioning. This topic describes how to mount a disk to an ECI pod by using a PVC.
Prerequisites
-
A VNode is deployed in your self-managed Kubernetes cluster.
-
Your cluster runs Kubernetes 1.16 or later and has the CSI-Provisioner component deployed.
ImportantFor deployment instructions for the CSI-Provisioner component, see alibaba-cloud-csi-driver. If you encounter issues during deployment, submit an issue on GitHub.
-
If your Kubernetes cluster is deployed in an on-premises data center, ensure a connection exists between the data center and Alibaba Cloud.
Usage notes
-
Disks are non-shared storage and can be mounted to only one pod at a time.
-
You can mount a disk only to an ECI pod that runs on a VNode in the same zone. Cross-zone mounting is not supported.
Statically mount a disk
-
Create a disk.
-
Log on to the ECS console.
-
Create a pay-as-you-go disk in the same region and zone as the VNode.
For more information about how to create a disk, see Create a data disk. After the disk is created, record its ID.
NoteIf you use an existing disk, make sure it is in the same region and zone as the VNode, and that it has not been partitioned or formatted.
-
-
Create a PersistentVolume (PV).
-
Create a file named static-disk-pv.yaml with the following content.
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 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 zone where the VNode resides.The following table describes the parameters.
Parameter
Description
driver
The driver type. Set this parameter to
diskplugin.csi.alibabacloud.comto use the Alibaba Cloud disk CSI plugin.volumeHandle
The disk ID.
-
Run the following command to create the PV.
kubectl create -f static-disk-pv.yaml
-
-
Create a PersistentVolumeClaim (PVC).
-
Save the following content as static-disk-pvc.yaml.
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 the PVC.
kubectl create -f static-disk-pvc.yaml
-
-
Mount the disk to an ECI pod.
-
Save the following content as static-disk-test.yaml.
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 the pod.
kubectl create -f static-disk-test.yaml -
Verify the result.
kubectl get pods -o wideNAME 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 directories in the pod. The mount directory
/datafor the disk is created.[root@k8s-01 ~]# kubectl exec -it static-disk-test -- bash root@static-disk-test:/# ls bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@static-disk-test:/# ls /data lost+found
-
Dynamically mount a disk
-
Create a StorageClass.
-
Create a file named disk-sc.yaml with the following content.
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: trueThe following table describes the parameters.
Parameter
Description
provisioner
The driver type. Set this parameter to
diskplugin.csi.alibabacloud.comto use the Alibaba Cloud disk CSI plugin.type
The disk category. Valid values:
-
cloud_essd: ESSD -
cloud_ssd: standard SSD -
cloud_efficiency: ultra disk.
-
available: The system attempts to create a standard SSD first. If standard SSDs are unavailable in the zone, the system creates an ultra disk instead.
NoteYou can also specify a comma-separated list of disk categories other than
available. For example,type: cloud_efficiency, cloud_ssd, cloud_essd. The system then attempts to create a disk from each specified category in order until one is successfully created.regionId, zoneId
The region and zone of the disk. They must be the same as those of the VNode.
fstype
The file system of the disk. The default value is
ext4.readonly
Specifies whether the disk is mounted as read-only. The default value is
false.-
true: The disk is read-only. -
false: The disk is read-write.
mkfsOptions
The parameters used to format the disk. Example:
mkfsOptions: "-O project,quota".diskTags
The tags to add to the disk. The format is
key1:value1,key2:value2.encrypted
Specifies whether to encrypt the disk. The default is
false.performanceLevel
The performance level of the ESSD. Valid values:
PL0,PL1,PL2, andPL3. For more information, see ESSDs.volumeBindingMode
The binding mode of the disk. The default value is
Immediate.reclaimPolicy
The reclaim policy for the disk. The default value is
Delete.Retainis also supported.-
Delete: When the PVC is deleted, the PV and the disk are also deleted. -
Retain: When the PVC is deleted, the PV and the disk are not deleted. You must manually delete them.
If data security is a high priority, use the
Retainpolicy to prevent accidental data loss.allowVolumeExpansion
Specifies whether to allow automatic expansion for the disk.
-
-
Run the following command to create the StorageClass.
kubectl create -f disk-sc.yaml
-
-
Create a PVC.
-
Save the following content as disk-pvc.yaml.
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 the PVC.
kubectl create -f disk-pvc.yamlThe system automatically creates a disk and the corresponding PV based on the StorageClass configuration.
-
-
Mount the disk to an ECI pod.
-
Save the following content as disk-test.yaml.
NoteUse a StatefulSet to mount the disk instead of a Deployment. Deployments are intended for stateless services and do not guarantee that pod state is preserved across restarts. In a multi-replica Deployment, you cannot configure an independent volume 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 the StatefulSet.
kubectl create -f disk-test.yaml -
Verify the result.
kubectl get pods -o wideExpected 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 directories in the pod. The mount directory
/datafor the disk is created.[root@k8s-master ~]# kubectl exec -it disk-test-0 -- bash root@disk-test-0:/# ls bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr root@disk-test-0:/# ls /data lost+found
-