Open In App

How to Resart Pod In kubernetes?

Last Updated : 30 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Kubernetes is an open-source container orchestration tool that helps in scaling, deploying, and managing containerized applications. In Kubernetes, the pods are the smallest deployable unit. In this guide, I will first discuss what Kubernetes is. Then I will discuss what is pods in Kubernetes. After this, I will walk you through the different steps to restart a pod in Kubernetes.

What is Kubernetes?

Kubernetes is a container orchestration tool that helps in scaling, deploying, and managing containerized applications. Kubernetes follows a master-slave architecture. Here, the master node, also called the control plane, manages all the cluster operations. On the other hand, worker nodes help in deploying and executing containers. The master node consists of 4 main components, the API server, which helps in interacting with the Kubernetes cluster; the scheduler, which helps in scheduling the pods to run on which node; ETCD, which stores all the Kubernetes cluster data; and controls managers, which help in managing different controllers like deployments, replica sets, and many more. Apart from this, in the worker node, there is kubelet, which helps in managing the pod lifecycle on a node; container runtime, which helps in running the containers; and kube-proxy, which helps in managing the network rules in the pods.

Kubernetes provides a variety of features, such as:

  • Autoscaling: Kubernetes provides both a horizontal pod autoscaler and a vertical pod autoscaler to scale up or down based on the amount of traffic the application receives.
  • Autohealing: Kubernetes automatically starts a new pod if there is any pod failure or deletion.
  • High Availability: Kubernetes provides a replica set controller, which helps in maintaining the desired number of pods running on a cluster.
  • Deployment process: Kubernetes helps automate the deployment process by rolling out new updates with zero downtime.
  • Roll Back: Kubernetes allows you to roll back with zero downtime to the previous version of the application if any issues occur in the current version of the application.

What is Pod in Kubernetes?

Pod is a smallest deployable unit in the Kubernetes . Pods encapsulate the application container , storage , network and other configurations . Each pod has its own IP . These pods can also interact with each other with these IP addresses . Kubernetes manages the lifecycle of the pods . Based on the traffic an application receives these pods can be scaled up or scaled down . Controllers like Deployment controllers helps in scaling , restarting and recovering pods form failures . In summary we can say pods are the simple deployable units which provides a flexible and scalable framework for orchestrating and deploying the containerized application in Kubernetes .

Pre-requisites

Before moving to next section make sure you have installed Minikube on your system . If not installed follow this Geeks For Geeks article Kubernetes Minikube to install Minikube on your system .

Steps to Restart Pod In Kubernetes

Method 1: Restart By Deleting Pod

Step 1: Create a cluster using minikube.

minikube start

minikube-start

Step 2: Create a deployment.

kubectl create deployment nginx-deployment --image=nginx --replicas=1

create-deployment

Step 3: See pod the status using the command below.

kubectl get pods

get-pods

Step 4: Delete a pod from the cluster.

kubectl delete pod <pod-name>

delete-pod

Step 5: Again see the pod status. You will observe even the pod is deleted, another pod will automatically run.

kubectl get pods

automatically-restarted-

Method 2: Restart Pod By Using Rollout Restart

You can also restart a pod by using kubectl rollout restart . Follow the step below to restart a pod:

Step 1: Create a deployment.

kubectl create deployment nginx-deployment --image=nginx --replicas=1

create-deployment

Step 2: Restart the pod using kubectl restart rollout. Here the running pod will be terminated and a new pod will immediately starts running.

kubectl rollout restart deployment nginx-deployment

rollout-restart

Method 3: Restart Pod By using Deployment Scale Methods

You can also scale your deployment to 0, to delete a pod and again scale it desired number of pods. Follow the steps below to restart a pod.

Step 1: Create a deployment.

kubectl create deployment nginx-deployment --image=nginx --replicas=1

create-deployment

Step 2: Scale the deployment to 0.

kubectl scale deployment/nginx-deployment --replicas=0

scale-to-0

Step 3: Again scale the deployment to desired number of pods.

kubectl scale deployment/nginx-deployment --replicas=1

restarted-pods

Method 4: Restart Pod By Updating Image

You can also set the image to new latest image to restart the pod.

Step 1: Create a deployment.

kubectl create deployment nginx-deployment --image=nginx --replicas=1

create-deployment

Step 2: Update the image to restart the pod.

kubectl set image deployment/nginx-deployment nginx=nginx:latest

set-image

Method 5: Restart Pod By Replacing Specified Pod

You can also replace the pod specified to restart the pod. Follow the steps below.

Step 1: Create a deployment.

kubectl create deployment nginx-deployment --image=nginx --replicas=1

create-deployment

Step 2: Get the pod name and replace it.

kubectl get pod nginx-deployment-6c878976f6-klcw7 -o yaml | kubectl replace -f -

replace

Conclusion

Here is this article you have first learned what is Kubernetes. Then you have learned what is pods in Kubernetes. After this you have create deployment on Kubernetes cluster. Then to restart a pod, you have deleted a pod from the Kubernetes cluster. Finally you have observed a new pod starts running automatically after deletion of the pod.

How to Restart Pod In Kubernetes? – FAQs

How to delete a Pod from Kubernetes cluster?

To delete a Pod in Kubernetes cluster you have to use the command “kubectl delete pod <pod-name>“.

What are the different problems you face when there is frequent pod restart?

Frequent pod restart may impact the application performance and may cause temporary downtime.

Is there any command which helps in restarting the pod directly?

No there isn’t a direct command which will directly restart a pod.

How to check pod logs?

To check pod logs you have to use “kubectl logs <pod-name>“.

What are the roles of deployment controller?

Deployment controller automatically manage the pod lifecycle such as restarts, scaling and recovery from the pod failures.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads