Skip to main content

Steps to Expose NFS in Kubernetes

Now that you have the NFS server set up, you can use it as shared storage in your Kubernetes cluster.

1. Create the Persistent Volume (PV)

Create a PersistentVolume resource in Kubernetes that points to your NFS server. Here's an example YAML:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /mnt/data  # Path to the directory on NFS server
    server: <nfs-server-ip>  # Replace with the NFS server's IP address

2. Create the Persistent Volume Claim (PVC)

Next, create a PersistentVolumeClaim (PVC) to request the shared volume:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi

3. Use the PVC in a Pod

Finally, mount the PersistentVolumeClaim into your pod as follows:

apiVersion: v1
kind: Pod
metadata:
  name: nfs-client-pod
spec:
  containers:
    - name: nfs-client-container
      image: busybox
      command: ["sleep", "3600"]
      volumeMounts:
        - mountPath: "/mnt/data"
          name: nfs-volume
  volumes:
    - name: nfs-volume
      persistentVolumeClaim:
        claimName: nfs-pvc

4. Apply the Configuration

Apply the PersistentVolume, PersistentVolumeClaim, and Pod configurations:

kubectl apply -f nfs-pv.yaml
kubectl apply -f nfs-pvc.yaml
kubectl apply -f nfs-client-pod.yaml

5. Verify the Setup

Once the Pod is running, verify that the NFS share is mounted inside the container:

kubectl exec -it nfs-client-pod -- df -h

You should see /mnt/data mounted and showing the space usage of the NFS share.