Mount an existing NFS share into K8s Pods

Mount an existing NFS share into K8s Pods

I hadn’t realised Kubernetes could mount an existing NFS share into a pod with just a little text inside a manifest. Of course, the share has to exist first, and Kubernetes can’t create the NFS storage without a persistent volume provisioner like nfs-subdir, but it is very useful.

It isn’t necessary to add anything to Kubernetes itself; it is built in. You just specify the following to mount a volume and associate it with a share like this: –

volumes:
  - name: nfs-vol
    nfs:
      server: 192.168.1.95
      path: /mnt/HDDPool2/Video

Then you mount the volume like this: –

volumeMounts:
  - name: nfs-vol
    mountPath: /opt/folderincontainer

So, a full manifest would look something like this: –

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: fedoratest
  name: fedoratest
  namespace: fedoratest
spec:
  template:
    spec:
      containers:
        - image: registry.fedoraproject.org/fedora:35
          name: fedora
          command: ["/bin/bash", "-c", "--"]
          args: ["while true; do sleep 30; done;"]
          volumeMounts:
            - name: nfs-vol
              mountPath: /opt/nfs
      volumes:
        - name: nfs-vol
          nfs:
            server: 192.168.1.95
            path: /mnt/HDDPool2/Video

An important thing to note is that the share must be set up with the correct permissions before it can be accessed and written to from the Pod.

One great feature of using this method is the Pod does not need to have nfs-common installed within it. The container doesn’t know that the folder is an NFS share. To the container, it is a local folder.

This is a compelling and easy-to-use feature.

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.