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.