However, occasionally developers are required to restart the Pod due to several unknown errors, recover the application from downtime, or implement new configurations. Unfortunately, there is no single command like “kubectl restart pod” to restart the Pod. However, there are many indirect methods to restart or reconfigure the Pods in Kubernetes.
This blog will demonstrate:
- What are the States of Pods
- Reasons to Restart the Pod
- How to Restart a Pod by Kubernetes Restart Policy?
- How to Restart a Pod by Applying Yaml Manifest?
- How to Create Deployment in Kubernetes?
- How to Restart a Deployment Pod with Kubectl
- Conclusion
What are the States of Pods?
Several states of Pods are specified in Kubernetes that can be used to analyze the Pod’s health and status. Following are the states of Kubernetes Pods:
- Pending: No container is started and waiting to start the container inside the Pod or the pod is created and waiting for essential resources.
- Running: When all resources are allocated to the pod and all containers inside the pod are in a running state.
- Succeed: When all containers within the pod complete their processing and are successfully terminated.
- Failed: If the pod’s container is terminated with an error, the pod status will be failed.
- Unknown: When the state of the pod cannot be analyzed. Usually, this error occurs due to communication breaking with the Kubernetes node.
Reasons to Restart the Pod
Pods are essential components of the Kubernetes cluster and they must work properly. If it stops working, it can create critical downtime for larger applications and may affect the application traffic. Here are several reasons, why developers are usually required to restart the Pod:
- Configuration Changes: When developers are required to configure or apply the changes, they are required to manually restart the Kubernetes Pod.
- Application Updates: When an application running in the Pod applies updates or needs to run an update, the Pod should be restarted to update the application (Deployment Scenario).
- Troubleshooting: To troubleshoot the containerized application running inside the pod, restarting the pod is essential.
- State Cleanup: To clean up the Pod state, the user may need to restart the Kubernetes Pod.
- Stuck in Terminating State: Sometimes, the pod may be stuck in a terminating state, in such a situation, restarting the pod is essential.
- Clear Resource Constraint: To access and address the resource constraints, pods may need to restart.
How to Restart a Pod by Kubernetes Restart Policy?
Pods can be executed within a Kubernetes Deployment and independently without any Deployment. Managing and restarting the independent Kubernetes pod is a little tricky as these pods do not have ReplicaSet to match the desired and current state of Pods. To process Kubernetes Pods effectively, the user can specify the Kubernetes restart policy in Pods.
The developer can apply the Restart policy through the command line and also in the Yaml file under the “spec” key. The Restart policy can have the listed values:
- Always: It will always restart the Kubernetes pod when it terminates.
- OnFailure: It will restart the Pod only when any failure occurs.
- Never: It will never restart the Pod.
To apply the restart policy on Pod, go through the below steps.
Step 1: Launch PowerShell
First, launch the PowerShell with administrative privileges via the Start menu:
Step 2: Start Kubernetes Cluster
Start the Kubernetes cluster using the “minikube start” command:
Note: At this point, the user may face an error while starting the cluster. To fix the error, make sure the Docker engine is running in the background and run the “minikube delete” command. This command will delete the existing cluster and recreate it from scratch:
Step 3: Start the Pod
Now, run the “kubectl run <pod-name> ‐‐image=<image-name> ‐‐restart=<restart-value>” command. Here, the “‐‐image” option specifies the image to run the container inside the pod, and the “‐‐restart” option specifies the Kubernetes restart policy:
Step 4: Access the Pods
To access the Kubernetes pod, use the given command:
Note: When the pod is deleted, it can not be configured again and users are required to recreate it. However, running a pod with a Yaml manifest is the best choice to recreate or restart the pod.
How to Restart a Pod by Applying Yaml Manifest?
Creating and applying changes through the Yaml manifest is useful for configuring new updates without deleting the existing pod, and when a user unintentionally deletes the pod, it can be easily recreated through the Yaml manifest.
To create the pod using the Yaml configuration file and to restart the pod by applying the modification, go through the following steps.
Step 1: Create “pod.yml” File
Create a file named “pod.yml” and add the given instructions to the file:
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:stable-perl
ports:
- containerPort: 80
Step 2: Create the Pod
Navigate to the directory where the “pod.yml” file exists:
Next, use the “kubectl create -f <filename.yml>” command to create a pod:
For verification, access the pods using the “kubectl get” command:
Here, the pod restart status is “0” as we have just created the pod:
Step 3: Make Modification
Now, open the “pod.yml” file, and make some changes such as configure the environment variable, or change the container image. For instance, we have modified the container image:
Step 4: Restart the Pod to Apply Configurations
Apply the changes on Kubernetes Pod using the “kubectl apply -f <pod.yml>” command:
Now, access the pod and verify whether the changes are applied and whether the pod is restarted or not:
The pod’s “RESTART” status is “1” which means we have successfully restarted the pod:
How to Create Deployment in Kubernetes?
To restart the Pod of Deployment, users must have at least one Deployment in the Kubernetes cluster. If you have already created the deployment, you can skip this section.
To create a Kubernetes deployment, follow the listed steps.
Step 1: Create “deployment.yml” File
Create a file named “deployment.yml” file and paste the below code into the file:
kind: Deployment
metadata:
name: html-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: html
image: rafia098/html-img:1.0
ports:
- containerPort: 80
Following is the description of the above code:
- The “kind” key is set as “deployment” to create a new Deployment.
- The “name” key sets the name of the Kubernetes Deployment.
- The “labels” key is used to label the deployment object.
- The “replicas” key is used to specify the number of pod replicas.
- The “containers” key sets the container configuration. It is used to specify the essential information for containerized applications such as container name, image, and container’s exposed port.
Step 2: Create Deployment
To create a deployment, use the “kubectl create -f <filename.yml>” command:
Step 3: Verification
To check if the deployment is created or not, get the Kubernetes deployment using the below command:
When Deployment is created, it creates ReplicaSet and pods. To access the ReplicaSet, use the “kubectl get rs” command:
To get the pods, use the below command:
The above-shown pod is created through deployment and can be recreated or restarted through various methods.
How to Restart a Deployment Pod With kubectl?
The “kubectl” is a command line tool that manages and operates Kubernetes resources. To restart a Pod with kubectl, go through the listed methods:
- Restart a Pod by Restarting the Deployment
- Restart a Pod by Deleting the Pod
- Restart a Pod by Down and UP Scaling
- Restart a Pod by Rolling Back to the Previous Deployment
- Restart a Pod by Setting the Environment Variable
Method 1: Restart a Pod by Restarting the Deployment
To restart the Kubernetes pod, users can restart the Deployment. Restarting deployment restarts the ReplicaSet and recreates the pods.
To restart the Pod by restarting deployment, the user can use the “kubectl rollout restart deployment/<deployment-name>” command:
For verification, access the pod using the “kubectl get” command. In the above section, the pod created by deployment has a different pod name, and the below pod has a different name which means on deployment restart, a pod is recreated:
Method 2: Restart a Pod by Deleting the Pod
Within deployment, whenever a pod is deleted or terminated due to some unknown reason, the deployment auto-healing mechanism recreates the pod with the help of ReplicaSet. The ReplicaSet is a controller of deployment that is responsible for matching the desired state with the current state of the pod.
To restart the pod, first get the pods live. To do this, use the “-w” option along with the “kubectl get pods” command:
Open another PowerShell window and use the “kubectl delete pod <pod-name>” command to delete the pod:
Launch the PowerShell window where the pod’s status is live. You can notice that, when the pod is deleted, the ReplicaSet automatically recreates and executes the new pod to match the current pods status to the desired status:
Method 3: Restart a Pod by Down and UP Scaling
Scaling the deployment can allow the user to specify the number of pod replicas. Users can effectively stop the running pods by scaling the deployment to zero and can recreate and restart the Pods by scaling the deployment and specifying the pod replicas.
To restart the pod, first scale the deployment and set its replicas for the pod to “0”. For this purpose, run the below command:
This will remove the deployment pod as shown below:
After that, again scale the deployment and run the required number of pod replicas by using “kubectl scale deployment <deployment-name> –replicas=<number of pod replicas>” command:
Now, again access the kubernetes pod using below command:
The difference in pod name verifies that we have successfully restarted the pod:
Method 4: Restart a Pod by Rolling Back to the Previous Deployment
Rolling back to the previous version of deployment for restarting the pod only is not a good option. However, the user can use the rollback mechanism to restart the pod.
To restart the pod by rolling back the deployment to the previous version, utilize the “kubectl rollout undo deployment <deployment-name>” command. This command will restore the previous deployment version and restart the pod according to deployment configurations:
Now, access the Kubernetes pods using the below command:
Method 5: Restart a Pod by Setting the Environment Variable
Setting the new environment variable, or modifying the Deployment configuration can recreate and restart the Kubernetes pod.
To restart the pod, the user can set the environment variable of deployment using the below command. For instance, we have set the “USER” environment variable:
For verification, get the Kubernetes pods using the below command:
We have covered the methods to restart the pods with kubectl.
Conclusion
To restart the pod, the user can either use the Kubernetes restart policy or apply the configuration on the pod using the “kubectl apply” command. To restart the deployment’s pod, different methods can be utilized such as restarting the Deployment, deleting the Pod, down and up Scaling, rolling back to the previous deployment, or restarting a pod by setting the environment variable or other configurations. This post has illustrated the methods to restart a pod with kubectl.