NAS provides shared, persistent storage for workloads where multiple Pods need to share data. This topic describes how to implement shared and persistent storage by using NAS.
Prerequisites
-
You have an ACK managed cluster.
-
You have obtained the kubeconfig file for the cluster and used kubectl to connect to it.
-
A file system has been created in the File Storage console. For more information, see Mount an NFS file system. The file system must be in the same availability zone as your Kubernetes cluster.
-
A mount target for the Container Service for Kubernetes cluster has been added to the created file system. For more information, see Manage mount targets. The VPC network for the file system mount must be the same as the VPC network of your Kubernetes cluster.
Background information
A NAS file system can be mounted by multiple Pods concurrently. If multiple Pods modify the same data, your application must handle data synchronization.
Use cases for stateful workloads with NAS:
-
Applications that require high disk I/O.
-
Higher read/write performance than OSS.
-
Cross-host file sharing, such as using NAS as a file server.
How to use NAS for stateful workloads:
-
Manually create a file system and add a mount target.
-
Manually create a persistent volume (PV) and a persistent volume claim (PVC).
This topic describes how to use the Alibaba Cloud flexvolume plug-in to access NAS storage with a PV and PVC.
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 this value with your actual mount target. path: "/k8s1" vers: "4.0"Parameter
Description
alicloud-pvnameThe name of the PV.
serverThe mount address of the NAS file system. In the NAS console, click File System List in the left-side navigation pane. Find the target file system and click Manage in the Actions column. On the management page, click Mount Targets. In the Mount Target section, the Mount address provides the value for this parameter.
pathThe directory path within the NAS file system to mount. If the specified directory does not exist, it is automatically created.
vers(Optional) The version of the NFS mount protocol. Valid values: 3 and 4.0. Default value: 3.
mode(Optional) The permissions of the mounted directory. By default, this parameter is not configured.
Note-
You cannot configure access permissions for the root directory of a NAS file system.
-
If the NAS file system contains a large amount of data, setting
modecan significantly slow the mount process or cause it to fail. We recommend that you do not configure this parameter.
-
-
Run the following command to create the PV.
kubectl create -f pv-nas.yaml
Verification
-
Log on to the ACK console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of your cluster or click Details in the Actions column.
-
In the left-side navigation pane, choose . The list shows the PV named pv-nas with a capacity of
5Gi, an access mode ofReadWriteMany, a reclaim policy ofRetain, and a status of Available. This indicates that the PV is created and not yet bound to a persistent volume claim.
Create a PVC
Create a persistent volume claim (PVC) for the NAS file system. Use a selector to bind this PVC to the specific PV that you created.
-
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 -
Run the following command to create the PVC.
kubectl create -f pvc-nas.yaml
Verification
-
Log on to the ACK console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of your cluster or click Details in the Actions column.
-
In the left-side navigation pane, choose . The list shows the PVC named pvc-nas with a status of Bound, a total capacity of
5Gi, an access mode ofReadWriteMany, and an associated PV named pv-nas.
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-nas -
Run the following command to create the Deployment.
kubectl create -f nas.yaml
Verification
-
Log on to the ACK console. In the left navigation pane, click Clusters.
-
On the Clusters page, click Manage Applications in the Actions column for the target cluster.
-
On the details page of the Cluster, choose in the left-side navigation pane to view the Deployment that you created. In the applications list, click the application name nas-static.
Verify shared storage
-
Run the following command to view the names of the Pods in the Deployment.
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 9m -
Run the following command to list the files in the /data directory.
kubectl exec nas-static-f96b6b5d7-r**** -- ls /dataNoteThe /data directory is empty.
-
Run the following command to create a file named nas in the /data directory of one of the Pods.
kubectl exec nas-static-f96b6b5d7-r**** -- touch /data/nas -
Check the files in the Pods.
Run the following command to check the files in the /data directory of the Pod.
kubectl exec nas-static-f96b6b5d7-r**** -- ls /dataExpected output:
nasRun the following command to check the files in the /data directory of the other Pod.
kubectl exec nas-static-f96b6b5d7-w**** -- ls /dataExpected output:
nasNoteThe file created in the /data directory of one Pod also appears in the /data directory of the other Pod. This indicates that the two Pods share the same NAS file system.
Verify persistent storage
-
Run the following command to delete the application's Pods.
kubectl delete pod nas-static-f96b6b5d7-r**** nas-static-f96b6b5d7-wthmbExpected output:
pod "nas-static-f96b6b5d7-r****" deleted pod "nas-static-f96b6b5d7-w****" deleted -
Run the following command to watch as Kubernetes deletes and recreates the Pods.
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 17s -
Run the following command to get the names of the new Pods.
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 21s -
Check the files in the Pods.
Run the following command to check the files in the /data directory of a Pod.
kubectl exec nas-static-f96b6b5d7-n**** -- ls /dataExpected output:
nasRun the following command to check the files in the /data directory of the other Pod.
kubectl exec nas-static-f96b6b5d7-w**** -- ls /dataExpected output:
nasNoteThe nas file that you created is not deleted. This indicates that the data in the NAS file system persists.