Integrating NFS as a storage class in Kubernetes cluster

Integrating NFS as a storage class in Kubernetes cluster

Why do we need NFS as a storage class in Kubernetes?

If you are trying to set up an on-premise Kubernetes cluster then NFS is the easiest way to use as a storage provider for Kubernetes.

There are multiple other options but they are a little complicated to set up as compared to NFS.

Here are some alternatives to NFS -

  • Ceph

  • Rook

  • OpenEBS

  • Longhorn

Requirement -

  1. A server with enough storage.

  2. Running Kubernetes cluster

NFS Server Configuration -

Install NFS server packages on your server.

Ubuntu -

sudo apt install nfs-kernel-server -y

CentOS-

sudo yum install nfs-utils -y

Start and enable the service-

sudo systemctl start nfs-server
sudo systemctl enable nfs-server

Configuration -

Create you directory which you want to share through NFS-

sudo mkdir /shareddata

We want all the files accessible to all clients, we need to change ownership and permissions for that directory.

sudo chown nobody:nogroup /shareddata

Add the following lines to the /etc/exports to share this directory over the network.

sudo vim /etc/exports
# paste this line and save it.
/shareddata *(rw,sync,no_subtree_check,no_root_squash)

The no_root_squash option allows root on the client side to have root-equivalent access on the NFS server side. This can be a security concern, so use it wisely.

After saving run the following command to export the directory over the network.

sudo exportfs -rv

Now configure the firewall to allow requests to the NFS across the internet.

sudo ufw allow nfs

Now the NFS server part is done. Let's start with the Kubernetes configuration.

Kubernetes configuration -

First of all, we need to install the NFS CSI driver -

We can install it using Helm-

helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
helm install nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner \
    --set nfs.server=x.x.x.x \
    --set nfs.path=/shareddata

Once the pod is in running condition, apply the storageclass manifest file. Here is the sample manifest-

allowVolumeExpansion: true
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  annotations:
    meta.helm.sh/release-name: nfs-client-provisioner
    meta.helm.sh/release-namespace: nfs-client-provisioner
    storageclass.kubernetes.io/is-default-class: "true"
  labels:
    app: nfs-subdir-external-provisioner
    app.kubernetes.io/managed-by: Helm
    chart: nfs-subdir-external-provisioner-4.0.18
    heritage: Helm
    release: nfs-client-provisioner
  name: nfs-client
parameters:
  archiveOnDelete: "true"
provisioner: cluster.local/nfs-client-provisioner-nfs-subdir-external-provisioner
reclaimPolicy: Delete
volumeBindingMode: Immediate

After applying this StorageClass. Create a sample pvc and see if it is bound. If output looks like below then you are good to go.

game-pvc             Bound    pvc-153be3f8-09ff-4a2d-bf68-5dc3a198c664   50Gi       RWO            nfs-client     13d

If you are still facing any issues please comment below. If you like this article please drop a like and feel free to comment as well.

Thanks

Did you find this article valuable?

Support Abhishek Singh by becoming a sponsor. Any amount is appreciated!