Thursday, June 22, 2023

Creating a Shared Persistent Volume for Multiple Pods in Kubernetes

Sharing persistent storage across multiple Pods in Kubernetes requires proper setup, and NFS (Network File System) is a commonly used solution. Below is a step-by-step guide for setting up a shared Persistent Volume (PV) using NFS:


1. NFS Server Setup

a. Install NFS Server
sudo apt-get update sudo apt-get install nfs-kernel-server
b. Create a Shared Directory
sudo mkdir -p /var/nfs/general sudo chown nobody:nogroup /var/nfs/general
c. Configure NFS Exports

Add the shared directory to /etc/exports:

/var/nfs/general *(rw,sync,no_subtree_check)
d. Restart NFS Server
sudo exportfs -a sudo systemctl restart nfs-kernel-server

2. Configure Persistent Volume (PV) in Kubernetes

a. Create a PV Configuration File (nfs-pv.yaml)
apiVersion: v1 kind: PersistentVolume metadata: name: nfs-pv spec: capacity: storage: 5Gi accessModes: - ReadWriteMany persistentVolumeReclaimPolicy: Retain nfs: server: <NFS_SERVER_IP> path: "/var/nfs/general"
  • Replace <NFS_SERVER_IP> with the IP address of your NFS server.
  • The ReadWriteMany access mode allows multiple Pods to read and write to the volume.

b. Apply the PV Configuration

kubectl apply -f nfs-pv.yaml

3. Create a Persistent Volume Claim (PVC)

a. Create a PVC Configuration File (nfs-pvc.yaml)
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: nfs-pvc spec: accessModes: - ReadWriteMany resources: requests: storage: 5Gi
b. Apply the PVC Configuration
kubectl apply -f nfs-pvc.yaml

4. Mount the PVC in a Pod

a. Create a Pod Configuration File (pod.yaml)
apiVersion: v1 kind: Pod metadata: name: mypod spec: containers: - name: mycontainer image: nginx volumeMounts: - mountPath: "/usr/share/nginx/html" name: nfs-vol volumes: - name: nfs-vol persistentVolumeClaim: claimName: nfs-pvc
b. Deploy the Pod
kubectl apply -f pod.yaml

5. Test and Validate

  1. Access the NFS Server:

    • Create a file in the shared directory:
      echo "Hello from NFS!" | sudo tee /var/nfs/general/index.html
  2. Verify in the Pod:

    • Enter the Pod and check the file:
      kubectl exec -it mypod -- cat /usr/share/nginx/html/index.html
    • You should see the content: Hello from NFS!
  3. Cross-Pod Validation:

    • If multiple Pods mount the same PVC, changes made by one Pod should be visible to others.

6. Notes and Considerations

  • Access Modes:

    • Use ReadWriteMany for shared access. Some storage types may not support this mode.
  • Storage Solutions:

    • Alternatives like Ceph, GlusterFS, or Longhorn provide similar shared storage capabilities.
  • Security:

    • Ensure proper permissions and network policies are in place to secure the NFS server.
  • High Availability:

    • For production setups, consider high-availability NFS configurations or other distributed file systems.

By following these steps, you can set up shared storage using NFS for multiple Pods in Kubernetes, enabling them to share data seamlessly.