0
0
Kubernetesdevops~5 mins

Why persistent storage matters in Kubernetes - Why It Works

Choose your learning style9 modes available
Introduction
Kubernetes runs applications in containers that can be stopped or moved anytime. Without persistent storage, any data saved inside a container is lost when the container stops. Persistent storage keeps data safe even if containers restart or move to other servers.
When you run a database in Kubernetes and want to keep its data safe after restarts.
When your app needs to save user files or uploads that must not disappear.
When you want to keep logs or reports generated by your app for later use.
When you want to share files between multiple containers running together.
When you want to upgrade or restart your app without losing its data.
Config File - persistent-volume-claim.yaml
persistent-volume-claim.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: example-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: standard

This file creates a PersistentVolumeClaim (PVC) named example-pvc. It requests 1 gigabyte of storage that can be read and written by one node at a time. The storageClassName tells Kubernetes which storage type to use, here the default standard class.

The PVC lets your app ask Kubernetes for storage that stays safe even if the app restarts or moves.

Commands
This command tells Kubernetes to create the PersistentVolumeClaim defined in the file. It reserves storage for your app to use persistently.
Terminal
kubectl apply -f persistent-volume-claim.yaml
Expected OutputExpected
persistentvolumeclaim/example-pvc created
This command checks the status of the PersistentVolumeClaim to see if the storage is ready to use.
Terminal
kubectl get pvc example-pvc
Expected OutputExpected
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE example-pvc Bound pvc-12345678-90ab-cdef-1234-567890abcdef 1Gi RWO standard 10s
This command shows detailed information about the PersistentVolumeClaim, including which storage volume it is using and its status.
Terminal
kubectl describe pvc example-pvc
Expected OutputExpected
Name: example-pvc Namespace: default StorageClass: standard Status: Bound Volume: pvc-12345678-90ab-cdef-1234-567890abcdef Labels: <none> Annotations: <none> Finalizers: [kubernetes.io/pvc-protection] Capacity: 1Gi Access Modes: RWO Events: <none>
Key Concept

Persistent storage in Kubernetes keeps your data safe and available even when containers stop, restart, or move.

Common Mistakes
Not creating a PersistentVolumeClaim before using storage in a pod.
Without a PVC, the pod has no guaranteed storage, so data will be lost when the pod restarts.
Always create and bind a PersistentVolumeClaim before mounting storage in your pod.
Using emptyDir volumes for data that must persist.
emptyDir storage is deleted when the pod stops, so data is lost.
Use PersistentVolumeClaims for any data that needs to survive pod restarts.
Summary
Create a PersistentVolumeClaim to reserve persistent storage in Kubernetes.
Check the PVC status to ensure storage is ready before using it.
Use persistent storage to keep data safe even if containers restart or move.