Posts How to Create a Dynamic NFS StorageClass with Helm and Use It in Kubernetes Deployments
Post
Cancel

How to Create a Dynamic NFS StorageClass with Helm and Use It in Kubernetes Deployments

How to Create a Dynamic NFS StorageClass with Helm and Use It in Kubernetes Deployments

In this tutorial, we’ll walk you through the process of creating a dynamic NFS StorageClass in Kubernetes using Helm. We’ll also cover how to create a PersistentVolumeClaim (PVC) and use it in your deployments. This guide is designed for those who want to manage persistent storage in Kubernetes clusters with NFS.

Prerequisites

Before you begin, make sure you have the following installed:

  • Kubernetes Cluster: A running Kubernetes cluster.
  • Helm: Helm installed and configured to interact with your Kubernetes cluster.
  • NFS Server: An existing NFS server with directories set up for Kubernetes to use.

If Helm is not installed, you can follow the official Helm installation guide to set it up.

Step 1: Add the NFS Subdir External Provisioner Helm Repository

The first step is to add the Helm repository that contains the NFS Subdir External Provisioner chart.

1
2
helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
helm repo update

Step 2: Create a Dynamic NFS StorageClass

Now that the Helm repository is added, we can use it to create a new dynamic NFS StorageClass. We will assume that your NFS server is running at 172.17.110.74, and you have different directories for production and homologation environments.

For Production:

1
2
3
export KUBECONFIG=k8s/k8s-config

helm install nfs-subdir-external-provisioner-prod nfs-subdir-external-provisioner/nfs-subdir-external-provisioner     --set nfs.server=192.168.1.56     --set nfs.path=/app-prod     --set storageClass.name=nfs-storage-class-prod     --set storageClass.provisionerName=k8s-sigs.io/nfs-storage-class-prod-provisioner     --set storageClass.pathPattern='app-prod'     --set storageClass.allowVolumeExpansion=true     --set storageClass.reclaimPolicy=Retain     --set storageClass.accessModes=ReadWriteMany

For Homologation:

1
2
3
export KUBECONFIG=k8s/k8s-config

helm install nfs-subdir-external-provisioner-hmg nfs-subdir-external-provisioner/nfs-subdir-external-provisioner     --set nfs.server=192.168.1.23     --set nfs.path=/app-hom     --set storageClass.name=nfs-storage-class-hmg     --set storageClass.provisionerName=k8s-sigs.io/nfs-storage-class-hmg-provisioner     --set storageClass.pathPattern='app-hom'     --set storageClass.allowVolumeExpansion=true     --set storageClass.reclaimPolicy=Retain     --set storageClass.accessModes=ReadWriteMany

Step 3: Create a PersistentVolumeClaim (PVC)

With the StorageClass created, you can now create a PVC that uses this StorageClass.

PVC for Homologation:

1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: hmg-pvc
  namespace: homolog
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: nfs-storage-class-hmg

Apply this PVC in your Kubernetes cluster:

1
kubectl --kubeconfig=k8s/k8s-config apply -f hmg-pvc.yaml

Step 4: Use the PVC in a Deployment

Now that you have a PVC, you can use it in your deployments. Below is an example of a Kubernetes Deployment that mounts the PVC created in the previous step.

Deployment Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hmg-app
  namespace: homolog
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hmg-app
  template:
    metadata:
      labels:
        app: hmg-app
    spec:
      containers:
      - name: app-container
        image: nginx:latest
        volumeMounts:
        - mountPath: "/data"
          name: hmg-storage
      volumes:
      - name: hmg-storage
        persistentVolumeClaim:
          claimName: hmg-pvc

Apply this Deployment in your Kubernetes cluster:

1
kubectl --kubeconfig=k8s/k8s-config apply -f hmg-deployment.yaml

Conclusion

By following this tutorial, you’ve successfully created dynamic NFS StorageClasses using Helm and applied them to different environments in your Kubernetes cluster. You’ve also learned how to create a PVC and use it in a Deployment, enabling persistent storage for your applications.

If you encounter any issues or have questions, feel free to leave a comment below or consult the official documentation for Helm and Kubernetes.


We hope this tutorial has been helpful. If you have any questions or suggestions, please leave a comment below!

This post is licensed under CC BY 4.0 by the author.