All Products
Search
Document Center

Container Service for Kubernetes:Use NAS volumes to share and persist data

Last Updated:Nov 16, 2023

You can use an Apsara File Storage NAS (NAS) file system to persist data and share the data among multiple pods. This topic describes how to use a NAS file system to persist and share data.

Prerequisites

Background information

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

Scenarios:
  • Your application requires high disk I/O.
  • You need a storage service that offers higher read and write throughput than Object Storage Service (OSS).
  • You want to share files across hosts. For example, you want to use a NAS file system as a file server.
Procedure
  1. Create a NAS file system and a mount target.
  2. Create a persistent volume (PV) and a persistent volume claim (PVC).

The following section describes how to create a PV and a PVC by using the FlexVolume plug-in provided by Alibaba Cloud and then mount a NAS file system.

Create a PV

  1. Create a file named pv-nas.yaml.
    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 the value with the mount target. 
          path: "/k8s1"
          vers: "4.0"
    ParameterDescription
    alicloud-pvnameThe name of the PV.
    serverThe mount target of the NAS file system. To obtain the mount target, log on to the NAS File System console. In the left-side navigation pane, click File System List, find the created file system, and then click Manage in the Actions column. On the page that appears, click Mount Targets. In the Mount Target section, copy the mount address in the IP Address of Mount Target column.
    pathThe mounted directory of the NAS file system. You can specify a subdirectory of a NAS file system. If no subdirectories exist, the system automatically creates a subdirectory.
    versThe version number of the Network File System (NFS) protocol. This parameter is optional. Valid values: 3 and 4.0. Default value: 3.
    modeThe permissions to access the mounted directory. This parameter is optional. By default, this parameter is left empty.
    Note
    • You are not allowed to grant permissions to access the root directory of a NAS file system.
    • 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 may be time-consuming or even fail. We recommend that you leave this parameter empty.
  2. Run the following command to create the PV:
    kubectl create -f pv-nas.yaml

Expected result

  1. Log on to the ACK console.

  2. In the left-side navigation pane of the ACK console, click Clusters.
  3. On the Clusters page, find the cluster that you want to manage and click its name or click Details in the Actions column.
  4. In the left-side navigation pane of the details page, choose Volumes > Persistent Volumes. Verify that the newly created PV is displayed.

Create a PVC

When you create a PVC of the NAS type, set the selector parameter to configure how to select a PV and bind it to the PVC.

  1. Create a file named pvc-nas.yaml.
    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 the PVC:
    kubectl create -f pvc-nas.yaml

Expected result

  1. Log on to the ACK console.

  2. In the left-side navigation pane of the ACK console, click Clusters.
  3. On the Clusters page, find the cluster that you want to manage and click its name or click Details in the Actions column.
  4. In the left-side navigation pane of the details page, choose Volumes > Persistent Volume Claims. Verify that the newly created PVC is displayed.

Create an application

  1. Create a file named 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 deploy an application:
    kubectl create -f nas.yaml

Expected result

  1. Log on to the ACK console.

  2. In the left-side navigation pane of the ACK console, click Clusters.
  3. On the Clusters page, find the cluster that you want to manage, and click the name of the cluster or click Applications in the Actions column.
  4. In the left-side navigation pane of the cluster details page, choose Workloads > Deployments. Verify that the newly created application is displayed.

Verify data sharing

  1. Run the following command to query the application pods.
    kubectl get pod

    Expected output:

    NAME                             READY   STATUS    RESTARTS   AGE
    nas-static-f96b6b5d7-r****       1/1     Running   0          9m
    nas-static-f96b6b5d7-w****       1/1     Running   0          9m
  2. Run the following command to query files in the /data path:
    kubectl exec nas-static-f96b6b5d7-r**** ls /data                    

    Expected output:

    kubectl exec nas-static-f96b6b5d7-w**** ls /data                   
    Note The output indicates that no file exists in the /data path.
  3. Run the following command to create a file named nas in the /data path of a pod:
    kubectl exec nas-static-f96b6b5d7-r**** touch /data/nas
  4. Query files in the pods.

    Run the following command to query files in the /data path of one pod:

    kubectl exec nas-static-f96b6b5d7-r**** ls /data

    Expected output:

    nas

    Run the following command to query files in the /data path of the other pod:

    kubectl exec nas-static-f96b6b5d7-w**** ls /data

    Expected output:

    nas
    Note When you create a file in the /data path of one pod, you can also find the file in the /data path of the other pod. This indicates that data in the NAS file system is shared by the two pods.

Verify data persistence

  1. Run the following command to delete all application pods:
    kubectl delete pod nas-static-f96b6b5d7-r**** nas-static-f96b6b5d7-wthmb

    Expected output:

    pod "nas-static-f96b6b5d7-r****" deleted
    pod "nas-static-f96b6b5d7-w****" deleted
  2. Run the following command to view how the pods are deleted and recreated:
    kubectl get pod -w -l app=nginx

    Expected output:

    NAME                             READY   STATUS              RESTARTS       AGE
    nas-static-f96b6b5d7-r****       1/1     Running             0              27m
    nas-static-f96b6b5d7-w****       1/1     Running             0              27m
    nas-static-f96b6b5d7-r****       1/1     Terminating         0              28m
    nas-static-f96b6b5d7-w****       0/1     Pending             0              0s
    nas-static-f96b6b5d7-w****       0/1     Pending             0              0s
    nas-static-f96b6b5d7-w****       0/1     ContainerCreating   0              0s
    nas-static-f96b6b5d7-w****       1/1     Terminating         0              28m
    nas-static-f96b6b5d7-n****       0/1     Pending             0              0s
    nas-static-f96b6b5d7-n****       0/1     Pending             0              0s
    nas-static-f96b6b5d7-n****       0/1     ContainerCreating   0              0s
    nas-static-f96b6b5d7-r****       0/1     Terminating         0              28m
    nas-static-f96b6b5d7-w****       0/1     Terminating         0              28m
    nas-static-f96b6b5d7-r****       0/1     Terminating         0              28m
    nas-static-f96b6b5d7-r****       0/1     Terminating         0              28m
    nas-static-f96b6b5d7-w****       1/1     Running             0              10s
    nas-static-f96b6b5d7-w****       0/1     Terminating         0              28m
    nas-static-f96b6b5d7-w****       0/1     Terminating         0              28m
    nas-static-f96b6b5d7-n****       1/1     Running             0              17s
  3. Run the following command to query the name of the recreated pod:
    kubectl get pod

    Expected output:

    NAME                             READY   STATUS    RESTARTS   AGE
    nas-static-f96b6b5d7-n****       1/1     Running   0          21s
    nas-static-f96b6b5d7-w****       1/1     Running   0          21s
  4. Query files in the pods.

    Run the following command to query files in the /data path of one pod:

    kubectl exec nas-static-f96b6b5d7-n**** ls /data

    Expected output:

    nas

    Run the following command to query files in the /data path of the other pod:

    kubectl exec nas-static-f96b6b5d7-w**** ls /data

    Expected output:

    nas
    Note The nas file still exists. This indicates that data is persisted to the NAS volume.