kubernetespersistencenfspersistent-volumespersistent-volume-claims

nfs share storage class to multiple volume


I have one NFS server. What I understood by reading PV and PVC is mapped 1:1. What I didn't get is the relationship with storage class.

Let's say for multiple services multiple directories I want to share over pv/pvc. For e.g., NFS server is available at 192.168.0.2, and want to share /data/service1/folder1, /data/service1/folder2 to service1 and /data/service2/folder1 /data/service1/folder2 to service2. Yes, I want to understand cross share too.

In this scenario how to proceed? Is it possible to directly use storage-class rather than creating pv and pvc (as it is anyway 1:1 mapped)?


Solution

  • In Kubernetes, PVs and PVCs are used to abstract and manage the underlying storage resources. While the typical use case is indeed a 1:1 mapping between PV and PVC, it's not the only way to use PVs and PVCs. In your scenario, you can achieve your goal by using the dynamic provisioning capability of Kubernetes and Storage Classes.

    If you want to share directories across different services, simply create separate PVCs and attach them to pods as needed. The storage backend (NFS server) handles the sharing of directories.

    By using a Storage Class and dynamic provisioning, you can abstract the details of the NFS configuration and create PVCs on-demand as needed. This approach is more scalable and easier to manage compared to creating individual PVs and PVCs for each directory and service combination.

    Remember that not all storage providers support dynamic provisioning, so make sure your storage class and setup align with your storage backend's capabilities.

    Example:

    The Storage Class

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: nfs-storage
    provisioner: example.com/nfs 
    parameters:
      server: 192.168.0.2
      path: /data/service1/meow
    

    PVCs:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc2
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 1Gi
      storageClassName: nfs-storage
    
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc1
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 1Gi
      storageClassName: nfs-storage
    

    Consuming Services (Pods)

    spec:
      volumes:
        - name: shared-data
          persistentVolumeClaim:
            claimName: pvc1
      containers:
        - name: service1-container
          image: service1-image
          volumeMounts:
            - name: shared-data
              mountPath: /shared-data
    

    .....

    spec:
      volumes:
        - name: shared-data
          persistentVolumeClaim:
            claimName: pvc2
      containers:
        - name: service2-container
          image: service2-image
          volumeMounts:
            - name: shared-data
              mountPath: /shared-data