How to setup NFS Server on Ubuntu 18.04 for Dynamic NFS Provisioning

This post will help you setting up NFS Server to be used later for Dynamic Provisioning for Kubernetes.

Lets begin. I am setting up NFS Server on Ubuntu 18.04 running on LXC VMs (because it is faster to provision and work on them). But this tutorial will work on Ubuntu 18.04 wherever it is running.

Setup the Server Side

  1. Setting up the NFS Server:
$ sudo apt-get update -y
$ sudo apt install nfs-kernel-server -y

2) Create a directory to be shared by multiples servers:

$ sudo mkdir -p /srv/nfs/mydata

As we want all clients to access the directory, we would like to remove the restrictive permissions of the export folder through the following commands:

$ sudo chown nobody:nogroup /srv/nfs/mydata
$ sudo chmod 777 /srv/nfs/mydata

3) Assign server access to clients using NFS Exports:

$ sudo vim /etc/exports

Put the entry below (we are giving access to all clients, we can also restrict them while specifying the IP)

/srv/nfs/mydata  *(rw,sync,no_subtree_check,no_root_squash,insecure)

We can restrict to 1 Client using the below command:

/srv/nfs/mydata  clientIP(rw,sync,no_subtree_check,no_root_squash,insecure)

We can create multiple entries like below:

/srv/nfs/mydata  clientIP1(rw,sync,no_subtree_check,no_root_squash,insecure)
/srv/nfs/mydata  clientIP2(rw,sync,no_subtree_check,no_root_squash,insecure)

We can also specify the Subnet Range:

/srv/nfs/mydata  x.x.x.x/24(rw,sync,no_subtree_check,no_root_squash,insecure)

4) Finally, export the shared directory:

$ sudo exportfs -a

5) Restart the services to make all configurations active:

$ sudo systemctl restart nfs-kernel-server

Check whether NFS Server is running fine:

$ sudo systemctl status nfs-kernel-server
   nfs-server.service - NFS server and services
   Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
   Active: active (exited) since Mon 2020-06-01 06:59:28 UTC; 10h ago
  Process: 992 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 979 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 992 (code=exited, status=0/SUCCESS)

Jun 01 06:59:21 nfs-server systemd[1]: Starting NFS server and services...
Jun 01 06:59:28 nfs-server systemd[1]: Started NFS server and services.

6) Check whether firewall is enabled or not:

sudo ufw status

If we want to keep it inactive then go to next step if not we can enable it and add rules accordingly to make it work for our NFS Server:

$ sudo ufw allow from [clientIP or clientSubnetIP] to any port nfs

Finally the command will look like below:

$ sudo ufw allow from x.x.x.x/24 to any port nfs
$ sudo ufw status

Configure all client machines:

1) Installing NFS Common after updating the OS

$ sudo apt-get update -y
$ sudo apt-get install nfs-common -y

2) Testing while mounting a NFS Server Mount to an existing directory or create a new directory

$ sudo mkdir -p /nfs_client
$ sudo mount server_IP:/srv/nfs/mydata /nfs_client

Now we can test it while creating a text file in either /nfs_client directory or /srv/nfs/mydata and find it from either side.

Hope it helps..


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: