Pods, ReplicaSets and Deployments
Mid-level Always asked 7 min
On this page
The question
What is the difference between a Pod and a container?
30-second answer
A container is one isolated process from one image. A Pod is the smallest thing Kubernetes schedules: a group of one or more containers that always run together on the same node and share a network namespace, so one IP address and localhost, plus the volumes the Pod declares. Usually a Pod holds one application container, sometimes with helpers, such as an init container that runs first or a sidecar that ships logs or proxies traffic. Pods are disposable; controllers replace them.
Understand it
Scale replicas, kill Pods, watch reconciliation
ExploreThe Deployment wants 3, so its ReplicaSet keeps 3 Pods. Actual matches desired; the controller just keeps watching.
View as text
A Deployment named orders above the ReplicaSet it owns, which shows desired, current and ready replicas. Below, one box per Pod with its generated name: running, terminating, crossed out, or a new one being created.
The Deployment wants 3, so its ReplicaSet keeps 3 Pods. Actual matches desired; the controller just keeps watching.
You delete a Pod. The ReplicaSet counts fewer than 3 and, within a second, creates a replacement, starting while the old ones terminate.
Back to 3. The replacements have new names and new IPs, and keep nothing of the deleted Pods: anything stored inside them is gone.
The Deployment wants 5, so its ReplicaSet keeps 5 Pods. Actual matches desired; the controller just keeps watching.
You delete 3 Pods. The ReplicaSet counts fewer than 5 and, within a second, creates 3 replacements, starting while the old ones terminate.
A Deployment named orders above the ReplicaSet it owns, which shows desired, current and ready replicas. Below, one box per Pod with its generated name: running, terminating, crossed out, or a new one being created.
The Deployment wants 3, so its ReplicaSet keeps 3 Pods. Actual matches desired; the controller just keeps watching.
You delete a Pod. The ReplicaSet counts fewer than 3 and, within a second, creates a replacement, starting while the old ones terminate.
Back to 3. The replacements have new names and new IPs, and keep nothing of the deleted Pods: anything stored inside them is gone.
The Deployment wants 5, so its ReplicaSet keeps 5 Pods. Actual matches desired; the controller just keeps watching.
You delete 3 Pods. The ReplicaSet counts fewer than 5 and, within a second, creates 3 replacements, starting while the old ones terminate.
Takeaway: A Deployment owns a ReplicaSet, and the ReplicaSet keeps the number of Pods equal to replicas. Delete a Pod and a new one, with a new name and IP, replaces it. Change replicas and it adds or removes Pods.
Deployment vs StatefulSet
Compareorders as a Deployment: identical, stateless Pods with random names. A replacement is a stranger, they all start together, and callers reach them only through the Service.
View as text
Four facts, names, storage, start order and address, above three Pods drawn side by side, each with the storage attached to it.
Deployment: Names: random, new each time. Storage: shared or none. Start: all at once. Address: the Service only. orders as a Deployment: identical, stateless Pods with random names. A replacement is a stranger, they all start together, and callers reach them only through the Service.
StatefulSet: Names: db-0, db-1, db-2, kept. Storage: one PVC per Pod, kept. Start: 0, then 1, then 2. Address: db-1.db, per Pod. db as a StatefulSet: Pods numbered from 0, started in order, each with its own PVC (PersistentVolumeClaim) and DNS name. A replaced db-1 comes back as db-1, on data-db-1.
Four facts, names, storage, start order and address, above three Pods drawn side by side, each with the storage attached to it.
Deployment: Names: random, new each time. Storage: shared or none. Start: all at once. Address: the Service only. orders as a Deployment: identical, stateless Pods with random names. A replacement is a stranger, they all start together, and callers reach them only through the Service.
StatefulSet: Names: db-0, db-1, db-2, kept. Storage: one PVC per Pod, kept. Start: 0, then 1, then 2. Address: db-1.db, per Pod. db as a StatefulSet: Pods numbered from 0, started in order, each with its own PVC (PersistentVolumeClaim) and DNS name. A replaced db-1 comes back as db-1, on data-db-1.
Takeaway: A Deployment runs interchangeable Pods: random names, shared or no storage, started together. A StatefulSet gives each Pod a stable name, its own volume and a DNS name, and starts and stops them in order.
In code
kubectl get pods -l app=orders -o wide
# orders-56fff5d9f5-4tvsj 1/1 Running 10.244.0.6
kubectl delete pod orders-56fff5d9f5-4tvsj
kubectl get pods -l app=orders -o wide
# orders-56fff5d9f5-rkj6h 1/1 Running 10.244.0.8 <- new name, new IP
kubectl get rs -l app=orders
# orders-56fff5d9f5 DESIRED 3 CURRENT 3 READY 3
kubectl scale deployment orders --replicas=5 # the ReplicaSet creates two moreapiVersion: apps/v1
kind: StatefulSet
metadata:
name: db
spec:
serviceName: db # a headless Service: clusterIP None, gives db-0.db, db-1.db
replicas: 3
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: postgres
image: postgres:17
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates: # one PVC (PersistentVolumeClaim) per Pod: data-db-0, data-db-1...
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 10GiapiVersion: v1
kind: Pod
metadata:
name: orders
spec:
initContainers:
- name: wait-for-db # runs to completion before the app starts
image: busybox:1.37
command: ["sh", "-c", "until nc -z db-0.db 5432; do sleep 2; done"]
containers:
- name: orders
image: registry.example.com/orders:1.4.2
volumeMounts: [{ name: logs, mountPath: /logs }]
- name: log-shipper # a sidecar: reads the same files, reaches orders on localhost
image: fluent/fluent-bit:3.2
volumeMounts: [{ name: logs, mountPath: /logs }]
volumes:
- name: logs
emptyDir: {}# Deleted, evicted or on a failed node: gone for good
Wrong kind: Pod
Wrong metadata: { name: orders }
# The ReplicaSet behind it recreates any Pod that disappears
Right kind: Deployment
Right metadata: { name: orders }
Right spec: { replicas: 3 }Solve it
Run PostgreSQL with 3 replicas: Deployment or StatefulSet?
A team deploys PostgreSQL as a Deployment with 3 replicas and one PersistentVolumeClaim. Two Pods stay in ContainerCreating with a Multi-Attach error, and after a node restart the database’s data seems to have moved. Explain what went wrong and design it properly.
- Read the error. A ReadWriteOnce volume attaches to one node at a time. A Deployment gives every replica the same claim, so only Pods on that node can mount it, and the others hang in ContainerCreating.
- See the deeper problem. Even on one node, three PostgreSQL servers writing one data directory corrupt it. Each replica needs its own disk and a stable identity: primary or standby, not interchangeable copies.
- Use a StatefulSet.
volumeClaimTemplatescreatesdata-db-0todata-db-2, one per Pod, and db-1 always reattaches to data-db-1. A headless Service givesdb-0.dba stable DNS name for the primary. - Do not stop there. A StatefulSet gives identity, not replication. Use PostgreSQL streaming replication through an operator, such as CloudNativePG, or a managed database, which also handles failover and backups.
- Check.
kubectl get pvclists one claim per Pod, all Bound; deleting db-1 brings back db-1 on the same claim; and the replicas report streaming from the primary.
kubectl apply -f db-statefulset.yaml
kubectl get pvc
# data-db-0 Bound 10Gi RWO
# data-db-1 Bound 10Gi RWO
# data-db-2 Bound 10Gi RWO
kubectl delete pod db-1
kubectl get pod db-1 -o jsonpath='{.spec.volumes[0].persistentVolumeClaim.claimName}' # data-db-1How to answer
- Containers in a Pod reach each other on localhost and must not share a port.
- Kubernetes schedules, scales and restarts Pods, not single containers.
- A Pod gets a new IP every time it is recreated, so callers use a Service.
- Scale an app by adding Pods, not containers inside one Pod.
Common mistakes
- Putting the application and its database in one Pod, so they cannot scale or fail separately.
- Treating a Pod as a long-lived server whose name and IP stay the same.
Follow-up questions
What is the difference between a Deployment and a ReplicaSet?
A ReplicaSet does one thing: keep a given number of identical Pods, matched by a label selector, running. A Deployment manages ReplicaSets to add rollouts. When you change its Pod template, such as the image tag, it creates a new ReplicaSet and shifts Pods from the old one to the new one step by step, keeping old ReplicaSets at zero replicas so you can roll back. You create Deployments; you almost never create a ReplicaSet yourself.
- The ReplicaSet name carries a hash of the Pod template, orders-56fff5d9f5.
- Scaling a Deployment changes the replicas of its current ReplicaSet.
- revisionHistoryLimit, 10 by default, sets how many old ReplicaSets are kept.
See it in the infographic: Scale replicas, kill Pods, watch reconciliation
When would you use a StatefulSet instead of a Deployment?
When each replica needs an identity of its own: databases, Kafka brokers, ZooKeeper or Elasticsearch nodes. A StatefulSet names its Pods db-0, db-1, db-2 and keeps those names across restarts, gives each its own PersistentVolumeClaim from volumeClaimTemplates that follows it to any node, gives each a stable DNS (Domain Name System) name through a headless Service, and starts, updates and stops them in order. Stateless services, where any replica can serve any request, belong in a Deployment.
- Scale-down removes the highest ordinal first and keeps its PVC (PersistentVolumeClaim).
- Pod management policy Parallel drops the ordered start when it is not needed.
- A StatefulSet does not replicate data; the application or an operator must.
What are init containers and sidecar containers?
Init containers run one after another, each to completion, before the app containers start: waiting for a dependency, running a migration or fetching configuration into a shared volume. If one fails, the kubelet retries it and the app never starts. Sidecars run beside the app for its whole life, sharing its network and volumes: log shippers, service mesh proxies, metrics adapters. Since Kubernetes 1.29 a sidecar is declared as an init container with restartPolicy: Always, so it starts first and stops last.
- Init containers keep setup tools out of the application image.
- Native sidecars do not block a Job from completing.
- Every container in the Pod counts toward its resource requests.
See it in the infographic: Scale replicas, kill Pods, watch reconciliation
You delete a Pod that belongs to a Deployment. What happens?
The Pod is sent SIGTERM, given its grace period, 30 seconds by default, and removed. Meanwhile its ReplicaSet controller sees one Pod fewer than desired and creates a replacement at once; the scheduler places it, possibly on another node. The new Pod has a new name, a new IP and an empty filesystem, apart from volumes it mounts. To really remove it, scale the Deployment down or delete the Deployment.
- Deleting Pods by hand is a quick restart; kubectl rollout restart does it for all Pods, gradually.
- Anything written inside the old container is lost.
- The Service stops sending traffic to the old Pod as soon as it starts terminating.
See it in the infographic: Scale replicas, kill Pods, watch reconciliation
Besides Deployments and StatefulSets, which workload types does Kubernetes offer, and when do you use them?
A DaemonSet runs one Pod on every node, or every matching node, for node agents such as log collectors, monitoring exporters and the network plugin. A Job runs Pods until a task completes successfully, with retries, such as a migration or a batch import. A CronJob creates Jobs on a schedule, such as a nightly report. A bare Pod is for experiments only, because nothing recreates it.
- Jobs set completions, parallelism and backoffLimit.
- CronJobs need concurrencyPolicy to avoid overlapping runs.
- DaemonSets follow new nodes automatically.
When do two containers belong in one Pod?
When they must live and die together on the same machine and share files or localhost: a log shipper reading the app's log volume, a service mesh proxy intercepting its traffic, or an adapter exposing its metrics. If they could scale separately or be updated on their own schedule, they belong in separate Pods.
Check yourself
Show answers
- 3 Pods, one with a new name: Correct. The ReplicaSet recreates the missing Pod, under a newly generated name.
- 2 Pods until you scale up again: Incorrect. replicas still says 3, so the controller closes the gap by itself.
- 3 Pods with the same names as before: Incorrect. Only StatefulSet Pods keep their names.
Show answers
- The Pod db-2; its claim data-db-2 stays: Correct. The highest ordinal goes first, and its PVC is kept by default.
- The Pod db-2 and its claim data-db-2: Incorrect. Claims stay unless the retention policy says Delete.
- A random Pod and its claim: Incorrect. StatefulSets remove Pods in reverse order, from the highest number.
Show answers
- The ReplicaSet controller, for the current ReplicaSet: Correct. The Deployment sets the ReplicaSet's replicas; the ReplicaSet controller creates Pods.
- The Deployment controller, directly: Incorrect. The Deployment controller manages ReplicaSets, not Pods.
- The scheduler: Incorrect. The scheduler only binds Pods that already exist to nodes.