DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

Kubernetes & HelmLesson 12 of 16
Your progress · 0%
On this page
  1. Volumes
  2. StorageClasses and CSI
  3. PersistentVolumes and claims
  4. StatefulSets: one disk per replica
↖ Course roadmap
Lesson 12 · Storage

Storage: data that outlives Pods

Pods are disposable, data is not. Volumes, PersistentVolumeClaims, StorageClasses and the EBS CSI driver, then a StatefulSet where each replica keeps its own disk, all on EKS.

30 min read · intermediate · hands-on

By the end of this lesson you'll be able to

0 of 4 complete
Lab status

Verified lab: a PVC creating a real encrypted EBS gp3 volume, data surviving Pod deletion, and a StatefulSet with one volume per replica, on the course's EKS cluster. The output shown is real.
Code walkthrough: EFS for shared ReadWriteMany storage (the driver was installed, not used).
Cloud lab: EBS needs EKS; on kind the built-in standard class gives node-local volumes that work the same way for practice.

Everything so far was stateless: kill a shop-web Pod and the new one is identical. A database isn't like that. Its data has to survive restarts, rescheduling and node failures. This lesson gives Pods real disks.

Volumes

A container's own filesystem lives and dies with the container. A volume is storage mounted into a Pod. Some kinds are as short-lived as the Pod (emptyDir, a scratch directory shared by its containers) or carry config (configMap, secret, from lesson 6). For data that must outlive the Pod, the volume points at a PersistentVolumeClaim.

StorageClasses and CSI

Kubernetes doesn't make disks itself. A CSI driver (Container Storage Interface) talks to the storage system. The course cluster has two, both EKS add-ons:

$ kubectl get csidrivers
NAME              ATTACHREQUIRED   PODINFOONMOUNT   STORAGECAPACITY   TOKENREQUESTS   REQUIRESREPUBLISH   MODES        AGE
ebs.csi.aws.com   true             false            false             <unset>         false               Persistent   54m
efs.csi.aws.com   false            false            false             <unset>         false               Persistent   60m
kubectl get csidrivers
  • kubectltalk to a Kubernetes cluster
  • getlist objects

EBS gives block disks attached to one node at a time (ReadWriteOnce). EFS gives a shared filesystem many Pods can mount at once (ReadWriteMany). A StorageClass says which driver to use and how:

# 12-storage.yaml (part 1)
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: gp3
provisioner: ebs.csi.aws.com
parameters:
  type: gp3
  encrypted: 'true'
volumeBindingMode: WaitForFirstConsumer # make the disk where the Pod lands
reclaimPolicy: Delete # delete the disk when the claim is deleted
allowVolumeExpansion: true

WaitForFirstConsumer matters on AWS: an EBS volume exists in one availability zone, and a Pod can only use it from a node in that zone. Waiting until the Pod is scheduled means the disk is created in the right zone. reclaimPolicy: Retain keeps the disk after the claim is gone, safer for production data.

Here is the whole chain the lab triggered, in order:

PersistentVolumes and claims

A Pod asks for storage through a PersistentVolumeClaim (PVC): "1Gi, ReadWriteOnce, class gp3". The driver creates a PersistentVolume (PV), the real disk, and binds it to the claim:

# 12-storage.yaml (part 2)
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: notes
spec:
  accessModes: ['ReadWriteOnce']
  storageClassName: gp3
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: writer
spec:
  containers:
    - name: writer
      image: busybox:1.37
      command: ['sh', '-c', 'date >> /data/visits.log; cat /data/visits.log; sleep 3600']
      volumeMounts:
        - name: data
          mountPath: /data
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: notes
$ kubectl get pvc notes
NAME    STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
notes   Bound    pvc-b71c5d71-baa9-4c8e-81b7-494753c42518   1Gi        RWO            gp3            <unset>                 13s
$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM           STORAGECLASS   ...
pvc-b71c5d71-baa9-4c8e-81b7-494753c42518   1Gi        RWO            Delete           Bound    default/notes   gp3            ...
$ kubectl logs writer
Mon Nov  9 06:45:28 UTC 2026
kubectl get pvc notes
  • kubectltalk to a Kubernetes cluster
  • getlist objects
kubectl logs writer
  • logsshow a Pod’s output

That PV is a real EBS volume in the AWS account, created in 13 seconds:

$ aws ec2 describe-volumes --volume-ids vol-0a4cf6ce3523ead79 --query "Volumes[0].{id:VolumeId,size:Size,type:VolumeType,encrypted:Encrypted,az:AvailabilityZone,state:State}" --output table
|  az        |  ap-south-1a            |
|  encrypted |  True                   |
|  id        |  vol-0a4cf6ce3523ead79  |
|  size      |  1                      |
|  state     |  in-use                 |
|  type      |  gp3                    |
aws ec2 describe-volumes --volume-ids vol-0a4cf6ce3523ead79 --query "Volumes[0].{id:VolumeId,size:Size,type:VolumeType,encrypted:Encrypted,az:AvailabilityZone,state:State}" --output table
  • awsthe AWS command line
  • --querypick fields from the JSON response
  • --outputoutput format

Now the test that matters: delete the Pod and start a new one with the same claim.

$ kubectl delete pod writer
pod "writer" deleted from default namespace
$ kubectl apply -f 12-storage.yaml   # recreates the writer Pod
$ kubectl logs writer
Mon Nov  9 06:45:28 UTC 2026
Mon Nov  9 06:46:39 UTC 2026
kubectl delete pod writer
  • kubectltalk to a Kubernetes cluster
  • deletedelete an object
kubectl apply -f 12-storage.yaml
  • applycreate or update from a file
  • -ffrom this file
kubectl logs writer
  • logsshow a Pod’s output

The first line was written by the old Pod, the second by the new one. The data lives on the volume, which belongs to the claim, not to any Pod. Deleting the claim is what deletes the data (with reclaimPolicy: Delete), so treat kubectl delete pvc with the same care as dropping a database. And a volume isn't a backup: take EBS snapshots or use Velero.

StatefulSets: one disk per replica

A Deployment's replicas are interchangeable and would all share one claim. A database cluster needs each replica to have its own disk and a stable identity. A StatefulSet gives both:

# 12-statefulset.yaml (excerpt)
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: db
spec:
  serviceName: db # a headless Service gives each Pod a DNS name: db-0.db
  replicas: 2
  template:
    spec:
      containers:
        - name: db
          image: busybox:1.37
          command: ['sh', '-c', 'echo "I am $(hostname)" > /data/id; sleep 3600']
          volumeMounts:
            - name: data
              mountPath: /data
  volumeClaimTemplates: # one PVC per replica: data-db-0, data-db-1
    - metadata:
        name: data
      spec:
        accessModes: ['ReadWriteOnce']
        storageClassName: gp3
        resources:
          requests:
            storage: 1Gi
$ kubectl get pods -l app=db -o wide
NAME   READY   STATUS              RESTARTS   AGE   IP       NODE
db-0   0/1     ContainerCreating   0          10s   <none>   ip-10-42-28-149.ap-south-1.compute.internal
$ kubectl get pvc
NAME        STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   ...
data-db-0   Bound     pvc-2b615ae0-ad35-4d89-8c27-d51c249b25a1   1Gi        RWO            gp3            ...
data-db-1   Pending                                                                        gp3            ...
$ kubectl exec db-0 -- cat /data/id
I am db-0
$ kubectl exec db-1 -- cat /data/id
I am db-1
$ kubectl delete pod db-0
pod "db-0" deleted from default namespace
$ kubectl exec db-0 -- cat /data/id
I am db-0
kubectl get pods -l app=db -o wide
  • kubectltalk to a Kubernetes cluster
  • getlist objects
  • -lonly objects with this label
  • -ooutput format
kubectl delete pod db-0
  • deletedelete an object

Three things to notice:

  1. Pods start in order: db-1 isn't created until db-0 is ready, which is why data-db-1 was still Pending (waiting for its Pod, because of WaitForFirstConsumer).
  2. Each replica got its own claim, named after it.
  3. The replacement db-0 came back with the same name and the same disk, so it still said "I am db-0".

Deleting a StatefulSet does not delete its claims. The lab removed them with kubectl delete pvc -l app=db afterwards, otherwise the EBS volumes keep costing money. In production, most teams run databases on a managed service (RDS) and keep StatefulSets for things like Kafka, Elasticsearch or caches.

⌘
Mini mission

Prove the data survives

Create the gp3 StorageClass, a 1Gi claim and the writer Pod. Delete and recreate the Pod twice and record the log. Then run the two-replica StatefulSet, record each replica's /data/id, delete db-0 and record it again. Clean up the claims at the end.

Input: 12-storage.yaml and 12-statefulset.yamlOutput: ~/storage.txt
Reveal one safe solution
$ kubectl apply -f 12-storage.yaml && kubectl wait --for=condition=Ready pod/writer --timeout=120s
$ for i in 1 2; do kubectl delete pod writer; kubectl apply -f 12-storage.yaml; kubectl wait --for=condition=Ready pod/writer --timeout=120s; done
$ kubectl logs writer > ~/storage.txt
$ kubectl apply -f 12-statefulset.yaml && kubectl rollout status statefulset/db
$ for p in db-0 db-1; do kubectl exec $p -- cat /data/id; done >> ~/storage.txt
$ kubectl delete pod db-0 && kubectl wait --for=condition=Ready pod/db-0 --timeout=120s
$ kubectl exec db-0 -- cat /data/id >> ~/storage.txt
$ kubectl delete -f 12-statefulset.yaml; kubectl delete pvc -l app=db; kubectl delete pod writer; kubectl delete pvc notes
kubectl apply -f 12-storage.yaml && kubectl wait --for=condition=Ready pod/writer --timeout=120s
  • kubectltalk to a Kubernetes cluster
  • applycreate or update from a file
  • -ffrom this file
  • &&run the next command only if this one worked
for i in 1 2; do kubectl delete pod writer; kubectl apply -f 12-storage.yaml; kubectl wait --for=condition=Ready pod/writer --timeout=120s; done
  • forloop over a list
  • dostart of what the loop runs each time
  • deletedelete an object
  • doneend of the loop
kubectl logs writer > ~/storage.txt
  • logsshow a Pod’s output
  • >write output to a file, replacing it
kubectl apply -f 12-statefulset.yaml && kubectl rollout status statefulset/db
  • rolloutmanage a rollout
for p in db-0 db-1; do kubectl exec $p -- cat /data/id; done >> ~/storage.txt
  • >>append output to a file
kubectl delete -f 12-statefulset.yaml; kubectl delete pvc -l app=db; kubectl delete pod writer; kubectl delete pvc notes
  • -lonly objects with this label

The writer log should have three timestamps. On kind, change storageClassName to standard.

Try it yourself

0 of 5 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 3

You delete a Pod that wrote to a PersistentVolumeClaim and create a new Pod with the same claim. What does it see?

Go deeper

Official reading for this lesson

  • DocsPersistent volumesKubernetes ↗
  • DocsStorage classesKubernetes ↗
  • DocsStatefulSetsKubernetes ↗
  • DocsAmazon EBS CSI driverAWS Docs ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

← PreviousScheduling: where Pods runNext lesson →Helm: packaging Kubernetes apps