Kubernetes

kubectl – How to Restart the Pod

A pod can also have one or more containers, one of which is the application container, and the others are the init container, which halts after it completes a job or the application container is ready to perform its function, and the sidecar container, which is affixed to the primary application container.  A container or pod will not always leave due to an application failure. In scenarios like this, you will need to restart your Kubernetes Pod explicitly. In this guide, you will explore how to force pods in a deployment to restart using several ways.

Pre-requisites

To restart the pod using kubectl, make sure you have installed the kubectl tool along with the minikube cluster. Otherwise, you will not be able to implement the prescribed article.

Note: We are implementing this guide on Ubuntu 20.04 Linux system. However, the operating system may vary as per your needs.

Methods to restart pods using Kubectl

To restart pods using Kubectl, you have to first run the minikube cluster by using the following appended command in the terminal.

$ minikube start

This process will take some time, so you have to wait for some time to complete the process effectively. Now we are ready to list the pods using the affixed command.

$ kubectl get pods

You can check that for the time being; only one pod is present in the system. Now we will create a deployment. So, initially, we have to build a configuration file for deployment. Create the file in the home directory of your system by the affixed command.

$ touch deployment.YAML

The file will be created in your home directory. Open it and write down the below-attached script in the configuration file and save it.

Now create the deployment by the following affixed kubectl command.

$ kubectl create –f deployment.yaml

The deployment will be created successfully, as shown in the attached image. Now again, enlist all the pods of your system by using the affixed kubectl command.

$ kubectl get pods

Check out that now 2 pods are running as displayed in the above screenshot.

Method 1:

A rolling restart will be used to restart each pod in order from deployment. This is the most recommended strategy because it will not cause a service break. Write the below-affixed command in the terminal.

$ kubectl rollout restart deployment <deployment name>

Replace the <deployment name> according to your need.

The command mentioned above will restart it. Your app will be accessible since most of the containers will be functioning.

Method 2:

The second method is to compel pods to restart and synchronize with the modifications you made by setting or changing an environment variable.

$ kubectl set env deployment <deployment name> DEPLOY_DATE="$(date)"

Replace the <deployment name> according to your need.

Method 3:

Reducing the number of deployment copies to zero and scaling back up to the appropriate state is another method for restarting Pods. This compels all current pods to cease and terminate, followed by the scheduling of fresh pods in their place. Limiting the number of copies to 0 will result in an outage. Hence a rolling restart is advised. Use the following appended command to set a deployment’s replicas to 0.

$ kubectl scale deployment <deployment name> --replicas=0

Replace the <deployment name> according to your need.

The command scale specifies the number of replicas that should be active for each pod. It effectively shuts down the process when users set it to zero. To start the said pod again, we are going to set its replica value more than 0.

$ kubectl scale deployment <deployment name> --replicas=1

Replace the <deployment name> according to your need.

Conclusion

Kubernetes is an effective container orchestration platform. However, difficulties do arise, as they do with all systems. So, restarting your pod will not resolve the fundamental issue that caused it to fail in the first place, so be sure to identify and resolve the root cause. I hope you can easily restart your pod by following any of the methods explained in this guide.

About the author

Kalsoom Akhtar

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