Sobes.tech
Middle+

What is the difference between working with Persistent Volumes (PV) in deployment and in StatefulSet?

sobes.tech AI

Answer from AI

During deployment, PVs are used either as statically or dynamically created volumes, which can be bound to any of the deployment's pods. When pods are restarted or scaled, they may access different PVs (if there are multiple and they are not bound to a specific pod via nodeSelector). Data on PVs remains, but the connection between a specific pod and PV is not guaranteed after the pod is recreated.

StatefulSet, on the other hand, is designed for stateful applications and provides a stable identity for each pod, including a stable network name and identifier. It uses volumeClaimTemplates to automatically create PersistentVolumeClaims (PVCs) for each pod, with each PVC and its associated PV having a unique binding to a specific pod (by index). This guarantees that when a pod is restarted, it will use the same PV.

Key differences:

Characteristic Deployment StatefulSet
Pod-PV connection Not guaranteed after recreation Guaranteed (by pod index)
PV/PVC management Manual (static/dynamic PVC) Automatic via volumeClaimTemplates
Number of PV/PVC One or multiple, shared One PV/PVC per pod
Usage Stateless applications Stateful applications, databases
Stable identity Not present Present (index, network name)

Example of volumeClaimTemplates in StatefulSet:

# volumeClaimTemplates for StatefulSet
volumeClaimTemplates:
  - metadata:
      name: data # PVC template name
    spec:
      accessModes:
        - ReadWriteOnce # access mode
      resources:
        requests:
          storage: 1Gi # requested storage size