Deployments, Rollouts and Scaling

Topics Covered

Deployments and ReplicaSets

The ownership chain

The pod template hash

Creating a Deployment fast

Selectors are immutable

revisionHistoryLimit

Deleting without the cascade

Reading Deployment status

Changing the image the fast way

Adopting and orphaned pods

Checking a Deployment quickly

When to use a bare ReplicaSet

Deployments are the workload you will create and modify most on both exams. A task might ask you to create one, scale it, change its image, roll it back, or find out why its pods are not becoming ready. To do those quickly you need a clear picture of the objects involved and of which one you should change. The concept of a Deployment is covered in Codemia's Containers and Kubernetes course; this lesson is about operating them under exam conditions.

The ownership chain

A Deployment does not create pods directly. It creates a ReplicaSet from its pod template, and the ReplicaSet creates and maintains the pods. Each object records its owner in metadata.ownerReferences, which is how garbage collection knows to delete the ReplicaSet and pods when you delete the Deployment.

A Deployment owns ReplicaSets, and each ReplicaSet keeps its pod count at the desired number.

When you change the pod template, for example the image, the Deployment creates a new ReplicaSet for the new template and gradually scales it up while scaling the old one down. The old ReplicaSet is kept, scaled to zero, so that a rollback can scale it back up. Changes that do not touch the template, such as the replica count, do not create a new ReplicaSet; they only change the current one's size.

bash
k -n shop get deploy,rs,pods -l app=web
k -n shop describe deploy web | grep -E "OldReplicaSets|NewReplicaSet"

The practical rule follows: change the Deployment, never the ReplicaSet or its pods. If you edit a ReplicaSet owned by a Deployment, the Deployment controller may overwrite your change. If you delete a pod, the ReplicaSet creates a replacement from the same template within seconds. Deleting pods is a useful way to force a restart, but it never changes what runs.

The pod template hash

Each ReplicaSet created by a Deployment gets a label called pod-template-hash, computed from the template, and so do its pods. The ReplicaSet name is the Deployment name plus that hash, such as web-6d4f9c8b7. The hash label keeps ReplicaSets from fighting over each other's pods: the Deployment's selector might be app=web, but each ReplicaSet's selector also includes its own hash.

You never set this label yourself, but recognising it helps when you read output. Pod names follow the same pattern with an extra random suffix, so web-6d4f9c8b7-x2kqp belongs to the ReplicaSet web-6d4f9c8b7.

Creating a Deployment fast

The imperative generator covers the common fields, and the kubectl speed lesson explained the generate, edit, apply workflow:

bash
k -n shop create deployment web --image=nginx:1.27 --replicas=3 --port=80 $do > web.yaml

The generated file has a selector with matchLabels app: web, a template with the same label, one container named after the image, and a strategy field left empty, which means the defaults apply. A complete Deployment that you might write after editing looks like this:

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: web
5  namespace: shop
6  labels:
7    app: web
8spec:
9  replicas: 3
10  revisionHistoryLimit: 5
11  selector:
12    matchLabels:
13      app: web
14  strategy:
15    type: RollingUpdate
16    rollingUpdate:
17      maxSurge: 1
18      maxUnavailable: 0
19  template:
20    metadata:
21      labels:
22        app: web
23    spec:
24      containers:
25      - name: nginx
26        image: nginx:1.27
27        ports:
28        - containerPort: 80

Note that the generator names the container after the image, nginx here, not after the Deployment. Tasks that later say update the container named web will fail with set image unless you named it web; read the container name with jsonpath before using set image.

Selectors are immutable

In apps/v1, a Deployment's selector cannot change after creation, and it must match the template's labels. If a task asks you to change the selector, you must delete and recreate the Deployment. You can add labels to the template freely, as long as the selector's labels remain present. Removing a selector label from the template is rejected.

This is a common trap in tasks that say add the label version=v2 to the Deployment's pods. Adding it under spec.template.metadata.labels is correct and triggers a rollout. Adding it to the selector is rejected by the API server. Adding it only to the Deployment's own metadata.labels changes nothing on the pods.

revisionHistoryLimit

Each old ReplicaSet kept for rollback consumes a small amount of space in etcd. revisionHistoryLimit, default 10, controls how many are kept. Setting it lower keeps the namespace tidy; setting it to 0 disables rollback entirely, because no old ReplicaSets survive. A task that asks you to keep only the last three revisions wants revisionHistoryLimit: 3.

Deleting without the cascade

Deleting a Deployment deletes its ReplicaSets and pods by default. Occasionally a task wants the pods to keep running, for example while you replace the Deployment with a new one that adopts them. The cascade flag controls this:

bash
k -n shop delete deploy web --cascade=orphan
k -n shop get rs,pods -l app=web

With orphan, the ReplicaSet and pods remain without an owner. A new Deployment with a matching selector and an identical template adopts the existing ReplicaSet. This is rarely needed, but it is the safe way to change a Deployment's selector without downtime.

Reading Deployment status

The default columns of kubectl get deploy tell you a lot once you know them. READY shows ready pods over desired replicas. UP-TO-DATE shows how many pods run the current template. AVAILABLE shows how many have been ready for at least minReadySeconds. During a healthy rollout, UP-TO-DATE climbs to the desired count; when it is done, all three columns match the replica count.

bash
k -n shop get deploy web
k -n shop get deploy web -o jsonpath='{.status.conditions[*].type}'; echo

The conditions are Available, which says whether enough pods are available, and Progressing, which says whether the rollout is moving or has stalled. Those conditions are what rollout status reads, as a later section shows.

Changing the image the fast way

Most Deployment changes on the exam are image updates, and set image is the quickest way to make one. It takes the container name, not the image name, followed by the new image. You can update several containers in one command, and an asterisk updates every container in the template:

bash
k -n shop set image deploy/web nginx=nginx:1.27
k -n shop set image deploy/web nginx=nginx:1.27 sidecar=busybox:1.36
k -n shop get deploy web -o jsonpath='{.spec.template.spec.containers[*].name}'; echo

If you give a container name that does not exist, set image fails with an error naming the missing container, so the jsonpath check above is worth running first when you did not create the Deployment yourself. Each set image creates a new revision and starts a rolling update.

Adopting and orphaned pods

A ReplicaSet counts every pod that matches its selector, including pods it did not create. If you create a bare pod with labels that match a running ReplicaSet's selector, including the pod-template-hash, the ReplicaSet adopts it and deletes one pod to keep the count right. The template hash label makes this rare for Deployments, but it is a real hazard with hand written ReplicaSets or with label changes. The reverse also applies: if you remove a selector label from a running pod, the ReplicaSet releases it and creates a replacement, and the released pod keeps running on its own. That is a useful debugging technique, because you can pull a misbehaving pod out of service, keep it for inspection, and let the controller restore full capacity.

bash
k -n shop label pod web-6d4f9c8b7-x2kqp app=debug --overwrite
k -n shop get pods -L app

Checking a Deployment quickly

When a task hands you an existing Deployment, four checks cover most of what you need: get with -o wide for image and readiness, get rs to see whether a rollout is in progress, describe for the strategy and recent events, and rollout history for what changed. Running them in that order takes under a minute and usually shows the problem before you edit anything.

When to use a bare ReplicaSet

Almost never. A ReplicaSet on its own keeps a pod count, but it cannot roll out a new template: changing its image affects only pods created afterwards, so existing pods keep the old image until they are deleted. A Deployment adds rollouts, history and undo on top at no extra cost. If a task asks for a ReplicaSet explicitly, write one from a generated Deployment by changing the kind and removing the strategy field, but otherwise prefer a Deployment every time.

Labels go on the template, not the selector

To label a Deployment's pods, add the label under spec.template.metadata.labels. The selector is immutable in apps/v1, and labels on the Deployment's own metadata never reach the pods.