DEV Community

Madhavam Saxena
Madhavam Saxena

Posted on

Rollback and Updating Deployment

Scaling a deployment:

kubectl scale <object_type> <object name> --replicas=<count_of_objects_needed>
Enter fullscreen mode Exit fullscreen mode

Updating a deployment:

kubectl get deployments
kubectl set image deployment <name_of_deployment> <name_of_new_image_that_you_want_to_give / old_image_name>=<actual_name_of_image(full path of repository)>:<tag>
Enter fullscreen mode Exit fullscreen mode

Now what it does is, this command basically looks for the actual_name_of_image on docker repository, and downloads it from there. Now after downloading it updates the current image of deployment with this downloaded image, and that's how a deployment is updated.

Note: Images are differentiated on the basis of tags, i.e K8s re-downloads the image if and only if a new tag is passed to it, if the tag is not mentioned while updating a deployment, it will not update the image. Hence, passing a tag while executing a set image command is necessary.

To check status of updation, one can execute:

kubectl rollout status deployment <name_of_deployment> 
                          OR
kubectl rollout status deployment/<name_of_deployment>
Enter fullscreen mode Exit fullscreen mode

Now, what if the tag passed doesn’t actually exists? I.e.

kubectl set image deployment/<name_of_deployment> <old_image_name>=<repo_name/image_name>:<some_random_value>
Enter fullscreen mode Exit fullscreen mode

It will still show that the deployment is updated although the deployment has not actually updated. To check if the deployment is actually updated, one can test it via:

kubectl rollout status deployment/<name_of_deployment>
Enter fullscreen mode Exit fullscreen mode

This will lead to an infinite loop of updation of deployment as, k8s follows rolling strategy, but since the tag passed doesn’t exists, the image will not be downloaded, and hence the new pod that's trying to spinnup will not get live, and the old pod will not terminate, and will be stuck in this.
In such cases, we need to rollback to the previous deployment by the following command:

kubectl rollout undo deployment/<name_of_deployment>
kubectl rollout status deployment/<name_of_deployment>
Enter fullscreen mode Exit fullscreen mode

We can check the history of deployment by following command:

kubectl rollout history deployment/<name_of_deployment>
Enter fullscreen mode Exit fullscreen mode

This wil list out all the deployments in the form of revisions.

To particularly inspect a deployment, we can use:

kubectl rollout history deployment/<name_of_deployment> --revision=<nth_deployment>
Enter fullscreen mode Exit fullscreen mode

This also prints the image which was used in that deployment.

Top comments (0)