Kubernetes Architecture··2 min read

Guaranteeing Database Persistence on Kubernetes: StatefulSets, PVCs, and Safe Node Drain Strategies

Shafikul Islam

Shafikul Islam

Backend & Cloud Infrastructure Engineer · Infrastructure & Cloud Team Lead

Guaranteeing Database Persistence on Kubernetes: StatefulSets, PVCs, and Safe Node Drain Strategies

Why Stateless Deployments Corrupt Stateful Workloads

A standard Kubernetes Deployment treats pods as interchangeable cattle. During a rolling update or node drain, Kubernetes can spin up a new pod before the old pod has cleanly flushed its write-ahead log (WAL) to disk, or delete a dynamically provisioned volume if a PersistentVolumeClaim (PVC) is accidentally removed.

When running stateful PostgreSQL and Redis clusters on Kubernetes, four architectural safeguards are mandatory.


1. Custom StorageClass with reclaimPolicy: Retain and WaitForFirstConsumer

The default Kubernetes StorageClass in most cloud providers sets reclaimPolicy: Delete. If a Helm chart uninstall or operator bug deletes the PVC, the underlying block storage volume is permanently wiped.

We always define a dedicated StorageClass for stateful data with reclaimPolicy: Retain and volumeBindingMode: WaitForFirstConsumer (ensuring the EBS/CSI volume is provisioned in the exact same Availability Zone as the scheduled pod):

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: production-block-retain
provisioner: ebs.csi.aws.com
volumeBindingMode: WaitForFirstConsumer
reclaimPolicy: Retain
allowVolumeExpansion: true
parameters:
  type: gp3
  iops: "6000"
  throughput: "250"
  encrypted: "true"

2. Protecting Quorum During Node Upgrades with PodDisruptionBudgets

When draining worker nodes for Linux kernel patching (kubectl drain), Kubernetes will evict pods as fast as possible unless constrained by a PodDisruptionBudget (PDB).

For a 3-node replicated stateful cluster, we enforce maxUnavailable: 1 so node maintenance never takes down quorum:

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: postgres-ha-pdb
spec:
  maxUnavailable: 1
  selector:
    matchLabels:
      app: postgres-ha

Combined with continuous WAL-G streaming to encrypted S3 object storage, our recovery point objective (RPO) stays under 10 seconds even in a complete availability zone failure.

Infrastructure & PostgreSQL Advisory

Scaling Infrastructure or Diagnosing P95 Latency?

I help engineering teams diagnose PostgreSQL indexing bottlenecks, concurrency limits, and K3s/Docker deployments. Book a 20-minute call with me to review your setup.

Related Technical Articles