Sub topic 12
Rolling back a bad release
This chapter assumes you can read a GitHub Actions workflow, know what a Deployment and a ReplicaSet are, and have used kubectl rollout status. A rollout can pass the pipeline and still fail in production. Rollback is how you return to the last known-good version quickly.
How Kubernetes tracks revisions
A Deployment keeps old ReplicaSets, one per pod template it has used. Each ReplicaSet is a revision. A rollback scales the old ReplicaSet back up and the current one down, using the same rolling update strategy as a normal deploy.
Only changes to .spec.template create a revision. Changing replicas does not. The number of kept revisions is set by .spec.revisionHistoryLimit, which defaults to 10. If you set it to 0, you cannot roll back.
The rollback commands
kubectl rollout history deployment/web -n shop
kubectl rollout undo deployment/web -n shop
kubectl rollout status deployment/web -n shop --timeout=120shistorylists revisions. Add--revision=Nto see the pod template of one revision, including its image.undoreturns to the previous revision. Use--to-revision=Nto pick a specific one.statuswaits for the reverted rollout to finish. Always run it, becauseundoreturns as soon as the change is accepted.
The CHANGE-CAUSE column of history is usually <none>. To fill it, set the kubernetes.io/change-cause annotation on the Deployment during the deploy step.
Rolling back automatically from the workflow
kubectl rollout status exits with a non-zero code when the rollout does not finish within the timeout. You can use that to trigger an undo.
- name: Update image
run: |
kubectl annotate deployment/web -n shop \
kubernetes.io/change-cause="deploy $GITHUB_SHA" --overwrite
kubectl set image deployment/web web="$IMAGE:$GITHUB_SHA" -n shop
- name: Wait for rollout
run: kubectl rollout status deployment/web -n shop --timeout=120s
- name: Roll back on failure
if: failure()
run: |
kubectl rollout undo deployment/web -n shop
kubectl rollout status deployment/web -n shop --timeout=120sThe if: failure() condition runs the step only when an earlier step failed. The job still ends red, which is what you want: the release failed, even though the service recovered.
The deployer from the RBAC chapter needs permission for this. undo patches the Deployment, which the patch and update verbs already allow. Reading ReplicaSets needs get, list and watch on replicasets in the apps group. Add that rule if undo returns a forbidden error.
Common errors
- Rolling back to the same bad image. If the image tag is mutable, such as
latest, the old revision may pull the new broken content. SHA tags avoid this. - Undoing twice.
undogoes to the previous revision, which after one undo is the bad one. Running it again redeploys the bad release. Checkhistoryfirst. - Config and schema changes. Rollback only reverts the pod template. It does not revert a ConfigMap changed in place, or a database migration.
- Pipeline drift. The cluster now runs an older SHA than
main. The next push redeploys the new code, so fix or revert the commit.
Exercise
A deploy sets the image to a new SHA, and kubectl rollout status times out. Why does the workflow above still show a failed run after a successful rollout undo, and why is that desirable?
Show answer
The rollout status step failed, and the if: failure() step only recovers the cluster. The job result stays failed. That is desirable because the release did not ship, and the team must see it and fix the commit.
Exercise
kubectl rollout undo is reported as forbidden for the deployer. Which resource and verbs should you check in the Role?
Show answer
Check that the Role allows get, list and watch on replicasets in the apps API group, and patch or update on deployments.