You can use a 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
Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster.
A NAS file system is created in the NAS File System console. For more information, see Mount an NFS file system on a Linux ECS instance. The NAS file system and the cluster are deployed in the same zone.
A mount target is added to the NAS file system. For more information, see Manage mount targets. The NAS file system and the cluster are deployed in the same virtual private cloud (VPC).
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
Create a NAS file system and a mount target.
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
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"Parameter
Description
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.
NoteYou are not allowed to grant permissions to access the root directory of a NAS file system.
If you set the
modeparameter 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.
Run the following command to create the PV:
kubectl create -f pv-nas.yaml
Expected result
Log on to the ACK console. In the left-side navigation pane, click Clusters.
On the Clusters page, find the cluster that you want to manage and click its name or click Details in the Actions column.
In the left-side navigation pane of the details page, choose . 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.
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-nasRun the following command to create the PVC:
kubectl create -f pvc-nas.yaml
Expected result
On the Clusters page, find the cluster that you want to manage and click its name or click Details in the Actions column.
In the left-side navigation pane of the details page, choose . Verify that the newly created PVC is displayed.
Log on to the ACK console. In the left-side navigation pane, click Clusters.
Create an application
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-nasRun the following command to deploy an application:
kubectl create -f nas.yaml
Expected result
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.
In the left-side navigation pane of the cluster details page, choose . Verify that the newly created application is displayed.
Log on to the ACK console. In the left-side navigation pane, click Clusters.
Verify data sharing
Run the following command to query the application pods where the Deployment is running:
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE nas-static-f96b6b5d7-r**** 1/1 Running 0 9m nas-static-f96b6b5d7-w**** 1/1 Running 0 9mRun the following command to query files in the /data path:
kubectl exec nas-static-f96b6b5d7-r**** -- ls /dataExpected output:
kubectl exec nas-static-f96b6b5d7-w**** -- ls /dataNoteThe output indicates that no file exists in the /data path.
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/nasQuery 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 /dataExpected output:
nasRun the following command to query files in the /data path of the other pod:
kubectl exec nas-static-f96b6b5d7-w**** -- ls /dataExpected output:
nasNoteWhen 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
Run the following command to delete all application pods:
kubectl delete pod nas-static-f96b6b5d7-r**** nas-static-f96b6b5d7-wthmbExpected output:
pod "nas-static-f96b6b5d7-r****" deleted pod "nas-static-f96b6b5d7-w****" deletedRun the following command to view how the pods are deleted and recreated:
kubectl get pod -w -l app=nginxExpected 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 17sRun the following command to query the name of the recreated pod:
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE nas-static-f96b6b5d7-n**** 1/1 Running 0 21s nas-static-f96b6b5d7-w**** 1/1 Running 0 21sQuery 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 /dataExpected output:
nasRun the following command to query files in the /data path of the other pod:
kubectl exec nas-static-f96b6b5d7-w**** -- ls /dataExpected output:
nasNoteThe nas file still exists. This indicates that data is persisted to the NAS volume.