Kubernetes is an open-source container management system that allows you to manage a cluster of virtualized servers. In the event your Kubernetes service fails or goes offline, this article will guide through how to restart pods in Kubernetes.
The “restart pod kubectl” command allows users to restart a specific pod in Kubernetes. This is useful when pods crash or if the user wants to restart a specific pod without stopping all of them.
What would you do if you were managing many pods in Kubernetes and saw that the status of Kubernetes pods was pending or inactive? Restarting pods in Kubernetes is the easiest approach to get them operating again.
In this article, you’ll learn how to reboot pods in a Kubernetes cluster in a step-by-step manner. So relax, unwind, and figure out how to keep your pods operating.
Let’s get this party started!
Prerequisites
Step-by-step instructions are included in this lesson. Make sure you have the following items in order to follow along:
How to Install Kubernetes on an Ubuntu Machine is a related article.
Changing the Number of Replicas in Kubernetes to Restart Pods
There are a variety of kubectl commands that may be used to restart pods, however for now, change the number of replicas in the deployment to restart pods.
You scale the number of deployment copies to zero with this technique, which stops and terminates all pods. After then, the pods are scaled back up to the correct condition in order to initiate the new pods that are scheduled to take their place.
If you set the number of replicas to zero, your application will go down since 0 replicas will halt all of the pods, and no application will be functioning at that time.
1. Run the commands below in your terminal to create a folder in your home directory and change the working directory to it.
The folder is named /nginx-deploy in this example, but you may rename it anything you like. Your Kubernetes deployment configuration files are kept in this folder.
cd /nginx-deploy/nginx-deploy/nginx-deploy/nginx-deploy/nginx-deploy/nginx-deploy/nginx-deploy/
2. Open your preferred code editor and put the settings below into it. Save the settings with a name that you like. The configuration is stored as nginx.yaml in the /nginx-deploy directory in our example.
The following code is included in the nginx.yaml file that the deployment requires:
- nginx-deployment is the name of the deployment.
- Tags: app:nginx – This tag is used for tagging.
- 2 replicas – Allows you to specify how many pods you’ll need for deployment.
- The “Spec” section enables you to provide container parameters such as the name of the image (nginx) container (nginx-pod).
# apiVersion: apps/v1: defining the Kubernetes API version # Defining the object’s type in order to build it: Deployment # The object’s metadata, which includes a name string, UID, and optional namespace, helps to identify it uniquely. nginx-deployment nginx-deployment nginx-deployment nginx-deploy # Labels are key/value pairs associated to objects like pods. # Labels are used to indicate the distinguishing features of things that are meaningful and useful to consumers. replicas: 2 labels: app: nginx spec: match selector metadata: metadata: metadata: metadata: metadata: metadata: metadata: information: metadata: metadata: metadata: metadata: metadata: metadata: metadata: metadata: metadata containers: – name: nginx-pod nginx-pod nginx-pod nginx-pod nginx-pod nginx-pod nginx-pod nginx- picture credit: nginx
3. Select the nginx.yaml file and build the deployment using the kubectl apply command, as shown below. apply -f nginx.yaml kubectl
apply -f nginx.yaml kubectl
Using the nginx.yaml file and the kubectl apply command to create the deployment
4. Now run the kubectl get command to see which pods are currently operating in the cluster, with the -o broad syntax providing a thorough view of all pods.
Verifying the Kubernetes cluster’s pods
5. Use the kubectl scale command to shut down each pod individually, since you specified 0 replicas (—replicas=0).
kubectl deployment at scale —replicas=0 nginx-deployment
The deployment is being scaled down to zero pods.
6. Verify the number of pods using the kubectl get pods command.
All of the pods are now terminating, as you can see below. Continue to execute the kubectl get pods command until you see the message “No resources found in default namespace.”
Verifying the Kubernetes cluster’s pods
7. Now, repeat steps 5–7 with the kubectl scale command. However, since you specified two replicas (—replicas=2), the program will now start two pods one by one.
kubectl deployment at scale —replicas=2 nginx-deployment
Two Pods are being used to scale the deployment.
8. Finally, check the number of pods operating using the command below. get pods using kubectl
Each pod operates and is back in business after restarting, as seen below.
Verifying the Kubernetes cluster’s pods are running
Using the rollout restart Command to restart pods
You previously set the number of replicas to zero to restart pods, however this results in an application outage and downtime. So, how can you prevent a blackout and downtime? Use the rollout restart command to resume the deployment.
To restart the pods one by one without disrupting the deployment, use the rollout restart command below (deployment nginx-deployment).
rollout restart kubectl deployment nginx-deployment
In the Kubernetes cluster, use the rollout restart command.
Run the kubectl command below to see the pods that are currently operating (get pods).
Two of the old pods indicate Terminating status, then two more show up with Running status in a matter of seconds, which is pretty quick. Why? Because the rollout restart command does not create any downtime.
Verifying the Kubernetes cluster’s pods
Updating the Environment Variable to Restart Pods
You’ve now learnt two methods for restarting the pods: changing replicas and rolling restart. You explicitly restarted the pods in both ways. However, after you alter the pod’s environment variable in the final approach, the pods will restart on their own.
1. Update the deployment by updating the DATE environment variable in the pod to null (=$()) using the kubectl set env command below. The pods will resume as soon as you update the deployment.
set env deployment nginx-deployment DATE=$ kubectl ()
Updating Deployment to Automatically Restart Pods
2. Now run the command below to see whether any pods are operating.
After upgrading the deployment, you’ll note that the old pods have a Terminating state, while the new pods have a Running status.
All Pods are listed here.
3. Finally, use the kubectl describe command to verify that the DATE environment variable has been correctly set to null.
The DATE variable is empty in the example below (null).
Checking to see whether the DATE variable is null
Conclusion
You learned multiple methods for restarting Kubernetes pods in the Kubernetes cluster in this article, which may help you swiftly resolve most pod-related problems.
Why not automate the restart procedure each time a pod stops operating, rather than manually restarting the pods?
The “kubectl restart pods in namespace” command is used to restart Pods in a Kubernetes cluster. The command can be run from the CLI, or through a kubectl proxy.
Frequently Asked Questions
How do I restart a pod in Kubernetes?
A: A pod is a group of containers that are associated with one or more namespaces. The command kubectl delete pods will remove the entire pod from its namespace and any related services, where it will be garbage collected automatically after three days.
How do you restart a pod in deployment?
A: If a pod is deployed, it can be restarted by swiping the light bar with your finger.
How do I restart node in Kubernetes?
A: If you are unable to restart a node in Kubernetes, here is what you can do.
1) Delete the old pods in your cluster and then recreate them with the same names but different ids. This will clear out any data on those nodes so theyll be available for new ones to use. Please note that this process may take some time as it involves deleting all of the containers/services running on these nodes before recreating them again via kubectl run commands.
Related Tags
- restart pod kubernetes command
- kubectl restart pods in deployment
- kubernetes restart pod without deployment
- kubectl rollout restart pod
- kubectl kill pod