Kubernetes system records every update for Deployment:
- We can list all of the revisions with the subcommand rollout:
// check the rollout history
$ kubectl rollout history deployment simple-nginx
deployments "simple-nginx"
REVISION CHANGE-CAUSE
1 <none>
2 kubectl edit deployment simple-nginx --record=true
3 kubectl set image deployment simple-nginx simple-nginx=nginx:stable --record=true
You will get three revisions, as in the preceding lines, for the Deployment simple-nginx. For Kubernetes Deployment, each revision has a matched ReplicaSet and represents a stage of running an update command. The first revision is the initial state of simple-nginx. Although there is no command tag for indication, Kubernetes takes its creation as its first version. However, you could still record the command when you create the Deployment.
- Add the flag --record after the subcommand create or run.
- With the revisions, we can easily resume the change, which means rolling back the update. Use the following commands to rollback to previous revisions:
// let's jump back to initial Deployment!
// with flag --to-revision, we can specify which revision for rollback processing
$ kubectl rollout undo deployment simple-nginx --to-revision=1
deployment.apps "simple-nginx"
// check if the rollback update is finished
$ kubectl rollout status deployment simple-nginx
...
deployment "simple-nginx" successfully rolled out
// take a look at ReplicaSets, you will find that the old ReplicaSet takes charge of the business now
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
simple-nginx-585f6cddcd 5 5 5 4h
simple-nginx-694f94f77d 0 0 0 4h
simple-nginx-b549cc75c 0 0 0 3h
// go ahead and check the nginx webpage or the details of Deployment
$ curl $SERVICE_URL
$ kubectl describe deployment simple-nginx
- Without specifying the revision number, the rollback process will simply jump back to previous version:
// just go back to previous status
$ kubectl rollout undo deployment simple-nginx
deployment.apps "simple-nginx"
// look at the ReplicaSets agin, now the latest one takes the job again
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
simple-nginx-585f6cddcd 0 0 0 4h
simple-nginx-694f94f77d 0 0 0 4h
simple-nginx-b549cc75c 5 5 5 4h