Comparing StatefulSet and Deployment

350 단어·2 분·원문(.md)

CategoryDeploymentStatefulSet
Primary PurposeStateless ApplicationsStateful Applications
Typical Use CasesWeb servers, API servers, batch workersDB, Kafka, Redis, ZooKeeper

Pod Identity #

In a Deployment, all Pods are considered identical, their names are random, and replacing one with another makes no difference.

However, StatefulSet Pods each have a unique and stable identity, and their names are preserved (even after restart), which is crucial for cluster configuration.

Storage #

Even when using PVCs with a Deployment, a 1:1 guarantee with Pods is not provided, and a different volume might be attached when a Pod is recreated. The application is responsible for data consistency.

StatefulSet ensures data stability by providing a dedicated PVC for each Pod, reusing the same volume when a Pod is recreated after deletion.

Scaling Method #

Deployment is fast for parallel scaling and has no concept of order.

StatefulSet creates Pods sequentially (e.g., db-0, db-1, db-2) and terminates them in reverse order (e.g., db-2, db-1, db-0). It prioritizes cluster stability.

CategoryDeploymentStatefulSet
Rolling UpdateDefaultSupported
Update OrderRandom, Pod-by-PodGuaranteed Pod Number Order
Risk of Destructive UpdatesLowCan lead to cluster collapse if mishandled

Networking #

Deployment uses a regular Service and does not concern itself with Pod IP changes.

StatefulSet requires a Headless Service and provides a stable DNS for each Pod, e.g., db-0.db.default.svc.cluster.local. This is essential for systems requiring direct communication between nodes.

Disaster Recovery Perspective #

When a Pod fails in a Deployment, it is replaced by another Pod, which is optimized for request-level recovery.

However, if a Pod fails in a StatefulSet, it must be recovered with the same Pod number and the same volume to maintain data consistency.

Cases where Deployment should be used:

  • Stateless API Server
  • Traffic handling is critical
  • No distinction between Pods is meaningful
  • Fast rolling deployments, etc.

In the case of StatefulSet:

  • Data must reside on local disk
  • Pod order / name is important
  • Master-follower architecture
  • When direct communication between nodes is required

Deployment = For traffic processing StatefulSet = For data persistence

This can be summarized as follows.

SRE/question/q_11.md