Pods, ReplicaSets, and Deployments: The Three-Layer Workload Model

Kubernetes deployment basics: pods, ReplicaSets, labels, revisions, probes and graceful shutdown, and how they fit together.

Executive Summary: Kubernetes runs stateless applications through three nested objects. A Pod is one or more containers that share a network identity and volumes, scheduled together on one node, and it is disposable. A ReplicaSet keeps a set number of identical pods running by matching labels. A Deployment manages ReplicaSets over time, creating a new one for each change to the pod template so it can roll out and roll back. You write Deployments; Kubernetes writes the other two. Add probes so Kubernetes knows when a pod is ready and when it is stuck, and handle SIGTERM so pods shut down cleanly.
Isometric nested glass boxes showing a Deployment containing a ReplicaSet that holds three colourful pods

A developer deletes a misbehaving pod to restart it, and a new one appears within seconds with a different name. They edit that new pod’s image directly, and nothing changes. They scale the ReplicaSet to five, and it bounces back to three. Each surprise has the same cause: they are editing objects that another controller owns, and that controller is doing its job of putting things back.

This is the reconciliation pattern from the control plane article applied to workloads. Each layer owns the layer below it, so you change things at the top.

Layer 1: the Pod

A pod is the smallest thing Kubernetes schedules. Its containers:

  • share one network namespace, so one IP address, and reach each other on localhost;
  • can share volumes, for example a log directory or a socket;
  • are always placed on the same node and started and stopped together.

Most pods have one application container. Extra containers are for tightly coupled helpers. Init containers run to completion, in order, before the main containers start: waiting for a dependency, running a migration, rendering configuration. Sidecar containers run alongside the main container for its whole life, such as a log shipper or a service mesh proxy. Kubernetes 1.29 enabled native sidecar support by default (as a beta feature): an init container with restartPolicy: Always starts before the app, keeps running, and is stopped after it, which fixes old problems such as Jobs that never finished because a sidecar proxy kept running.

Pods are cattle. They get new names and new IP addresses when recreated, their local filesystem disappears, and they never move between nodes; a “moved” pod is really a new pod. That is why you almost never create bare pods. A bare pod on a node that fails is simply gone.

Layer 2: the ReplicaSet

A ReplicaSet has three things: a desired replica count, a label selector and a pod template. Its controller counts the pods that match the selector and creates or deletes pods to reach the desired count. Delete one of its pods and it creates a replacement; scale a ReplicaSet directly and its owning Deployment scales it back.

Labels are the glue. The ReplicaSet does not keep a list of its pods; it selects them. If you remove the matching label from a running pod, the ReplicaSet no longer counts it and starts a new one, while the relabeled pod keeps running outside its control. That is actually a useful trick for taking a misbehaving pod out of service to debug it without killing it.

You rarely create ReplicaSets yourself. Deployments create them.

Layer 3: the Deployment

A Deployment adds change management. Every time the pod template changes (a new image, a new environment variable, new resource values), the Deployment creates a new ReplicaSet and gradually shifts replicas from the old one to the new one. Old ReplicaSets are kept, scaled to zero, as revision history, which is what kubectl rollout undo uses.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: billing-api
  namespace: billing
  labels:
    app: billing-api
spec:
  replicas: 3
  revisionHistoryLimit: 5
  selector:
    matchLabels:
      app: billing-api
  template:
    metadata:
      labels:
        app: billing-api
    spec:
      terminationGracePeriodSeconds: 30
      containers:
        - name: api
          image: ghcr.io/acme/billing-api:1.9.0
          ports:
            - name: http
              containerPort: 8080
          resources:
            requests:
              cpu: 250m
              memory: 256Mi
            limits:
              memory: 512Mi
          startupProbe:
            httpGet: { path: /healthz, port: http }
            failureThreshold: 30
            periodSeconds: 2
          readinessProbe:
            httpGet: { path: /ready, port: http }
            periodSeconds: 5
          livenessProbe:
            httpGet: { path: /healthz, port: http }
            periodSeconds: 10
            failureThreshold: 3
kubectl apply -f deployment.yaml
kubectl get deploy,rs,pods -l app=billing-api
# deployment.apps/billing-api          3/3
# replicaset.apps/billing-api-7c9f8d6b  3   (the current template)
# pod/billing-api-7c9f8d6b-2xkqp        Running ...

The ReplicaSet name has a hash of the pod template appended, and pod names add another random suffix. The ownership chain is recorded in each object’s metadata.ownerReferences, and deleting the Deployment deletes everything below it.

Two rules about the selector: it must match the template’s labels, and it is immutable after creation. Changing it means deleting and recreating the Deployment, so choose stable labels such as app and keep version information out of the selector.

Probes: telling Kubernetes what healthy means

Probe Question On failure
startupProbe Has the app finished starting? Keep waiting until the threshold, then restart; other probes are paused until it passes
readinessProbe Should this pod receive traffic right now? Removed from Service endpoints; not restarted
livenessProbe Is the app stuck beyond recovery? Container is restarted

Keep liveness checks shallow: they should test that the process can respond, not that the database is up. A liveness probe that checks a shared dependency will restart every pod at once when that dependency has a blip, turning a minor incident into an outage. Put dependency checks, if anywhere, in readiness.

When pods crash

If a container keeps exiting, the kubelet restarts it with an exponential backoff, starting at 10 seconds and doubling up to five minutes, and the pod shows CrashLoopBackOff. The backoff is the symptom; the cause is in kubectl logs POD --previous and the Last State section of kubectl describe pod, which shows the exit code and reason, such as OOMKilled.

Graceful shutdown

When a pod is deleted, during a rollout, scale-down or node drain, Kubernetes removes it from Service endpoints and sends SIGTERM to its containers at roughly the same time, then waits terminationGracePeriodSeconds (30 by default) before SIGKILL. Because endpoint removal propagates across nodes asynchronously, a little traffic can still arrive after SIGTERM. Applications should therefore stop accepting new work but finish in-flight requests, and many teams add a short preStop sleep of a few seconds so load balancers catch up before the app starts shutting down. The same PID 1 signal rules from the Dockerfile article apply: use exec-form commands so your process actually receives the signal.

Frequently Asked Questions

What is the difference between a Pod and a Deployment?

A Pod is a single, disposable instance of one or more containers. A Deployment declares how many identical pods should run and manages them over time through ReplicaSets, replacing failed pods and rolling out changes to the pod template.

Why does my pod come back after I delete it?

It is owned by a ReplicaSet, usually created by a Deployment, which notices that fewer pods exist than desired and creates a replacement. To remove pods permanently, scale down or delete the Deployment.

Should I create ReplicaSets directly?

Rarely. Deployments create and manage ReplicaSets and add rolling updates and rollback history. Create a Deployment for stateless applications and let it handle ReplicaSets.

What is the difference between readiness and liveness probes?

A failing readiness probe removes the pod from Service traffic without restarting it. A failing liveness probe restarts the container. Use readiness for temporary unavailability and liveness only for unrecoverable states.

Change the top, let the layers follow

Edit Deployments, not ReplicaSets or pods. Give each Deployment stable labels, realistic probes, resource requests and a graceful shutdown path, and Kubernetes takes care of keeping the right number of healthy pods running through crashes, node failures and every release. Pods now need a stable address, which is what Services provide. The official documentation is worth keeping open: the Deployment documentation and the Pod documentation. Related reading on this site: Kubernetes Services and rolling updates and rollbacks.

Originally posted on 20 February 2024 and post last modified on 20 February 2024

Deployments DevOps Kubernetes Pods

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *