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

Explore

The Deployment wants 3, so its ReplicaSet keeps 3 Pods. Actual matches desired; the controller just keeps watching.

3
0
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

Compare

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.

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.

Who owns whom, Deployment, ReplicaSet, Pod, containerA Deployment owns one ReplicaSet per version of its Pod template, the current one scaled up and older ones kept at zero for rollback. Each ReplicaSet owns its Pods and keeps their count equal to replicas. Each Pod holds one or more containers that share its IP address and volumes. A StatefulSet owns its Pods directly, plus one PersistentVolumeClaim per Pod.Deployment ordersReplicaSet v2replicas 3ReplicaSet v1replicas 0, forrollbackPod ...-x2kfpPod ...-9qzrtcontainer orderssidecar containerStatefulSet dbPod db-0PVC data-db-0
Who owns whom, Deployment, ReplicaSet, Pod, containerA Deployment owns one ReplicaSet per version of its Pod template, the current one scaled up and older ones kept at zero for rollback. Each ReplicaSet owns its Pods and keeps their count equal to replicas. Each Pod holds one or more containers that share its IP address and volumes. A StatefulSet owns its Pods directly, plus one PersistentVolumeClaim per Pod.Deployment ordersReplicaSet v2replicas 3ReplicaSet v1replicas 0, forrollbackPod ...-x2kfpPod ...-9qzrtcontainer orderssidecar containerStatefulSet dbPod db-0PVC data-db-0
Who owns whom: Deployment, ReplicaSet, Pod, container

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 more
Delete a Pod, watch the ReplicaSet replace it under a new name
apiVersion: 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: 10Gi
A StatefulSet: stable names, ordered start, one volume per Pod
apiVersion: 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: {}
Two containers in one Pod share localhost and a volume
# 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 }
A bare Pod is never replaced; let a Deployment own it
Deleting a Pod, the ReplicaSet controller closes the gapYou delete one Pod through the API server. The kubelet stops its containers. The ReplicaSet controller, watching, sees 2 Pods where 3 are desired and creates a new Pod object. The scheduler binds it to a node and that node's kubelet starts it, so the count is back to 3.kubeletSchedulerReplicaSet controllerAPI serverYoudelete pod ...-4tvsjstop its containers2 of 3: create a Podbind ...-rkj6h to node-1...-rkj6h Running
Deleting a Pod, the ReplicaSet controller closes the gapYou delete one Pod through the API server. The kubelet stops its containers. The ReplicaSet controller, watching, sees 2 Pods where 3 are desired and creates a new Pod object. The scheduler binds it to a node and that node's kubelet starts it, so the count is back to 3.kubeletSchedulerReplicaSet controllerAPI serverYoudelete pod ...-4tvsjstop its containers2 of 3: create a Podbind ...-rkj6h to node-1...-rkj6h Running
Deleting a Pod: the ReplicaSet controller closes the gap

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.

  1. 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.
  2. 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.
  3. Use a StatefulSet. volumeClaimTemplates creates data-db-0 to data-db-2, one per Pod, and db-1 always reattaches to data-db-1. A headless Service gives db-0.db a stable DNS name for the primary.
  4. 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.
  5. Check. kubectl get pvc lists 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-1
One claim per Pod, and the same name and claim after a restart

How 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.

See it in the infographic: Deployment vs StatefulSet

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.

See it in the infographic: Deployment vs StatefulSet

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

1. A Deployment has 3 replicas. You delete one of its Pods. A few seconds later, what does kubectl get pods show?
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.
2. The StatefulSet db runs db-0, db-1 and db-2. You scale it to 2 replicas. What is removed?
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.
3. You run kubectl scale deployment orders --replicas=5 on a Deployment at 3. What creates the 2 new Pod objects?
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.