All Products
Search
Document Center

Container Service for Kubernetes:Mount a statically provisioned NAS volume

Last Updated:Dec 25, 2024

File Storage NAS (NAS) is a distributed file system that supports shared access and elastic scaling and provides high reliability and high performance. NAS is suitable for scenarios such as big data analysis, data sharing, web applications, and log storage. The Container Storage Interface (CSI) plug-in allows you to use an existing NAS file system to create persistent volumes (PVs) and persistent volume claims (PVCs) and mount the PVs and PVCs to workloads for persistent storage and shared storage of data.

Prerequisites

  • The CSI plug-in is installed in the cluster. For more information about how to update csi-plugin and csi-provisioner, see Update csi-plugin and csi-provisioner.

    Note

    If FlexVolume is used in your cluster, upgrade FlexVolume to CSI because FlexVolume is deprecated. For more information, see Upgrade from FlexVolume to CSI. Choose Operations > Add-ons and click the Storage tab to check the storage component type.

  • 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.

  • Make sure that the existing NAS file system meets the following requirements. Otherwise, create a new NAS file system that meets the requirements or mount a dynamically provisioned NAS volume. For more information, see Create a file system.

    • The protocol type is Network File System (NFS). Extreme NAS file systems support only NFSv3.

    • The mount target and the cluster node to which you want to mount the NAS file system belong to the same virtual private cloud (VPC) and the Status of the mount target is Available. For more information about how to create a mount target, see Manage mount targets.

      Note

      If you want to encrypt data in a NAS volume, you can configure the encryption settings when you create the NAS file system.

Limits

  • You cannot mount NAS file systems that use the Server Message Block (SMB) protocol.

  • General-purpose and Extreme NAS file systems have different limits on mounting scenarios, the number of file systems, and file sharing protocols. For more information about the supported states, see Limits.

  • Statically provisioned NAS volumes cannot be expanded.

Usage notes

Step 1: Mount a statically provisioned NAS volume

Use kubectl

  1. Create a statically provisioned PV.

    1. Create a file named pv-nas.yaml and copy the following content to the file:

      apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: pv-nas
        labels:
          alicloud-pvname: pv-nas
      spec:
        capacity:
          storage: 5Gi
        accessModes:
          - ReadWriteMany
        csi:
          driver: nasplugin.csi.alibabacloud.com
          volumeHandle: pv-nas   # Specify the name of the PV. 
          volumeAttributes:
            server: "0c47****-mpk25.cn-shenzhen.nas.aliyuncs.com"  # The mount target of the NAS file system, which is the same as the VPC of the cluster. 
            path: "/csi"  # Specify the subdirectory of the NAS file system. 
        mountOptions:
        - nolock,tcp,noresvport
        - vers=3

      Parameter

      Description

      name

      The name of the PV.

      labels

      The labels that you want to add to the PV

      storage

      The capacity of the NAS volume.

      Note

      In actual scenarios, the capacity of the NAS volume is not limited by the storage parameter. For more information about the available capacity of the NAS volume, see Limits.

      accessModes

      The access mode of the PV. The default value is ReadWriteMany. You can also set the value to ReadWriteOnce or ReadOnlyMany.

      driver

      The type of the storage driver that is used to provision the volume. In this example, the parameter is set to nasplugin.csi.alibabacloud.com. This indicates that the CSI plug-in provided by Alibaba Cloud is used.

      volumeHandle

      The unique identifier of the PV. Enter the name of the PV. If multiple PVs are used, the identifier of each PV must be unique.

      server

      The mount target of the NAS file system, which must be the same as the VPC of the cluster.

      Note

      Replace the value with the domain name of a mount target. For more information about how to view the domain name of a mount target, see Manage mount targets.

      path

      The subdirectory of the NAS file system that you want to mount. The parent directory of the General-purpose NAS file system is /.

      Important

      If you want to mount an Extreme NAS file system, set this parameter to a subdirectory of the /share directory. Example: /share/csi.

      mountOptions

      Configure the options parameter and specify the NFS version in the mountOptions field. The version of the NFS protocol. We recommend that you use NFSv3. Extreme NAS file systems support only NFSv3. For more information about the NFS protocol, see NFS.

    2. Create a statically provisioned PV.

      kubectl create -f pv-nas.yaml
  2. When you create a PVC of the NAS type, set the selector parameter to specify how to select a PV and bind the PV to the PVC.

    1. Create a file named pvc-nas.yaml and copy the following content to the file:

      kind: PersistentVolumeClaim
      apiVersion: v1
      metadata:
        name: pvc-nas
      spec:
        accessModes:
          - ReadWriteMany
        resources:
          requests:
            storage: 5Gi
        selector:
          matchLabels:
            alicloud-pvname: pv-nas

      Parameter

      Description

      Example

      name

      The name of the PVC.

      pvc-nas

      accessModes

      The access mode of the PVC. The default value is ReadWriteMany. You can also set the value to ReadWriteOnce or ReadOnlyMany.

      ReadWriteMany

      storage

      The claimed capacity of the NAS volume that can be used by the application. The claimed capacity cannot exceed the total capacity of the NAS volume.

      Note

      In actual scenarios, the capacity of the NAS volume is not limited by the storage parameter. For more information about the available capacity of the NAS volume, see Limits.

      5Gi

      matchLabels

      The labels used to select a PV and bind the PV to the PVC.

      pv-nas

    2. Create a statically provisioned PVC.

      kubectl create -f pvc-nas.yaml
  3. Deploy an application named nas-static and associate the PVC with the application.

    1. The following YAML template provides an example of the nas.yaml file that is used to create the nas-static application.

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nas-static
        labels:
          app: nginx
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: nginx
        template:
          metadata:
            labels:
              app: nginx
          spec:
            containers:
            - name: nginx
              image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6
              ports:
              - containerPort: 80
              volumeMounts:
                - name: pvc-nas
                  mountPath: "/data"
            volumes:
              - name: pvc-nas
                persistentVolumeClaim:
                  claimName: pvc-nas

      Parameter

      Description

      mountPath

      The path of the container to which the NAS volume is mounted.

      claimName

      The name of the PVC mounted to the application.

    2. Deploy an application named nas-static and associate the PVC with the application.

      kubectl create -f nas.yaml
  4. View the pod information.

    kubectl get pod

    Expected output:

    NAME                          READY   STATUS    RESTARTS   AGE
    nas-static-5b5cdb85f6-n****   1/1     Running   0          32s
    nas-static-c5bb4746c-4****    1/1     Running   0          32s

Use the ACK console

Step 1: Create a PV

  1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, choose Volumes > Persistent Volumes.

  3. On the Persistent Volumes page, click Create.

  4. In the Create PV dialog box, configure the parameters and click Create. The following table describes the parameters.

    Parameter

    Description

    Example

    PV Type

    Select NAS.

    NAS

    Volume Name

    The name of the volume. The name must be unique in the cluster.

    pv-nas

    Capacity

    The capacity of the PV.

    Note

    A NAS file system provides unlimited capacity. This parameter does not limit the storage usage of the NAS file system but defines the capacity of the PV.

    20Gi

    Access Mode

    You can select ReadWriteMany or ReadWriteOnce. Default value: ReadWriteMany.

    ReadWriteMany

    Enable CNFS

    Specify whether to enable Container Network File System (CNFS). After you enable CNFS, you must perform the following operations:

    Enable

    Mount Target Domain Name

    You must configure this parameter when CNFS is disabled.

    You can select Select Mount Target or Custom to configure the NAS file system that you want to mount. For more information about how to view the domain name of a mount target, see Manage mount targets.

    0c47****-mpk25.cn-shenzhen.nas.aliyuncs.co

    Advanced Options (Optional)

    Mount Path: the mount path of the NAS file system. The mount path must start with a forward slash (/), which indicates the root directory. After you configure this parameter, the PV is mounted to the specified subdirectory.

    • If the specified subdirectory does not exist, the system automatically creates the subdirectory in the NAS file system and mounts the subdirectory to the cluster.

    • If you do not configure this parameter, the root directory of the NAS file system is mounted.

    • If you want to mount an Extreme NAS file system, set this parameter to a subdirectory of the /share directory. Example: /share/data.

    /data

    Reclaim Policy: This parameter is set to Delete by default. You can also set this parameter to Retain.

    • Delete: If the reclaim policy is deleted, you must configure the archiveOnDelete parameter.

      • If you set the archiveOnDelete parameter to true, the related PV and NAS file system are renamed after you delete a PVC. The related PV and NAS file system are not deleted together with the PVC.

      • If you set the archiveOnDelete parameter to false, the related PV and NAS file system are also deleted when you delete a PVC.

    • Retain: When a PVC is deleted, the related PV and NAS file system are retained and can only be manually deleted.

    If you have high requirements for data security, we recommend that you use the Retain policy to prevent data loss caused by user errors.

    Retain

    Mount Options: the optional parameters for mounting a NAS file system, including the version of the NFS protocol. We recommend that you use NFS v3. Extreme NAS file systems support only NFS v3. For more information about the NFS protocol, see NFS.

    v3

    Label

    Add labels to the PV.

    pv-nas

    After you create the PV, you can view the PV on the Persistent Volumes page.

Step 2: Create a PVC

  1. In the left-side navigation pane of the details page, choose Volumes > Persistent Volume Claims.

  2. On the Persistent Volume Claims page, click Create.

  3. In the Create PVC dialog box, configure the parameters. After you configure the parameters, click Create.

    Parameter

    Description

    Example

    PVC Type

    Select NAS.

    NAS

    Name

    The name of the PVC. The name must be unique in the cluster.

    pvc-nas

    Allocation Mode

    In this example, Existing Volumes is selected.

    Note

    If no PV is created, you can set the Allocation Mode parameter to Create Volume and configure the required parameters to create a PV.

    Select Existing Volumes.

    Existing Storage Class

    Click Select PV. Find the PV that you want to use and click Select in the Actions column.

    Existing Volumes

    Capacity

    The capacity of the PV.

    Note
    • The capacity claimed by the PVC cannot exceed the capacity of the PV that is bound to the PVC.

    20

    Access Mode

    The default value is ReadWriteMany. You can also set the value to ReadWriteOnce or ReadOnlyMany.

    ReadWriteMany

Step 3: Create an application

  1. In the left-side navigation pane of the details page, choose Workloads > Deployments.

  2. On the Deployments tab, click Create from Image.

  3. Configure the parameters of the application. Then, click Create.

    The following table describes the key parameters. Retain the default values for other parameters. For more information, see Create a stateless application by using a Deployment.

    Section

    Parameter

    Description

    Example

    Basic Information

    Name

    Enter a custom name for the Deployment. The name must meet the format requirements displayed in the console.

    test-nas

    Replicas

    The number of pod replicas provisioned by the Deployment.

    2

    Container

    Image Name

    The address of the image used to deploy the application.

    anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6

    Required Resources

    Specify the number of vCores and the amount of memory required by the application.

    0.25 vCores and 0.5 GiB of memory

    Volume

    Click Add PVC and configure the parameters.

    • Mount Source: Select the PVC you created.

    • Container Path: Specify the container path to which you want to mount the NAS file system.

    • Mount Source: pvc-nas

    • Container Path: /data

    image.png

  4. Run the following command to query the deployment progress of the application:

    1. On the Deployments page, click the name of the application that you want to manage.

    2. On the Pods tab, check whether the pod is in the Running state.

Step 2: Verify that the NAS volume can persist and share data

The Deployment you created in the preceding steps contains two pods. The two pods are mounted to the same NAS file system. You can use one of the following methods to verify that the NAS volume can persist and share data.

  • Recreate the pod. Access the NAS volume from a recreated pod to check whether the original data still exists in the NAS file system. If the data still exists, data persistence is enabled.

  • Create a file in one pod and access the file from the other pod. If you can access the file, data sharing is enabled.

Verify that the NAS file system can be used to persist data

  1. Query the pod on which the application resides.

    kubectl get pod 

    Expected output:

    NAME                          READY   STATUS    RESTARTS   AGE
    nas-static-5b5cdb85f6-a****   1/1     Running   0          32s
    nas-static-c5bb4746c-b****    1/1     Running   0          32s
  2. Create a file in a pod.

    1. Check whether a file exists in the /data path that is mounted to the application.

      In this example, the nas-static-5b5cdb85f6-a**** pod is used.

      kubectl exec nas-static-5b5cdb85f6-a**** -- ls /data

      No output is returned. This indicates that no file exists in the /data path.

    2. Create a file named nas in the /data path of the pod.

      kubectl exec nas-static-5b5cdb85f6-a**** -- touch /data/nas
  3. Access the file you created from the other pod.

    In this example, the nas-static-5b5cdb85f6-b**** pod is used.

    kubectl exec nas-static-5b5cdb85f6-b**** -- ls /data

    Expected output:

    nas
  4. Recreate a pod.

    1. Delete the pod.

      kubectl delete pod nas-static-5b5cdb85f6-a****
    2. Open another command-line interface (CLI) and view how the pod is deleted and recreated.

      kubectl get pod -w -l app=nginx
  5. Verify that the file still exists after the pod is deleted.

    1. Query the name of the recreated pod.

      kubectl get pod

      Expected output:

      NAME                                  READY    STATUS    RESTARTS   AGE
      nas-static-5b5cdb85f6-c****   1/1     Running   0          32s
      nas-static-c5bb4746c-a****    1/1     Running   0          32s
    2. Query the files in the /data path of the recreated pod.

      In this example, the nas-static-5b5cdb85f6-c**** pod is used.

      kubectl exec nas-static-5b5cdb85f6-c**** -- ls /data

      Expected output:

      nas

      The nas file still exists in the /data path. This indicates that data is persisted to the NAS file system.

Verify that data in the NAS file system can be shared across pods

  1. Query the pod on which the application resides.

    kubectl get pod 

    Expected output:

    NAME                          READY   STATUS    RESTARTS   AGE
    nas-static-5b5cdb85f6-n****   1/1     Running   0          32s
    nas-static-c5bb4746c-4****    1/1     Running   0          32s
  2. Query the files in the /data path of the two pods.

    kubectl exec nas-static-5b5cdb85f6-n**** -- ls /data
    kubectl exec nas-static-c5bb4746c-4**** -- ls /data

    No output is returned. This indicates that no file exists in the /data path.

  3. Create a file named nas in the /data path of a pod:

    In this example, the nas-static-5b5cdb85f6-n**** pod is used.

     kubectl exec nas-static-5b5cdb85f6-n**** -- touch /data/nas
  4. Query the files in the /data path of the two pods.

    1. Query the files in the /data path of the nas-static-5b5cdb85f6-n**** pod.

      kubectl exec nas-static-5b5cdb85f6-n**** -- ls /data

      Expected output:

      nas
    2. Query the files in the /data path of the nas-static-c5bb4746c-4**** pod.

      kubectl exec nas-static-c5bb4746c-4**** -- ls /data

      Expected output:

      nas

      When you create a file in the /data path of one pod, you can also find the file in the /data path of the other pod. This indicates that data in the NAS file system is shared by the two pods.

References