08 — Deployment strategies¶
How you move from version N to N+1 with controlled risk: recreate, rolling, blue-green and canary. A rollout is also a security event: it decides which build reaches users, and how fast you can take it back. This chapter runs a manual canary of catalog with two Deployments behind one Service, then rolls back a bad image. Progressive delivery (automated) is forward-referenced.
Estimated time: ~15 min read · ~60 min hands-on Prerequisites: 01.04: the rolling-update mechanism and the ServiceAccounts · 01.02: readiness gates a rollout depends on You'll know after this: • compare recreate, rolling, blue-green, and canary strategies and their failure modes · • run a manual canary using two Deployments and one Service selector · • reason about traffic-splitting limitations of Service-selector canaries · • explain why a rollback is a security decision, not only an availability one · • choose a deployment strategy for a given risk profile · • recognize when automated progressive delivery (Argo Rollouts/Flagger) becomes worth the cost
Why this exists¶
01.04 gave you the mechanism of a rolling update: surge math, readiness gating, rollback. But "how the bytes get swapped" is not the same as "how you release safely". A rolling update exposes every user to the new version as it rolls. If the new version is subtly broken (a bad query, a memory leak that shows under real traffic, a bug only some requests hit), you have already degraded everyone before the rollout finishes, and rollback is a reaction to damage already done.
Security adds a second worry. A new build runs with the same ServiceAccount, network reach and mounted secrets as the build it replaces. A rollout is therefore a security event: each Pod it replaces changes which code holds those identities. The question is how many users run that code before you can stop it.
Real release engineering asks: can I expose the new version to a tiny slice first, watch real signals, and abort with near-zero blast radius? That is the domain of deployment strategies: recreate, rolling, blue-green and canary, each trading speed, cost, risk and operational complexity differently. This chapter is the conceptual map, a hands-on manual canary of the Bookstore catalog, and a rollback. It is the release-strategy face of the Declarative Deployment pattern. The automated version (Argo Rollouts or Flagger, with metric-gated promotion) is in 07.05.
Mental model¶
Every strategy is a different answer to two questions: how many versions run at once, and how is traffic split between them.
- Recreate: stop all old, then start all new. One version at a time; 100% cut. Simple, but a downtime window. Use only when versions cannot coexist (incompatible schema or protocol).
- Rolling (01.04): gradually replace old Pods with new, both versions briefly serving, traffic split roughly by replica ratio. No downtime and no extra full environment, but no control over which users hit new, and the new version reaches everyone by the end.
- Blue-green: run the full new version ("green") alongside the old ("blue"), test green privately, then flip 100% of traffic at once (swap a Service selector or load-balancer target). Instant cutover, instant rollback (flip back), but 2× resources during the overlap and still a big-bang exposure.
- Canary: run a small amount of new ("canary") next to old ("stable"), send it a small fraction of real traffic, watch error, latency and business metrics, then ramp up (or abort). Smallest blast radius and real-traffic validation, but it needs traffic splitting and good signals.
Manual canary by replica ratio: if one Service selects both a 9-replica stable Deployment and a 1-replica canary Deployment, ≈10% of requests hit canary (load-balanced across endpoints). Crude but real, and the exact mechanism we use below before introducing the precise (mesh/Gateway-weighted) form later.
Threats and controls¶
A rollout moves a new build onto the identity, network reach and secrets of the Deployment it replaces. Each canary Pod here carries the same baseline as the stable Pods, so the canary is no looser than the build it tests. The table lists the threats this chapter owns.
| Threat | Control taught here | Goes deeper |
|---|---|---|
| A Pod that copies a Service's labels receives its traffic | Label discipline: the shared app label is set only on the Deployments' templates, and the endpoints show any Pod that matches it |
02.02; who may create such a Pod, 05.07 |
| A canary ships without the baseline and runs as root or with a writable root filesystem | The canary carries the full baseline, and the namespace refuses any Pod that lacks it, so a ReplicaSet cannot create one | 05.02 |
| A bad image reaches every user before anyone notices | Readiness keeps an image that cannot start from serving. A canary limits how many users run a build that starts, and scaling it to 0 stops it | 07.05 |
Two limits matter. A build that starts and misbehaves gets past readiness, so only metrics and a person or controller catch it. And a rollback restores code, not state: credentials the bad build read stay valid until they are rotated. The rollback section covers both.
Diagrams¶
Blue-green traffic switch (Mermaid)¶
flowchart LR
subgraph before["Before flip"]
s1["Service catalog
selector: track=blue"]
b1["Deploy catalog-blue v1
(serving 100%)"]
g1["Deploy catalog-green v2
(running, 0 traffic, tested privately)"]
s1 --> b1
end
subgraph after["After flip (one selector change)"]
s2["Service catalog
selector: track=green"]
b2["catalog-blue v1
(idle, kept for instant rollback)"]
g2["catalog-green v2
(serving 100%)"]
s2 --> g2
end
before -->|"patch Service selector
blue → green (atomic)"| after
after -.->|"problem? flip back green → blue"| before
Canary weight ramp (Mermaid)¶
flowchart TD
T["catalog canary v2: ramp gated by metrics
manual here, automated in Part 07"]
S0["Step 0
stable=9 canary=0
100 percent v1: baseline"]
S1["Step 1
stable=9 canary=1
approx 10 percent v2: watch error rate and p99
one Service, both selected"]
S2["Step 2
stable=7 canary=3
approx 30 percent v2: metrics still healthy, proceed"]
S3["Step 3
stable=4 canary=6
approx 60 percent v2: soak under real load"]
S4["Step 4
stable=0 canary=10
100 percent v2: promote, canary spec becomes the new stable"]
AB["Abort
any step bad, canary to 0
near-zero blast radius, v1 untouched"]
T --> S0 --> S1 --> S2 --> S3 --> S4
S1 -.->|bad| AB
S2 -.->|bad| AB
S3 -.->|bad| AB
Hands-on with the Bookstore¶
Assumed working directory: the guide repo root (full-guide/). This
chapter needs the bookstore namespace and its quota from
01.03, the ServiceAccounts from
01.04, and the bookstore/catalog:dev
image loaded into the cluster. The canary introduces a Service a little
ahead of Part 02. It is kept minimal, only
so the split is observable.
1. Two tracks behind one Service¶
The key idea: a Service selects on app: catalog only. Two Deployments,
catalog-stable and catalog-canary, both label their Pods app: catalog
(so the Service load-balances across both) plus a track: stable|canary
label (so you can scale and observe each one). The traffic split is the
replica ratio.
Both Pod templates carry the full baseline and the
catalog-sa ServiceAccount from 01.04.
The file is
examples/bookstore/raw-manifests/30-catalog-canary.yaml.
The copy below matches it, without the header comments:
apiVersion: v1
kind: Service
metadata:
name: catalog
namespace: bookstore
labels:
app: catalog
spec:
selector:
app: catalog # no track key: selects BOTH tracks
ports:
- name: http
port: 80
targetPort: http # the container's named port (8080)
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: catalog-stable
namespace: bookstore
labels:
app: catalog
track: stable
spec:
replicas: 9 # about 90% of the Ready endpoints
revisionHistoryLimit: 5 # old ReplicaSets kept as rollback targets
selector:
matchLabels:
app: catalog
track: stable # owns only the stable Pods
template:
metadata:
labels:
app: catalog # the label the Service selects
track: stable
component: backend
spec:
serviceAccountName: catalog-sa
automountServiceAccountToken: false
securityContext:
runAsNonRoot: true
runAsUser: 65532
runAsGroup: 65532
seccompProfile:
type: RuntimeDefault
containers:
- name: catalog
image: bookstore/catalog:dev # the image the stable track runs
imagePullPolicy: IfNotPresent
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
ports:
- name: http
containerPort: 8080
env:
- name: PORT
value: "8080"
readinessProbe:
httpGet: { path: /readyz, port: http }
periodSeconds: 5
livenessProbe:
httpGet: { path: /healthz, port: http }
periodSeconds: 10
resources:
requests:
cpu: 50m
memory: 64Mi
limits:
cpu: 250m
memory: 128Mi
volumeMounts:
- name: scratch
mountPath: /tmp/cache
volumes:
- name: scratch
emptyDir:
sizeLimit: 64Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: catalog-canary
namespace: bookstore
labels:
app: catalog
track: canary
spec:
replicas: 1 # about 10% of the Ready endpoints
revisionHistoryLimit: 5
selector:
matchLabels:
app: catalog
track: canary # owns only the canary Pods
template:
metadata:
labels:
app: catalog # also selected by the same Service
track: canary
component: backend
spec:
serviceAccountName: catalog-sa
automountServiceAccountToken: false
securityContext:
runAsNonRoot: true
runAsUser: 65532
runAsGroup: 65532
seccompProfile:
type: RuntimeDefault
containers:
- name: catalog
image: bookstore/catalog:dev # the candidate image, in a real rollout
imagePullPolicy: IfNotPresent
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
ports:
- name: http
containerPort: 8080
env:
- name: PORT
value: "8080"
- name: LOG_LEVEL
value: "debug" # a marker that this is the canary
readinessProbe:
httpGet: { path: /readyz, port: http }
periodSeconds: 5
livenessProbe:
httpGet: { path: /healthz, port: http }
periodSeconds: 10
resources:
requests:
cpu: 50m
memory: 64Mi
limits:
cpu: 250m
memory: 128Mi
volumeMounts:
- name: scratch
mountPath: /tmp/cache
volumes:
- name: scratch
emptyDir:
sizeLimit: 64Mi
The canary runs the same image tag as stable here, so the lab measures routing,
not a new build. The LOG_LEVEL variable is only a marker: the catalog reads
it to set its log level and does nothing else with it. In a real rollout the
canary runs the candidate image, which should pass the same checks as stable
first (05.03).
Both tracks carry the baseline from 01.01 unchanged.
Two details differ from that example. The scratch volume mounts at
/tmp/cache, not /tmp, so the one writable path has a name that says what it
is for. The catalog writes no files today
(its source), so the volume is
headroom: a future cache write lands in a capped volume instead of failing
against the read-only root. sizeLimit: 64Mi is that cap, so a runaway write
cannot fill the node's disk. That is the threat in
01.03.
Don't run both catalog stacks at once. The canary file defines
app: catalogPods in the same namespace as the canonical catalog from 01.04. Together they would make the shared Service select 13 Pods (3 + 9 + 1), and the 90/10 split below would be wrong. The first command below retires the canonical Deployment. To go back to it later, delete the canary file and apply10-catalog-deploy.yamlagain.
Retire the canonical Deployment, apply the canary, and check that one Service fronts both tracks:
kubectl delete deployment/catalog -n bookstore --ignore-not-found
kubectl apply -f examples/bookstore/raw-manifests/30-catalog-canary.yaml
kubectl rollout status deployment/catalog-stable -n bookstore
kubectl rollout status deployment/catalog-canary -n bookstore
kubectl get pods -n bookstore -l app=catalog -L track
kubectl get endpointslices -n bookstore -l kubernetes.io/service-name=catalog \
-o jsonpath='{range .items[*].endpoints[*]}{.conditions.ready}{"\n"}{end}' \
| sort | uniq -c
service/catalog created
deployment.apps/catalog-stable created
deployment.apps/catalog-canary created
deployment "catalog-stable" successfully rolled out
# ...
deployment "catalog-canary" successfully rolled out
NAME READY STATUS RESTARTS AGE TRACK
catalog-canary-7bd4cf6bc5-6rclj 1/1 Running 0 77s canary
catalog-stable-5cc48c67c7-4jznq 1/1 Running 0 77s stable
catalog-stable-5cc48c67c7-6nj6d 1/1 Running 0 78s stable
catalog-stable-5cc48c67c7-8xfvx 1/1 Running 0 71s stable
catalog-stable-5cc48c67c7-j96g7 1/1 Running 0 78s stable
catalog-stable-5cc48c67c7-kpkqq 1/1 Running 0 77s stable
catalog-stable-5cc48c67c7-svxtp 1/1 Running 0 77s stable
catalog-stable-5cc48c67c7-t46sr 1/1 Running 0 79s stable
catalog-stable-5cc48c67c7-wwq7g 1/1 Running 0 77s stable
catalog-stable-5cc48c67c7-xv684 1/1 Running 0 71s stable
10 true
Ten Ready endpoints: nine from the stable track and one from the canary. The
TRACK column is the split you are about to measure.
2. Measure the split from the Pods' own counters¶
The catalog counts each request it answers in http_requests_total, labelled
by handler and status code, and serves the counts on /metrics on the app
port. The catalog's logs do not record requests, so the lab reads each Pod's
counter through the API server's proxy. The probe calls only /books. The
kubelet's probes call /healthz and /readyz, which have their own handler
labels, so they do not inflate the count.
The probe is a client Pod with the baseline and its own ServiceAccount,
probe-sa, so it holds no catalog identity. The busybox image declares no
user, so the probe sets runAsUser to 65534 (nobody). The image is pinned by
digest, with the tag in a comment, as the baseline asks for third-party images.
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: ServiceAccount
metadata:
name: probe-sa
namespace: bookstore
automountServiceAccountToken: false
---
apiVersion: v1
kind: Pod
metadata:
name: curlbox
namespace: bookstore
labels:
component: probe
spec:
serviceAccountName: probe-sa
automountServiceAccountToken: false
restartPolicy: Never
securityContext:
runAsNonRoot: true
runAsUser: 65534
runAsGroup: 65534
seccompProfile:
type: RuntimeDefault
containers:
- name: curlbox
image: busybox@sha256:73aaf090f3d85aa34ee199857f03fa3a95c8ede2ffd4cc2cdb5b94e566b11662 # busybox:1.36
command:
- sh
- -c
- for i in $(seq 1 200); do wget -qO- http://catalog/books >/dev/null; done
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
resources:
requests: { cpu: 10m, memory: 16Mi }
limits: { cpu: 100m, memory: 64Mi }
volumeMounts:
- name: tmp
mountPath: /tmp
volumes:
- name: tmp
emptyDir: {}
EOF
The Pod sends 200 requests and exits. restartPolicy: Never leaves it
Succeeded, and the next command waits for that:
kubectl wait --for=jsonpath='{.status.phase}'=Succeeded pod/curlbox -n bookstore --timeout=180s
pod/curlbox condition met
The counters start at zero for new Pods and only go up, so read them before and after a probe and subtract. This helper sums them per track:
# served: the 200 responses on /books per track, from each catalog Pod's own
# counter. Pods being deleted are skipped. A missing track label shows <none>.
served() {
kubectl get pods -n bookstore -l app=catalog \
-o custom-columns=POD:.metadata.name,TRACK:.metadata.labels.track,DELETING:.metadata.deletionTimestamp --no-headers |
while read -r pod track deleting; do
[ "$deleting" = "<none>" ] || continue
kubectl get --raw "/api/v1/namespaces/bookstore/pods/$pod:8080/proxy/metrics" |
awk -v t="$track" '/^http_requests_total/ && /handler="books"/ && /code="200"/ {n += $NF}
END {print t, n + 0}'
done | awk '{ s[$1] += $2 } END { for (t in s) print t, s[t] }' | sort
}
Before the first probe, the counters are zero:
served
canary 0
stable 0
After the probe, read them again:
kubectl wait --for=jsonpath='{.status.phase}'=Succeeded pod/curlbox -n bookstore --timeout=180s
served
pod/curlbox condition met
canary 16
stable 184
Of the 200 requests, 16 reached the canary and 184 reached stable. That is 8%, against 10% for one canary Pod in ten. Delete the probe Pod before the next run:
kubectl delete pod curlbox -n bookstore
3. Ramp and abort: change only the replica counts¶
Ramping, measuring and aborting change the replica counts and nothing else. This step moves to three canary Pods and seven stable Pods, then probes again:
kubectl scale deploy/catalog-canary -n bookstore --replicas=3
kubectl scale deploy/catalog-stable -n bookstore --replicas=7
kubectl rollout status deployment/catalog-canary -n bookstore
kubectl get pods -n bookstore -l app=catalog -L track
served
deployment "catalog-canary" successfully rolled out
NAME READY STATUS RESTARTS AGE TRACK
catalog-canary-7bd4cf6bc5-6rclj 1/1 Running 0 3m51s canary
catalog-canary-7bd4cf6bc5-7xwwn 1/1 Running 0 26s canary
catalog-canary-7bd4cf6bc5-nm8dr 1/1 Running 0 28s canary
catalog-stable-5cc48c67c7-6nj6d 1/1 Running 0 3m52s stable
catalog-stable-5cc48c67c7-8xfvx 1/1 Running 0 3m45s stable
catalog-stable-5cc48c67c7-j96g7 1/1 Running 0 3m52s stable
catalog-stable-5cc48c67c7-kpkqq 1/1 Running 0 3m51s stable
catalog-stable-5cc48c67c7-svxtp 1/1 Running 0 3m51s stable
catalog-stable-5cc48c67c7-wwq7g 1/1 Running 0 3m51s stable
catalog-stable-5cc48c67c7-xv684 1/1 Running 0 3m45s stable
canary 16
stable 140
The first canary Pod keeps its count of 16. The two new canary Pods start at zero, and the two stable Pods that scaled down take their counts with them. Probe again, after deleting the previous probe Pod and applying the manifest from step 2:
kubectl delete pod curlbox -n bookstore --ignore-not-found
# apply the probe manifest from step 2 again, then wait:
kubectl wait --for=jsonpath='{.status.phase}'=Succeeded pod/curlbox -n bookstore --timeout=180s
served
pod/curlbox condition met
canary 91
stable 265
This run sent 75 of the 200 requests to the canary (91 less 16), about 37%. Three Pods in ten predict 30%. One run of 200 requests is a small sample, so the share will wander. Read the metric over a longer window before you ramp again, and compare the canary with stable over the same period, as the production notes say.
Abort uses the same commands in reverse. The canary's replicas go to zero, the Service stops sending it traffic once its Pods are gone, and the stable track returns to full size:
kubectl scale deploy/catalog-canary -n bookstore --replicas=0
kubectl scale deploy/catalog-stable -n bookstore --replicas=9
kubectl rollout status deployment/catalog-stable -n bookstore
kubectl get endpointslices -n bookstore -l kubernetes.io/service-name=catalog \
-o jsonpath='{range .items[*].endpoints[*]}{.conditions.ready}{"\n"}{end}' \
| sort | uniq -c
deployment "catalog-stable" successfully rolled out
9 true
4. A Pod that copies the Service's label (deliberate)¶
This step is deliberate. The Service selects on a label, and any Ready Pod that
carries that label becomes an endpoint. The Pod below is a copy of the catalog
container with app: catalog and no track label. It carries the full
baseline, so admission accepts it:
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: catalog-copy
namespace: bookstore
labels:
app: catalog
spec:
serviceAccountName: catalog-sa
automountServiceAccountToken: false
securityContext:
runAsNonRoot: true
runAsUser: 65532
runAsGroup: 65532
seccompProfile:
type: RuntimeDefault
containers:
- name: catalog
image: bookstore/catalog:dev
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080
readinessProbe:
httpGet: { path: /readyz, port: http }
periodSeconds: 5
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
resources:
requests: { cpu: 50m, memory: 64Mi }
limits: { cpu: 250m, memory: 128Mi }
volumeMounts:
- name: scratch
mountPath: /tmp/cache
volumes:
- name: scratch
emptyDir:
sizeLimit: 64Mi
EOF
kubectl wait --for=condition=Ready pod/catalog-copy -n bookstore --timeout=120s
kubectl get pods -n bookstore -l app=catalog -L track
served
pod/catalog-copy condition met
NAME READY STATUS RESTARTS AGE TRACK
catalog-copy 1/1 Running 0 15s
catalog-stable-5cc48c67c7-6fzzl 1/1 Running 0 44s stable
catalog-stable-5cc48c67c7-6nj6d 1/1 Running 0 5m14s stable
catalog-stable-5cc48c67c7-8xfvx 1/1 Running 0 5m7s stable
catalog-stable-5cc48c67c7-j96g7 1/1 Running 0 5m14s stable
catalog-stable-5cc48c67c7-kpkqq 1/1 Running 0 5m13s stable
catalog-stable-5cc48c67c7-pzrr9 1/1 Running 0 43s stable
catalog-stable-5cc48c67c7-svxtp 1/1 Running 0 5m13s stable
catalog-stable-5cc48c67c7-wwq7g 1/1 Running 0 5m13s stable
catalog-stable-5cc48c67c7-xv684 1/1 Running 0 5m7s stable
<none> 0
stable 265
Probe again, with the same probe manifest as step 2:
kubectl delete pod curlbox -n bookstore
# apply the probe manifest from step 2 again, then wait:
kubectl wait --for=jsonpath='{.status.phase}'=Succeeded pod/curlbox -n bookstore --timeout=180s
served
error: timed out waiting for the condition on pods/curlbox
<none> 28
stable 437
The wait gave up after 180 seconds on a node shared with other workloads. The
counters read afterwards are complete: 28 to the copy and 172 to stable, which
is 200. On a busy node, a probe can run longer than the wait allows. Give the
wait more time, and read the counters once the Pod reaches Succeeded.
The copy took 28 of the 200 requests. From the Service's side, the copy and a
canary Pod look the same. Admission did not stop it, because the copy meets the
baseline. What stops it is RBAC. Create Pods, Deployments, Jobs, CronJobs and
DaemonSets only for principals who need them, because each of those kinds makes
Pods (05.01). Add an admission
rule that accepts the app: catalog label only from the release's
ServiceAccount
(05.07). Narrowing
the Service
selector does not help, because the copy matches it by design. Delete the probe
Pod and the copy:
kubectl delete pod curlbox -n bookstore
kubectl delete pod catalog-copy -n bookstore
5. A bad image lands on stable (deliberate)¶
Treat a bad image as an incident. Stop its spread first, restore the last good revision second, and keep the evidence third. This step deploys a tag that does not exist, so the bad image cannot start. That failure is deliberate. Readiness is what keeps its Pods out of the Service. A bad image that starts and misbehaves passes readiness, and only the canary's metrics, read by a person or a controller, will catch it.
kubectl set image deploy/catalog-stable -n bookstore catalog=bookstore/catalog:candidate-404
kubectl rollout status deployment/catalog-stable -n bookstore --timeout=60s
kubectl get pods -n bookstore -l app=catalog -L track
kubectl get endpointslices -n bookstore -l kubernetes.io/service-name=catalog \
-o jsonpath='{range .items[*].endpoints[*]}{.conditions.ready}{"\n"}{end}' \
| sort | uniq -c
deployment.apps/catalog-stable image updated
Waiting for deployment "catalog-stable" rollout to finish: 5 out of 9 new replicas have been updated...
error: timed out waiting for the condition
NAME READY STATUS RESTARTS AGE TRACK
catalog-stable-5cc48c67c7-6nj6d 1/1 Running 0 10m stable
catalog-stable-5cc48c67c7-8xfvx 1/1 Running 0 10m stable
catalog-stable-5cc48c67c7-j96g7 1/1 Running 0 10m stable
catalog-stable-5cc48c67c7-kpkqq 1/1 Running 0 10m stable
catalog-stable-5cc48c67c7-pzrr9 0/1 Completed 0 5m38s stable
catalog-stable-5cc48c67c7-svxtp 1/1 Running 0 10m stable
catalog-stable-5cc48c67c7-wwq7g 1/1 Running 0 10m stable
catalog-stable-5cc48c67c7-xv684 1/1 Running 0 10m stable
catalog-stable-64d4dc76b6-bbh7q 0/1 ContainerCreating 0 70s stable
catalog-stable-64d4dc76b6-cvp64 0/1 ContainerCreating 0 68s stable
catalog-stable-64d4dc76b6-gglrl 0/1 Pending 0 57s stable
catalog-stable-64d4dc76b6-mqvrh 0/1 ContainerCreating 0 68s stable
catalog-stable-64d4dc76b6-xhhjb 0/1 Pending 0 59s stable
7 true
Seven Pods are Ready. The new Pods never become Ready, so none of them is
an endpoint.
Roll back to the last revision. 01.04
shows rollout undo, and the warning it prints for an object created with
kubectl apply. The rollback here runs the same command:
kubectl rollout undo deploy/catalog-stable -n bookstore
kubectl rollout status deployment/catalog-stable -n bookstore
kubectl get deploy catalog-stable -n bookstore -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
kubectl get endpointslices -n bookstore -l kubernetes.io/service-name=catalog \
-o jsonpath='{range .items[*].endpoints[*]}{.conditions.ready}{"\n"}{end}' \
| sort | uniq -c
kubectl get rs -n bookstore -l app=catalog,track=stable
deployment "catalog-stable" successfully rolled out
bookstore/catalog:dev
9 true
NAME DESIRED CURRENT READY AGE
catalog-stable-5cc48c67c7 9 9 9 11m
catalog-stable-64d4dc76b6 0 0 0 2m6s
The last-applied-configuration annotation is not updated by a rollback, so
fix the file in version control before the next kubectl apply. Otherwise the
apply puts the bad image back. The revision history and digest pinning that make
a rollback safe are covered in 01.04.
A rollback restores code, not state. The bad build may have read credentials or called services with the identity it had. Those stay valid until they are rotated (05.04). Keep the bad ReplicaSet at zero replicas until you have read it.
6. Refusals, shown once¶
Two refusals that a Deployment can meet are demonstrated in other chapters, so this chapter does not repeat them. A bare Pod with no baseline is refused, as 01.01 shows. A Deployment whose template has no baseline is accepted with a warning, and its ReplicaSet cannot create the Pod, as 01.04 shows. A canary is a Deployment like any other, so both rules apply to it unchanged. The reason is printed in the ReplicaFailure condition of the Deployment, where you look first.
Why this is "manual". The traffic share follows the replica ratio, the split is coarse, and you watch the metrics and run the
scalecommands. Nothing promotes or aborts on its own, and the ratio cannot give 5%. The automated form is 07.05: Argo Rollouts or Flagger drive the ramp from metrics, and a service mesh or Gateway API does weighted routing instead of replica ratio. The shape, two tracks behind one Service that you observe and then ramp or abort, is the same. The automation and the precision change.Lineage note. The canonical catalog is
10-catalog-deploy.yamlfrom 01.04.30-catalog-canary.yamlis the strategy demo, and the two must not run at once. To return to the canonical catalog, delete the canary file withkubectl delete -f examples/bookstore/raw-manifests/30-catalog-canary.yaml, then apply10-catalog-deploy.yaml. In the full Bookstore, a progressive delivery controller (07.05) replaces hand-run tracks.
Strategy comparison¶
| Strategy | Versions live | Traffic cut | Extra cost | Rollback | Use when |
|---|---|---|---|---|---|
| Recreate | 1 | 0→100% (downtime) | none | redeploy old | versions can't coexist; downtime acceptable |
| Rolling | 2 (briefly) | gradual by replica | ~maxSurge | rollout undo |
default for stateless; backward-compatible changes |
| Blue-green | 2 (full) | 100% flip | 2× during overlap | flip selector back (instant) | need instant cutover/rollback; can pay 2× |
| Canary | 2 (small canary) | small → ramped | ~canary size | scale canary to 0 (tiny blast) | risky change; have metrics + traffic split |
Decision shortcut: rolling by default; blue-green when you need an instant, atomic cut with instant rollback and can afford double; canary when the change is risky enough that you want real-traffic validation at <100% before committing; recreate only when two versions truly cannot run together (then pair with an expand/contract migration, 01.07, to avoid needing recreate at all).
How it works under the hood¶
- All of these are just label/selector and replica arithmetic on top of
01.04. Kubernetes core has no
"canary" or "blue-green" object. A rolling update is the Deployment
controller managing two ReplicaSets. Blue-green is two Deployments and a
Service whose
selectoryou patch to repoint endpoints atomically. Canary is two Deployments selected by one Service, with the split emerging from how many Ready endpoints each contributes. - The Service and EndpointSlice layer is what makes traffic move. A Service selects Pods by label. The EndpointSlice controller (00.04) keeps the endpoint set in sync with matching Ready Pods, and kube-proxy load-balances across them roughly evenly. So "10% to canary" means "the canary contributes about 10% of the Ready endpoints", and a blue-green flip means "change the selector, the endpoint set is recomputed, traffic follows". Full mechanics are in 02.02. Readiness probes (01.02) gate every one of these: an unready canary gets no traffic.
- Replica-ratio canary is statistical and coarse. Splitting by Pod count
means the minimum step is
1/(stable+canary), the split is only approximately even (connection reuse and keep-alive skew it), and which users hit the canary is random (there is no sticky or by-header routing). Precise, weighted or sticky canaries need an L7 router that does weighted traffic splitting (a service mesh, the Gateway API, or an ingress with canary annotations). That is the step to 07.05. - Promotion and abort are a control loop you, or a controller, close.
Nothing built in watches canary metrics and decides. By hand, you read the
dashboards and run
kubectl scale. With Argo Rollouts or Flagger, a controller runs the ramp as steps, queries Prometheus or SLOs as analysis, and auto-promotes or auto-aborts. That is the same canary control loop, automated and declarative (07.05), with less toil and less waiting for a person.
Production notes¶
In production: even with canary or blue-green, schema and API changes must be backward-compatible (expand and contract). During any of these strategies, two code versions hit the same database, and a destructive migration breaks the version that did not expect it. Decouple schema changes from code releases (01.07, 07.03).
In production: blue-green's instant rollback is only real if stateful side effects are handled. Sessions, in-flight jobs, cache warmth and, above all, database writes do not "flip back". Keep the old stack warm, drain connections on cutover (01.02, preStop and grace), and make sure writes from green are readable by blue if you might revert.
In production: a canary is worthless without good signals and an SLO. Define the metrics that gate promotion (error rate, p99 latency, saturation, a key business metric) before you canary, and compare the canary against the stable baseline at the same time, not against history. Without analysis, you have just done a slower rolling update with extra steps.
In production: prefer automated progressive delivery (Argo Rollouts or Flagger, with a mesh or Gateway for weighted routing) over hand-running
kubectl scale. People are slow and inconsistent at watching graphs at 3am. Automation gives precise weights, metric-gated promotion and automatic abort on an SLO breach (07.05). The manual canary teaches the concept; automation is the production form.In production: account for the cost and quota of the overlap. Blue-green needs about 2× capacity during the window. A canary needs its own headroom, plus enough stable capacity to avoid under-provisioning while you split. On EKS, GKE or AKS this transiently raises node count and spend (06.06). Size the
ResourceQuota(01.03) to allow the overlap, or the rollout stalls at admission.
Quick Reference¶
# manual canary by replica ratio (one Service selecting app=<APP>)
kubectl scale deploy/<APP>-canary -n <NS> --replicas=<N> # ramp up
kubectl scale deploy/<APP>-stable -n <NS> --replicas=<N> # ramp down
kubectl scale deploy/<APP>-canary -n <NS> --replicas=0 # ABORT
kubectl get pods -n <NS> -l app=<APP> -L track # the split
kubectl get endpointslices -n <NS> -l kubernetes.io/service-name=<SVC> \
-o jsonpath='{range .items[*].endpoints[*]}{.conditions.ready}{"\n"}{end}' \
| sort | uniq -c # ready vs not ready
# bad image: restore the last revision, then read the bad ReplicaSet
kubectl rollout undo deploy/<D> -n <NS>
kubectl rollout status deploy/<D> -n <NS>
kubectl get rs -n <NS> -l app=<APP>
# blue-green: flip a Service selector atomically
kubectl patch svc <SVC> -n <NS> -p '{"spec":{"selector":{"track":"green"}}}'
kubectl patch svc <SVC> -n <NS> -p '{"spec":{"selector":{"track":"blue"}}}' # rollback
# rolling: pin the new image by digest, never by a moving tag (01.04)
kubectl set image deploy/<D> -n <NS> <CTR>=<image>@sha256:<digest>
# recreate: spec.strategy.type: Recreate, only when versions cannot coexist
Manual canary, one Service and two tracks (the full manifest is in the hands-on section):
Service <APP> selector app=<APP>, no track key
Deployment <APP>-stable labels app=<APP>,track=stable replicas 9
Deployment <APP>-canary labels app=<APP>,track=canary replicas 1
each selector.matchLabels includes its own track
each Pod template carries the full baseline and a dedicated ServiceAccount
Checklist:
- Strategy matches the change's risk (rolling default; canary for risky)
- Service selector is the shared label; per-track label only for scaling
- Each Deployment's
selectorincludes the track label (owns only its Pods) - Every track carries the baseline and a dedicated ServiceAccount
- Endpoints checked for Pods you did not create (no stray
applabel) - Schema/API changes backward-compatible (two versions coexist safely)
- Canary gated on defined metrics vs. the live stable baseline
- Rollback path proven (canary to 0,
rollout undo), incl. side effects - Bad ReplicaSet kept to read; revisions pinned by digest
- Quota/cost allows the overlap; automate via progressive delivery in prod
Test your understanding¶
Try each before opening the answer drawer. The act of trying is the exercise; the answer is the check.
-
Why is "rolling update" still considered risky for a high-impact change, even though it's the default and has no downtime? What does canary add that rolling doesn't?
Show answer
A rolling update exposes 100% of users to the new version by the end of the rollout. If the new version has a subtle bug only some requests hit, you have already degraded everyone before you can detect it and roll back. Canary keeps a small slice of traffic on the new version, you watch real signals, and abort with near-zero blast radius before promotion. Rolling validates "the binary starts and probes pass"; canary validates "real traffic looks healthy" (see §Why this exists and §Mental model). -
In the manual-canary recipe, the Service selects only
app: catalogand nottrack: stable|canary. Why? What would change if the Service selector includedtrack: stable?
Show answer
With `app: catalog` only, both Deployments' Pods end up in the same Service's EndpointSlice. The replica ratio determines the traffic split. If `track: stable` were added to the selector, the Service would only route to stable Pods and the canary would get zero traffic, which defeats the whole point. The trick is that the Service selector is the *shared* label, while each Deployment's `selector.matchLabels` includes track to own its own Pods exclusively (see the hands-on section, step 1). -
A teammate applies a Pod with the full baseline and the label
app: catalogto test something. Within seconds the Service sends it requests. Which control should have stopped this, and why did the baseline not?
Show answer
The baseline says how a Pod runs: non-root, read-only root, no capabilities. It says nothing about who may run one, so a Pod that carries the baseline passes admission. What should have stopped it is RBAC that limits who can create Pods in the namespace, and every workload kind that makes them (Deployments, Jobs, CronJobs and DaemonSets, not only bare Pods) ([05.01](../05-security/01-authn-authz-rbac.md)), together with an admission rule that accepts the `app: catalog` label only from the release's ServiceAccount ([05.07](../05-security/07-admission-control-and-policy-as-code.md)). Narrowing the Service selector does not help, because the teammate's Pod matches it by design. After any unexpected Pod, check the endpoints. The extra Pod shows up there. -
A bad image lands on
catalog-stable, and you runkubectl rollout undo. The previous revision uses the tagbookstore/catalog:dev, which someone re-pushed last week. What did the rollback restore, and what did it leave in place?
Show answer
It restored the previous pod template, so the Pods use that image reference again. The reference is a moving tag, so you get whatever the tag points to now, which may not be the build you reviewed. Pinning the image by digest is the fix, as [01.04](04-replicasets-and-deployments.md) explains. The rollback also leaves in place the credentials and network reach the bad build used. They stay valid until you rotate them ([05.04](../05-security/04-secrets-and-cluster-hardening.md)). -
A 9-stable-1-canary split should give about 10% of requests to the canary, but the canary's
http_requests_totalcounter shows about 17%. List two plausible causes.
Show answer
(1) Connection reuse: a client that opened a connection to the canary keeps sending requests over it. On persistent connections the split is per connection, not per request. (2) Stable Pods went unready, so only 5 of the 9 stable Pods took traffic. With the canary Ready, that is 1 in 6, which is about 17%. Count the Pods per track with `kubectl get pods -L track`, as the Quick Reference shows. This is why precise weights need L7 weighted routing (see [how it works under the hood](#how-it-works-under-the-hood)).
Further reading¶
- Ibryam & Huß, Kubernetes Patterns (2nd ed.), the Declarative Deployment pattern: rolling, fixed (recreate), blue-green and canary, expressed declaratively, with their trade-offs.
- Rosso et al., Production Kubernetes, the chapter on application considerations: release and rollout strategy, and safe delivery in production. For the rolling-update foundation, see Lukša, Kubernetes in Action (2nd ed.), the chapter on Deployments.
- Pod Security admission:
https://kubernetes.io/docs/concepts/security/pod-security-admission/, which
defines the
restrictedlevel that refuses a canary without the baseline. - Official Deployment reference, with the strategy field and rolling back a Deployment: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/, and the canary discussion in https://kubernetes.io/docs/concepts/cluster-administration/manage-deployment/#canary-deployments.