Kubernetes

Kubectl Set Image Command

The kubectl set command is used to overwrite or set the given cluster. It allows the user to overwrite the property while working similarly to the kubectl run command. Therefore, the kubectl set image command updates or overwrites the container image. The set image command is used to make the deployment to update the image in Kubernetes.

In this article, the kubectl set image command will be discussed in detail, and with the help of examples, we will show the working and output of the command. Let’s begin learning.

Before we move forward, let’s discuss the prerequisites that are essential to execute the kubectl commands.

Prerequisites:

To design and develop Kubernetes, the Ubuntu operating system needs to be installed. We are using the Ubuntu 20.04 system, where a minikube is installed to run and execute the Kubernetes commands. Minikube allows you to execute the single node Kubernetes cluster in a virtual machine environment and makes it easier for you to run Kubernetes locally.

Once you have installed Ubuntu 20.04, start the terminal either by using the Ctrl+Alt+T command or by using the application bar of the operating system. After reaching the terminal, type in the ‘minikube start’ command to boost and launch the minikube cluster.

Below is the sample output of the ‘minikube start’ command to give you a basic idea of what you should expect while executing this command. The command, along with the output, is given in the cited below image.

Now that we have set up the system to execute the Kubernetes commands let’s begin to learn about the kubectl set image command. The first thing that you need to do is to check whether a deployment is already created or not. For that matter, you need to run the ‘get deployment’ command.

What is Kubectl Get Deployment?

The kubectl get deployment allows you to check whether a deployment has been created or not. When you execute the kubectl get deployment command, five inspected fields are returned, and they are Name, Ready, Up-To-Date, Available, and Age.

Name: The Name attribute lists all the deployment names present in the namespace.

Ready: The Ready attribute tells the available number of replicas to the user with the pattern of either desired or ready.

Up-to-date: The Up-to-date attribute tells the number of replicas that are updated to get the desired state.

Available: It tells the number of replicas readily available for the user

Age: It gives the number of times an application has run.

When we executed the get deployment command, it returned Name = nginx-deployment, Ready = 3/3, Up-To-Date = 3, Available = 3, and Age = 4m13s, which means an nginx-deployment is available with 3 replicas, and it has run for 4m13s so far. The command, along with the output, is given in the cited below image.

After inspecting the deployment in the cluster, the next step is to check the deployment rollout status. For that matter, the ‘kubectl rollout status deployment/ngnix-deployment’ command is executed. The command, along with the output, is given in the cited below image.

Note that the rollout of deployment will only trigger if the pod template of the deployment has changed or updated. Let us see how to update a deployment.

Updating a Deployment

The following steps must be performed in order to update the deployment:

Run the ‘kubectl set image deployment’ command to update the current deployment to nginx: 1.16.1 image. The set image command will overwrite the existing deployment and update it to the 1.16.1 image.

Below is the set image deployment command to update a deployment. The command, along with the output, is given in the cited below image.

However, below is the alternative option of set image command that can be used:

Both versions of the set image command will give the same output. Now, to see the status of the rollout, execute the required command. The command, along with the output, is given in the cited below image.

After successfully rolling out the deployment, the deployment can be viewed. The ‘kubectl get deployments’ command is used to get more details of the updated deployments. As discussed above, the command ‘get deployment’ will show the Name, Ready, Available, Up-to-date, and Age of the updated deployment. The command, along with the output, is given in the cited below image.

Now run the ‘kubectl get rs’ command and check what updates have been made to the deployment. The command, along with the output, is given in the cited below image.

Note that, in the example given above, when a pod is updated by the deployment, the old set of replicas is scaled down to 0 replicas, while a new set of replicas is created and scaled up to 3 replicas.

Rolling Back a Deployment 

After rolling out the deployment, there might be a chance that you want to roll back the deployment. This can happen due to several reasons, and one of them could be unstable deployment like crash looping.

Luckily, the system has recorded all the rollout history so that if you want to roll back at any time, you can do that without getting in any trouble. The revision history of deployment is created when the rollout of the deployment is triggered, which means the new revision will be created only when the pod template of deployment is changed.

Suppose you have mistakenly entered nginx:1.161 instead of nginx:1.16.1 resulting in a stuck rollout. However, the following output still is generated after executing the ‘kubectl set image deployment’ command. The command, along with the output, is given in the cited below image.

Now execute the ‘kubectl get rs’ command in order to see the rollout status. It will list all the status of rollouts present in the namespace. The command, along with the output, is given in the cited below image.

Conclusion:

This article has covered the details of the set image command that works with deployment. The commands with the corresponding output are given above, and each one of them is explained in detail so that you do not have any problems while executing them on your own. Run and play with the commands to understand them quickly and easily.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content