Kubernetes Deployment rollout and rollback¶
Kubernetes Deployment rollout and rollback refers to the mechanisms used to update application code or configuration progressively and revert to a previous state if issues arise.^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
Deployment Updates and Revisions¶
A Deployment is a Kubernetes resource that provides declarative updates for Pods and ReplicaSets.^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md] When the Pod template (.spec.template) of a Deployment is updated—for example, changing the container image version—Kubernetes initiates a rollout process.^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
During this process, the Deployment controller creates a new ReplicaSet to manage the updated pods and gradually transitions the state from the old ReplicaSet to the new one at a controlled rate.^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md] Not every update triggers a new history revision; only changes to the .spec.template field are recorded in the rollout history, whereas scaling operations (changing .spec.replicas) are not.^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
Monitoring Rollout Status¶
The status of a deployment update can be checked using the rollout status command.^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
[kubectl](<./kubectl.md>) rollout status deployment <deployment-name>
If the update is successful, the command will confirm that the deployment "successfully rolled out".^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md] However, if the configuration contains an error or results in an unstable state (such as an image that cannot be pulled), the command will wait until a timeout occurs.^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]
Rollback History¶
Kubernetes maintains a history of the changes applied to a Deployment, referred to as Revisions.^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md] Users can view this history to see the sequence of updates:
[kubectl](<./kubectl.md>) rollout history deployment <deployment-name>
This command lists the revision numbers and the change cause (if the --record flag was used during the update).^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md] To view specific details for a particular revision, use the --revision flag^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]:
[kubectl](<./kubectl.md>) rollout history deployment <deployment-name> --revision=<revision-number>
Performing a Rollback¶
If a new version introduces instability—such as a ImagePullBackOff error due to an invalid image tag—a rollback can be performed to revert the Deployment to a previous, stable state^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md].
The rollout undo command is used to initiate a rollback^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]:
# Rollback to the previous revision
[kubectl](<./kubectl.md>) rollout undo deployment <deployment-name>
# Rollback to a specific revision
[kubectl](<./kubectl.md>) rollout undo deployment <deployment-name> --to-revision=<revision-number>
Once the rollback is executed, Kubernetes updates the Deployment to use the Pod template from the specified previous revision, effectively restoring the application to its earlier configuration^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md].
Related Concepts¶
Sources¶
^[400-devops__06-Kubernetes__k8s-ithelp__Day8__README.md]