DevFox Labs
HomeCoursesRoadmapsChallengesToolsAboutContact
beginner30 minby DevFoxUpdated on 1 Aug 2026

Persistent Storage for Your Data

Pods are disposable and disks are not. PersistentVolumes, claims, StorageClasses and access modes — enough to run something stateful without losing it.

  • Kubernetes
Persistent Storage for Your Data. Read this lesson at .

Previous lesson · 10 of 16ConfigMaps and SecretsNext lesson · 12 of 16Probes, Restarts and Self-Healing

On this page

  • The volumes that are not persistent
  • Three objects, and why
  • A claim, and a pod that uses it
  • Access modes, and the one that misleads
  • The mistake: replicas sharing one disk
  • Diagnosing a claim
  • Cheat sheet
  • Where to go next
DevFox Labs

Structured lessons and courses for developers who care about craft.

Platform

HomeCoursesRoadmapsAll lessonsChallengesAboutContactRSS feed

Legal

Terms of ServicePrivacy PolicyCookie Policy

© 2026 DevFox Labs. All rights reserved.

    A pod's filesystem dies with the pod, exactly as a container's did in Docker — and in Kubernetes pods are deleted far more casually. A node is drained, a rollout replaces every replica, a scheduler moves work around. Anything written inside is gone each time.

    So a database needs storage that is not part of the pod, and it needs that storage to follow the pod if it is rescheduled onto a different machine. By the end of this lesson you will know the three objects that make that work, why the indirection between them exists, and what Pending on a claim is telling you.

    The volumes that are not persistent

    Not every volume is about durability, and knowing the temporary ones first stops you reaching for a claim when you do not need one.

    emptyDir is created when the pod starts and deleted when it goes away. It exists so containers in one pod can share a directory — the sidecar in the pods lesson used it — and as scratch space:

    yaml
    volumes:
      - name: cache
        emptyDir: {}
      - name: scratch
        emptyDir:
          medium: Memory # a tmpfs, capped by the pod's memory limit

    configMap and secret volumes project configuration into the filesystem, as the last lesson covered. Read-only, and not storage.

    All three are tied to the pod's lifetime. For anything that must survive, you need the persistent set.

    Three objects, and why

    Persistent storage in Kubernetes is deliberately split in three, and the split is the concept worth learning.

    PersistentVolumeClaim

    What you write.

    A request: this much storage, with this access mode, of this class. Lives in a namespace, and a pod refers to it by name.

    "I need 10 GB that survives restarts" — and nothing about where it comes from.

    PersistentVolume

    The real storage.

    A cloud disk, an NFS export, a path on a node. Cluster-scoped, belonging to no namespace, and in practice created for you.

    StorageClass

    What the cluster can create on demand.

    Fast SSD, cheap spinning disk, replicated — and the driver that provisions it.

    You write the middle one. The cluster owns the other two.

    The middle object exists so that the same manifests work on your laptop and in production. A pod that named a disk directly could not do that.

    Static and dynamic provisioning

    Once, an administrator created PVs by hand and claims were matched against them — static provisioning. Now a StorageClass creates the PV when a claim appears — dynamic provisioning — so in practice you write claims and never write a PV. Static provisioning still turns up for pre-existing storage such as an NFS share.

    A claim, and a pod that uses it

    Look at what your cluster can provision:

    kind installs local-path, which allocates a directory on the node. Managed clusters give you the provider's disks — gp3 on EKS, standard-rwo on GKE.

    Then the claim:

    Pending here is expected with local-path, which waits until a pod actually uses the claim before deciding which node to put it on. Other provisioners bind immediately.

    Now mount it:

    Bound, with a PV created for it that you never wrote.

    Why PGDATA points at a subdirectory

    A freshly provisioned volume is often not empty — many drivers leave a lost+found directory — and Postgres refuses to initialise into a non-empty directory. Pointing PGDATA at a subdirectory of the mount avoids it. Most database images document an equivalent, and it is a real half-hour of confusion if you miss it.

    Then prove it works, which is the only way to know:

    Access modes, and the one that misleads

    An access mode says how many nodes may mount the volume, and it is a property of the underlying storage rather than a wish.

    ModeShortMeaning
    ReadWriteOnceRWOOne node may mount it read-write
    ReadOnlyManyROXMany nodes may mount it read-only
    ReadWriteManyRWXMany nodes may mount it read-write
    ReadWriteOncePodRWOPExactly one pod, cluster-wide

    ReadWriteOnce is the one that catches people, because "once" sounds like one pod and means one node. Two pods on the same node can share an RWO volume; two pods on different nodes cannot.

    And crucially: most block storage only supports RWO. Cloud disks attach to one machine at a time — that is a property of the hardware model, not a Kubernetes limitation. ReadWriteMany needs a networked filesystem: NFS, EFS, Azure Files, CephFS.

    That single fact explains the mistake below.

    The mistake: replicas sharing one disk

    Bad — three replicas, one claim, one RWO volume:

    Good — a StatefulSet, where each pod gets its own claim:

    The bad version fails in the most confusing way available: which failure you get depends on where the scheduler happened to put the pods.

    The pods land on different nodes

    A visible, stuck failure.

    Whichever pod is on the node holding the volume starts normally. The rest sit in ContainerCreating forever with a FailedAttachVolume event.

    Annoying, and at least it tells you.

    They all land on one node

    All three start, and corrupt the data.

    RWO means one node, so three pods on one node can all mount it — and three Postgres processes writing to one data directory destroy it.

    Nothing reports a problem until the data is already wrong.

    Same manifest, same cluster, two different outcomes decided by chance

    volumeClaimTemplates is the fix and the reason StatefulSets exist: one claim per pod, created automatically, named after the pod, and reattached to the same pod when it is rescheduled.

    Databases in Kubernetes are a decision

    A StatefulSet gives a database stable storage and stable names. It does not give it replication, failover, backups or a safe upgrade path — those are the database's own concerns, which is why operators like CloudNativePG and Zalando's Postgres operator exist. For anything with real data behind it, a managed database is very often the better engineering call, and using one does not make your cluster less legitimate.

    Diagnosing a claim

    A PVC stuck in Pending has a small set of causes, and describe names which one:

    MessageCause
    no storage class is setNo default class; name one explicitly
    WaitForFirstConsumerNormal — waiting for a pod to be scheduled
    ProvisioningFailedThe driver could not create it; read the text
    FailedAttachVolume on a podRWO volume wanted on a second node
    exceeded quotaA ResourceQuota caps storage in the namespace

    Two more things about the objects themselves.

    A PVC cannot usually shrink, and can often grow. If the StorageClass has allowVolumeExpansion: true, editing the claim's storage request resizes the volume in place. Shrinking is not supported by any common driver.

    Deleting a PVC may delete your data. The PV's persistentVolumeReclaimPolicy decides: Delete, which is the default for dynamically provisioned volumes, destroys the underlying disk; Retain keeps it for manual recovery.

    For anything you would be upset to lose, set Retain and take real backups. A PVC deleted by a careless kubectl delete -f is otherwise indistinguishable from a deliberate one.

    Cheat sheet

    Where to go next

    Your data survives its pod. What the cluster still cannot tell is whether a running pod is actually working — a wedged process looks identical to a healthy one, and traffic keeps arriving at it.

    Probes are the answer, and they are next. They also complete the Services story: readiness is what decides whether a pod appears in the endpoints list. Before moving on, run the durability test above yourself. Deleting a database pod and finding your table intact is the only evidence that counts.

    text
    Pod  ──mounts──>  PVC  ──bound to──>  PV  ──is──>  real disk
                       │                    ▲
                       └──── created by ────┘
                            StorageClass (dynamically)
    text
    NAME                 PROVISIONER             DEFAULT   AGE
    standard (default)   rancher.io/local-path   true      1h
    text
    NAME            STATUS    VOLUME     CAPACITY   ACCESS   AGE
    postgres-data   Pending                                  3s
    text
    NAME            STATUS   VOLUME       CAPACITY   ACCESS   AGE
    postgres-data   Bound    pvc-8f3c1a   8Gi        RWO      2m
    text
    Events:
      Warning  ProvisioningFailed  no persistent volumes available for
                                   this claim and no storage class is set
    bash
    kubectl get storageclass
    yaml
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: postgres-data
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 8Gi
      # storageClassName: standard   # omitted = the default class
    bash
    kubectl apply -f pvc.yaml
    kubectl get pvc
    yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: postgres
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: postgres
      template:
        metadata:
          labels:
            app: postgres
        spec:
          volumes:
            - name: data
              persistentVolumeClaim:
                claimName: postgres-data
          containers:
            - name: postgres
              image: postgres:16
              env:
                - name: POSTGRES_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: notes-secrets
                      key: DATABASE_PASSWORD
                - name: PGDATA
                  value: /var/lib/postgresql/data/pgdata
              volumeMounts:
                - name: data
                  mountPath: /var/lib/postgresql/data
    bash
    kubectl apply -f postgres.yaml
    kubectl get pvc
    bash
    kubectl exec deploy/postgres -- psql -U postgres \
      -c "CREATE TABLE notes (id serial, body text);"
    
    kubectl delete pod -l app=postgres        # the Deployment replaces it
    
    kubectl exec deploy/postgres -- psql -U postgres -c "\dt"
    # the table is still there
    yaml
    spec:
      replicas: 3
      template:
        spec:
          volumes:
            - name: data
              persistentVolumeClaim:
                claimName: postgres-data
    yaml
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: postgres
    spec:
      replicas: 3
      serviceName: postgres
      selector:
        matchLabels:
          app: postgres
      template:
        metadata:
          labels:
            app: postgres
        spec:
          containers:
            - name: postgres
              image: postgres:16
              volumeMounts:
                - name: data
                  mountPath: /var/lib/postgresql/data
      volumeClaimTemplates:
        - metadata:
            name: data
          spec:
            accessModes: [ReadWriteOnce]
            resources:
              requests:
                storage: 8Gi
    bash
    kubectl describe pvc postgres-data
    bash
    kubectl get pv     # look at the RECLAIM POLICY column
    kubectl patch pv pvc-8f3c1a \
      -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
    yaml
    # Ephemeral: dies with the pod
    volumes:
      - name: cache
        emptyDir: {}
      - name: scratch
        emptyDir: { medium: Memory } # tmpfs
    
    # Persistent: a claim, then mount it
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: postgres-data
    spec:
      accessModes: [ReadWriteOnce]
      resources:
        requests:
          storage: 8Gi
      storageClassName: standard # omit for the default class
    ---
    spec:
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: postgres-data
      containers:
        - name: postgres
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
    
    # More than one replica? One claim each, via a StatefulSet
    volumeClaimTemplates:
      - metadata: { name: data }
        spec:
          accessModes: [ReadWriteOnce]
          resources: { requests: { storage: 8Gi } }
    bash
    # What this cluster can provision
    kubectl get storageclass
    
    # The objects
    kubectl get pvc                       # namespaced: your requests
    kubectl get pv                        # cluster-wide: the real storage
    kubectl describe pvc postgres-data    # why it is Pending
    
    # Proving durability (the only real test)
    kubectl exec deploy/postgres -- psql -U postgres -c "\dt"
    kubectl delete pod -l app=postgres
    kubectl exec deploy/postgres -- psql -U postgres -c "\dt"
    
    # Access modes — "Once" means one NODE, not one pod
    #   ReadWriteOnce     RWO   one node read-write   (most block storage)
    #   ReadOnlyMany      ROX   many nodes read-only
    #   ReadWriteMany     RWX   many nodes read-write (needs NFS/EFS/...)
    #   ReadWriteOncePod  RWOP  exactly one pod
    
    # Growing a volume (if the class allows it)
    kubectl patch pvc postgres-data \
      -p '{"spec":{"resources":{"requests":{"storage":"16Gi"}}}}'
    
    # Keeping the disk when the claim is deleted
    kubectl get pv                        # check RECLAIM POLICY
    kubectl patch pv pvc-8f3c1a \
      -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'