Configuring Persistent Volumes with Real-World Examples and Production Scenarios

KUBERNATES COURSE

Data is the heart of applications: databases, message queues, user-uploaded files, and essential logs must not disappear on a container restart or a node replacement. For this you need volumes that can outlive the app’s lifecycle, providing reliability, backups, and performance at production grade. In this guide you’ll find a practical path, with examples for Docker and Kubernetes environments, design tips, and operational advice to reduce risks and increase resilience.

Key concepts to know

Volumes vs ephemeral filesystems

A container is stateless by definition: its internal filesystem is ephemeral and can be recreated at any time. A volume is a separate storage area, mounted into the container, that persists independently of the running process. In Docker, distinguish between bind mounts (map a host directory), named volumes (managed by the Docker engine), and third-party drivers (NFS, cloud, Ceph). In Kubernetes you use specific objects: PersistentVolume (PV), PersistentVolumeClaim (PVC), and StorageClass to define capacity, access modes, and provisioning.

Access modes and drivers

Typical access modes are ReadWriteOnce (RWO, one node at a time), ReadOnlyMany (ROX), and ReadWriteMany (RWX, multiple nodes). The choice depends on the workload: a relational database often uses RWO, whereas content shared across multiple web replicas may require RWX. In Kubernetes, CSI (Container Storage Interface) drivers enable dynamic provisioning on major clouds (EBS/EFS on AWS, Persistent Disk/Filestore on GCP, Managed Disks/Azure Files on Azure) and on on-prem storage (NFS, Ceph, NetApp, Portworx, Longhorn).

Practical examples with Docker

Named volume for PostgreSQL

A first scenario: start a database and verify that data persists across container recreation.

docker volume create pgdata
docker run -d --name pg 
  -e POSTGRES_PASSWORD=SuperSecret123 
  -v pgdata:/var/lib/postgresql/data 
  -p 5432:5432 postgres:16

# Create a test table
docker exec -it pg psql -U postgres -c "CREATE TABLE demo(id INT PRIMARY KEY);"

# Remove the container and recreate it with the same volume
docker rm -f pg
docker run -d --name pg 
  -e POSTGRES_PASSWORD=SuperSecret123 
  -v pgdata:/var/lib/postgresql/data 
  -p 5432:5432 postgres:16

# Verify that the table exists
docker exec -it pg psql -U postgres -c "dt"
  

The pgdata volume survives container removal. In production, consider drivers that support snapshots and replication, or a managed NFS/filestore, and avoid non-orchestrated bind mounts on non-redundant hosts.

Docker Compose and quick backup

With Docker Compose you define volumes declaratively and can build simple backup tasks.

version: "3.9"
services:
  db:
    image: postgres:16
    environment:
      - POSTGRES_PASSWORD=SuperSecret123
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:
    driver: local
  

Example of a “hot” backup of the volume (for consistency, prefer logical backups such as pg_dump or storage-level snapshots):

docker run --rm 
  -v pgdata:/from 
  -v $(pwd):/to 
  alpine sh -c "cd /from && tar czf /to/pgdata-backup.tgz ."
  

Practical examples with Kubernetes

Dynamic provisioning on cloud

A modern approach uses StorageClass to create volumes on demand. Example on AWS with gp3, encrypted and with expansion enabled:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: gp3
provisioner: ebs.csi.aws.com
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
parameters:
  type: gp3
  encrypted: "true"
  fsType: ext4
  

The PVC requests capacity and access mode; provisioning happens when a Pod is scheduled, thanks to WaitForFirstConsumer aligning zone and volume.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pgdata
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: gp3
  resources:
    requests:
      storage: 20Gi
  

StatefulSet for databases

For stateful workloads, use a StatefulSet with volumeClaimTemplates so each replica gets its own volume.

apiVersion: v1
kind: Secret
metadata:
  name: pg-secret
type: Opaque
stringData:
  password: SuperSecret123
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
spec:
  serviceName: postgres
  selector:
    matchLabels:
      app: postgres
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      securityContext:
        fsGroup: 999
      containers:
      - name: postgres
        image: postgres:16
        env:
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: pg-secret
              key: password
        ports:
        - containerPort: 5432
        volumeMounts:
        - name: data
          mountPath: /var/lib/postgresql/data
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: gp3
      resources:
        requests:
          storage: 20Gi
  

The fsGroup field helps with permissions when the UID in the container does not match the one on the volume’s filesystem.

Sharing via NFS (RWX)

To share files among multiple Pods/replicas, one option is NFS or a managed file service (EFS/Filestore). Here is an example of static binding:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 100Gi
  accessModes: ["ReadWriteMany"]
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 10.0.0.10
    path: /export/data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
spec:
  accessModes: ["ReadWriteMany"]
  resources:
    requests:
      storage: 50Gi
  volumeName: nfs-pv
---
apiVersion: v1
kind: Pod
metadata:
  name: nfs-test
spec:
  containers:
  - name: app
    image: alpine
    command: ["sh","-c","sleep 3600"]
    volumeMounts:
    - name: shared
      mountPath: /data
  volumes:
  - name: shared
    persistentVolumeClaim:
      claimName: nfs-pvc
  

Real-world scenarios and architectural decisions

Transactional databases

For PostgreSQL/MySQL: prefer block volumes with guaranteed IOPS (EBS gp3/io2, PD Balanced/SSD). Keep RWO with a StatefulSet; enable periodic snapshots and logical backups (pg_dump, mysqldump, or native tools such as pgBackRest). Avoid RWX on NFS for databases except for specific, well-tested cases.

Web applications with shared content

For user-uploaded assets served by multiple replicas, use shared RWX storage (EFS/Filestore/Azure Files) or, better yet, object storage (S3/GCS) with a CDN. If you stick with a POSIX filesystem, provide local caches and invalidation policies to reduce latency.

Batch and data processing

For temporary jobs with large volumes, use ephemeral PVCs with a Delete policy, or temporary objects such as emptyDir for non-persistent data. If jobs crunch shared datasets, consider RWX read-only and output on RWO to reduce locks and contention.

Production operations

Backups, snapshots, and restore

Implement a 3-2-1 strategy: three copies, two media, one offsite. On Kubernetes, tools like Velero allow object backups and volume snapshots (with CSI integration). For application-consistent database backups, combine storage-level snapshots with writer freezes or with logical backups. Regularly test restores.

# Example of expanding a PVC (if the StorageClass allows it)
kubectl patch pvc pgdata -p '{"spec":{"resources":{"requests":{"storage":"40Gi"}}}}'
  

Data security

Enable encryption at rest (EBS, PD, Managed Disks) and in transit where applicable (NFS over TLS, signed SMB). Manage permissions with securityContext, fsGroup, and appropriate UID/GID; limit unnecessary privileges. Keep secrets in a Secret manager or KMS and apply retention policies and ReclaimPolicy=Retain to avoid accidental deletions.

Performance and reliability

Size IOPS/throughput based on the workload: monitor p95/p99 latency. Choose vendor-recommended filesystems (ext4 or xfs) and sensible mount options (noatime to reduce metadata writes, where appropriate). Use volumeBindingMode=WaitForFirstConsumer to align zone and volume, and avoid unsupported “cross-zone” setups. For multi-location HA, favor application-level replication (database replication) over high-latency synchronous shared storage.

Quick troubleshooting

Pod in Pending

Often due to zone constraints or lack of capacity. Check events and the StorageClass.

kubectl describe pvc pgdata
kubectl describe pod postgres-0
  

Permission denied on mount

Typical UID/GID mismatch. Set fsGroup in the Pod/StatefulSet spec, or use an initContainer to fix permissions.

# Example of permission fix (assess security impact)
chown -R 999:999 /var/lib/postgresql/data
  

Performance below expectations

Check the volume’s IOPS limits, network saturation (for RWX), filesystem metrics, and slow queries. On cloud, switch to classes with dedicated IOPS and reduce contention by sharing fewer workloads on the same volume.

Final checklist

– Choose the volume type based on the workload (RWO vs RWX, block vs file).

– Define a StorageClass with expansion, encryption, and correct binding.

– Set an appropriate ReclaimPolicy: Retain for critical data.

– Plan backups and restore tests, not just snapshots.

– Verify permissions and UID/GID; apply a securityContext.

– Monitor latency, IOPS, and throughput; adjust the storage class.

– Document migration and emergency procedures.

Next steps

Put this into practice: create a test environment, try volumes with a real database, configure backups, and simulate a restore. If you want a review of your architecture or help to go to production with confidence, contact me: we can assess storage, security, and performance for your specific case together.

Se vuoi farmi qualche richiesta o contattarmi per un aiuto riempi il seguente form

    Comments