Persistent Volume
StatefulSet
Kubernetes
Data Recovery
Cluster Architecture

What happens to persistent volume if the StatefulSet got deleted and re-created?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

StatefulSets are a Kubernetes workload API object that manage stateful applications. They provide unique identities to their pods comprising stable, unique network identifiers, persistent storage, and orderly deployment and scaling. When using a StatefulSet, the associated PersistentVolumes often store critical data insights. Therefore, understanding the behavior of these PersistentVolumes upon deletion and recreation of a StatefulSet is crucial for managing application state and ensuring data durability.

What is a Persistent Volume?

A Persistent Volume (PV) in Kubernetes is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. It is independent of the normal lifecycle of a pod that uses the PV, which ensures data persistence across pod restarts and failures.

Persistent Volume Claims

A Persistent Volume Claim (PVC) is a request for storage by a user. It is similar to a pod in that pods consume node resources, while a PVC consumes PV resources. PVCs can request specific size and access modes (such as read/write or read-only).

StatefulSet and Persistent Volume Interaction

When a StatefulSet is created, typically, each pod in the set requires one or more PersistentVolumeClaims which in turn are bound to corresponding PersistentVolumes. This binding ensures that the data stored on these volumes remains intact even if the pods themselves are deleted or moved around.

What Happens to Persistent Volumes When a StatefulSet is Deleted?

When a StatefulSet is deleted:

  • The StatefulSet itself is removed.
  • Pods managed by the StatefulSet are deleted.
  • However, the PVCs (and by extension the PVs) are not automatically deleted.

The retention of PVCs and PVs is crucial because it provides a safety net against accidental data loss from operations such as deleting the StatefulSet. The default behavior can be altered by changing the persistentVolumeReclaimPolicy in the PV definition or setting owner references, but by default, Kubernetes aims to protect against data loss.

Recreating a StatefulSet

When a StatefulSet is re-created after deletion:

  • New pods are created according to the StatefulSet specifications.
  • These new pods will attempt to bind to the existing PVCs.
  • If the identifiers and the number of replicas remain the same, the new pods will reattach to the same PersistentVolumes, thereby accessing the same data as before.

This behavior underscores the "statefulness" of this type of workload - even over deletions and recreation of the managing workload API objects.

Benefits and Risks

Benefits:

  • Data Durability: Data is not lost even if the StatefulSet is accidentally deleted.
  • Statefulness: The system preserves the state of applications across pod recreation.

Risks:

  • Resource Orphaning: Unused PVCs and PVs can continue to consume resources if not manually cleaned up.
  • Configuration Drift: If configurations are changed between deletions and recreation, unexpected behaviors might derive from old data persisted in PVCs.

Best Practices

  • Regular Backups: Automatically or manually, ensure backups for PersistentVolumes are done periodically.
  • PVC and PV Management: Monitor and manage orphaned PVCs and PVs to reclaim resources and prevent quota exhaustion.
  • Immutable Configurations: Use immutable configurations where possible to prevent mismatches between deployments.
OperationEffect on PV/PVCData Safety
Delete StatefulSetNo impactData retained
Recreate StatefulSetBinds to existing PVCsData reused

Conclusion

The design of PersistentVolumes and PersistentVolumeClaims in Kubernetes caters directly to scenarios where data retention is crucial. By not deleting PVs and PVCs with the deletion of a StatefulSet, Kubernetes provides a robust mechanism to manage stateful applications robustly and resiliently, ensuring that critical data persists beyond the lifecycle of any individual pod or controller object. Understanding and properly managing these components are key to harnessing the full power of Kubernetes stateful capabilities.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.