k8s

How to Set Up NFS Server for Kubernetes Persistent Volumes

🛠️ How to Set Up NFS Server for Kubernetes Persistent Volumes

This guide walks you through installing and configuring an NFS server for use with Kubernetes PersistentVolumes (PV) and PersistentVolumeClaims (PVC).


🧩 Step 1: Install NFS Server

sudo apt update
sudo apt install nfs-kernel-server

📁 Step 2: Create Shared Directories

Create directories for Kubernetes apps:

sudo mkdir -p /nfs/mysql
sudo mkdir -p /nfs/mariadb
sudo mkdir -p /nfs/wordpress
sudo mkdir -p /nfs/moodle
sudo mkdir -p /nfs/moodledata
sudo mkdir -p /nfs/grafana
sudo mkdir -p /nfs/prometheus

sudo chown -R nobody:nogroup /nfs
sudo chmod -R 777 /nfs

✅ These directories match the path: fields used in your Kubernetes PersistentVolumes.


📝 Step 3: Configure NFS Exports

Edit the NFS export configuration:

sudo nano /etc/exports

Add the following lines (replace with your actual subnet):

/nfs/mysql     192.168.88.0/24(rw,sync,no_subtree_check,no_root_squash)
/nfs/mariadb   192.168.88.0/24(rw,sync,no_subtree_check,no_root_squash)
/nfs/wordpress 192.168.88.0/24(rw,sync,no_subtree_check,no_root_squash)
/nfs/moodle    192.168.88.0/24(rw,sync,no_subtree_check,no_root_squash)
/nfs/moodledata        192.168.88.0/24(rw,sync,no_subtree_check,no_root_squash)
/nfs/grafana   192.168.88.0/24(rw,sync,no_subtree_check,no_root_squash)
/nfs/prometheus 192.168.88.0/24(rw,sync,no_subtree_check,no_root_squash)

Explanation:

  • rw: read/write access
  • sync: write operations are flushed immediately
  • no_subtree_check: improves reliability
  • no_root_squash: allows root access from clients (needed for containers)

🔄 Step 4: Export Shared Directories

Apply the configuration:

sudo exportfs -a

🚀 Step 5: Start and Enable NFS Server

sudo systemctl restart nfs-kernel-server
sudo systemctl enable nfs-kernel-server

🧪 Step 6: Test from a Kubernetes Node or Client

Install NFS client (on client or node):

sudo apt install nfs-common

Create a mount point and test:

sudo mkdir -p /mnt/nfs-test
sudo mount 192.168.88.61:/nfs/mysql /mnt/nfs-test

Check permissions:

touch /mnt/nfs-test/testfile.txt

📌 Optional: Make NFS Mount Persistent (Not Required for K8s)

If testing outside of Kubernetes, add to /etc/fstab:

192.168.88.61:/nfs/mysql /mnt/nfs-test nfs defaults 0 0

✅ Ready for Kubernetes

Now you can create PersistentVolumes using:

nfs:
  server: 192.168.88.61
  path: /nfs/mysql   # or /nfs/mariadb, /nfs/wordpress, /nfs/prometheus, etc.

Make sure these paths are exported and accessible from all Kubernetes nodes.

0
Backlog
0
To Do
0
In Progress
0
Done