Installation and Usage of CSI NFS/SMB
- Storage in containers: inherently ephemeral, but persistent by necessity.
- CSI NFS driver: installation and usage.
- CSI SMB driver: installation and usage.
- Key principles; the same logic applies to object storage.
Introduction
Managing storage in containerized environments has always been a challenge, although it has diminished over time.
A container is designed to be ephemeral. It should start quickly and be optimized to minimize external dependencies.
By definition, a container is portable and must be able to change execution contexts without affecting its operation.
By default, any data generated within the container is lost when the container stops.
However, not all applications can do without persistent data. Some must be able to permanently store the data they manipulate. The associated containers have needed to recover their information on each startup.
That's why we use the concept of volumes. These are storage spaces mounted within containers, designed to persist data using various protocols and access methods.
Kubernetes is no exception to this rule and has its own objects that allow it to provide data persistence solutions.
The K8S approach, as always, aims to be modular and has evolved over time.
To begin, it's important to distinguish three possible storage modes:
- Block mode: geared toward high performance.
Often used in an exclusive access and write mode, allowing only one container at a time to access the data.
This is equivalent to mapping a disk with a filesystem directly within the container.
- Mode object: designed for multi-access and high redundancy.
Widely used in public cloud environments, especially with services like S3 that are offered by all major providers.
Data is manipulated through HTTP methods.
- File sharing mode: also geared toward multiple access.
A more traditional method based on protocols such as SMB (Server Message Block) or NFS (Network File System) to share information across an enterprise network.

Click the image to enlarge.
In all cases, Kubernetes utilizes specific objects.
We will start with the lowest-level object, which is the PersistentVolume (pv). It can be provisioned statically or dynamically, but it always defines the storage mode. It is responsible for persisting the data.
Next, we have the PersistentVolumeClaim (pvc). This corresponds to a request for a pv. We specify the expected characteristics (access mode, size, etc.) and Kubernetes attempts to bind it to a corresponding pv if possible:
- Either by offering an existing volume that is available and matches the specified characteristics (size and access method).
- Or by automatically provisioning a compatible volume within a StorageClass (sc)
The StorageClass (sc) is the last object associated with storage. It allows you to define a pool of provisioned resources that meet certain criteria, from which Kubernetes can draw to create pv objects on demand, as requested by pvc objects.
This avoids having to declare pvs in advance of pvcs.
Generally, an sc is associated with a storage technology, and more specifically, it relies on a CSI (Container Storage Interface) driver, which the pv will inherit.
Here's another concept to understand, but it's very important. As of the date this article was written, Kubernetes includes several drivers that allow it to natively utilize storage protocols, such as NFS, to manage pvs. These are called In-Tree drivers.
However, this usage is now deprecated and being replaced by drivers based on the CSI standard. For reasons of maintainability and efficiency, there are no longer plans to natively integrate storage drivers into Kubernetes. CSI allows anyone who wants to publish their own drivers, often for their own storage solution, so that they can then be integrated into a K8S cluster and allow the cluster to rely on that solution.
As long as the CSI standard is respected, you can, for example, offer a StorageClass (or a pv directly) to provide pods with data persistence capabilities specific to the characteristics associated with the CSI driver.
The whole thing may seem complicated, but in reality, it helps to clearly differentiate between the provisioning of PersistentVolumes and their consumption.

Click the image to enlarge.
The entire CSI (Container Storage Interface), StorageClass, and PersistentVolume aspects can be handled by dedicated teams (or not) without a direct connection to those who express storage needs through persistent volume claims (PVCs).
For example, the developer doesn't need to know the details of how a volume is provisioned. What they want is to have x GB of space available in their application, accessible by one or more pods simultaneously. It doesn't matter whether the data ultimately ends up on a storage array, a file share, or something else.
Similarly, the storage administrator can evolve their equipment and offer StorageClasses that meet different performance or availability requirements independently of the rest of the system.
For hardware vendors or cloud providers, they have the freedom to develop a CSI driver for their solution to make it compatible with Kubernetes.
Finally, for K8S, the project developers can focus on the core of the solution, container orchestration, without having to maintain storage drivers internally. This work is best left to those who are in the best position to do it: storage solution providers.
In this article, we will focus on the file sharing mode. For the block mode, feel free to take a look at this section of my Kubernetes Cookbook, which describes how to use this mode in a VMware context (but the logic is applicable to other ecosystems). You will also find additional explanations about data persistence within Kubernetes.
We will therefore cover two cases: NFS and SMB.

Click the image to enlarge.
I will only briefly mention StorageClasses for these two protocols. The CSI driver associated with each of them easily allows the use of StorageClasses, but I will focus here on a PVC/PV usage, with manual PV provisioning.
CSI NFS
NFS is the historical file sharing protocol in Linux environments. Widely used in businesses, it has had several versions (version 4.2 at the time of this article) and remains very widely used in companies.
It is performant and easily allows you to create shares that are accessible for both reading and writing by multiple clients. I will not go into more detail about how NFS works here; that is not the purpose of this article.
Prerequisites
To begin, it is necessary for the K8S worker nodes to meet certain conditions. They must have the NFS tools installed and the rpcbind service running. The commands you need to enter may vary depending on the distribution. In my case, using VMs under PhotonOS, the instructions are as follows:
tdnf install nfs-utils
tdnf install rpcbind
systemctl enable rpcbind
systemctl start rpcbind

Click the image to enlarge.
Each worker node (and even though it is not necessarily required, the control plane as well) must have these packages deployed and the rpcbind service started.
Feel free to use a tool like Ansible to ensure this.
Installation
Once the prerequisites are in place, you need to install the CSI driver corresponding to the use of NFS.
Even though it is still possible to do without it at the time of writing this article by relying on the In-Tree drivers (as I have done here), it is advisable to get into the habit of using the CSI driver from now on. The In-Tree drivers will eventually be completely removed from K8S.
The NFS CSI driver is installed via helm. If you have not yet installed helm on your cluster, now is the time to do so, as helm is a widely used and very easy-to-use package manager for Kubernetes: simply download the binary.
Here are the commands to enter.
helm repo add csi-driver-nfs https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/charts
helm install csi-driver-nfs csi-driver-nfs/csi-driver-nfs --namespace kube-system --version v4.9.0

Click the image to enlarge.
(The version may evolve over time). Check the GitHub repository associated with the driver.
We use no customization and deploy the NFS CSI driver with its default configuration in the kube-system namespace. Some people might prefer to dedicate a specific namespace for it. It's up to you.
Usage
Once deployed, you can directly start creating a volume using this CSI driver.
For the purpose of this article, I will use a dedicated namespace called dev-democsishare-lan.
This is the first thing I will generate with the command kubectl.exe create ns dev-democsishare-lan.
Next, you can define your persistent volume (PV) using the file 01-pv-democsishare-nfs.yml:
---
apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/provisioned-by: nfs.csi.k8s.io
name: pv-democsishare-nfs
namespace: dev-democsishare-lan
labels:
environment: dev
network: lan
application: nfs
tier: default
type: nfs
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
csi:
driver: nfs.csi.k8s.io
volumeHandle: /Volume1/nfsshare/kub.coolcorp.priv/namespaces/dev-democsishare-lan/nfs/
volumeAttributes:
server: 192.168.10.152
share: /Volume1/nfsshare/kub.coolcorp.priv/namespaces/dev-democsishare-lan/nfs/
(The location of the share is given here as an example and must be adapted to your use).
This uses best practices that I usually follow, starting with labels.
The most important elements are in the spec: section.
This is where you declare the desired storage space. In my example, 1Gi.
A small but important point about this concept. In the case of NFS (and SMB), where a share and not a disk is exposed, the volume size is given here only as an indication.
A share can be based on an underlying disk volume that is much larger than the size requested in the Persistent Volume (PV). Nevertheless, this allows you to classify the PV and provides an indication of the required storage capacity.
We will also define the access method:
- ReadWriteMany (RWX): Allows multiple nodes to access the PV for both reading and writing.
- ReadWriteOnce (RWO): Allows only one node at a time to mount the PV for reading and writing.
- ReadOnlyMany (ROX): Allows multiple nodes to mount the PV, but only for reading.
In my case, I am using ReadWriteMany (RWX).
We also find the volume deletion policy: what should happen if the volume is no longer requested by a Persistent Volume Claim (PVC)? Several modes are possible:
- Retain: If the application is deleted and the PV is no longer used, it remains within the cluster, and the data is preserved. It is then possible to re-map this volume and recover the data if necessary.
- Recycle: If the application is deleted, the PV is purged of its data but remains available for new use.
- Delete: If the application is deleted, the PV is deleted (along with the data).
I have chosen Retain here; it is a conservative policy, but be careful, you can quickly end up with unmounted PVs scattered around, and you need to clean them up from time to time.
We continue with the csi subsection. This is where we specify which driver to use; in this case, nfs.
The remaining parameters depend on the driver used, but the volumeHandle option is common to all CSI drivers. It is the unique identifier of the volume.
This is extremely important because operations such as attaching, detaching, and mounting the volume depend on this identifier. In the case of dynamic provisioning, the value of the volumeHandle is generated automatically, but in a case where you create a PV manually, you must ensure that you choose a unique ID.
Apart from the prohibition of certain characters, there are no restrictions on how to construct this identifier.
In my case, I reuse the network target location. However, this may not be sufficient (you might have multiple persistent volumes pointing to the same NFS export). Please ensure that this value remains unique within the cluster. Yes, a persistent volume is an object that does not depend on a namespace and remains common to the entire cluster.
The following options are commonly found for an NFS mount. The server name, the export name... You might also find advanced options, such as the version of NFS to use (by default, version 3). All of this is documented in the CSI NFS driver documentation.
A small aside: access mode options, deletion policy, and the CSI driver should also be used in the case of a StorageClass.
All volumes dynamically provisioned within this StorageClass would inherit these characteristics.
You just need to start creating the persistent volume using the command kubectl apply -f 01-pv-democsishare-nfs.yml.
At this stage, you can already check that the persistent volume is available with the command
kubectl get pv nom_du_pv

Click the image to enlarge.
It is correctly configured, but not mounted.
This is normal because we haven't written the persistent volume claim (PVC) yet. Here it is, taken from the file 02-pvc-democsishare-nfs.yml:
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-democsishare-nfs
namespace: dev-democsishare-lan
labels:
environment: dev
network: lan
application: nfs
tier: default
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
volumeName: pv-democsishare-nfs
storageClassName: ""
It inherits the same characteristics as the persistent volume, except for the CSI driver. In fact, in the PVC, you don't need to know the details of how the volume is managed. You simply specify the desired access mode, the required storage capacity, and, in our case, the name of the volume to use.
With a StorageClass, you wouldn't need to specify the volume name. However, here, to avoid any problems, it is better to include the StorageClass option but leave the parameter empty... because if Kubernetes can ignore the volume name, if it finds a StorageClass that meets its expectations, it will provision a new volume within that StorageClass rather than using the persistent volume you specified.
Once the PVC has been applied (kubectl apply -f 02-pvc-democsishare-nfs.yml), we can now recheck the status of the persistent volume. It is now mounted, and we see the name of the associated PVC.

Click the image to enlarge.
You can also check the status of the PVC with the kubectl command get pvc nom_pvc -n nom_namespace:

Click the image to enlarge.
This time, you need to specify the namespace, because the PVC belongs to a specific namespace. The PVC is also in a 'bound' state because a volume has been found that meets its requirements (in this case, our manually created persistent volume).
We can continue with the Deployment object. I won't describe its configuration in detail; I encourage you to read this article for more information on its usage and to review the associated definition.
Here is the content of the file 03-deploy-democsishare-nfs.yml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-democsishare-nfs
namespace: dev-democsishare-lan
labels:
environment: dev
network: lan
application: nfs
spec:
strategy:
type: Recreate
selector:
matchLabels:
environment: dev
network: lan
application: nfs
template:
metadata:
labels:
environment: dev
network: lan
application: nfs
spec:
nodeSelector:
network: lan
containers:
- name: demo-nginx-democsishare-nfs
image: nginx:1.27
resources:
requests:
memory: "8Mi"
cpu: "250m"
limits:
memory: "256Mi"
cpu: 1
livenessProbe:
initialDelaySeconds: 5
periodSeconds: 5
exec:
command:
- ls /usr/share/nginx/html
ports:
- containerPort: 80
volumeMounts:
- name: pv-democsishare-nfs
mountPath: "/usr/share/nginx/html"
volumes:
- name: pv-democsishare-nfs
persistentVolumeClaim:
claimName: pvc-democsishare-nfs
What interests us here is the volumes section. This is where we associate the previously created persistent volume claim (PVC) with a volume within the pod. Note that the volume is not directly the persistent volume (PV), even though I'm using the same name here for simplicity. The volume is a concept specific to the pod.
Furthermore, higher up in the definition of the container associated with the pod, we specify that this volume will be mounted at /usr/share/ngingx/html.
Therefore, our PV is linked to our PVC, and this same PVC is used within the Deployment to create a volume. This volume will then be mapped to a local path within the container of the pod.
Once the Deployment is started using the command kubectl apply -f 03-deploy-democsishare-nfs.yml, we can visualize this association
After verifying that the pod has been launched using the command kubectl get pod -n nom_namespace.
We connect interactively to it using the command kubectl exec -it nom_pod -n nom_namespace sh and use the command df -h to list the mount points.
We can see our NFS share mounted within the container at the desired path.

Click the image to enlarge.
Of course, this implies that on the storage side, you have authorized the IPs of the K8S nodes to connect to the NFS export.
Even though the share is mounted in the container, the IP seen from the storage device is the IP of the node running the container. Make sure to declare all your nodes, as the pod can be executed on any of them if the deployment rules allow it.
Furthermore, if you want to use NFS 4, you will need to configure the appropriate authentication information in the driver options at the PV level.
In my case, I simply authorized the IPs of my nodes on my Terramaster NAS for that specific export.
We continue the demonstration with the following objects. I won't go into detail here; that's not the purpose. But we will use a Service to expose the pod, and then an Ingress for access to the service from outside the cluster, using a certificate managed by Traefik.
If all of this doesn't make much sense to you, feel free to take a look here (the concept of ingress) and here (an example of deploying a complete application).
For informational purposes, here is the content of the file 04-svc-democsishare-nfs.yml, which defines the Service:
---
kind: Service
apiVersion: v1
metadata:
name: svc-democsishare-nfs
namespace: dev-democsishare-lan
labels:
environment: dev
network: lan
application: nfs
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
selector:
environment: dev
network: lan
application: nfs
Then, the content of the file 05-ing-democsishare-nfs.yml for the certificate and Ingress:
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ing-democsishare-nfs
namespace: dev-democsishare-lan
labels:
environment: dev
network: lan
application: nfs
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web,websecure
ingressClassName: traefik-lan
traefik.ingress.kubernetes.io/router.tls: "true"
spec:
ingressClassName: traefik-lan
tls:
- hosts:
- democsinfs.coolcorp.priv
secretName: sec-certificate-democsinfs
rules:
- host: democsinfs.coolcorp.priv
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: svc-democsishare-nfs
port:
number: 80
Now that everything is in place, I will place a file named index.html directly on the NFS share, via my NAS.

Click the image to enlarge.
In this way, the NGINX image running within the container in my pod will be able to deliver the content of the HTML code it contains.
Of course, I previously made sure to set up a DNS entry that points to the URL described in the Ingress so that requests can be accepted.
It is indeed the content of my index.html file that is displayed in my browser. If I were to restart my pod (and therefore my container) now, the data would remain because it is on the share.
Similarly, if I want to increase the number of pods by updating my Deployment to allow a larger replica count of my pod, each pod will have access to the share and can therefore deliver the expected output. This is possible because I am in ReadWriteMany mode. This ensures high availability for my NGINX instance.

Click the image to enlarge.
CSI SMB
SMB (Server Message Block) is the file sharing protocol used in Microsoft environments.
Note that you often hear about CIFS (Common Internet File System) as well. However, CIFS, which was a specific version of SMB developed by Microsoft, is no longer current.
Prerequisites
It is now SMB version 3 that is the standard... but people tend to mix the two and often continue to use the term CIFS to refer to SMB shares.
In fact, one of the prerequisites for deploying on K8S nodes is installing the cifs-utils package... In my case, and under PhotonOS, the command is tdnf install cifs-utils.

Click the image to enlarge.
Installation
As with NFS, the SMB CSI driver can be deployed using Helm. The commands are as follows:
helm repo add csi-driver-smb https://raw.githubusercontent.com/kubernetes-csi/csi-driver-smb/master/charts
helm install csi-driver-smb csi-driver-smb/csi-driver-smb --namespace kube-system --version v1.16.0

Click the image to enlarge.
(The version may evolve over time). Check the GitHub repository associated with the driver.
Usage
With SMB CSI, we will find the same logic as with NFS. However, you will need connection credentials to access the share.
In an enterprise environment, SMB shares are often correlated with a Active Directory (AD). Access to the share is subject to providing a domain account that has the appropriate permissions for the share.
Therefore, I will need to create a service account in my lab's AD directory.

Click the image to enlarge.
This svc_k8s_smb account from my coolcorp.priv domain will be declared as having write permissions on my share, which is still provided by my Terramaster NAS, but this time via the SMB protocol.

Click the image to enlarge.
Furthermore, by also granting rights to my own account, I can directly browse the share from my workstation.
It is necessary to store the newly created connection credentials in a Kubernetes Secret so that it can be used by the SMB CSI driver within a persistent volume (PV).
Be careful when choosing the password for your service account. Certain special characters may be misinterpreted during the creation of a secret. Be vigilant; the secret is created, but the password is incorrect.
In my case, I declare the secret in interactive mode and within a dedicated namespace called inf-lan-smb that I created previously (kubectl create ns inf-lan-smb).
Here is the command:
kubectl create secret generic nom_du_secret --from-literal username=nom_du_compte --from-literal password="password_du_compte" --from-literal domain=domaine_nom_court -n nom_namespace

Click the image to enlarge.
I chose to dedicate a namespace called inf-smb-lan to store the secret. This allows me to centralize the credentials needed to access my SMB shares. Remember, a K8S secret only obfuscates the information it contains using base64 encoding. Always be vigilant about access to the secret.
We can now focus on the file 01-pv-democsishare-smb.yml, which will contain the definition of the PV:
---
apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/provisioned-by: smb.csi.k8s.io
name: pv-democsishare-smb
namespace: dev-democsishare-lan
labels:
environment: dev
network: lan
application: smb
tier: default
type: smb
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
mountOptions:
- dir_mode=0777
- file_mode=0777
csi:
driver: smb.csi.k8s.io
volumeHandle: //storage.coolcorp.priv/smbshare/kub.coolcorp.priv/namespaces/dev-democsishare-lan/smb
volumeAttributes:
source: //storage.coolcorp.priv/smbshare/kub.coolcorp.priv/namespaces/dev-democsishare-lan/smb
nodeStageSecretRef:
name: secret-smb-default
namespace: inf-smb-lan
This is very similar to what we saw for NFS. We find the logic of accessModes and persistentVolumeReclaimPolicy, along with mountOptions.
Indeed, when the PV is presented to the pod and mounted in the associated container(s), it is necessary to specify the equivalent Linux permissions with which to mount the share. Here, we are not bothering and using the horrible 777, which allows all groups and users within the containers. However, this is not so bad, since access is actually performed by the service account.
The service account can be found at the end of the file in the nodeStageSecretRef: section, which references the secret created previously.
This subsection is within the CSI section, where we now specify the use of the smb.csi.k8s.io driver. As with NFS, we find the concept of a volumeHandle, to which particular attention must be paid to ensure its uniqueness.
Then, we reference the share.
We apply the file to create the volume using the command kubectl apply -f 01-pv-democsishare-smb.yml.
We can ensure that it is available using the command kubectl get pv nom_pv.
For now, the persistent volume (PV) is not associated with a persistent volume claim (PVC), but this is only temporary. Here is the content of the file 02-pvc-democsishare-smb.yaml, which describes the PVC:
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-democsishare-smb
namespace: dev-democsishare-lan
labels:
environment: dev
network: lan
application: smb
tier: default
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
volumeName: pv-democsishare-smb
storageClassName: ""
Nothing new under the sun; it's identical to the one used with NFS, except for the name and label. However, as with NFS, the CSI driver concept does not appear in the PVC. I am simply specifying the access type, size, and the name of the PV.
I hope you are now starting to see the benefit of this approach. In our example, we could switch from NFS to SMB and vice versa without having to modify the PVCs.
If I had chosen more generic names for my PVs, I could have easily decided to change protocols on my end by changing the CSI drivers and updating the configuration of my PVs, without requiring any modifications to the PVCs (However, be aware that this still requires redeploying the PVCs).
Once applied using the command kubectl apply -f 02-pvc-democsishare-smb.yaml, we can run the PV check command again to observe that it is now associated with the PVC.

Click the image to enlarge.
For the rest, we deploy the application via the Deployment 03-deploy-democsishare-smb.yml, and at the bottom of the file, there is a definition of the volume, which relies on the PVC to mount the PV in the context of the NGINX container in /usr/share/nginx/htlm.
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-democsishare-smb
namespace: dev-democsishare-lan
labels:
environment: dev
network: lan
application: smb
spec:
strategy:
type: Recreate
selector:
matchLabels:
environment: dev
network: lan
application: smb
template:
metadata:
labels:
environment: dev
network: lan
application: smb
spec:
nodeSelector:
network: lan
containers:
- name: demo-nginx-democsishare-smb
image: nginx:1.27
resources:
requests:
memory: "8Mi"
cpu: "250m"
limits:
memory: "256Mi"
cpu: 1
livenessProbe:
initialDelaySeconds: 5
periodSeconds: 5
exec:
command:
- ls /usr/share/nginx/html
ports:
- containerPort: 80
volumeMounts:
- name: pv-democsishare-smb
mountPath: "/usr/share/nginx/html"
volumes:
- name: pv-democsishare-smb
persistentVolumeClaim:
claimName: pvc-democsishare-smb
As with NFS, after launching the deployment using the command kubectl apply -f 03-deploy-democsishare-smb.yml.
We ensure that the pod is active and we can verify the mount within its context using the following commands: kubectl exec -it nom_pod -n nom_namespace sh
df -h

Click the image to enlarge.
I will continue by applying the remaining objects:
The service via the file 04-svc-democsishare-smb.yml:
---
kind: Service
apiVersion: v1
metadata:
name: svc-democsishare-smb
namespace: dev-democsishare-lan
labels:
environment: dev
network: lan
application: smb
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
selector:
environment: dev
network: lan
application: smb
Then the Ingress with the file 05-ing-democsishare-smb.yml:
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ing-democsishare-smb
namespace: dev-democsishare-lan
labels:
environment: dev
network: lan
application: smb
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web,websecure
ingressClassName: traefik-lan
traefik.ingress.kubernetes.io/router.tls: "true"
spec:
ingressClassName: traefik-lan
tls:
- hosts:
- democsismb.coolcorp.priv
secretName: sec-certificate-democsismb
rules:
- host: democsismb.coolcorp.priv
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: svc-democsishare-smb
port:
number: 80
I place an index.html file this time in the SMB share and attempt to access the site via the URL I have dedicated to it.

Click the image to enlarge.
This time, it is indeed the content of my index.html file located in my SMB share that is processed by NGINX.

Click the image to enlarge.
Using a Subfolder
I am also using SMB to highlight another principle that may be useful: the use of subpath.
This option works equally well with SMB and NFS.
Let's say I want to access a subdirectory of my share without having to dedicate a PV to that subdirectory.
I can simply add the subPath: option at the level of my Deployment. If I take my previous example with SMB and redeploy my application, but this time with the following options.
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-democsishare-smbsub
namespace: dev-democsishare-lan
labels:
environment: dev
network: lan
application: smb
spec:
strategy:
type: Recreate
selector:
matchLabels:
environment: dev
network: lan
application: smb
template:
metadata:
labels:
environment: dev
network: lan
application: smb
spec:
nodeSelector:
network: lan
containers:
- name: demo-nginx-democsishare-smb
image: nginx:1.27
resources:
requests:
memory: "8Mi"
cpu: "250m"
limits:
memory: "256Mi"
cpu: 1
livenessProbe:
initialDelaySeconds: 5
periodSeconds: 5
exec:
command:
- ls /usr/share/nginx/html
ports:
- containerPort: 80
volumeMounts:
- name: pv-democsishare-smb
mountPath: "/usr/share/nginx/html"
subPath: subfolder
volumes:
- name: pv-democsishare-smb
persistentVolumeClaim:
claimName: pvc-democsishare-smb
I access the index.html file located in the subfolder subdirectory of my share; it is now to this index htlm that my NGINX application accesses. I did not have to modify the pv or pvc.

Click the image to enlarge.
Key Principles
Before concluding, I would like to list a few rules regarding pvs/pvcs.
- The pv/pvc relationship is unique. You cannot have multiple pvcs referencing the same pv, and vice versa. In the case of a File Sharing mode, as here, if you need to expose the same share to multiple pods from different Deployments, you will need to create as many pvs and pvcs as there are Deployments…with each pv having a unique volumeHandle, but with identical configuration at the level of the share being sought.
- For the CSI NFS and SMB presented in this article, the use of StorageClass is possible, which eliminates the need to define the pv, as it will be automatically provisioned within the StorageClass. However, in this case, a specific subdirectory will be created automatically for each volume. The name of this subdirectory will be generated automatically, and you do not have the option to choose it.
- Once a pvc is created, it cannot be modified. You must delete the pvc (being careful about the space reclaim policy applied to the volume) in order to modify these characteristics. Some CSI drivers allow StorageClasses with features that can be enabled for volume expansion, but this is mainly for block mode usage. In this case, you can resize a pvc, which will result in resizing of the pv that was automatically provisioned.
- A pv attached to a pvc cannot be deleted; to free up a pv, you must first delete the pvc that is using it.
Once the pvc is deleted, the space reclaim policy applies.
- It is possible to attach a pv with a Retain space reclaim policy, but this requires specific operations (which I will describe in a separate article).
- Be aware of automatic failovers in case of node failure for persistent volumes using a ReadWriteOnce access mode. By definition, this access method does not allow multiple pods to access the data. If a node suddenly fails and stops responding, Kubernetes cannot determine whether the node is isolated (in which case it still has access to the data) or if it is truly down (and no longer locks the data). The pods will not necessarily fail over automatically to the surviving nodes simply because Kubernetes no longer has visibility into the data access. It does not want to risk re-mounting a persistent volume intended for exclusive access on other nodes. You will likely need to intervene manually to release the persistent volumes and force them to be mounted elsewhere.
Conclusion
Using persistent storage is not straightforward under Kubernetes. However, once you understand the logic, you realize that it offers great modularity and fits perfectly into a cloud-based approach where compute and storage are separated. Don't hesitate to experiment with persistent volumes, persistent volume claims, and StorageClasses to familiarize yourself with each concept and avoid simply using the available drivers.
I didn't specify this, but you could use persistent volumes directly without worrying about persistent volume claims, but this is a bad practice that can lead to management problems in the long run. Therefore, always prefer the combination of persistent volume/persistent volume claim with or without a StorageClass and the use of the CSI driver.
As explained at the beginning of this article, I focused on SMB and NFS for file-sharing needs. However, you can find explanations about block mode and the use of StorageClasses in the section dedicated to the vSphere CSI driver in my Kubernetes CookBook.
Remember that not everyone uses their storage in the same way, and needs may vary from one company to another, or even from one application to another. I have also not illustrated cloud (object) storage, for example using a persistent volume based on an S3 bucket, but the logic remains the same as presented here.
This is the advantage of Kubernetes: its modular and object-oriented nature can be complicated to understand at first, but later you will easily be able to apply these concepts, regardless of the underlying infrastructure.
For those who wish, my example files are available on my GitHub.