Lesson 2 of 11 · 9 min · Core objects

Deployments and ReplicaSets

Covers: Kubernetes 1.34 (k3s), kubectl 1.34. Updated 13 Nov 2026. Part of the DevOps Engineer roadmap.

What you will learn

  • The three-level ownership: Deployment → ReplicaSet → Pods
  • A complete Deployment manifest and what each field does
  • Self-healing and scaling, demonstrated
  • Labels and selectors, and why the selector is immutable
  • Other workload kinds: StatefulSet, DaemonSet, Job, CronJob

In simple words

You almost never start a Pod by hand, because a lone Pod that dies stays dead. Instead you tell a Deployment "keep three copies of this running". If one crashes, it starts another. If you need more, you change one number. This lesson builds one and watches it heal itself.

Words to know

  • Deployment: the object that keeps a chosen number of identical Pods running and updates them safely.
  • Replica: one copy of your Pod.
  • Label: a name tag on an object, such as app: web, used to find and group things.
  • Selector: the rule that picks objects by their labels.
  • Manifest: the YAML file in which you describe what you want.

Why Deployments

A bare Pod is not replaced if it dies. A ReplicaSet keeps a stated number of identical Pods running. A Deployment manages ReplicaSets for you and adds rolling updates and rollbacks (next lesson). You normally create Deployments, and Kubernetes creates the ReplicaSets and Pods:

Deployment web          (what you write: image, replicas, strategy)
  └── ReplicaSet web-57b5dddfb4   (one per pod-template version)
        ├── Pod web-57b5dddfb4-kzt9d
        ├── Pod web-57b5dddfb4-ptcxt
        └── Pod web-57b5dddfb4-xhj6g

A complete manifest

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  labels: {app: web}
spec:
  replicas: 3
  selector:
    matchLabels: {app: web}
  template:
    metadata:
      labels: {app: web}
    spec:
      containers:
        - name: nginx
          image: nginx:1.26-alpine
          ports:
            - containerPort: 80
  • replicas: how many Pods you want.
  • selector.matchLabels: how the Deployment finds the Pods it owns.
  • template: the Pod blueprint. Its labels must match the selector. Everything under template.spec is a normal Pod spec: containers, image, ports, environment, resources, probes, volumes.
  • The selector is immutable after creation: changing which Pods a Deployment owns would orphan them. To change it, create a new Deployment.

Apply and watch:

kubectl apply -f deployment.yaml
kubectl rollout status deployment/web
kubectl get deploy,rs,pods -l app=web
deployment.apps/web                3/3     3     3
replicaset.apps/web-57b5dddfb4     3       3     3
pod/web-57b5dddfb4-kzt9d           1/1     Running
pod/web-57b5dddfb4-ptcxt           1/1     Running
pod/web-57b5dddfb4-xhj6g           1/1     Running

Pod names are <replicaset name>-<random>, and the ReplicaSet's name contains a hash of the Pod template. Confirm who owns a Pod: kubectl get pod ... -o jsonpath='{.items[0].metadata.ownerReferences[0].kind}' printed ReplicaSet.

Self-healing

Delete one Pod on purpose:

kubectl delete pod web-57b5dddfb4-kzt9d --wait=false
kubectl get pods -l app=web

A few seconds later:

web-57b5dddfb4-kfkzp   1/1   Running      # a NEW pod with a new name
web-57b5dddfb4-ptcxt   1/1   Running
web-57b5dddfb4-xhj6g   1/1   Running

The ReplicaSet controller saw "3 wanted, 2 existing" and created another. The same happens if a node dies: Pods that were on it are recreated elsewhere (after a timeout). Self-healing covers crashed containers, deleted Pods and lost nodes, not application bugs: a Pod that starts and immediately crashes is restarted forever (CrashLoopBackOff).

Scaling

kubectl scale deployment/web --replicas=5
kubectl get deploy web

showed 5/5 ready. Scale to zero (--replicas=0) to stop an app without deleting its configuration. In production, change replicas in the manifest in Git; or let the Horizontal Pod Autoscaler (HPA) adjust it automatically based on CPU or custom metrics (kubectl autoscale deployment web --min=2 --max=10 --cpu-percent=70, requires the metrics server and CPU requests on the Pods). Do not set replicas in the manifest if an HPA owns the number.

For availability run at least two or three replicas across different nodes (and zones), and add a PodDisruptionBudget so voluntary disruptions (node drains for upgrades) never remove too many at once.

Useful commands

kubectl get deploy web -o yaml            # desired spec and current status
kubectl describe deploy web               # replicas, strategy, conditions, events
kubectl set image deployment/web nginx=nginx:1.27-alpine   # change the image (starts a rollout)
kubectl rollout restart deployment/web    # new Pods with the same spec (picks up changed ConfigMaps/Secrets)
kubectl edit deploy web                   # edit the live object (avoid; use Git + apply)
kubectl delete deploy web                 # deletes the ReplicaSet and Pods too

Deleting a Deployment deletes what it owns (ownership/garbage collection). To delete only the Deployment and keep Pods: --cascade=orphan (rarely wanted).

Other workload controllers

Kind For
Deployment stateless applications (web servers, APIs)
StatefulSet stateful apps needing stable names and storage per replica (databases, Kafka): Pods db-0, db-1, each with its own volume
DaemonSet one Pod on every (or selected) node: log collectors, monitoring agents, network plugins
Job run to completion once (a migration, a batch task); retries on failure
CronJob a Job on a schedule (replaces cron for cluster tasks)

Choose by the workload's nature; stateful systems need much more care than stateless ones, and for production databases managed services (RDS, Cloud SQL) are often the better answer.

Common mistakes

  • Template labels that do not match the selector (the API server rejects it).
  • Changing the selector of an existing Deployment.
  • Editing Pods directly. The controller replaces them with the template's version.
  • One replica in production. One node or one rollout means downtime.
  • Not setting resource requests and limits, so the scheduler and autoscaler cannot do their job (a later lesson).
  • Using latest with imagePullPolicy surprises: Kubernetes may not pull a changed image, or pulls different ones on different nodes.
  • Deleting a Pod to "restart" an app instead of rollout restart.

Try it yourself

  1. Create the Deployment above, delete a Pod and watch it come back. Note the new name.
  2. Scale to 5 and back to 3, and watch Pods being created and terminated with kubectl get pods -w.
  3. Find the owner reference of a Pod and the selector of the ReplicaSet with kubectl get ... -o yaml.
  4. Break the template labels so they no longer match the selector and read the error.

Check yourself

What is the relationship between Deployment, ReplicaSet and Pod?

A Deployment owns ReplicaSets (one per Pod-template version), and each ReplicaSet owns and maintains a set of identical Pods.

Why does deleting a Pod managed by a Deployment not remove it permanently?

The ReplicaSet controller notices fewer Pods than desired and creates a new one. The desired state is the Deployment's replica count.

When would you use a StatefulSet or a DaemonSet instead of a Deployment?

StatefulSet for workloads needing stable identity and per-replica storage (databases); DaemonSet to run one Pod on each node (agents such as log collectors).

Why can't you change a Deployment's selector?

The selector defines which Pods the controller owns. Changing it would orphan existing Pods, so the API makes it immutable.

Interview questions

How does Kubernetes keep an application available when a node fails?

The node becomes NotReady; after a timeout its Pods are evicted and the ReplicaSet controller creates replacements, which the scheduler places on healthy nodes. Running multiple replicas across nodes and zones, with PodDisruptionBudgets, prevents downtime in the meantime.

How do you scale an application automatically?

Use a HorizontalPodAutoscaler, which adjusts replicas based on observed metrics (CPU requests utilisation by default); it needs resource requests and a metrics source. Cluster autoscaling adds nodes when Pods cannot be scheduled.

What is kubectl rollout restart for?

To perform a rolling restart of a Deployment's Pods without changing the spec, for example to pick up a changed ConfigMap or Secret mounted as environment variables.

Summary

  • Deployment → ReplicaSet → Pods: you declare replicas and a Pod template; controllers keep reality matching.
  • Self-healing replaces deleted or failed Pods; scale manually or with an HPA; run several replicas.
  • Labels in the template must match the immutable selector.
  • Use StatefulSet, DaemonSet, Job and CronJob for other workload shapes.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on Kubernetes for DevOps Engineer and scores every answer, with what to fix.