Container Network File System (CNFS) lets you mount the same directory of a NAS file system to multiple pods simultaneously. This eliminates data-sharing issues across pods without requiring application-level coordination.
This topic walks you through mounting a NAS file system as a shared volume using a StatefulSet, then verifying that data written by one pod is immediately visible to others.
Before you begin
Review the following constraints before proceeding:
The minimum required component versions are: csi-plugin and csi-provisioner at v1.24.11-5221f79-aliyun or later, and storage-operator at v1.24.105-825188d-aliyun or later. If your versions are older, update the components before proceeding.
CNFS does not guarantee data consistency when multiple pods write to the same file concurrently.
Prerequisites
Before you begin, ensure that you have:
NAS activated. If this is your first time, visit the NAS product page and follow the on-screen instructions
An ACK cluster running Kubernetes 1.20 or later, with the Container Storage Interface (CSI) plug-in as the volume plug-in. For setup instructions, see Create an ACK managed cluster
csi-plugin and csi-provisioner at version v1.24.11-5221f79-aliyun or later. To update, see Install and update the CSI components
storage-operator at version v1.24.105-825188d-aliyun or later. To update, see Manage components
kubectl connected to your cluster. For setup instructions, see Get a cluster kubeconfig and connect to the cluster using kubectl
For background on CNFS and how to manage NAS file systems with it, see CNFS overview and Use CNFS to manage NAS file systems (recommended).
Step 1: Create the workload
Create three resources that wire together your NAS file system and a two-replica StatefulSet:
A PersistentVolume (PV) named
cnfs-nas-static-pvthat references a CNFS object namedcnfs-nas-filesystem, which represents your existing NAS file system.A PersistentVolumeClaim (PVC) named
cnfs-nas-static-pvcbound to the PV.A StatefulSet named
cnfs-nas-static-stswith two replicas, each mounting the PVC at/data.
Run the following command to create all three resources at once:
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolume
metadata:
name: cnfs-nas-static-pv
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 50Gi
csi:
driver: nasplugin.csi.alibabacloud.com
fsType: nfs
volumeAttributes:
containerNetworkFileSystem: cnfs-nas-filesystem # Reference the CNFS object that represents your NAS file system.
mountProtocol: nfs
path: /
volumeAs: subpath
volumeCapacity: "true"
volumeHandle: cnfs-nas-static-pv
mountOptions:
- nolock,tcp,noresvport # nolock: disables NFS file locking; noresvport: improves connection resilience on failover
- vers=3 # NFS protocol version; vers=3 is recommended for general workloads
persistentVolumeReclaimPolicy: Retain
storageClassName: cnfs-nas-sc
volumeMode: Filesystem
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: cnfs-nas-static-pvc
namespace: default
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Gi
storageClassName: cnfs-nas-sc
volumeMode: Filesystem
volumeName: cnfs-nas-static-pv # Bind to the PV created above.
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 50Gi
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: cnfs-nas-static-sts
labels:
app: busybox
spec:
serviceName: "busybox"
replicas: 2
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh"]
args: ["-c", "sleep 3600;"]
volumeMounts:
- mountPath: "/data"
name: cnfs-nas-static-pvc
volumes:
- name: cnfs-nas-static-pvc
persistentVolumeClaim:
claimName: cnfs-nas-static-pvc
EOFStep 2: Verify the mount
Run the following command to check the NFS mount on the first pod:
kubectl exec cnfs-nas-static-sts-0 -- mount | grep nfsExpected output:
971134b0e8-****.cn-zhangjiakou.nas.aliyuncs.com:/ on /data type nfs (rw,nosuid,nodev,relatime,user_id=0,group_id=0,default_permissions,allow_other)The output shows the NAS mount target at /data. Do not proceed to the next step until you see a mount target in the output.
Step 3: Write data from one pod
Run the following command to write a 1 GiB temporary file from pod cnfs-nas-static-sts-0:
kubectl exec cnfs-nas-static-sts-0 -ti -- sh -c 'dd if=/dev/zero of=/data/1G.tmpfile bs=1G count=1;'Verify the file was created:
kubectl exec cnfs-nas-static-sts-0 -- ls -arlth /dataExpected output:
total 1G
-rw-r--r-- 1 root root 1.0G Dec 15 12:11 1G.tmpfileStep 4: Verify data sharing across pods
Run the following command to confirm that pod cnfs-nas-static-sts-1 can see the same file:
kubectl exec cnfs-nas-static-sts-1 -- ls -arlth /dataExpected output:
total 1G
-rw-r--r-- 1 root root 1.0G Dec 15 12:11 1G.tmpfileBoth pods see 1G.tmpfile, confirming that the shared NAS volume is working correctly.
CNFS does not guarantee data consistency when multiple pods write to the same file concurrently. To prevent file corruption, avoid writing to the same file from multiple pods at the same time.
What's next
To mount different directories in a NAS file system to different pods for storage isolation, see Use CNFS to manage isolated NAS volumes (recommended).
To automatically expand a NAS volume when usage exceeds a threshold, see Use CNFS to automatically scale out NAS volumes.