This topic describes how to mount an Apsara File Storage NAS file system on a pod in a Container Service for Kubernetes cluster by using the FlexVolume driver. You can mount the file system as a static volume, persistent volume (PV), or persistent volume claim (PVC). After the file system is mounted, the Kubernetes cluster can access the file system.

Prerequisites

  1. A Kubernetes cluster is created. For more information, see Create an ACK cluster.

    If you use a user-created Kubernetes cluster, download and install the Alibaba Cloud FlexVolume driver.

  2. The version of the FlexVolume driver is v1.12.6.52-f6604e5-aliyun or later.

    If the version of the FlexVolume driver is earlier than the recommended version, upgrade your driver. For more information, see Upgrade system components.

  3. A file system is created. For more information, see Create a file system.

    The file system and the Kubernetes cluster must reside in the same zone.

  4. A mount target is created for the file system. For more information, see Create a mount target.

    The mount target and the Kubernetes cluster must reside in the same virtual private cloud (VPC).

Background information

The FlexVolume driver allows you to mount a file system on a Kubernetes pod by using the following methods:
  • Mount the file system as a static volume.
  • Mount the file system as a static PV or PVC.

Mount the file system as a static volume

  1. Create a pod. In this example, an NGINX pod is created.
    1. Create a YAML Ain't Markup Language (YAML) file. In this example, the nas-deploy.yaml file is created.
    2. Copy the following script to the nas-deploy.yaml file and replace the parameter values with values that are specific to your environment.
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nas-static
        labels:
          app: nginx
      spec:
        replicas: 1
        selector:
          matchLabels:
            app: nginx
        template:
          metadata:
            labels:
              app: nginx
          spec:
            containers:
            - name: nginx
              image: nginx
              ports:
              - containerPort: 80
              volumeMounts:                          #Configure the volumeMounts node and the volumes node based on your NAS file system.
                - name: nas1
                  mountPath: "/data"
            volumes:                          
            - name: "nas1"
              flexVolume:
                driver: "alicloud/nas"
                options:
                  server: "0cd8b4a576-grs79.cn-hangzhou.nas.aliyuncs.com"
                  path: "/k8s"
                  vers: "3"
                  options: "nolock,tcp,noresvport"
      Table 1. Parameters
      Parameter Description
      server The domain name of the mount target.
      path The path to the mount directory. You can specify the path to the root directory or a subdirectory in the NAS file system. If you specify a subdirectory that does not exist, the system automatically creates and mounts the subdirectory.
      vers The version number of the Network File System (NFS) protocol. Version 3 and version 4 are available. Version 3 is recommended and specified by default.
      mode The access mode of the mount directory.
      Note
      • This parameter is valid only if the mount directory is a subdirectory of the NAS file system.
      • If a NAS file system stores a large volume of data, we recommend that you do not configure the mode parameter. Otherwise, the process of mounting the file system may require a long period of time or the mount may fail.
      options The mount options. If the vers parameter is set to 3, the nolock, tcp, and noresvport options are specified by default. If the vers parameter is set to 4, the noresvport option is specified by default.
    3. Run the following command to create a pod:
      kubectl apply -f nas-deploy.yaml

      Replace nas-deploy.yaml with the name of your YAML file.

  2. Run the following command to query the pod that is created:
    kubectl get pod
  3. Run the following command to verify the mount result:
    kubectl exec -ti nas-static-85xxxx-wb7s9 sh
    mount | grep nfs

    Replace nas-static-85xxxx-wb7s9 with the name of your pod.

    The following figure shows an example of a successful mount.Mount result

Mount the file system as a static PV or PVC

  1. Create a PV.

    You can create a PV by using a YAML file or in the Container Service console.

    • Create a PV by using a YAML file.
      1. Create a YAML file. In this example, the nas-pv.yaml file is created.
      2. Copy the following script to the nas-pv.yaml file.
        apiVersion: v1
        kind: PersistentVolume
        metadata:
          name: pv-nas
        spec:
          capacity:
            storage: 5Gi
          storageClassName: nas
          accessModes:
            - ReadWriteMany
          flexVolume:
            driver: "alicloud/nas"
            options:
              server: "0cd8b4a576-uih75.cn-hangzhou.nas.aliyuncs.com"
              path: "/k8s"   
              vers: "3"
              options: "nolock,tcp,noresvport"

        For more information about the parameters in the script, see Table 1.

    • Create a PV in the Container Service console.
      1. Log on to the Container Service console.
      2. Choose Container Service - Kubernetes > Clusters > Persistent Volumes. On the page that appears, click Create.
      3. In the Create PV dialog box, set the parameters.
        Table 2. Parameters
        Parameter Description
        PV Type Select NAS.
        Volume Name The name of the PV. The name must be unique in the Kubernetes cluster. In this example, the name of the PV is pv-nas.
        Volume Plug-in The FlexVolume and Container Storage Interface (CSI) drivers are supported.
        Capacity The capacity of the PV.
        Note The capacity of the PV cannot exceed the capacity of the file system.
        Access Mode Valid values: ReadWriteMany and ReadWriteOnce. Select the access mode based on your business requirements.
        Mount Target Domain Name The domain name of the mount target, for example, file-system-id.region.nas.aliyuncs.com. Set this parameter based on your business requirements.
        Subdirectory The path to the mount directory. The path must start with a forward slash (/). Specify a subdirectory in a file system to mount.
        • If the specified subdirectory does not exist, the system automatically creates and mounts the subdirectory.
        • This parameter is optional. The default mount directory is the root directory of the file system.
        Permissions The access mode of the mount directory, such as 755, 644, and 777. This parameter is optional. By default, the original mode of the PV is specified.
        Note
        • This parameter is valid only if the mount directory is a subdirectory of the NAS file system.
        • If a NAS file system stores a large volume of data, we recommend that you do not configure this parameter. Otherwise, the process of mounting the file system may require a long period of time or the mount may fail.
        chmod (Change Mode) The change mode of access modes. Valid values: Non-recursive and Recursive.
        • Non-recursive: Mode changes apply only to the mount directory. The subdirectories and files in the mount directory are not affected.
        • Recursive: Mode changes apply to the mount directory, and the subdirectories and files in the mount directory.
        Version The version number of the NFS protocol. Version 3 and version 4 are available. Version 3 is recommended and specified by default.
        Label The labels of the PV.
      4. After the configuration is complete, click Create.
  2. Create a PVC.

    You can create a PVC by using a YAML file or in the Container Service console.

    • Create a PVC by using a YAML file.
      1. Create a YAML file. In this example, the nas-pvc.yaml file is created.
      2. Copy the following script to the nas-pvc.yaml file.
        apiVersion: v1
        kind: PersistentVolumeClaim
        metadata:
          name: pvc-nas
        spec:
          accessModes:
            - ReadWriteMany
          storageClassName: nas
          resources:
            requests:
              storage: 5Gi
    • Create a static PVC in the Container Service console.
      • Choose Persistent Volumes > Persistent Volume Claim. On the Persistent Volume Claims tab, click Create.
      • In the Create PVC dialog box, set the parameters.
  3. Create a pod. In this example, an NGINX pod is created.
    1. Create a YAML file. In this example, the nas-pod.yaml file is created.
    2. Copy the following script to the nas-pod.yaml file and replace the parameter values with values that are specific to your environment.
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nas-static
        labels:
          app: nginx
      spec:
        replicas: 1
        selector:
          matchLabels:
            app: nginx
        template:
          metadata:
            labels:
              app: nginx
          spec:
            containers:
            - name: nginx
              image: nginx
              ports:
              - containerPort: 80
              volumeMounts:                         
                - name: pvc-nas
                  mountPath: "/data"
            volumes:
              - name: pvc-nas
                persistentVolumeClaim:
                  claimName: pvc-nas
    3. Run the following command to create a pod:
      kubectl apply -f nas-pod.yaml

      Replace nas-pod.yaml with the name of your YAML file.

  4. Run the following command to verify the mount result:
    kubectl exec -ti nas-static-85xxxx-wb7s9 sh
    mount | grep nfs

    Replace nas-static-85xxxx-wb7s9 with the name of your pod.

    The following figure shows an example of a successful mount.Mount result