This topic describes how to mount a volume, a static persistent volume (PV), or a static persistent volume claim (PVC) to access an Apsara File Storage NAS file system from a Container Service for Kubernetes cluster by using the flexVolume driver.

Prerequisites

  1. You have created a Kubernetes cluster. For more information, see Create a Kubernetes cluster.

    If your cluster is a self-built cluster, download and install the Alibaba Cloud flexVolume driver.

  2. Make sure that the version of the flexVolume driver you use is v1.12.6.52-f6604e5-aliyun or later.

    If the version of your flexVolume driver is earlier than the recommended version, we recommend that you update your driver. For more information, see Upgrade system components.

  3. You have created a file system. For more information, see Create file systems.

    The file system you have created and your Kubernetes cluster must be in the same zone.

  4. You have added a mount target. For more information, see Add a mount target.

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

Background information

With the flexVolume driver provided by Alibaba Cloud, you can access Apsara File Storage NAS file systems by using the following methods:
  • Mount a volume
  • Mount a static PV and PVC

Mount a volume

Use the nas-deploy.yaml file to create pods.
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: 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 mount target of your Apsara File Storage NAS file system.
path The mounted directory in your Apsara File Storage NAS file system. You can mount a sub-directory as a volume. If no sub-directory exists in the Apsara File Storage NAS file system, the system automatically creates and then mounts a sub-directory.
vers The version number of the Network File System (NFS) mount protocol. Version 3 and version 4.0 are supported. The recommended and default version is version 3.
mode The access permission on the mounted directory.
Note
  • You cannot configure the mode parameter if the root directory is mounted.
  • If the Apsara File Storage NAS file system stores a large amount of data, the process of mounting the volume may require a long period of time or fail. In this case, we do not recommend that you configure the mode parameter.
options The options for mounting the Apsara File Storage NAS file system. If you do not configure the parameter, the default value is nolock,tcp,noresvport for the V3 protocol, and noresvport for the V4.0 protocol.

Mount a static PV and PVC

  1. Create a PV.

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

    • Use the nas-pv.yaml file to create a PV.
      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 parameters, see Table 1.

    • Use the Alibaba Cloud Container Service console to create a PV.
      1. Log on to the Container Service console.
      2. Choose Container Service-Kubernetes > Clusters > Persistent Volumes. Select the target cluster and click Create.
      3. In the Create PV dialog box, set related parameters.create pv
        Parameter Description
        PV Type Select NAS.
        Volume Name The name of the PV. It must be unique in the cluster. In this example, use pv-nas as the name.
        Capacity The capacity of the PV.
        Note The capacity of the PV cannot exceed the disk capacity.
        Access Mode The access mode is ReadWriteMany by default.
        Mount Point Domain Name The mount target of the Apsara File Storage NAS file system, for example, file-system-id.region.nas.aliyuncs.com. You can set this parameter based on your business requirements.
        Subpath The sub-directory of the Apsara File Storage NAS file system. It must start with a forward slash (/). After the PV is created, the specified sub-directory is mounted as the PV.
        • If no sub-directory exists in the root directory of an Apsara File Storage NAS file system, the system automatically creates a sub-directory.
        • You do not need to specify this parameter. The root directory of the Apsara File Storage NAS file system is mounted by default.
        Permissions The access permission on the mounted directory. For example, you can set this parameter to 755, 644, or 777.
        • The permission can only be set when a sub-directory is mounted as the PV.
        • You do not need to specify this parameter. The default permission is the original permission of the Apsara File Storage NAS file system.
        • If the Apsara File Storage NAS file system stores a large amount of data, the process of mounting the volume may require a long period of time or fail. In this case, we do not recommend that you configure this parameter.
        Version The version number of the Network File System (NFS) mount protocol. Version 3 and version 4.0 are supported. The recommended and default version is version 3.
        Label The label of the PV.
      4. After you set the parameters, click Create.
  2. Use the nas-pvc.yaml file to create a PVC.
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-nas
    spec:
      accessModes:
        - ReadWriteMany
      storageClassName: nas
      resources:
        requests:
          storage: 5Gi
  3. Use the nas-pod.yaml file to create pods.
    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