Verifying Data Persistence

To validate if our data on the volume is persistent or not, we will create a hello.txt file and delete our pod, When we again create a new pod with the same volume attached, we should our hello.txt file as it was before.

Accessing the Pod

To access our pod we can make use of the exec command,

kubectl exec -it mybox -- /bin/sh

Creating hello.txt File

We will move to the demo directory and create a hello.txt file,

cd demo
echo Hello > hello.txt
exit

Destroying and Recreating the pod

To destroy and recreate the pod we can use,

kubectl delete -f pod.yaml
kubectl apply -f pod.yaml

Verify the Test File Persists

If we now visit again our demo directory, it will be as it was when we deleted the pod

kubectl exec -it mybox -- /bin/sh

cd demo
ls demo
cat hello.txt
exit

Configure a Pod to Use a PersistentVolume for Storage

Using a PersistentVolume with a Pod in Kubernetes ensures that data stored by the Pod persists beyond the Pod’s lifecycle. This is crucial for applications that require durable storage, such as databases or file servers, as it prevents data loss when Pods are restarted, rescheduled, or scaled up/down. PersistentVolumes provides a reliable way to manage and share storage resources across multiple Pods, enabling stateful applications to maintain data integrity and availability in dynamic containerized environments.

Similar Reads

What is Kubernetes?

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It provides features for scheduling, scaling, load balancing, and managing containerized workloads across a cluster of machines....

Understanding Kubernetes Component

Kubernetes Pod: A Kubernetes Pod is the smallest deployable unit in Kubernetes, representing one or more containers that share network and storage resources and are scheduled together on the same host. Pods are used to encapsulate and deploy application components and are managed by Kubernetes controllers. Persistent Volume: A Persistent Volume (PV) in Kubernetes is a piece of networked storage provisioned by an administrator that persists beyond the lifecycle of individual Pods. It allows Pods to store data persistently and is accessed using a Persistent Volume Claim (PVC). Persistent Volume Claim: A Persistent Volume Claim (PVC) in Kubernetes is a request for storage by a Pod. It allows Pods to dynamically request and use Persistent Volumes (PVs) without needing to know the details of the underlying storage infrastructure....

1. Using PersistentVolumes in Pods

Define volume type and size using a YAML file (e.g., pv.yaml). Specify access modes (read-write, read-only) and reclaim policy (retain, recycle, delete)...

2. Create a PersistentVolume Claim (PVC)

This will allow us to reserve this volume so that we can use it in any of our pods,...

3. Create a Pod

We can create a pod and use our claim to attach the persistent volume to it....

Verifying Data Persistence

To validate if our data on the volume is persistent or not, we will create a hello.txt file and delete our pod, When we again create a new pod with the same volume attached, we should our hello.txt file as it was before....

Kubernetes Persistent Volume – FAQs

Why would I need to use a PersistentVolume?...