Sub topic 1
Why some workloads need more than a Deployment
Prerequisites
This chapter assumes you can:
- Read and apply YAML manifests with
kubectl. - Describe Pods, Deployments, Services, and namespaces.
- Explain what a container volume is.
You don't need to know PersistentVolumes and PersistentVolumeClaims yet. A later chapter reviews them.
What a Deployment assumes
A Deployment manages a set of Pods that are interchangeable. Kubernetes gives each Pod a random suffix, such as web-6d4cf56db6-x2k9q. It starts them in any order, and it replaces a failed Pod with a new one that has a new name and a new IP address. The Pods share one template, so any replica can serve any request.
This model works well for stateless workloads such as web front ends, API servers, and workers that read from a queue. If a replica disappears, no one notices, because it held nothing that the others lack.
Many workloads break this assumption. Consider a three-node database cluster:
- One node is the primary and the others are replicas. Each node must know which role it has.
- Each node keeps its own data on disk. Node 1's data is not interchangeable with node 2's.
- Nodes find each other by address. If addresses change on every restart, the cluster configuration goes stale.
- Nodes often must start, stop, and upgrade in a specific order.
Four things a Deployment does not give you
| Need | Deployment behavior | Consequence |
|---|---|---|
| Stable name | Random Pod names that change on replacement | Peers cannot refer to a specific member |
| Stable network identity | No per-Pod DNS record. The Service load-balances across Pods | You cannot address one replica directly |
| Per-Pod storage | All replicas reference the same volume spec | Pods share one volume or lose data on restart |
| Ordered operations | Pods are created and updated in parallel | A replica may start before the primary it depends on |
The storage row is the one that surprises people. Suppose a Deployment with three replicas mounts one PersistentVolumeClaim. With a ReadWriteOnce volume, the volume can be attached to only one node at a time. Replicas scheduled on other nodes stay stuck in ContainerCreating. If all three do mount it, they write into the same directory, which most databases cannot tolerate. You cannot give each replica its own claim by editing the Deployment, because the template is identical for every Pod.
What a StatefulSet changes
A StatefulSet is a workload controller like a Deployment, but it treats each Pod as an individual. It gives you four guarantees that match the table above:
- Each Pod gets a fixed name made of the StatefulSet name and an ordinal, for example
web-0,web-1,web-2. - Each Pod gets a stable DNS name through a headless Service.
- Each Pod gets its own PersistentVolumeClaim, created from a template.
- Pods are created, scaled, and updated in order by default.
When web-1 is rescheduled, the replacement is still called web-1. It mounts the same claim, so it finds the data the old web-1 wrote. The identity belongs to the ordinal, not to a particular Pod object or node.
State kept in an external service, such as a managed database, does not count. A Deployment can use it without problems.
Seeing the difference
Create a Deployment and a StatefulSet with the same label and compare the Pod names:
kubectl get pods -l app=webThe Deployment lists names with random suffixes. The StatefulSet lists web-0, web-1, web-2. Delete one Pod from each. The Deployment's replacement gets a new random name. The StatefulSet's replacement reuses the old name.
Common mistakes
- Using a StatefulSet for a stateless app. You get slower rollouts and extra storage to manage, with no benefit.
- Using a Deployment with a shared volume for a clustered database. It works with one replica, then fails when you scale.
- Assuming a StatefulSet replicates data. It does not. It provides identity and storage. Replication is the job of the application, such as PostgreSQL streaming replication or etcd's Raft protocol.
- Assuming a StatefulSet gives high availability by itself. Without correct application-level replication, three Pods are three independent copies of nothing.
The next chapters build the pieces in this order: the StatefulSet object, the headless Service, stable DNS names, and then per-Pod storage.
Exercise
A team runs a three-replica Deployment of a database. All Pods mount one ReadWriteOnce PersistentVolumeClaim. Two Pods stay in ContainerCreating. Name two reasons this design is wrong for a clustered database, and name the workload type that fits better.
Show answer
First, a ReadWriteOnce volume attaches to only one node at a time, so Pods on other nodes cannot mount it. Second, even if all Pods could mount it, they would write to the same directory, and each database member needs its own data. A Deployment also gives Pods random names, so peers cannot address a specific member. A StatefulSet fits better because it gives each Pod a stable name and its own PersistentVolumeClaim.