🐶
Kubernetes

Kubernetes Pod Storage Sharing: A Complete Guide

By Jan on 02/06/2025

Learn various methods and best practices to effectively share storage between Kubernetes pods for seamless data access and application functionality.

Kubernetes Pod Storage Sharing: A Complete Guide

Table of Contents

Introduction

In Kubernetes, sharing storage between pods and ensuring data persistence is crucial for many applications. This is where Kubernetes Volumes come into play. Volumes provide a way to abstract away the underlying storage infrastructure and offer a consistent way to manage data in your pods.

Step-by-Step Guide

To share storage between Kubernetes pods, you use Volumes. Here's a breakdown:

  1. Define a Volume in your Pod specification:

    apiVersion: v1
    kind: Pod
    metadata:
      name: my-pod
    spec:
      containers:
      - name: my-container
        image: my-image
        volumeMounts:
        - mountPath: /data
          name: shared-volume
      volumes:
      - name: shared-volume
        emptyDir: {} 

    This example uses emptyDir, which creates a temporary directory that exists as long as the Pod runs.

  2. Types of Volumes:

    • emptyDir: Temporary storage that disappears when the Pod is gone. Useful for scratch space or inter-container communication.
    • hostPath: Mounts a directory from the host node. Use with caution due to security and portability concerns.
    • persistentVolumeClaim: The recommended way for persistent storage. You create a PersistentVolume (PV) and then a PersistentVolumeClaim (PVC) in your Pod to request it.
  3. Persistent Storage:

    • Create a PersistentVolume (PV): This defines the actual storage, like a network-attached storage (NAS).

      apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: my-pv
      spec:
        capacity:
          storage: 1Gi
        accessModes:
        - ReadWriteOnce
        persistentVolumeReclaimPolicy: Retain
        nfs:
          server: nfs-server.example.com
          path: /some/path
    • Create a PersistentVolumeClaim (PVC): Pods use this to request a PV.

      apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: my-pvc
      spec:
        accessModes:
        - ReadWriteOnce
        resources:
          requests:
            storage: 500Mi
    • Mount the PVC in your Pod:

      volumes:
      - name: shared-volume
        persistentVolumeClaim:
          claimName: my-pvc
  4. Important Considerations:

    • Access Modes: Control how Pods can access the volume (e.g., ReadWriteOnce, ReadOnlyMany).
    • Storage Class: Group PVs with similar characteristics (performance, availability).
    • Security: Be mindful of data security, especially when using hostPath.

By using Volumes effectively, you can enable data sharing and persistence for your Kubernetes applications.

Code Example

This code provides examples of using Kubernetes Volumes for sharing storage between pods. It demonstrates using emptyDir for temporary storage shared between containers in a pod, hostPath for mounting a directory from the host node (use with caution), and PersistentVolumes and PersistentVolumeClaims for persistent storage using NFS. The examples include YAML configurations for creating Pods, PersistentVolumes, and PersistentVolumeClaims, along with explanations of key concepts like access modes and reclaim policies. Remember to replace placeholder values with your actual configuration details and adapt the examples to your specific needs.

This code example demonstrates how to share storage between Kubernetes pods using different types of Volumes.

1. emptyDir Volume:

This example creates a Pod with two containers sharing an emptyDir volume for inter-container communication.

apiVersion: v1
kind: Pod
metadata:
  name: emptydir-pod
spec:
  containers:
  - name: producer
    image: alpine
    command: ["sh", "-c", "for i in $(seq 1 5); do echo \"Message $i\" > /data/message.txt; sleep 1; done"]
    volumeMounts:
    - mountPath: /data
      name: shared-data
  - name: consumer
    image: alpine
    command: ["sh", "-c", "tail -f /data/message.txt"]
    volumeMounts:
    - mountPath: /data
      name: shared-data
  volumes:
  - name: shared-data
    emptyDir: {}

2. hostPath Volume (Use with caution):

This example mounts a directory from the host node into a Pod.

apiVersion: v1
kind: Pod
metadata:
  name: hostpath-pod
spec:
  containers:
  - name: my-container
    image: nginx
    volumeMounts:
    - mountPath: /usr/share/nginx/html
      name: host-volume
  volumes:
  - name: host-volume
    hostPath:
      path: /var/www/html 
      type: Directory

3. Persistent Volume and Persistent Volume Claim:

a) Create a PersistentVolume (PV):

This example defines a PV using NFS.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: nfs-server.example.com
    path: /volume1

b) Create a PersistentVolumeClaim (PVC):

This PVC requests storage from the defined PV.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi

c) Mount the PVC in your Pod:

This Pod uses the PVC to access the persistent storage.

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: my-container
    image: my-image
    volumeMounts:
    - mountPath: /data
      name: persistent-storage
  volumes:
  - name: persistent-storage
    persistentVolumeClaim:
      claimName: nfs-pvc

Important Notes:

  • Replace placeholder values like nfs-server.example.com and /volume1 with your actual NFS server details.
  • Carefully consider the accessModes and persistentVolumeReclaimPolicy for your specific needs.
  • Explore different storage providers and options for PVs based on your environment and requirements.

This code example provides a starting point for understanding and using Volumes in Kubernetes. Remember to adapt these examples to your specific application and infrastructure needs.

Additional Notes

General:

  • Ephemeral vs. Persistent: Understand the difference between emptyDir (temporary) and persistentVolumeClaim (lasts beyond Pod lifecycle). Choose based on your data's importance and whether it needs to survive restarts.
  • Volume Mounting: A volume itself is just a storage resource. You need to use volumeMounts within a container's definition to specify where inside the container that volume should be accessible.
  • Dynamic Provisioning: For more advanced setups, you can configure Kubernetes to automatically create PVs when a PVC is made, simplifying storage management.

Persistent Volumes (PVs):

  • Reclaim Policies: The persistentVolumeReclaimPolicy (e.g., Retain, Delete, Recycle) determines what happens to the PV when it's released by a PVC. Retain is often safer for important data, requiring manual cleanup.
  • Storage Provider Integration: PVs abstract the underlying storage. You'll need to configure them based on your chosen provider (AWS EBS, Azure Disk, Google Persistent Disk, etc.).

Persistent Volume Claims (PVCs):

  • Resource Requests: When defining a PVC, specify the desired storage requests. Kubernetes will try to find a suitable PV that meets these requirements.
  • Binding: A PVC needs to be bound to a PV before it can be used. Kubernetes handles this automatically if a matching PV is available.

Troubleshooting:

  • Events: Use kubectl describe pv <pv-name> and kubectl describe pvc <pvc-name> to get detailed information and events, which can help diagnose issues.
  • Logs: Check the logs of Pods that are having trouble accessing volumes for clues.

Best Practices:

  • Use Labels: Apply labels to PVs and PVCs to organize them and make it easier to select the right storage for your Pods.
  • Separate Concerns: Define PVs separately from Pods. This promotes reusability and makes it easier to manage storage independently.
  • Security: If using hostPath, be extremely cautious as it can pose security risks. Consider alternatives like network-attached storage for sensitive data.

Summary

This table summarizes how to share storage between Kubernetes pods using Volumes:

Concept Description Example
What are Volumes? A mechanism to provide storage to containers within a Pod. N/A
Defining a Volume Volumes are defined within the Pod specification. volumes:
- name: shared-volume
emptyDir: {}
Volume Types
emptyDir Temporary storage that exists only while the Pod runs. emptyDir: {}
hostPath Mounts a directory from the host node (use cautiously). hostPath:
path: /some/directory
persistentVolumeClaim The recommended way for persistent storage. Uses a PersistentVolume (PV) and PersistentVolumeClaim (PVC). persistentVolumeClaim:
claimName: my-pvc
Persistent Storage
PersistentVolume (PV) Defines the actual storage (e.g., NAS). apiVersion: v1
kind: PersistentVolume
...
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
` ...
PersistentVolumeClaim (PVC) Pods use this to request a PV. apiVersion: v1
kind: PersistentVolumeClaim
...
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
Mounting a PVC volumes:
- name: shared-volume
persistentVolumeClaim:
claimName: my-pvc
Important Considerations
Access Modes Control how Pods can access the volume (e.g., ReadWriteOnce, ReadOnlyMany). N/A
Storage Class Group PVs with similar characteristics (performance, availability). N/A
Security Be mindful of data security, especially when using hostPath. N/A

Conclusion

Volumes are essential for managing data in Kubernetes, enabling data sharing between containers within a Pod and data persistence beyond a Pod's lifecycle. By understanding the different types of Volumes, such as emptyDir, hostPath, and persistentVolumeClaim, and how to define and use them in your Pod specifications, you can ensure that your applications have access to the storage they need. Remember to choose the appropriate Volume type based on your specific requirements, considering factors like data persistence, access modes, and security implications. By leveraging Volumes effectively, you can build robust and scalable applications on Kubernetes.

References

  • How to share volumes between pods in kubernetes : r/kubernetes How to share volumes between pods in kubernetes : r/kubernetes | Posted by u/karthik14111986 - 5 votes and 10 comments
  • How To Share Storage Between Kubernetes Pods ? - GeeksforGeeks How To Share Storage Between Kubernetes Pods ? - GeeksforGeeks | A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
  • How to share data between containers with K8s shared volume ... How to share data between containers with K8s shared volume ... | A quick guide to using Kubernetes shared volumes to communicate data or config between two Kubernetes containers.
  • Kubernetes Shared Storage: The Basics and a Quick Tutorial Kubernetes Shared Storage: The Basics and a Quick Tutorial | Learn how Kubernetes storage works and see how to spin up a pod with two containers and share data between them.
  • File based data exchange between pods and daemon-set - General ... File based data exchange between pods and daemon-set - General ... | Hi everyone, I am searching for a solution to create a (persistent) volume on each node in the cluster where multiple pods can write into and a daemon set can read from. My first idea was to use the hostPath volume. But after reading the documentation, it comes with some downsides, especially that containers must run as root when writing. After that I thought the local volume could solve my problem. But with local persistent volumes, the scheduler ensures that a pod using a local persistent vo...
  • Communicate Between Containers in the Same Pod Using a Shared ... Communicate Between Containers in the Same Pod Using a Shared ... | This page shows how to use a Volume to communicate between two Containers running in the same Pod. See also how to allow processes to communicate by sharing process namespace between containers. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts.
  • Kubernetes Pods Storage and Volume Sharing | Baeldung on Ops Kubernetes Pods Storage and Volume Sharing | Baeldung on Ops | Learn how to share storage between Kubernetes pods.
  • kubernetes - Sharing volumes between pods on different clusters ... kubernetes - Sharing volumes between pods on different clusters ... | Aug 28, 2022 ... Can you share a PV, between clusters: in theory yes. You can mirror one PV object into another cluster, given you have similar storage ...
  • Kubernetes Documentation Concepts Storage Volumes Kubernetes Documentation Concepts Storage Volumes | Kubernetes volumes provide a way for containers in a pods to access and share data via the filesystem. There are different kinds of volume that you can use for different purposes, such as: populating a configuration file based on a ConfigMap or a Secret providing some temporary scratch space for a pod sharing a filesystem between two different containers in the same pod sharing a filesystem between two different pods (even if those Pods run on different nodes) durably storing data so that it stays available even if the Pod restarts or is replaced passing configuration information to an app running in a container, based on details of the Pod the container is in (for example: telling a sidecar container what namespace the Pod is running in) providing read-only access to data in a different container image Data sharing can be between different local processes within a container, or between different containers, or between Pods.

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait