Skip to content

Installing and Using NFS Share on Linux with Kubernetes

Last updated on July 30, 2024

In this blog post, we’ll walk through the steps to set up an NFS share on Debian and Fedora, and then use it in a Kubernetes deployment with GraphHopper. We’ll use specific Kubernetes manifests for setting up Persistent Volumes (PV), Persistent Volume Claims (PVC), and Storage Classes (SC). Finally, we’ll demonstrate how to use this NFS share in a Kubernetes deployment.

Step 1: Setting Up NFS Server

On Debian

  1. Install NFS server packages:
   sudo apt update
   sudo apt install nfs-kernel-server
  1. Create the NFS export directory:
   sudo mkdir -p /srv/nfs
   sudo chown nobody:nogroup /srv/nfs
   sudo chmod 777 /srv/nfs
  1. Configure NFS exports:
    Edit the /etc/exports file and add the following line:
   /srv/nfs    *(rw,sync,no_subtree_check,no_root_squash)
  1. Export the NFS shares:
   sudo exportfs -a
  1. Start and enable the NFS server:
   sudo systemctl restart nfs-kernel-server
   sudo systemctl enable nfs-kernel-server

On Fedora

  1. Install NFS server packages:
   sudo dnf install nfs-utils
  1. Create the NFS export directory:
   sudo mkdir -p /srv/nfs
   sudo chown nobody:nogroup /srv/nfs
   sudo chmod 777 /srv/nfs
  1. Configure NFS exports:
    Edit the /etc/exports file and add the following line:
   /srv/nfs    *(rw,sync,no_subtree_check,no_root_squash)
  1. Export the NFS shares:
   sudo exportfs -a
  1. Start and enable the NFS server:
   sudo systemctl restart nfs-server
   sudo systemctl enable nfs-server

Step 2: Prepare Worker Nodes

On each worker node, install the nfs-common package to ensure they can mount NFS shares:

On Debian

sudo apt update
sudo apt install nfs-common

On Fedora

sudo dnf install nfs-utils

Step 3: Configure Kubernetes to Use NFS

We’ll use the following Kubernetes manifests to set up our NFS-backed storage.

PersistentVolume (PV)

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /srv/nfs
    server: ip_address

Replace ip_address with the IP address of your NFS server.

PersistentVolumeClaim (PVC)

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

StorageClass (SC)

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: manual
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: Immediate

Step 4: Deploy GraphHopper with NFS

Here is an example snippet for your deployment.yaml to mount the NFS storage.
*here’s just a deployment part with volumes.

---
        volumeMounts:
        - name: nfs-storage
          mountPath: "/data/"
      volumes:
      - name: nfs-storage
        persistentVolumeClaim:
          claimName: nfs-pvc

Step 5: Applying the Kubernetes Manifests

Apply the manifests in the following order:

  1. PersistentVolume (PV):
   kubectl apply -f pv.yaml
  1. PersistentVolumeClaim (PVC):
   kubectl apply -f pvc.yaml
  1. StorageClass (SC):
   kubectl apply -f sc.yaml
  1. Deployment:
   kubectl apply -f deployment.yaml

Graph Data Files

Ensure that the graph data files for GraphHopper are placed in the /srv/nfs directory on your NFS server. This allows the GraphHopper container to access the data from the mounted NFS share.

Published inKubernetesLinuxOtherShell