Tue. Oct 4th, 2022

Hello Android

All android in one place

NFS – Static Persistent Storage in Kubernetes

4 min read

Hello, readers! This article talks about NFS – Static Persistent Storage in Kubernetes with a detailed demonstration around the NFS setup.

So, let us begin! 🙂


What is Static Persistent Storage in Kubernetes?

Kubernetes offers various kinds of Storage facilities for the application to store the data according to its nature. That is, we have ephemeral storage for the data that does not persist. For the data that is supposed to stay even after the deletion of the pod or application in a DR scenario, we have persistent volumes.

Persistent volumes are of the below kinds-

  1. Static Persistent Volume: The storage capacity needs to be defined beforehand. Also, the application teams need to create Persistent volume and Persistent Volume claims to block the storage space.
  2. Dynamic Persistent Volume: The storage capacity does not need to be defined beforehand. We can create persistent volume claims dynamically to request the persistent storage space at runtime.

In this article, we will be focusing on setting up a Static Persistent Storage solution. We will be exploring Network File System (NFS) protocol for the setup.


NFS – The Static Persistent Storage Solution

Network File System (NFS) is a protocol that enables us to transfer and share data/files between various on-premise machines and servers easily.

This protocol is available for the Server as well as the Client machine for the file exchange.

With regards to the context of this topic, we will be focusing on setting up the NFS on the server machine to block the storage and make it accessible through the Kubernetes infrastructure.


Step 1: Install NFS on the Server

As a backend solution, we will need to provision a Virtual Machine (Server) in the same network as that of the Kubernetes Cluster. We can mark this server specifically for the storage of the persistent data across all the applications within the cluster.

We also assume a disk provisioned specially for the NFS application storage for the VM to be backed by.

For this example, we assume a Persistent disk of 2 Tb created and mounted on the Virtual machine as shown below-

#1. Create a directory for the disk mount
mkdir /nfs-data/
#2. Mount the disk on the VM at the above created directory as the mount-point
mount /dev/sdc /nfs-data/
#3. Give all the read-write permissions to the NFS mount point
chmod -R 777 /nfs-data

After setting up the Virtual Machine, we need to execute the below steps to install NFS on the server machine.

sudo yum -y install nfs-utils

echo "/nfs-static-data      a.b.c.d/xy(rw,sync,no_root_squash,no_subtree_check)" >> /etc/exports
 
systemctl start rpcbind nfs-server

systemctl enable rpcbind nfs-server

firewall-cmd --add-service=nfs --permanent

firewall-cmd --add-service={nfs3,mountd,rpc-bind} --permanent

firewall-cmd --reload

The above commands install the NFS utility over the Virtual machine and also saves the permanent connection to the cluster subnet range (a.b.c.d/xy). We then start the NFS utility service using the systemctl commands. At last, we add the firewall rules with regards to NFS on the virtual machine as a permanent value.


Step 2: Open connections to NFS

Once the NFS setup is established, we now set up our volume for the first application on the setup.

At first, we create a folder of the application within the nfs mount point for isolating the storage space.

cd /nfs-data/
mkdir <<app_name>>/<<folder_name>>

We then whitelist the connection for the above created folder within the mountpoint to the cluster subnet range-

vi exports
#now a file opens, add the below line of code in the file and then press ESC wq! to save and exit

/nfs-data/<<app_name>>/<<folder_name>> x.x.x.x/xx(rw,sync,no_root_squash,no_subtree_check)

#execute the below command 
exportfs -ra


Step 3: Create a Persistent Volume

Once we establish and whitelist connection between the storage folder and cluster, we now create a Persistent volume to block the storage from within the disk.

Persistent Volume.YAML

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-test
spec:
  capacity:
    storage: 4Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: x.x.x.x
    path: "/nfs-data/app_name/folder_name"

Here, we block the storage space of 4Gi from within the disk through the NFS mount point as the path.


Step 4: Create a Persistent Volume Claim

Now, we claim the above created storage space through a Persistent Volume claim in Kubernetes using the below YAML-

Persistent_Volume_Claim.YAML

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-test
spec:
  resources:
    requests:
      storage: 3Gi
  volumeName: "pv-test" # Persistent Volume Name
  accessModes:
    - ReadWriteMany
  storageClassName: ""

Step 5: Mount PVC in Kubernetes Deployment

As a final step, we need to mount the above setup within the Kubernetes deployment to let the data storage to the backend solution happen.

Deployment.YAML

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nfs-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      volumes:
      - name: pv-test
        persistentVolumeClaim:
          claimName: pvc-test
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - name: pv-test 
          mountPath: /nfs-data/<<app_name>>/<<folder_name>>/ 

Conclusion

By this, we have approached the end of this topic. Feel free to comment below, in case you come across any questions. For more such posts related to Kubernetes Storage, Stay tuned with us.

Till then, Happy Learning!! 🙂

www.hello-android.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Hello android © All rights reserved. | Newsphere by AF themes.