How to use NFS as a volume in Kubernetes?

Lyheng Tep
2 min readMar 1, 2023

--

Sharing data between multiple pods can be effortful, and complicated. Therefore in this post, I will be telling you how to centralize the data in one place using Network File System (NFS) and let pods mount the data from it.

So what is NFS? NFS or Network File System is a distributed file system that allows users to access files over a computer network just like local storage is accessed.

I assume that you have an NFS server available in your own infrastructure and you have a basic understanding of Persistent Volume (PV) as well as Persistent Volume Claim (PVC), but if you don't know what they are you can check out my previous post here. https://medium.com/@teplyheng/what-are-persistent-volume-and-persistent-volume-claim-in-kubernetes-a0d977b890bf

First thing first, I will create a yml file called nfs-pv.yml.

apiVersion: v1
kind: PersistentVolume
metadata:
name: hello-nfs-pv
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: "/hello-app/"
server: "10.80.80.147"
readOnly: false

In the nfs section, I have included a path to mount data to our NFS server and also the IP of NFS.

Note: please make sure that the mounted path exists before you mount it to pods, otherwise you will counter an error that path does not exist on the NFS server.

Now, I copy this file to a master node and apply it

kubectl apply -f nfs-pv.yml

Next, I will claim this newly created resource by creating a Persistent Volume Claim called nfs-pvc.yml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: hello-nfs-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Gi

I will copy nfs-pvc.yml over to a master node and apply it.

kubectl apply -f nfs-pvc.yml

Eventually, you can let your pod access a new volume.

apiVersion: v1
kind: Service
metadata:
name: helloworld-app-svc
labels:
app: helloworld
spec:
type: NodePort
ports:
- protocol: TCP
port: 3000
targetPort: 3000
nodePort: 32222
selector:
app: helloworld
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: helloworld
labels:
app: helloworld
spec:
replicas: 2
selector:
matchLabels:
app: helloworld
template:
metadata:
labels:
app: helloworld
spec:
containers:
- name: helloworld
image: hello-world:0.0.1
ports:
- containerPort: 3000
volumeMounts:
- name: logs
mountPath: "/usr/logs"
readinessProbe:
tcpSocket:
port: 3000
initialDelaySeconds: 5
periodSeconds: 10
failureThreshold: 3
volumes:
- name: logs
persistentVolumeClaim:
claimName: hello-nfs-pvc

By using NFS, you can collectively store data such as log files, application assets, and so on in a server. Thanks for reading.

--

--

Lyheng Tep
Lyheng Tep

Written by Lyheng Tep

Hi I am Lyheng. I am a software engineer from Cambodia. Coding is my passion. Find me at: https://lyhengtep.com

No responses yet