All Products
Search
Document Center

Elastic Container Instance:Mount a NAS file system

Last Updated:Nov 01, 2024

File Storage NAS (NAS) is a distributed file system provided by Alibaba Cloud. NAS supports shared access and auto scaling. NAS features high reliability and high performance. After you deploy the CSI-Provisioner component provided by Alibaba Cloud on a self-managed Kubernetes cluster, you can mount NAS file systems as volumes on the cluster. You can mount NAS file systems as statically or dynamically provisioned volumes. This topic describes how to use a PersistentVolumeClaim (PVC) to mount a NAS file system to an elastic container instance-based 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.

    Important

    For 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

  • NAS is a shared storage service. You can mount a NAS file system to multiple pods. If a NAS file system is mounted to multiple pods, the data in the file system is shared by the pods. In this case, an application must be able to synchronize data across these pods when data modifications are made to multiple pods.

  • Do not delete the mount target before you unmount the NAS file system. Otherwise, an operating system hang may occur.

Mount a NAS file system as a statically provisioned volume

  1. Create a NAS file system and add a mount target.

    1. Log on to the NAS console.

    2. Create a NAS file system and add a mount target.

      The NAS file system and mount target must reside in the same VPC as the VNode. For more information, see Create a NAS file system and Manage mount targets.

  2. Create a persistent volume (PV).

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

      apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: static-nas-pv
        labels:
          alicloud-pvname: static-nas-pv
      spec:
        capacity:
          storage: 25Gi
        accessModes:
          - ReadWriteMany
        csi:
          driver: nasplugin.csi.alibabacloud.com
          volumeHandle: static-nas-pv
          volumeAttributes:
            server: "2564f4****-ysu87.cn-beijing.nas.aliyuncs.com"
            path: "/test"
        mountOptions:
        - nolock,tcp,noresvport
        - vers=3

      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 nasplugin.csi.alibabacloud.com. This indicates that the Container Storage Interface (CSI) plug-in provided by Alibaba Cloud for NAS is used.

      volumeHandle

      The unique ID of the PV. The value must be the same as the name defined in metadata.

      server

      The mount point of the NAS file system.

      path

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

      vers

      The version number of the Network File System (NFS) protocol that is used to mount the NAS file system. We recommend that you use NFS v3. Extreme NAS supports only NFS v3.

    2. Run the following command to create a PV:

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

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

      apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: static-nas-pvc
      spec:
        accessModes:
        - ReadWriteMany
        resources:
          requests:
            storage: 25Gi
        selector:
          matchLabels:
            alicloud-pvname: static-nas-pv 
    2. Run the following command to create a PVC:

      kubectl create -f static-nas-pvc.yaml
  4. Mount the NAS file system to two elastic container instance-based pods.

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

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: static-nas-test
        labels:
          app: nginx
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: 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
              volumeMounts:
                - name: pvc-nas
                  mountPath: /data
            volumes:
              - name: pvc-nas
                persistentVolumeClaim:
                  claimName: static-nas-pvc
    2. Run the following command to create a Deployment:

      kubectl create -f static-nas-test.yaml
    3. View the results.

      kubectl get pods -o wide

      The following command output is expected:

      NAME                              READY   STATUS    RESTARTS   AGE   IP            NODE                                  NOMINATED NODE   READINESS GATES
      
      static-nas-test-5c4b6d4bd-4kggt   1/1     Running   0          95s   172.16.XX.XX   cn-beijing.vnd-2ze8nd8xcl33t4pa****   <none>           <none>
      static-nas-test-5c4b6d4bd-ql6m4   1/1     Running   0          95s   172.16.XX.XX   cn-beijing.vnd-2ze8nd8xcl33t4pa****   <none>           <none>

      Check the file directories in the pods and verify that the /data mount directory is generated for the NAS file system. In addition, verify that the files written to the first pod can be viewed in the second pod. This indicates that the two pods share the NAS file system.

      Mount a NAS file system as a statically provisioned volume by using CSI

Mount a NAS file system as a dynamically provisioned volume

  1. Create a NAS file system and add a mount target.

    1. Log on to the NAS console.

    2. Create a NAS file system and add a mount target.

      The NAS file system and mount target must reside in the same VPC as the VNode. For more information, see Create a NAS file system and Manage mount targets.

  2. Create a StorageClass.

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

      apiVersion: storage.k8s.io/v1
      kind: StorageClass
      metadata:
        name: alicloud-nas-subpath
      mountOptions:
      - nolock,tcp,noresvport
      - vers=3
      parameters:
        volumeAs: subpath
        server: "0cd8b4a576-g****.cn-hangzhou.nas.aliyuncs.com:/k8s/"
      provisioner: nasplugin.csi.alibabacloud.com
      reclaimPolicy: Retain

      Parameter

      Description

      mountOptions

      The mount options, such as the NFS version.

      volumeAs

      The PV type. A value of subpath indicates a subdirectory. In this case, CSI-Provisioner automatically creates a subdirectory of the NAS file system.

      server

      The mount target of the NAS file system if you set the volumeAs parameter to subpath.

      provisioner

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

      reclaimPolicy

      The reclaim policy of the PV. Default value: Delete. You can also set this parameter to Retain.

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

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

      If you require higher data security, we recommend that you use the Retain mode to prevent data loss caused by user errors.

    2. Run the following command to create a StorageClass:

      kubectl create -f nas-sc.yaml
  3. Create a PVC of the NAS type.

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

      kind: PersistentVolumeClaim
      apiVersion: v1
      metadata:
        name: nas-pvc
      spec:
        accessModes:
        - ReadWriteMany
        storageClassName: alicloud-nas-subpath
        resources:
          requests:
            storage: 25Gi
    2. Run the following command to create a PVC:

      kubectl create -f nas-pvc.yaml
  4. Mount the NAS file system to two elastic container instance-based pods.

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

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nas-test
        labels:
          app: nginx
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: 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
              volumeMounts:
                - name: pvc-nas
                  mountPath: /data
            volumes:
              - name: pvc-nas
                persistentVolumeClaim:
                  claimName: nas-pvc
    2. Run the following command to create a Deployment:

      kubectl create -f nas-test.yaml
    3. View the results.

      kubectl get pods -o wide

      The following command output is expected:

      NAME                              READY   STATUS    RESTARTS   AGE     IP             NODE                                  NOMINATED NODE   READINESS GATES
      nas-test-76c54d4b4-f7b88          1/1     Running   0          4m41s   172.16.XX.XX   cn-beijing.vnd-2ze8nd8xcl33t4pa****   <none>           <none>
      nas-test-76c54d4b4-lqz4b          1/1     Running   0          4m41s   172.16.XX.XX   cn-beijing.vnd-2ze8nd8xcl33t4pa****   <none>           <none>

      Check the file directories in the pods and verify that the /data mount directory is generated for the NAS file system. In addition, verify that the files written to the first pod can be viewed in the second pod. This indicates that the two pods share the NAS file system.

      Mount a NAS file system as a dynamically provisioned volume by using CSI