Use NAS dynamic volumes

更新时间:
复制 MD 格式

With dynamic provisioning for File Storage NAS, you can automatically provision storage for workloads on demand, eliminating the need to create PVs in advance. This feature provides data persistence and sharing, allowing multiple Pods to read and write simultaneously, and simplifies storage management for use cases like web applications and log retention.

How it works

With dynamic volumes, when an application creates a PVC, the system automatically creates a new storage volume and a corresponding PV on demand. This provisioning is determined by the StorageClass specified in the PVC. This mode offers greater flexibility and supports automatic storage volume expansion.

Mount mode

You can specify the mount mode with the volumeAs parameter in a StorageClass. This parameter defines the relationship between a PV and the NAS file system.

Mount mode

Description

Scenario

subpath

In subpath mode, each PV is mapped to a unique subdirectory within a NAS file system. This mode facilitates data isolation.

  • Multiple Pods mount the same subdirectory of a NAS file system.

  • Multiple Pods mount different subdirectories of the same NAS file system for data isolation.

sharepath

In sharepath mode, all PVs map to the same NAS directory defined in the StorageClass. Therefore, all PVCs using this StorageClass access the same shared directory.

Multiple Pods across different namespaces need to mount the same NAS subdirectory.

filesystem (Not recommended)

In file system mode, a new, dedicated NAS file system instance is dynamically created for each PV.

This mode is suitable for applications that require strict isolation in terms of performance and security. It enables the dynamic creation and deletion of independent NAS file systems and mount targets for each application, but incurs higher costs.

Workflow

The workflow for mounting a NAS dynamic volume is as follows.

  1. Create a StorageClass: Define a storage template, declare the NAS mount target, and select a mount mode (such as subpath or sharepath) based on your business requirements.

  2. Create a PVC: An application uses a PVC to claim storage resources. The system automatically creates a PV and binds it to the PVC based on the specified StorageClass.

  3. Mount the volume in your application: Mount the bound PVC to an application Pod. The volume is then available as a persistent directory inside the container.

Prerequisites

  • The csi-plugin and csi-provisioner components are installed.

    CSI components are installed by default. Verify that they remain installed. You can check the installation status on the Add-ons page. We recommend upgrading the CSI components to the latest version.
  • To use subpath and sharepath, you need an Apsara File Storage NAS file system that meets the following requirements. Otherwise, create a file system.

    NAS has limitations on mount connectivity, the number of file systems, and protocol types.
    • Protocol: Only NFS is supported.

    • VPC: The Apsara File Storage NAS file system must be in the same VPC as your cluster. Apsara File Storage NAS supports cross-zone mounting but not cross-VPC mounting.

    • Mount Target: Add a mount target that is in the same VPC as your cluster and is active. For more information, see Manage mount targets. Note the mount target address.

    • (Optional) Encryption Type: To encrypt data in a volume, configure the encryption type when you create the Apsara File Storage NAS file system.

Usage notes

  • Do not delete mount targets: To prevent node I/O errors, do not delete a mount target in the NAS console while its corresponding volume is in use.

  • Concurrent writes: NAS provides shared storage. When multiple Pods mount the same volume, your application must handle potential data consistency issues from concurrent writes.

    For more information about limitations on concurrent writes to NAS, see Multiple processes writing to the same log file and Latency in reading updated file content across ECS instances.
  • Mount performance: If you configure the securityContext.fsgroup parameter in your application, kubelet recursively runs chmod or chown after the volume is mounted, significantly increasing Pod startup time.

    To optimize mount performance, see NAS volume FAQ.

Method 1: Mount a volume using subpath

In this method, the CSI provisioner automatically creates a dedicated subpath in the NAS file system for each PersistentVolumeClaim (PVC). This subpath functions as the PersistentVolume (PV).

1. Create a StorageClass

A StorageClass acts as a template for provisioning dynamic volumes and defines the source and behavior of the storage resources.

kubectl

  1. Create a file named alicloud-nas-subpath.yaml with the following content:

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      # The name of the StorageClass, which must be unique within the cluster.
      name: alicloud-nas-subpath  
    mountOptions:
    - nolock,tcp,noresvport
    - vers=3
    parameters:
      # Set to subpath.
      volumeAs: subpath
      # The format of the server field is <nas-server-address>:/<path>.
      server: "0cd8b4a576-g****.cn-hangzhou.nas.aliyuncs.com:/k8s"
      archiveOnDelete: "true"
    provisioner: nasplugin.csi.alibabacloud.com
    reclaimPolicy: Retain
    allowVolumeExpansion: true

    Parameter

    Description

    mountOptions

    The mount options for NAS, including the NFS protocol version. By default, volumes are mounted using NFSv3. You can specify a different version with an option such as vers=4.0. For information about the NFS protocols supported by different NAS types, see NFS protocols.

    parameters.volumeAs

    The volume provisioning mode. Set to subpath.

    parameters.server

    The NAS mount target and the subpath to mount, in the format <nas-server-address>:/<path>.

    • <nas-server-address>: The mount target of the NAS file system. For more information, see Manage mount targets.

    • :/<path>: The NAS subpath to mount. If this parameter is not set or the specified subpath does not exist, the root directory is mounted by default.

      • General-purpose NAS: The root directory is /.

      • Extreme NAS: The root directory is /share. When you mount a subpath, the path must start with /share, such as /share/data.

    parameters.archiveOnDelete

    Specifies whether to delete the data on the backend storage when a PVC is deleted and its reclaimPolicy is set to Delete.

    This option acts as a safeguard to prevent accidental data loss on shared NAS storage.
    • true (default): The directory and its files are not deleted. Instead, they are archived and renamed in the format archived-{pvName}.{timestamp}.

    • false: The corresponding directory and its data are permanently deleted.

      This operation deletes the NAS subpath and its files, not the NAS file system itself.

      To delete a NAS file system, see Delete a file system.

    In scenarios where PVs are frequently created and deleted, setting this parameter to false may cause the CSI controller's task queue to become blocked, which prevents new PVs from being created. For more information, see The controller's task queue is full and new PVs cannot be created when you use a dynamic NAS volume.

    provisioner

    The driver. Set this parameter to nasplugin.csi.alibabacloud.com when you use the Alibaba Cloud NAS CSI plug-in.

    reclaimPolicy

    The reclaim policy of the PV.

    • Delete (default): When the PVC is deleted, the backend storage data is processed based on the archiveOnDelete setting.

    • Retain: When the PVC is deleted, the PV and the data in the NAS file system are not deleted and must be deleted manually. This policy is recommended for scenarios that require high data security to prevent accidental data loss.

    allowVolumeExpansion

    Supported only by General-purpose NAS.

    Allows dynamic expansion of PVs created by this StorageClass by modifying the PVC capacity.

    This StorageClass uses the NAS directory quota feature to manage PV capacity, enabling precise size control. You can expand the volume online by editing the spec.resources.requests.storage field of the PVC. For more information, see Set directory quotas for dynamic NAS volumes.

    NAS directory quotas take effect asynchronously. If you perform high-speed, large-volume data writes immediately after a PV is created or expanded, you may exceed the specified capacity because the quota is not yet fully enforced. For more information, see Usage limits.
  2. Create the StorageClass.

    kubectl create -f alicloud-nas-subpath.yaml

Console

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

  2. On the Clusters page, click the name of your cluster. In the left navigation pane, click Volumes > StorageClasses.

  3. Click Create, specify a unique name for the StorageClass within the cluster, select NAS as the PV Type, and then follow the on-screen instructions to create the StorageClass.

    The key parameters are described in the following table.

    Parameter

    Description

    Select Mount Target

    The mount target of the NAS file system.

    Volume Mode

    The volume provisioning mode. Select Subdirectory. A subpath is automatically created under the specified mount path. Data is stored at <NAS mount target>:<mount path>/<pv-name>/.

    Mount Path

    The NAS subdirectory to mount. If this parameter is not specified, the root directory is mounted by default.

    If the directory does not exist in the NAS file system, it is automatically created and then mounted.
    • General-purpose NAS: The root directory is /.

    • Extreme NAS: The root directory is /share. When you mount a subdirectory, the path must start with /share, such as /share/data.

    Reclaim Policy

    The reclaim policy of the PV.

    • Delete (default): When the PVC is deleted, the backend storage data is processed based on the archiveOnDelete setting.

    • Retain: When the PVC is deleted, the PV and the data in the NAS file system are not deleted and must be deleted manually. This policy is recommended for scenarios that require high data security to prevent accidental data loss.

    Mount Options

    The mount options for NAS, including the NFS protocol version. By default, volumes are mounted using NFSv3. You can specify a different version with an option such as vers=4.0. For information about the NFS protocols supported by different NAS types, see NFS protocols.

    After the StorageClass is created, you can view it in the StorageClasses list.

2. Create a PVC

A PersistentVolumeClaim (PVC) is an application's request for storage that triggers an associated StorageClass to dynamically provision and bind a matching PersistentVolume (PV).

kubectl

  1. Create a file named nas-pvc.yaml with the following content:

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata: 
      name: nas-csi-pvc
    spec:
      accessModes:
      - ReadWriteMany 
      # Specify the StorageClass to use.
      storageClassName: alicloud-nas-subpath  
      resources: 
        requests:
          # Declare the required capacity of the storage volume.
          storage: 20Gi  

    Parameter

    Description

    accessModes

    The access mode. Valid values:

    • ReadWriteMany (default): The volume can be mounted as read-write by many nodes.

    • ReadWriteOnce: The volume can be mounted as read-write by a single node.

    • ReadOnlyMany: The volume can be mounted as read-only by many nodes.

    storageClassName

    The name of the StorageClass to use.

    storage

    The required capacity of the storage volume. By default, this declaration acts as a resource request and does not limit the actual storage space available to the pod.

    The capacity limit of a NAS file system is determined by its specifications. For more information, see General-purpose NAS and Extreme NAS.

    However, when allowVolumeExpansion in the StorageClass is set to true, this value becomes a strictly enforced hard limit. The CSI plug-in sets the NAS directory quota based on this configuration to limit the available capacity of the PV.

  2. Create the PVC.

    kubectl create -f nas-pvc.yaml
  3. Check the PV.

    kubectl get pv

    The expected output is similar to the following, which shows that the StorageClass automatically provisioned a PersistentVolume (PV) and bound it to the PVC.

    NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                 STORAGECLASS             VOLUMEATTRIBUTESCLASS   REASON   AGE
    nas-a7540d97-0f53-4e05-b7d9-557309******   20Gi       RWX            Retain           Bound    default/nas-csi-pvc   alicloud-nas-subpath     <unset>                          5m

Console

  1. In the left navigation pane of the cluster management page, choose Storage > PVCs.

  2. On the Persistent Volume Claims page, click Create, configure the PVC parameters, and then click Create.

    Parameter

    Description

    PVC Type

    Select NAS.

    Name

    The name of the PVC. The name must be unique within the namespace.

    Allocation Mode

    Select Use StorageClass.

    Existing Storage Class

    Click Select Storage Class and choose the StorageClass that you created.

    Total Capacity

    The required capacity of the storage volume. By default, this declaration acts as a resource request and does not limit the actual storage space available to the pod.

    The capacity limit of a NAS file system is determined by its specifications. For more information, see General-purpose NAS and Extreme NAS.

    However, when allowVolumeExpansion in the StorageClass is set to true, this value becomes a strictly enforced hard limit. The CSI plug-in sets the NAS directory quota based on this configuration to limit the available capacity of the PV.

    Access Mode

    The access mode. Valid values:

    • ReadWriteMany (default): The volume can be mounted as read-write by many nodes.

    • ReadWriteOnce: The volume can be mounted as read-write by a single node.

    • ReadOnlyMany: The volume can be mounted as read-only by many nodes.

3. Create an application and mount NAS

After the PVC is created, you can mount the volume to an application. This section shows how to create two Deployments that reference the same PVC, allowing multiple pods to share the same NAS subpath.

kubectl

Create two Deployments that reference the same PVC to allow them to share the same subpath of the NAS file system.

To mount different subpaths from the same NAS file system to different pods, you must create a unique StorageClass and a corresponding PVC for each subpath.
  1. Create two files named nginx-1.yaml and nginx-2.yaml with the following content.

    The configurations for both applications are nearly identical. They both reference the same PVC.
    • nginx-1.yaml

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nas-test-1     
        labels:
          app: nginx
      spec:
        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: nas-pvc
                  # The mount path for the NAS volume inside the container.
                  mountPath: "/data"          
            volumes:
              - name: nas-pvc                 
                persistentVolumeClaim:
                  # Reference the previously created PVC.
                  claimName: nas-csi-pvc      
    • nginx-2.yaml

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nas-test-2     
        labels:
          app: nginx
      spec:
        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: nas-pvc
                  # The mount path for the NAS volume inside the container.
                  mountPath: "/data"           
            volumes:
              - name: nas-pvc                 
                persistentVolumeClaim:
                  # Reference the previously created PVC.
                  claimName: nas-csi-pvc       
  2. Create the two Deployments.

    kubectl create -f nginx-1.yaml -f nginx-2.yaml
  3. Check the status of the pods.

    kubectl get pod -l app=nginx

    Expected output:

    NAME                         READY   STATUS    RESTARTS   AGE
    nas-test-1-b75d5b6bc-*****   1/1     Running   0          51s
    nas-test-2-b75d5b6bc-*****   1/1     Running   0          44s
  4. Inspect each pod to verify that it uses the correct PersistentVolumeClaim (PVC).

    Replace <podName> with the actual name of each pod.
    kubectl describe pod <podName> | grep "ClaimName:"

    In the expected output, both pods use the same PVC, which confirms that they share the same subpath of the NAS file system.

Console

Repeat the following steps to create two Deployments that use the same PVC, which allows them to share the same subpath of the NAS file system.

  1. On the ACK Clusters page, click the name of your cluster. In the left navigation pane, click Workloads > Deployments.

  2. Click Create from Image and follow the on-screen instructions to configure and create the application.

    The key parameters are described below. You can keep the default values for other parameters. For more information, see Create a stateless application (Deployment).

    Section

    Parameter

    Description

    Basic Information

    Replicas:

    The number of replicas for the Deployment.

    Container

    Image

    The address of the image used to deploy the application.

    Required Resources

    The vCPU and memory resources required by the container.

    Volumes

    Click Add PVC and then configure the parameters.

    • Mount Source: Select the PVC that you created.

    • Container Path: Enter the path within the container where the NAS file system will be mounted.

    After the deployment is complete, you can click the application name on the Deployments page and check the Pods tab to confirm that the pods are running properly (their status is Running).

To verify that the storage is shared and persistent, see Verify shared and persistent storage.

Method 2: Mount using sharepath mode

In this mode, when PVs are created for PVCs that use this StorageClass, the provisioner does not create new directories. Instead, all PVs use the path defined in the StorageClass, mapping each PV to the same NAS directory.

1. Create a StorageClass

A StorageClass acts as a template for provisioning dynamic volumes and defines the source and behavior of the storage resources.

kubectl

  1. Create a file named alicloud-nas-sharepath.yaml with the following content.

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: alicloud-nas-sharepath
    mountOptions:
    - nolock,tcp,noresvport
    - vers=3
    parameters:
      volumeAs: sharepath
      # The format of the server field is <nas-server-address>:/<path>
      server: "0cd8b4a576-g****.cn-hangzhou.nas.aliyuncs.com:/k8s"
    provisioner: nasplugin.csi.alibabacloud.com
    reclaimPolicy: Retain

    Parameter

    Description

    mountOptions

    The mount options for NAS, including the NFS protocol version. By default, volumes are mounted using NFSv3. You can specify a different version with an option such as vers=4.0. For information about the NFS protocols supported by different NAS types, see NFS protocols.

    parameters.volumeAs

    The volume provisioning mode. Set to sharepath.

    parameters.server

    The NAS mount target and the subpath to mount, in the format <nas-server-address>:/<path>.

    • <nas-server-address>: The mount target of the NAS file system. For more information, see Manage mount targets.

    • :/<path>: The NAS subpath to mount. If this parameter is not set or the specified subpath does not exist, the root directory is mounted by default.

      • General-purpose NAS: The root directory is /.

      • Extreme NAS: The root directory is /share. When you mount a subpath, the path must start with /share, such as /share/data.

    provisioner

    The driver. Set this parameter to nasplugin.csi.alibabacloud.com when you use the Alibaba Cloud NAS CSI plug-in.

    reclaimPolicy

    The reclaim policy for the PV. For sharepath mode, this must be Retain.

  2. Create the StorageClass.

    kubectl create -f alicloud-nas-sharepath.yaml

Console

  1. On the Clusters page, click the name of your cluster. In the left navigation pane, click Volumes > StorageClasses.

  2. Click Create, specify a unique name for the StorageClass, and select NAS as the PV Type.

    Parameter

    Description

    Select Mount Target

    The mount target of the NAS file system.

    Volume Mode

    The volume provisioning mode. In this example, select Shared Directory, which enables sharepath mode.

    Mount Path

    The NAS subdirectory to mount. If this parameter is not specified, the root directory is mounted by default.

    If the directory does not exist in the NAS file system, it is automatically created and then mounted.
    • General-purpose NAS: The root directory is /.

    • Extreme NAS: The root directory is /share. When you mount a subdirectory, the path must start with /share, such as /share/data.

    Reclaim policy

    For sharepath mode, this must be Retain.

    Mount options

    The mount options for NAS, including the NFS protocol version. By default, volumes are mounted using NFSv3. You can specify a different version with an option such as vers=4.0. For information about the NFS protocols supported by different NAS types, see NFS protocols.

2. Create a PVC

A PersistentVolumeClaim (PVC) is an application's request for storage that triggers an associated StorageClass to dynamically provision and bind a matching PersistentVolume (PV).

To enable data sharing across namespaces, you will create a PVC with the same name in two different namespaces. Although the PVCs share a name, they are distinct resources because they are in different namespaces. The StorageClass provisions a separate PV for each PVC, and both PVs point to the same shared directory on the NAS file system.

kubectl

  1. Create two namespaces, ns1 and ns2.

    kubectl create ns ns1
    kubectl create ns ns2
  2. Create a file named pvc.yaml with the following content to create a PVC with the same name in both the ns1 and ns2 namespaces.

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata: 
      name: nas-csi-pvc
      namespace: ns1
    spec:
      accessModes:
      - ReadWriteMany 
      storageClassName: alicloud-nas-sharepath
      resources: 
        requests:
          storage: 20Gi
    ---
    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata: 
      name: nas-csi-pvc
      namespace: ns2
    spec:
      accessModes:
      - ReadWriteMany 
      storageClassName: alicloud-nas-sharepath
      resources: 
        requests:
          storage: 20Gi

    Parameter

    Description

    accessModes

    The access mode. Valid values:

    • ReadWriteMany (default): The volume can be mounted as read-write by many nodes.

    • ReadWriteOnce: The volume can be mounted as read-write by a single node.

    • ReadOnlyMany: The volume can be mounted as read-only by many nodes.

    storageClassName

    The name of the StorageClass to use.

    storage

    The required capacity of the storage volume. By default, this declaration acts as a resource request and does not limit the actual storage space available to the pod.

    The capacity limit of a NAS file system is determined by its specifications. For more information, see General-purpose NAS and Extreme NAS.

    However, when allowVolumeExpansion in the StorageClass is set to true, this value becomes a strictly enforced hard limit. The CSI plug-in sets the NAS directory quota based on this configuration to limit the available capacity of the PV.

  3. Create the PVCs.

    kubectl create -f pvc.yaml
  4. Check the PV status to confirm that PVs have been automatically created and bound to the PVCs.

    kubectl get pv

    The expected output is similar to the following. The status of both PVs is Bound, and the CLAIM column shows that each PV is bound to a PVC in a different namespace (ns1/nas-csi-pvc and ns2/nas-csi-pvc).

    NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                 STORAGECLASS             VOLUMEATTRIBUTESCLASS   REASON   AGE
    nas-0b448885-6226-4d22-8a5b-d0768c******   20Gi       RWX            Retain           Bound    ns1/nas-csi-pvc       alicloud-nas-sharepath   <unset>                          74s
    nas-bcd21c93-8219-4a11-986b-fd934a******   20Gi       RWX            Retain           Bound    ns2/nas-csi-pvc       alicloud-nas-sharepath   <unset>                          74s

Console

  1. Create namespaces.

    • On the Clusters page, click the name of your cluster. In the left navigation pane, click Namespaces and Quotas.

    • Click Launch and follow the on-screen instructions to create the ns1 and ns2 namespaces.

  2. In the left navigation pane of the cluster management page, choose Storage > PVCs.

  3. In the ns1 namespace, create a storage claim.

    On the Persistent Volume Claims page, select ns1 from the Namespace dropdown list and follow the on-screen instructions to create the PVC.

    Parameter

    Description

    PVC Type

    Select NAS.

    Name

    The name of the PVC. The name must be unique within the namespace.

    Allocation Mode

    Select Use StorageClass.

    Existing Storage Class

    Click Select Storage Class and choose the StorageClass that you created.

    Capacity

    The required capacity of the storage volume. By default, this declaration acts as a resource request and does not limit the actual storage space available to the pod.

    The capacity limit of a NAS file system is determined by its specifications. For more information, see General-purpose NAS and Extreme NAS.

    However, when allowVolumeExpansion in the StorageClass is set to true, this value becomes a strictly enforced hard limit. The CSI plug-in sets the NAS directory quota based on this configuration to limit the available capacity of the PV.

    Access Mode

    The access mode. Valid values:

    • ReadWriteMany (default): The volume can be mounted as read-write by many nodes.

    • ReadWriteOnce: The volume can be mounted as read-write by a single node.

    • ReadOnlyMany: The volume can be mounted as read-only by many nodes.

  4. Follow the previous step to create another PVC in the ns2 namespace.

    After creation, return to the Persistent Volume Claims page. In both the ns1 and ns2 namespaces, confirm that both PVCs are bound to their automatically created PVs.

3. Create an application and mount the NAS

After the PVCs are created, you can mount their bound PVs to your applications. In this section, you will create an application in each of the two namespaces and mount the corresponding PVC. This configuration allows both applications to share the NAS subdirectory defined in the StorageClass.

kubectl

  1. Create two files named nginx-ns1.yaml and nginx-ns2.yaml with the following content.

    The configurations for both applications are nearly identical. They each bind to the PVC in their respective namespace.
    • nginx-ns1.yaml

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nas-test
        namespace: ns1   
      spec:
        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: nas-pvc
                  mountPath: "/data"
            volumes:
              - name: nas-pvc
                persistentVolumeClaim:
                  claimName: nas-csi-pvc
    • nginx-ns2.yaml

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nas-test
        namespace: ns2   
      spec:
        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: nas-pvc
                  mountPath: "/data"
            volumes:
              - name: nas-pvc
                persistentVolumeClaim:
                  claimName: nas-csi-pvc
  2. Create the two Deployments.

    kubectl create -f nginx-ns1.yaml -f nginx-ns2.yaml
  3. Check the Pod status.

    kubectl get pod -A -l app=nginx

    Expected output:

    NAMESPACE   NAME                         READY   STATUS    RESTARTS   AGE
    ns1         nas-test-b75d5b6bc-*****     1/1     Running   0          2m19s
    ns2         nas-test-b75d5b6bc-*****     1/1     Running   0          2m11s
  4. Check the detailed configuration of a Pod to confirm the PVC mount.

    Replace <namespace-name> and <pod-name> with the actual namespace and Pod name.
    kubectl describe pod -n <namespace-name> <pod-name> | grep "ClaimName:"

    In the expected output, the two Pods mount ns1/nas-csi-pvc and ns2/nas-csi-pvc respectively.

Console

  1. On the ACK Clusters page, click the name of your cluster. In the left navigation pane, click Workloads > Deployments.

  2. In the ns1 namespace, create a Deployment and mount the corresponding PVC.

    1. Select Namespace from the Namespace dropdown list and click Create from Image.

    2. Follow the on-screen instructions to create the application.

      The key parameters are described below. Leave other parameters at their default values. For more information, see Create a stateless workload (Deployment).

      Section

      Parameter

      Description

      Basic Information

      Replicas:

      The replica count of the Deployment.

      Container

      Image

      The address of the image used to deploy the application.

      Required Resources

      The required vCPU and memory resources.

      Volumes

      Click Add PVC and configure the parameters.

      • Mount Source: Select the PVC that you created.

      • Container Path: Enter the path within the container where the NAS file system will be mounted, such as /data.

  3. Follow the previous step to create another Deployment in the ns2 namespace and mount the corresponding PVC.

    Return to the Deployments page. In the ns1 and ns2 namespaces, check the status of each Deployment to confirm that the Pods are running and have mounted the correct PVCs.

To verify the results, see Verify shared and persistent storage.

Method 3: Mount a volume in filesystem mode

The filesystem mode allows you to dynamically create and manage a dedicated NAS file system and mount target for an application. Unlike the sharepath mode, each PV created in filesystem mode corresponds to a separate NAS file system instance.

Each PV of the filesystem type corresponds to a dedicated NAS file system and one mount target.
  • By default, when you delete a PV of the filesystem type, the corresponding NAS file system and mount target are retained. To automatically delete the NAS file system and mount target when the PV is deleted, add the following parameters to the StorageClass:

    • reclaimPolicy: Delete

    • parameters.deleteVolume: "true"

  • When using an ACK dedicated cluster, grant the necessary permissions to the csi-provisioner.

    Authorization process

    Using NAS volumes of the filesystem type involves the dynamic creation and deletion of NAS file systems and mount targets. Therefore, the csi-provisioner component in your ACK dedicated cluster requires the corresponding permissions.

    The following is the minimum set of permissions required for the RAM policy.

    {
        "Action": [
            "nas:DescribeMountTargets",
            "nas:CreateMountTarget",
            "nas:DeleteFileSystem",
            "nas:DeleteMountTarget",
            "nas:CreateFileSystem"
        ],
        "Resource": [
            "*"
        ],
        "Effect": "Allow"
    }

    You can grant the permissions by using one of the following methods:

    • Add the preceding NAS permissions to the custom policy of the master RAM role for your ACK dedicated cluster. For more information, see Modify the content and description of a custom policy. On the cluster details page, click the Cluster Resources tab, find the Master RAM Role (such as KubernetesMasterRole-xxx), and attach the policy to this role.

    • Create a RAM user, attach the preceding RAM policy, and generate an access key. Then, configure the access key in the env variables for the csi-provisioner.

      env:
      - name: CSI_ENDPOINT
        value: unix://socketDir/csi.sock
      - name: ACCESS_KEY_ID
        value: ""
      - name: ACCESS_KEY_SECRET
        value: ""

1. Create a StorageClass

A StorageClass is the provisioning template for a dynamic volume. It defines the source and behavior of the storage resources.

  1. Create a file named alicloud-nas-fs.yaml with the following content to define a StorageClass in filesystem mode:

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: alicloud-nas-fs
    mountOptions:
    - nolock,tcp,noresvport
    - vers=3
    parameters:
      volumeAs: filesystem
      fileSystemType: standard
      storageType: Performance
      regionId: cn-beijing
      zoneId: cn-beijing-e
      vpcId: "vpc-2ze2fxn6popm8c2mzm****"
      vSwitchId: "vsw-2zwdg25a2b4y5juy****"
      accessGroupName: DEFAULT_VPC_GROUP_NAME
      deleteVolume: "false"
    provisioner: nasplugin.csi.alibabacloud.com
    reclaimPolicy: Retain

    Parameter

    Description

    mountOptions

    The mount options for NAS, including the NFS protocol version. By default, volumes are mounted using NFSv3. You can specify a different version with an option such as vers=4.0. For information about the NFS protocols supported by different NAS types, see NFS protocols.

    parameters.volumeAs

    Set Mount type to filesystem.

    parameters.fileSystemType

    The type of the NAS file system. Valid values:

    • standard (default): General-purpose NAS.

    • extreme: Extreme NAS.

    parameters.storageType

    The storage class of the NAS file system.

    • Valid values for General-purpose NAS:

      • Performance (default): Performance

      • Capacity: Capacity

    • Valid values for Extreme NAS:

      • standard (default): Standard

      • advance: Advanced

    parameters.regionId

    The region of the NAS file system. This must be the same region as the cluster.

    parameters.zoneId

    The availability zone of the NAS file system. In the same VPC, NAS supports cross-zone mounts.

    parameters.vpcId

    The VPC for the file system's mount target. This must be the same VPC as the cluster.

    parameters.vSwitchId

    The ID of the VSwitch for the file system's mount target.

    parameters.accessGroupName

    The access group of the NAS file system's mount target. The default value is DEFAULT_VPC_GROUP_NAME.

    parameters.deleteVolume

    Specifies whether to delete the PV and its corresponding NAS file system and mount target when the PVC is deleted. Because NAS is a shared file system, you must configure both deleteVolume and reclaimPolicy for security purposes.

    provisioner

    The driver. Set this parameter to nasplugin.csi.alibabacloud.com when you use the Alibaba Cloud NAS CSI plug-in.

    reclaimPolicy

    The PV, the corresponding NAS file system, and the mount target are deleted along with the PVC only if the PV reclaim policy is set to Delete and the deleteVolume parameter is set to true.

  2. Run the following command to create the StorageClass.

    kubectl create -f alicloud-nas-fs.yaml

2. Create a PVC

  1. Create a file named nas-pvc-fs.yaml with the following content to request storage resources:

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: nas-csi-pvc-fs
    spec:
      accessModes:
        - ReadWriteMany
      storageClassName: alicloud-nas-fs
      resources:
        requests:
          storage: 20Gi

    The following describes the parameters:

    Parameter

    Description

    accessModes

    The access mode. Valid values:

    • ReadWriteMany (default): The volume can be mounted as read-write by many nodes.

    • ReadWriteOnce: The volume can be mounted as read-write by a single node.

    • ReadOnlyMany: The volume can be mounted as read-only by many nodes.

    storage

    The capacity of the NAS volume to create.

    The minimum capacity for Extreme NAS is 100 GiB. If Extreme NAS is defined in the StorageClass, the value of storage must be ≥ 100 GiB.

    storageClassName

    The name of the StorageClass that this claim requests.

  2. Create the PVC.

    kubectl create -f nas-pvc-fs.yaml

3. Create application and mount NAS

  1. Refer to the following to create nas-fs.yaml.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: deployment-nas-fs
      labels:
        app: nginx-test
    spec:
      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: nas-pvc
                mountPath: "/data"
          volumes:
            - name: nas-pvc
              persistentVolumeClaim:
                claimName: nas-csi-pvc-fs
  2. Create the Deployment.

    kubectl create -f nas-fs.yaml
  3. Check the PVC status.

    kubectl get pvc nas-csi-pvc-fs

    The following output shows that the PVC is successfully bound to the PV:

    NAME             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS      VOLUMEATTRIBUTESCLASS   AGE
    nas-csi-pvc-fs   Bound    nas-8338540d-34a1-4796-89cf-be2a7*******   20Gi       RWX            alicloud-nas-fs   <unset>                 13m
  4. Check the ID of the newly created NAS file system in the PV details.

    Replace nas-8338540d-34a1-4796-89cf-be2a7******* with the actual PV name.
    kubectl describe pv nas-8338540d-34a1-4796-89cf-be2a7******* | grep "fileSystemId"

    The output shows the ID of the newly created NAS file system. You can view this file system on the File System List page in the NAS console.

Verify shared and persistent storage

After you successfully deploy the application, you can verify that the storage volume works as expected. This section uses subpath mode to create nas-test-1 and nas-test-2 as an example for verification.

Shared storage

Persistent storage

To verify shared storage, create a file in one Pod and then view it in another.

  1. List the Pods to get their names.

    kubectl get pod | grep nas-test

    Expected output:

    nas-test-1-b75d5b6bc-*****           1/1     Running   0          50s
    nas-test-2-b75d5b6bc-*****           1/1     Running   0          60s
  2. Create a file in one of the Pods.

    Take the Pod named nas-test-1-b75d5b6bc-***** as an example:

    kubectl exec nas-test-1-b75d5b6bc-***** -- touch /data/test.txt
  3. Check if the file exists in the other Pod.

    Take the Pod named nas-test-2-b75d5b6bc-***** as an example.

    kubectl exec nas-test-2-b75d5b6bc-***** -- ls /data

    Expected output:

    test.txt

    The output shows the new file, confirming that data can be shared among Pods.

To verify persistent storage, restart the Deployment and check if the data still exists in the new Pod's file system.

  1. Restart the Deployment to trigger Pod recreation.

    kubectl rollout restart deploy nas-test-1
  2. Check the Pods and wait for the new Pod's status to become Running.

    kubectl get pod | grep nas-test

    Expected output:

    nas-test-1-5bb845b795-*****           1/1     Running   0          115m
    nas-test-2-5b6bccb75d-*****           1/1     Running   0          103m
  3. Check if the file created earlier exists in the new Pod.

    Take the Pod named nas-test-1-5bb845b795-***** as an example.

    kubectl exec nas-test-1-5bb845b795-***** -- ls /data

    Expected output:

    test.txt

    The output shows the previously written file, confirming that the data is persistent.

For production environments

  • Security and data protection

    • Use the Retain reclaim policy: Set the reclaimPolicy of your StorageClass to Retain. This prevents data loss if a PVC is accidentally deleted.

    • Use permission groups for access control: NAS uses permission groups to manage network access. Follow the principle of least privilege: add only the private IP addresses of cluster nodes or their vSwitch CIDR blocks to the permission group. Avoid overly broad permissions, such as 0.0.0.0/0.

  • Performance and cost optimization

    • Select an appropriate NAS type: Refer to File system selection to choose a NAS type that meets your application's IOPS and throughput requirements.

    • Optimize mount options (mountOptions): Adjust NFS mount parameters based on your workload characteristics. For example, using protocol versions such as vers=4.0 or vers=4.1 can improve performance and file locking capabilities in some scenarios. For workloads with large-scale file I/O, test different rsize and wsize values to optimize read and write throughput.

  • Operations and reliability

    • Configure health checks: Configure a liveness probe for your application pods to verify that the mount point is healthy. If a mount point becomes unresponsive, ACK automatically restarts the pod, triggering a remount of the storage volume.

    • Set up monitoring and alerts: Use container storage monitoring to configure alerts to promptly detect storage volume issues or performance bottlenecks.

Resource release

To avoid unexpected charges, release the associated resources in the following order when the NAS storage volume is no longer required.

  1. Delete the workload

    • Action: Delete all workloads (such as Deployments and StatefulSets) that use the NAS storage volume. This prevents application Pods from mounting, reading from, or writing to the volume.

    • Example command: kubectl delete deployment <your-deployment-name>

  2. Delete the PVC

    • Action: Delete the PVC associated with the workload. After the PVC is deleted, the behavior of the bound PV and the backend NAS depends on the reclaimPolicy defined in the corresponding StorageClass.

      • Subpath mode

        • If reclaimPolicy is Retain: After the PVC is deleted, its bound PV enters the Released state. The PV object and the corresponding subdirectory and data on the backend NAS are retained and must be deleted manually.

        • If reclaimPolicy is Delete: After the PVC is deleted, its bound PV is automatically deleted. The handling of the backend NAS subdirectory depends on the archiveOnDelete parameter:

          • If archiveOnDelete is "true": The backend data is not deleted but is renamed and archived as archived-{pvName}.{timestamp}.

          • If archiveOnDelete is "false": The PV's corresponding subdirectory on the backend NAS and all its data are permanently deleted. Proceed with caution.

          In an ACK Serverless cluster, due to permission restrictions, even if reclaimPolicy: Delete is configured, the backend NAS directory and data are not deleted or archived. Only the PV object is deleted.
      • Sharepath mode

        This mode supports only a reclaimPolicy of Retain. After the PVC is deleted, its bound PV enters the Released state. The PV object, the backend shared directory, and all data are retained.

      • Filesystem mode

        • If reclaimPolicy is Retain: After the PVC is deleted, its bound PV enters the Released state. The PV object, the dynamically created backend NAS file system, and the mount point are all retained.

        • If reclaimPolicy is Delete: After the PVC is deleted, its bound PV is automatically deleted. The handling of the backend NAS file system depends on the deleteVolume parameter:

          • If deleteVolume is "false": The backend NAS file system and mount point are retained and must be deleted manually.

          • If deleteVolume is "true": The backend NAS file system and mount point are automatically deleted. Proceed with caution.

    • Example command: kubectl delete pvc <your-pvc-name>

  3. Delete the PV

    • Action: You can delete a PV when it is in the Available or Released state. This action only removes the PV definition from the Kubernetes cluster but does not delete any data from the backend NAS file system.

    • Example command: kubectl delete pv <your-pv-name>

  4. Delete the backend NAS file system (optional)

    • subpath and sharepath modes: See Delete a file system. This operation permanently deletes all data on the NAS, and the data cannot be recovered. Proceed with caution. Before you perform this operation, you must confirm that the NAS has no business dependencies.

    • filesystem mode: If the backend NAS file system is not automatically deleted when you delete a PVC, see Delete a file system to locate and manually delete the corresponding file system.

Related documents