Open In App

How To Share Storage Between Kubernetes Pods ?

Kubernetes or K8s is an open-sourced container orchestration technology that is used for automating the manual processes of deploying, managing and scaling applications by the help of containers. Sharing storage between Pods can be achieved by the help of Kubernetes Volumes, a Volume in Kubernetes is a data storing feature with the help of which we can store data that is persistent and can be accessed by multiple Kubernetes Pod. In this article we will discuss how to How to share storage between Kubernetes Pods using Volumes.

What Is Kubernetes?

Kubernetes is an open-source container orchestration technology used for automating the software deployment process, scaling applications and managing them. In simple words, it is a container orchestration technology, it enables us to work with multiple containers. Kubernetes was developed by Google, it was released on 9 September 2014 and was later donated to the Cloud Native Computing Foundation (CNCF). It is estimated that about 92% businesses that uses any Container Orchestration tool is using Kubernetes.



What Is Pod?

Pod is the smallest or fundamental component of a Kubernetes architecture. It is an abstraction over containers or in simpler terms, it is a collection of Linux containers. Pod creates a running environment or a layer on top of the Container. Kubernetes Pods are ephemeral, this means that if a Pod fails, then Kubernetes can automatically create its new replica.

Pod creates a running environment or a layer on top of the Container. Pods are used because Kubernetes wants to abstract away the container runtime or container technologies so that we can replace them if we want to. The pod is usually meant to run one application container inside of it. You can run multiple containers inside one Pod but usually, it’s only the case if you have one main application container and a helper container or some side service that has to run inside of that pod.



What Is Storage In Kubernetes?

Storage in Kubernetes is like an external plug-in to your cluster. If any application in a Kubernetes cluster wants to store data, they can either do it persistently or non persistently. The default option for Storage in Kubernetes is non persistent, so if you use a Configmap along with a Pod, then the data stored in the Configmap by the Pod will also be erased when the Pod dies. Non persistent Storage would store data in a temporary storage directory until the Pod goes down and then the data is also erased.

Whereas Persistent Storage doesn’t depend on the Pod lifecycle and will still be there when Pod dies and new one gets created so that the new Pod can pick up where the previous Pod left off. This Pod will read the existing data from that storage to get up to date data. Also we don’t know on which Node the new Pod restarts so the Persistent Storage is available on all the nodes.

Persistant Volume

A PersistentVolume is a piece of storage in a Kubernetes cluster that an administrator has provisioned. PersistentVolume has a Pod independent lifecycle so it can preserve state even after a Pod dies and a new Pod is created. PersistentVolume (PV) is a Kubernetes cluster resource just like a node. A Pod is immutable therefore, when it dies, all the data created during its lifetime will be lost. Most of the applications require this characteristic of Pod but sometimes applications (for example, database applications) needs to preserve state and that is where Kubernetes PersistentVolumes (PVs) are used by the cluster administrator which can store information persistently unaffected even after Pod dies and new Pod is created.

Three important Characteristics of Kubernetes Persistent Volumes are:

Persistant Volume Claims (PVC)

PersistentVolumeClaim (PVC) is a way for user to request for Persistent storage. With Persistant Volume Claims, an application running on a container can request a specific type of storage. For example, the user can request for a 10GB storage or the user can specify the way the application needs to access data from Storage (like read only, write only, read/write, etc.)

To use a Persistent Volume, developers have to explicitly configure the application YAML file to use Persistent Volume components, in other words application have to claim that Volume storage and that is done using Persistent Volume Claims (PVCs). Persistent Volume Claims (PVCs) are also created using YAML configurations and unlike Persistent Volumes they are namespaced hence they should be present in the same namespace as the Pod for the application requesting for Persistent Storage.

How To Share Storage Between Kubernetes Pods: A Step-By-Step Guide

Follow this step by step guide to understand how to share storage between Kubernetes Pods.

Step 1: Starting A Kubernetes Cluster

minikube start

Step 2: Creating A PersistentVolumeClaim (PVC)

For our Pod to have shared storage, we will use a PersistentVolumeClaim (PVC) that is connected to both the Pods hence both the Pods can share a same PersistentVolumeClaim (PVC) Storage.

touch storage.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: shared-storage
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

Step 3: Creating The Configuration Files

touch pod-one.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-one
spec:
volumes:
- name: shared-storage
persistentVolumeClaim:
claimName: shared-storage
containers:
- name: container-one
image: busybox
command: ["sh", "-c", "echo 'GeeksforGeeks' > /data/output.txt && tail -f /dev/null"]
volumeMounts:
- mountPath: "/data"
name: shared-storage
touch pod-two.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-two
spec:
volumes:
- name: shared-storage
persistentVolumeClaim:
claimName: shared-storage
containers:
- name: container-two
image: busybox
command: ["sh", "-c", "tail -f /dev/null"]
volumeMounts:
- mountPath: "/data"
name: shared-storage

Step 3: Applying The Configuration File

kubectl apply -f storage.yaml

kubectl apply -f pod-one.yaml
kubectl apply -f pod-two.yaml
kubectl get pods

Step 4: Checking The Output

 kubectl exec -it pod-one -- cat /data/output.txt 

And since this Pod is sharing storage with pod-two as well, we can also use pod-two to access “/data/output.txt” and it will still store the same text:

kubectl exec -it pod-two -- cat /data/output.txt

Step 5. Deleting the Pod

We have clearly established in our previous step that we can share storage between two Kubernetes Pods using Volumes, now let’s check if we can delete pod-one and still access the data since it is now stored in the PVC:

 kubectl delete pod pod-one

kubectl exec -it pod-two -- cat /data/output.txt


This is how we can have shared storage where even after pod-one had terminated, we could access the data using pod-two. Pat yourself on the back because we have just learned how to share storage between Kubernetes Pods.

Conclusion

Kubernetes or K8s is an open-sourced container orchestration technology that is used for automating the manual processes of deploying, managing and scaling applications by the help of containers. In Kubernetes, data persistence doesn’t come out of the box. Data persistence is something that we have to explicitly configure for each application that needs saving data between Pod restarts. A PersistentVolume (PV) is a Kubernetes cluster resource just like a Node and this resource has a Pod independent lifecycle and are used to preserve state even after a Pod dies. This is why we use We hope that this article taught you about how to share storage between Kubernetes Pods. Make sure to follow other articles on GeeksforGeeks to know about more tools in DevOps.

Sharing Storage Between Kubernetes Pods – FAQ’s

Why Do We Lose Data When A Pod Dies?

A Pod is immutable hence, when it dies, all the data created during its lifetime will be lost.

What Is Kubernetes Volume?

Volume in Kubernetes is a data storing feature with the help of which we can store data that is persistent and can be accessed by containers in a Kubernetes pod.

Why Do We Need Kubernetes Volumes?

When a Pod is deleted, all the data that is associated with the Pod is also deleted along with the Pod, we need Kubernetes Volume because they provide us a way to save or persist the data even after the Pod is deleted.

How To Share Storage Between Kubernetes Pods?

In Kubernetes, sharing storage between Pods can be achieved by the help of Kubernetes Volumes, a Volume in Kubernetes is a data storing feature with the help of which we can store data that is persistent and can be accessed by containers in a Kubernetes pod.

What Are The Characteristics Of Kubernetes Persistent Volumes?

Three important Characteristics of Kubernetes Persistent Volumes are:

  1. Persistent Volumes do not depend on the pod lifecycle.
  2. Persistent Volumes are available on all nodes.
  3. Persistent Volumes survives even if cluster crashes.

    Article Tags :