Sharing storage across multiple pods in Kubernetes

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

Kubernetes, the open-source platform designed to automate deploying, scaling, and operating application containers, is known for its powerful orchestration capabilities. However, when it comes to managing storage, things can get a little tricky, especially if you want to share storage across multiple pods. In this tutorial, we will explore how to effectively share storage across multiple pods in Kubernetes, using various methods and resources inherent to the platform.

Before we dive into the sharing methods, it’s important to understand the two fundamental concepts in Kubernetes that allow for storage sharing: Volumes and Persistent Volumes.

Understanding Volumes in Kubernetes

A Kubernetes volume is essentially a directory accessible to all containers running in a pod. It lasts as long as the pod, not the individual container, thus allowing data to survive container restarts.

EmptyDir Volumes

Let’s start with a basic example of an EmptyDir volume with a shared directory within a pod containing two containers:

apiVersion: v1
kind: Pod
metadata:
  name: shared-storage-pod
spec:
  containers:
  - name: container1
    image: nginx
    volumeMounts:
    - name: shared-volume
      mountPath: /usr/share/nginx/html
  - name: container2
    image: debian
    volumeMounts:
    - name: shared-volume
      mountPath: /shared
  volumes:
  - name: shared-volume
    emptyDir: {}

In the above example, an EmptyDir volume is created and mounted at /usr/share/nginx/html for the first container and at /shared for the second container. This allows both containers to share the same storage space.

Output for a successful creation:

pod/shared-storage-pod created

Persistent Volumes and Persistent Volume Claims

PersistentVolume (PV) and PersistentVolumeClaim (PVC) are higher-level resources that allow for more permanent storage which is independent of pod lifecycle.

Shared Persistent Volume Claim

The following example shows how to create a PV and a shared PVC across multiple pods:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: shared-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: /data/shared-pv
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: shared-pvc
spec:
  resources:
    requests:
      storage: 5Gi
  accessModes:
    - ReadWriteMany

Now that we have a shared PVC, let’s use it across two different pods:

apiVersion: v1
kind: Pod
metadata:
  name: pod-1
spec:
  containers:
  - name: myapp-container
    image: nginx
    volumeMounts:
    - name: my-shared-storage
      mountPath: /usr/share/nginx/html
  volumes:
  - name: my-shared-storage
    persistentVolumeClaim:
      claimName: shared-pvc
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-2
spec:
  containers:
  - name: myapp-container
    image: nginx
    volumeMounts:
    - name: my-shared-storage
      mountPath: /usr/share/nginx/html
  volumes:
  - name: my-shared-storage
    persistentVolumeClaim:
      claimName: shared-pvc

Once both pods are created, the containers in each pod will have access to the same shared storage space defined by the PVC, shared-pvc.

Output for created pods:

pod/pod-1 created
pod/pod-2 created

Advanced Storage Sharing Techniques

For even more advanced sharing scenarios, Kubernetes offers several other options, like using network file systems such as NFS or distributed storage solutions.

Using NFS for Shared Storage

Below is an example of how to set up an NFS server and create a PV that uses an NFS share:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /usr/local/share/nfsdata
    server: nfs-server-ip

Now, let’s mount this NFS-backed PV to multiple pods:

// Reusing the previous PVC definition
apiVersion: v1
kind: Pod
metadata:
  name: nfs-pod-1
spec:
  containers:
  - name: myapp-container
    image: nginx
    volumeMounts:
    - name: my-nfs-storage
      mountPath: /usr/share/nginx/html
  volumes:
  - name: my-nfs-storage
    persistentVolumeClaim:
      claimName: shared-pvc
---
apiVersion: v1
kind: Pod
metadata:
  name: nfs-pod-2
spec:
  containers:
  - name: myapp-container
    image: nginx
    volumeMounts:
    - name: my-nfs-storage
      mountPath: /usr/share/nginx/html
  volumes:
  - name: my-nfs-storage
    persistentVolumeClaim:
      claimName: shared-pvc

Once these pods are up, they will be sharing storage through the NFS server specified.

Conclusion

When it comes to sharing storage across multiple pods in Kubernetes, understanding and efficiently using volumes and persistent volume claims is crucial. By following the examples provided, one can handle simple to complex storage sharing scenarios, aiding in a more versatile Kubernetes environment.

Ensuring that storage is consistently accessible and managing the various access modes will facilitate a seamless application deployment process across multiple pods that require shared data resources.