
Updating the YAML and using kubectl apply
For demo purposes, copy deploy.yaml to deploy_1.12.2.yaml and change the nginx version to 1.12.2, as follows:
image: nginx:1.12.2
Then run the kubectl apply command with the --record option:
$ kubectl apply -f deploy_1.12.2.yaml --record
deployment.apps "my-nginx" configured
This will perform the same thing as the kubectl set image command, so you can see that the nginx image version has been bumped up to 1.12.2; also, the OldReplicaSets/NewReplicaSet combination has been changed as follows:
$ kubectl describe deploy my-nginx
Name: my-nginx
…
…
Pod Template:
Labels: run=my-nginx
Containers:
my-nginx:
Image: nginx:1.12.2
...
...
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True ReplicaSetUpdated
OldReplicaSets: my-nginx-77769b7666 (3/3 replicas created)
NewReplicaSet: my-nginx-69fbc98fd4 (1/1 replicas created)
After a few moments, NewReplicaSet will be ready. Then there will be a total of three ReplicaSets existing on your system:
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
my-nginx-54bb7bbcf9 0 0 0 7m
my-nginx-69fbc98fd4 3 3 3 1m
my-nginx-77769b7666 0 0 0 6m
You can also see the rollout history:
$ kubectl rollout history deployment my-nginx
deployments "my-nginx"
REVISION CHANGE-CAUSE
1 kubectl create --filename=deploy.yaml --save-config=true --record=true
2 kubectl set image deployment/my-nginx my-nginx=nginx:1.12.0 --record=true
3 kubectl apply --filename=deploy_1.12.2.yaml --record=true
Whenever you want to revert to a previous ReplicaSet, which means rolling back to the previous nginx version, you can use kubectl rollout undo with the --to-revision option. For example, if you want to roll back to revision 2 in your history (kubectl set image deployment/my-nginx my-nginx=nginx:1.12.0 --record=true), specify --to-revision=2:
$ kubectl rollout undo deployment my-nginx --to-revision=2
deployment.apps "my-nginx" rolled back'
A few moments later, Deployment will deactivate the current ReplicaSet, which uses the Pod template with nginx version 1.12.2, and will then activate the ReplicaSet which uses nginx version 1.12, as follows:
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
my-nginx-54bb7bbcf9 0 0 0 8m
my-nginx-69fbc98fd4 0 0 0 2m
my-nginx-77769b7666 3 3 3 7m