Kubernetes

Kubernetes Restart Deployment

The purpose of this article is to help the user who wants to know how to restart the deployments in Kubernetes. Here, we will learn how to restart the pod deployment in Kubernetes. We occasionally find ourselves in situations where the container is not working properly. In this case, we need to restart the deployment of pods efficiently for better output.

Pre-requisites

Ubuntu’s latest version must be installed on your system and your system must be a 64-bit operating system. If you are a Windows user, you should first install Virtual Box in your system because it allows us to install any other operating system virtually alongside the one that is currently running on the machine. After installation, run the Kubernetes library on your system. You must be very familiar with Kubernetes, Kubectl, pods, and container concepts to understand this topic correctly.

How Do We Restart the Deployment in Kubernetes?

We only restart the deployment if any trouble occurs while running the pods. We will explain the whole process of restarting a deployment in different steps and how kubectl can do just that very quickly.

Step 1: Start Kubernetes Local Container

In this step, we will discuss that we will create a local container minikube environment in Kubernetes by running the command.

> minikube start

Text Description automatically generated

The output is shown in the above-attached screenshot. A local container was created successfully in Kubernetes.

Step 2: Create a YAML File or Deployment Configuration File

In this step, we will discuss how we will create a local container minikube environment in Kubernetes by running the attached command. A local container was created successfully in Kubernetes.

> nano deplo. yaml

We created a pod that acts as Kubernetes. After command execution, the YAML file opens on the system. The YAML file contains the name of the container, the image value of the container and many other specifications, as we can see in the screenshot below.

Text Description automatically generated

Step 3: Create Deployment in Kubernetes

In this step, we run the configuration file to create or install the deployment in the system by running the command in the system with the help of kubectl.

~ kubectl create -f deplo. Yaml

Following command execution, the result appeared as shown in the attached screenshot. In this way, we create the deployment in the system. This pod is now running in the system as “ngnix-deployment.”

Step 4: Enlist Running Deployments in the Kubernetes System

With every step, we will move towards our main step. So, first, we determine how many deployments are active in the system. So, we run the command to check the command’s visibility.

> kubectl get deployments

The result after command execution is shown in the attached screenshot above. As we can see, only two deployment pods are running at this time. Our created deployment is shown in the list with an age of “10s.’

Step 5: Deployment Kubectl Scaling with Replicas = 0

This is the method through which we restart the pods in Kubernetes. When there is no YAML file, we may instantly restart the pods in Kubernetes by scaling the number of deployment replicas to zero, which immediately stops all pods. We will run the command now. It will all the running pods from Kubernetes.

> kubectl scale deployment ngnix-deployment –replicas=0

By executing this command, we scaled the deployment in Kubernetes. The name of the module is nginx-deployment. In the command, we set the replicas value to zero, which means we are scaling down the deployment, which will remove all existing pods. Wait until all pods have finished.

Step 5: Deployment Kubectl Scaling with Replicas = 3

In this step, we run the command to scale or restart the deployment by setting the replicas value to 3. The command is:

> kubectl scale deployment nginx-deployment –replicas=3

In the screenshot, by executing this command, three pods are initialized one by one because the value of replicas is 3.

Step 6: Restart Deployment by Using the Rollout Restart Method

In this step, we will restart the pods by using the Kubectl rollout restart method without having any influence on deployments. We execute the command to restart the pod deployment.

> kubectl rollout restart deployment nginx-deployment

As seen in the preceding command, the pod’s name, Nginx-deployment, is successfully restarted.

Step 7: Show a List of Running Pods in Kubernetes

In this step, we will run the command to see if all these pods were created or not. So, the command is:

> kubectl get pods

Text Description automatically generated

Following command execution, we will see a list of all the pod deployments that have been restarted, along with their names. So, in this way, we can restart the pod deployment easily in Kubernetes. As seen in the screenshot above, all of the pods are currently running but only one is reporting an error.

Step 8: Delete Pod from Kubernetes

This step teaches us how to delete the pod that is causing an error in the Kubernetes deployment. For pod deletion, we run the command:

> kubectl delete pods --field-selector=status.phase=Failed

As in the screenshot, we saw that we could delete the pod easily just by giving it the status “failed.” Now, all pods are running perfectly on Kubernetes.

Step 9: Update the Deployment Environment

In this step, we will learn how to update the environment of any deployment easily in Kubernetes. We run the following command to change the pod environment.

> kubectl set env deployment nginx-deployment APP_VERSION=”2

The environment for pod deployment is successfully updated when we run the command, as shown in the attached screenshot.

Conclusion

We could easily restart the deployment by using the rollout restart method. As previously stated, we terminate and then restart the deployments in Kubernetes. For a better understanding of pod deployment restart, you can also practice all these examples in Kubernetes.

About the author

Kalsoom Bibi

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