This topic describes typical scenarios in which a NAS file system is needed for creating a stateful service, and the procedure for how to use one.

Scenarios and method

If a NAS file system is mounted to multiple pods, the pods share the data in the NAS file system. After a pod modifies the data stored in the NAS file system, the application supported by the pods is required to automatically update the modified data for the other pods.

Scenarios for using a NAS file system
  • You want to create or run applications that demand high disk I/O performance.
  • You need a storage service that has higher read and write performance than OSS.
  • You want to share files across hosts. For example, you want to use a NAS file system as a file server.

Method of using a NAS file system:

  1. Manually create a NAS file system and add a mount point to it.
  2. Manually create a PV and a PVC.

This topic describes how to use Alibaba Cloud NAS services in the PV/ PVC mode by using the flexvolume plugin provided by Alibaba Cloud.

Prerequisites

  • You have created a Kubernetes cluster. For more information, see Create a Kubernetes cluster.
  • You have connected to the Kubernetes cluster by using kubectl, see Connect to a Kubernetes cluster by using kubectl.
  • You have created a NAS file system in the NAS console. For more information, see Create a file system. You must make sure that the NAS file system and your Kubernetes cluster are in the same zone.
  • You have added a mount point for your Kubernetes cluster in the created NAS file system. For information, see Add a mount point. You must make sure that the NAS file system and your cluster are in the same VPC.

Create a PV

  1. Create a pv-nas.yaml file.
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-nas
      labels:
        alicloud-pvname: pv-nas
    spec:
      capacity:
        storage: 5Gi
      accessModes:
        - ReadWriteMany
      flexVolume:
        driver: "alicloud/nas"
        options:
          server: "***-**.cn-hangzhou.nas.aliyuncs.com"   ////Replace this value with your mount point.
          path: "/k8s1"
          vers: "4.0"

    Parameter description

    • alicloud-pvname: indicates a PV name.
    • server: indicates a NAS mount point. To view your mount point, log on to the NAS console, click File System List in the left-side navigation pane, select the target file system, and click Manage in the Action column to view the Mount Address in the Mount Point area. The mount address is the mount point of your NAS file system.

    • path: indicates the NAS mount directory. You can mount a NAS sub-directory to your cluster. If the NAS sub-directory specified by you does not exist, the system automatically creates the NAS sub-directory and mounts it to your cluster.
    • vers: (optional) indicates the version number of the NFS mount protocol. NFS file system V3.0 and V4.0 are available. The default is V4.0.
    • mode: (optional) indicates the access permission to the mount directory. By default, this parameter is not set.
      Note
      • Access permission to the root directory of the NAS file system cannot be set.
      • If you set the mode parameter for a NAS file system that stores a large amount of data, the process of mounting the NAS file system to a cluster may take an excessive amount of time or even fail. We recommend that you do not set this parameter.
  2. Run the following command to create a PV:
    $ kubectl create -f pv-nas.yaml

Result

In the left-side navigation pane under Kubernetes, choose Clusters > Volumes, and select the target cluster to view the created PV.

Create a PVC

Create a PVC for the NAS file system. Specifically, you need to set the selector field to filter for the created PV so that you can associate the PVC with the correct PV.

  1. Create a pvc-nas.yaml file.
    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: pvc-nas
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 5Gi
      selector:
        matchLabels:
          alicloud-pvname: pv-nas
  2. Run the following command to create a PVC:
    $ kubectl create -f pvc-nas.yaml

Result

In the left-side navigation pane under Kubernetes, choose Application > Volumes Claim, and select the target cluster and namespace to view the created PVC.

Create an application

  1. Create a nas.yaml.
    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: nginx
            ports:
            - containerPort: 80
            volumeMounts:
              - name: pvc-nas
                mountPath: "/data"
          volumes:
            - name: pvc-nas
              persistentVolumeClaim:
                claimName: pvc-nas
  2. Run the following command to create a deployment:
    $ kubectl create -f nas.yaml

Result

In the left-side navigation pane under Kubernetes, choose Application > Deployment, and select the target cluster and namespace to view the created deployment.

Verify that the NAS file system is shared by pods

  1. Run the following command to view the pods in which the created deployment resides:
    $ kubectl get pod
    NAME                             READY   STATUS    RESTARTS   AGE
    nas-static-f96b6b5d7-rcb2f       1/1     Running   0          9m
    nas-static-f96b6b5d7-wthmb       1/1     Running   0          9m
  2. Run the following commands to view the files in the /data path of each pod:
    $ kubectl exec nas-static-f96b6b5d7-rcb2f ls /data
    $ kubectl exec nas-static-f96b6b5d7-wthmb ls /data
    Note The two /data paths are empty.
  3. Run the following command to create file nas in the /data path of one pod:
    $ kubectl exec nas-static-f96b6b5d7-rcb2f touch /data/nas
  4. Run the following commands to view the files in the /data path of each pod:
    $ kubectl exec nas-static-f96b6b5d7-rcb2f ls /data
    nas
    $ kubectl exec nas-static-f96b6b5d7-wthmb ls /data
    nas
    Note After you create the file in the /data path of one pod, the file then exists in both the /data paths of the two pods. This means that the two pods share the NSA file system.

Verify that data on the NAS file system are stored persistently

  1. Run the following command to remove all the pods of the created application:
    $ kubectl delete pod nas-static-f96b6b5d7-rcb2f nas-static-f96b6b5d7-wthmb
    pod "nas-static-f96b6b5d7-rcb2f" deleted
    pod "nas-static-f96b6b5d7-wthmb" deleted
  2. Open another kubectl interface and run the following command to view the process in which the original pods are removed and new pods are created by Kubernetes:
    $ kubectl get pod -w -l app=nginx
    NAME                             READY   STATUS    RESTARTS   AGE
    nas-static-f96b6b5d7-rcb2f       1/1     Running   0          27m
    nas-static-f96b6b5d7-wthmb       1/1     Running   0          27m
    nas-static-f96b6b5d7-rcb2f   1/1   Terminating   0     28m
    nas-static-f96b6b5d7-wnqdj   0/1   Pending   0     0s
    nas-static-f96b6b5d7-wnqdj   0/1   Pending   0     0s
    nas-static-f96b6b5d7-wnqdj   0/1   ContainerCreating   0     0s
    nas-static-f96b6b5d7-wthmb   1/1   Terminating   0     28m
    nas-static-f96b6b5d7-nwkds   0/1   Pending   0     0s
    nas-static-f96b6b5d7-nwkds   0/1   Pending   0     0s
    nas-static-f96b6b5d7-nwkds   0/1   ContainerCreating   0     0s
    nas-static-f96b6b5d7-rcb2f   0/1   Terminating   0     28m
    nas-static-f96b6b5d7-wthmb   0/1   Terminating   0     28m
    nas-static-f96b6b5d7-rcb2f   0/1   Terminating   0     28m
    nas-static-f96b6b5d7-rcb2f   0/1   Terminating   0     28m
    nas-static-f96b6b5d7-wnqdj   1/1   Running   0     10s
    nas-static-f96b6b5d7-wthmb   0/1   Terminating   0     28m
    nas-static-f96b6b5d7-wthmb   0/1   Terminating   0     28m
    nas-static-f96b6b5d7-nwkds   1/1   Running   0     17s
  3. Run the following command to view the new pods created by Kubernetes:
    $ kubectl get pod
    NAME                             READY   STATUS    RESTARTS   AGE
    nas-static-f96b6b5d7-nwkds       1/1     Running   0          21s
    nas-static-f96b6b5d7-wnqdj       1/1     Running   0          21s
  4. Run the following commands to view the files in the /data path of each pod:
    $ kubectl exec nas-static-f96b6b5d7-nwkds ls /data 
    nas
    $ kubectl exec nas-static-f96b6b5d7-wnqdj ls /data
    nas
    Note The created file, namely, file nas has not been removed. This means that data in the NAS file system can be stored persistently.