All Products
Search
Document Center

Elastic Container Instance:Mount a disk

Last Updated:Oct 13, 2023

Disks are block-level storage products provided by Alibaba Cloud. Disks feature low latency, high performance, high durability, and high reliability. You can use FlexVolume to mount disks as volumes to a self-managed Kubernetes cluster. You can mount disks as statically or dynamically provisioned volumes. This topic describes how to use a persistent volume claim (PVC) to mount a disk to an Elastic Container Instance-based pod.

Prerequisites

  • A virtual node (VNode) is deployed in a self-managed Kubernetes cluster.

  • 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

  1. Create a disk.

    1. Log on to the Elastic Compute Service (ECS) console.

    2. In the region and zone in which the VNode resides, create a pay-as-you-go disk.

      For more information about how to create a disk, see Create a disk. After the disk is created, record the ID of the disk.

      Note

      If 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 or formatted.

  2. Create a persistent volume (PV).

    1. Create a file named static-pv-disk.yaml and copy the following template into the file. Modify the parameters in the template as required.

      apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: d-2zehdahrwoa7srga****
        labels:
          failure-domain.beta.kubernetes.io/zone: cn-beijing-k
          failure-domain.beta.kubernetes.io/region: cn-beijing
      spec:
        capacity:
          storage: 20Gi
        storageClassName: disk
        accessModes:
          - ReadWriteOnce
        flexVolume:
          driver: "alicloud/disk"
          fsType: "ext4"
          options:
            volumeId: "d-2zehdahrwoa7srga****"

      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 alicloud/disk. This indicates that the FlexVolume plug-in provided by Alibaba Cloud for disks is used.

      fstype

      The file system type of the disk.

      volumeId

      The ID of the disk.

      Note

      The name of the PV must be the same as the disk ID that is specified by the volumeId parameter.

    2. Run the following command to create a PV:

      kubectl create -f static-pv-disk.yaml
  3. Create a PVC.

    1. Create a file named static-pvc-disk.yaml and copy the following template into the file:

      apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: static-pvc-disk
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: disk
        resources:
          requests:
            storage: 20Gi
    2. Run the following command to create a PVC:

      kubectl create -f static-pvc-disk.yaml
  4. Mount the disk to an Elastic Container Instance-based pod.

    1. Create a file named static-test-disk.yaml and copy the following template into the file:

      apiVersion: v1
      kind: Pod
      metadata:
        name: static-test-disk
        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-pvc-disk
    2. Run the following command to create a pod:

      kubectl create -f static-test-disk.yaml
    3. View the result.

      kubectl get pods -o wide

      The following command output is expected:

      NAME                 READY   STATUS    RESTARTS   AGE    IP             NODE                                  NOMINATED NODE   READINESS GATES
      static-test-disk     1/1     Running   0          116s   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.

      静态云盘flex

Mount a disk as a dynamically provisioned volume

Deploy Disk Controller

Before you mount a disk as a dynamically provisioned volume, you must deploy Disk-Controller. Disk-Controller is used to automatically create a disk and the related PV.

  1. Create a YAML file that is used to deploy Disk-Controller.

    Create a file named disk-controller.yaml and copy the following template into the file.

    • Replace the values of the special.keyid and special.keysecret parameters with the AccessKey ID and AccessKey secret of your Alibaba Cloud account. For more information about how to obtain the AccessKey pair of your Alibaba Cloud account, see Obtain an AccessKey pair.

    • Replace the value of the ECS_ENDPOINT parameter with the actual endpoint. For more information about how to obtain the endpoint, see the "Endpoints" section of the Request syntax topic.

    kind: StorageClass
    apiVersion: storage.k8s.io/v1
    metadata:
      name: alicloud-disk-essd
    provisioner: alicloud/disk
    parameters:
      type: cloud_essd
    ---
    kind: StorageClass
    apiVersion: storage.k8s.io/v1beta1
    metadata:
      name: alicloud-disk-efficiency
    provisioner: alicloud/disk
    parameters:
      type: cloud_efficiency
    ---
    kind: StorageClass
    apiVersion: storage.k8s.io/v1beta1
    metadata:
      name: alicloud-disk-ssd
    provisioner: alicloud/disk
    parameters:
      type: cloud_ssd
    ---
    kind: StorageClass
    apiVersion: storage.k8s.io/v1beta1
    metadata:
      name: alicloud-disk-available
    provisioner: alicloud/disk
    parameters:
      type: available
    ---
    kind: ClusterRole
    apiVersion: rbac.authorization.k8s.io/v1beta1
    metadata:
      name: alicloud-disk-controller-runner
    rules:
      - apiGroups: [""]
        resources: ["persistentvolumes"]
        verbs: ["get", "list", "watch", "create", "delete"]
      - apiGroups: [""]
        resources: ["persistentvolumeclaims"]
        verbs: ["get", "list", "watch", "update"]
      - apiGroups: ["storage.k8s.io"]
        resources: ["storageclasses"]
        verbs: ["get", "list", "watch"]
      - apiGroups: [""]
        resources: ["events"]
        verbs: ["list", "watch", "create", "update", "patch"]
      - apiGroups: [""]
        resources: ["endpoints"]
        verbs: ["get", "list", "watch", "create", "update"]
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: alicloud-disk-controller
      namespace: kube-system
    ---
    kind: ClusterRoleBinding
    apiVersion: rbac.authorization.k8s.io/v1beta1
    metadata:
      name: run-alicloud-disk-controller
    subjects:
      - kind: ServiceAccount
        name: alicloud-disk-controller
        namespace: kube-system
    roleRef:
      kind: ClusterRole
      name: alicloud-disk-controller-runner
      apiGroup: rbac.authorization.k8s.io
    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: cloud-config
      namespace: kube-system
    data:
      special.keyid: "*****************"
      special.keysecret: "***************************"
    ---
    kind: Deployment
    apiVersion: apps/v1
    metadata:
      name: alicloud-disk-controller
      namespace: kube-system
    spec:
      selector:
        matchLabels:
          app: alicloud-disk-controller
      replicas: 1
      strategy:
        type: Recreate
      template:
        metadata:
          labels:
            app: alicloud-disk-controller
        spec:
          serviceAccount: alicloud-disk-controller
          containers:
            - name: alicloud-disk-controller
              image: registry.cn-hangzhou.aliyuncs.com/acs/alicloud-disk-controller:v1.16.9.55-c43698d4-aliyun
              env:
              - name: ECS_ENDPOINT
                value: "https://ecs.aliyuncs.com" 
              - name: ACCESS_KEY_ID
                valueFrom:
                  configMapKeyRef:
                    name: cloud-config
                    key: special.keyid
              - name: ACCESS_KEY_SECRET
                valueFrom:
                  configMapKeyRef:
                    name: cloud-config
                    key: special.keysecret
              volumeMounts:
                - name: cloud-config
                  mountPath: /etc/kubernetes/
                - name: logdir
                  mountPath: /var/log/alicloud/
          volumes:
            - name: cloud-config
              emptyDir: {}
            - name: logdir
              emptyDir: {}
  2. Run the following command to deploy Disk-Controller:

    kubectl create -f disk-controller.yaml
  3. View the result.

    kubectl -n kube-system get pods

    The following command output is expected:

    NAME                                        READY   STATUS    RESTARTS   AGE
    alicloud-disk-controller-677b59c5cd-r5dqc   1/1     Running   0          29m

Mount a disk

  1. Create a StorageClass.

    1. Create a file named sc-disk.yaml and copy the following template into the file. Modify the parameters in the template as required.

      kind: StorageClass
      apiVersion: storage.k8s.io/v1
      metadata:
        name: alicloud-disk-essd-beijing-k
      provisioner: alicloud/disk
      parameters:
        type: cloud_essd
        regionId: cn-beijing
        zoneId: cn-beijing-k
      reclaimPolicy: Delete

      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 alicloud/disk. This indicates that the FlexVolume plug-in provided by Alibaba Cloud for disks is used.

      type

      The type of the disk. Valid values:

      • cloud_essd: The system creates an enhanced SSD (ESSD).

      • cloud_ssd: The system creates an SSD.

      • cloud_efficiency: The system creates an ultra disk.

      • available: The system first attempts to create an SSD. If no SSD is available in the zone, the system creates an ultra disk.

      Note

      You 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. This indicates that the system attempts to create a disk of the specified types in sequence. The system stops trying once 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.

      reclaimPolicy

      The reclaim policy of the disk. Default value: Delete. Valid values:

      • Delete: When a PVC is deleted, the related PV and disk are also deleted.

      • Retain: When a PVC is deleted, the related PV and disk are retained and need to be manually deleted.

      If you require higher data security, we recommend that you use the Retain policy to prevent accidental deletion.

    2. Run the following command to create a StorageClass:

      kubectl create -f sc-disk.yaml
  2. Create a PVC.

    1. Create a file named pvc-disk.yaml and copy the following template into the file:

      apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: pvc-disk
      spec:
        accessModes:
        - ReadWriteMany
        resources:
          requests:
            storage: 20Gi
        storageClassName: alicloud-disk-essd-beijing-k
    2. Run the following command to create a PVC:

      kubectl create -f pvc-disk.yaml

      The system automatically creates a disk and the related PV based on the StorageClass configurations.

  3. Mount the disk to an Elastic Container Instance-based pod.

    1. Create a file named test-disk.yaml and copy the following template into the file:

      Note

      We 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: test-disk
      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: pvc-disk
    2. Run the following command to create a StatefulSet:

      kubectl create -f test-disk.yaml
    3. View the result.

      kubectl get pods -o wide

      The following command output is expected:

      NAME          READY   STATUS    RESTARTS   AGE     IP              NODE                                  NOMINATED NODE   READINESS GATES
      test-disk-0   1/1     Running   0          3m29s   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.

      fv动态云盘