In this lesson, you will learn how to trigger an immediate "undo" button in Kubernetes to revert your application to a stable state when a new deployment introduces unexpected bugs.
The Mechanism of Rollbacks
When you perform a rolling update on a Deployment, Kubernetes doesn't just delete your old Pods and spin up new ones instantly. Instead, it creates a new ReplicaSet for the new version while keeping the old ReplicaSet intact as a historical record. This separation is the secret to how Kubernetes can perform instant rollbacks.
Because the previous ReplicaSet still exists in the background, "rolling back" is simply a matter of telling the Deployment controller to shift the traffic and scale back up the previous version’s ReplicaSet while scaling down the problematic one.
Performing the Undo
If you deploy a new version and notice your application behaving poorly, you can revert the entire Deployment to its previous state with a single command:
kubectl rollout undo deployment/my-app-name
This command instructs the Deployment to transition back to the revision immediately preceding the current one. If you need to return to a specific, older version (for example, if you want to skip over the last two updates), you can first view your deployment history:
kubectl rollout history deployment/my-app-name
Once you identify the revision number you want (e.g., revision 2), you can target it specifically:
kubectl rollout undo deployment/my-app-name --to-revision=2
Why it Works
This process is instantaneous because the underlying containers and configuration for the previous revision are already stored within the cluster's metadata. You aren't rebuilding images or redeploying code from scratch; you are simply instructing the control loop to reconcile the cluster toward a previous Desired State.
Because this is a native Kubernetes feature, the rollback process follows the same rules as your rolling updates, ensuring that the transition remains graceful and maintains your application’s availability even during the recovery phase.
Key Takeaway: Kubernetes preserves past Deployment configurations as historical ReplicaSets, allowing you to instantly revert to a stable version using the kubectl rollout undo command.