Open In App

How To Share Storage Between Kubernetes Pods ?

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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

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

  • You can skip this step if you already have a Kubernetes Cluster running in your machine. But in case you don’t have a Cluster running enter the following command to create Kubernetes locally using Minikube:
minikube start

Starting A Minikube

  • Minikube is a one-node Kubernetes cluster where master processes and work processes both run on one node.

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.

  • We are setting the storage size to 1GB in this configuration file. Enter the following command for creating a file named storage.yaml:
touch storage.yaml
  • And then paste the following code inside it.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: shared-storage
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

Step 3: Creating The Configuration Files

  • Now that we have our PVC configuration file ready, we will create a two Pods, called pod-one and pod-two that will share the same PCV. Create a file called pod-one.yaml
touch pod-one.yaml
  • Enter the following code inside this pod-one.yaml file.
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
  • This configuration file will create a Pod named pod-one that echoes (prints) “GeeksforGeeks” to the “output.txt” in the data directory.
  • This Pod is connected to the Persistent Volume Claim (PVC) called shared-storage for storage. Similarly create another file called pod-two.yaml
touch pod-two.yaml
  • Paste the following code inside this pod-two.yaml file.
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
  • This Pod is not performing any action in the “data” directory but is connected to the same Persistent Volume Claim (PVC) hence it will also output the “GeeksforGeeks” text, and since the Pods are using a Persistent Volume Claim, pod-two can output the text even after we delete pod-one.

Step 3: Applying The Configuration File

  • Now that the configuration files are ready, let’s create PersistentVolumeClaim in the cluster using the configuration file:
kubectl apply -f storage.yaml

Applying Configuration File

  • And then we can also apply the configuration files to create Pods by entering the following command:
kubectl apply -f pod-one.yaml
  • And the pod-one will be created, similarly for pod-two.yaml:
kubectl apply -f pod-two.yaml
  • Pods may take some time to create their containers. Now, if we check the Pods running in our Kubernetes cluster by the following command:
kubectl get pods
  • You will see the two Pods with shared storage running:

Listing Kubernetes Pods

Step 4: Checking The Output

  • We know that the Pod pod-one will echo the text “GeeksforGeeks” in the “/data/output.txt” file so let’s print the output.txt file using the following command:
 kubectl exec -it pod-one -- cat /data/output.txt 
  • you will get a similar output where it prints “GeeksforGeeks”:

Kubernetes Pod Execution

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

kubernetes pod2 exeution

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
  • And your pod-one will be deleted:

kubernetes pod deletion

  • Now that pod-one has been terminated, but since we have shared storage, we can also get the output “GeeksforGeeks” using the pod-two. For that we will have to enter the following command:
kubectl exec -it pod-two -- cat /data/output.txt


  • And we can still get the output as “GeeksforGeeks”, this is due to the PersistentVolumeClaim (PVC) that does not die along with the Pods it is connected to.

kubernetes pod2 execution

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.


    Like Article
    Suggest improvement
    Share your thoughts in the comments

    Similar Reads