Even if you maintain the best pod systems, sometimes you may need to restart pods. The reason may be related to a kub outage, a failure of the pod itself, or a change in its configuration.

Unfortunately, in Kubernetes (unlike, for example, Docker), there is no direct restart command for pods. The kubectl CLI does not provide a built-in command like kubectl restart pod or kubectl restart deployment. However, there are several workarounds for restarting pods using kubectl.

Why you might want to restart a pod?

Despite restarting a Kubernetes pod is not a standard operation, it can be useful in the following situations:

  • Applying configuration changes. If the pod’s configuration (environment variables, image versions, etc.) has been changed, a manual restart may be required for the changes to take effect.
  • Recovery. If a container in the pod has failed, restarting the pod may be necessary to restore the application’s functionality.
  • Pod termination. If the pod was terminated due to an Out Of Memory (OOM) error, it will require a restart after changes to the resource specifications are made.
  • Releasing resources. A pod may consume too much memory or CPU resources, causing performance issues or affecting other workflows. Restarting the pod helps release those resources and resolve or mitigate the issues.

How can you restart a pod?

There are several common methods for 'restarting’ pods using combinations of kubectl commands.

Note. After a 'restart', the pod name may change. You can view the current list of pods using the command kubectl get pods.

1. Using Rollout Restart

This command triggers a rolling update for all pods in a deployment. Kubernetes sequentially terminates the old pods, one at a time, creating a new one for each terminated with the updated configuration.

Example:

kubectl rollout restart deployment &LTdeployment_name> -n &LTnamespace>

2. Deleting the Pod

If you manually delete a pod, Kubernetes will automatically create a new one based on the corresponding configuration. This happens because Kubernetes operates declaratively – it automatically replaces any missing object (pod).

Example:

kubectl delete pod &LTpod_name> -n &LTnamespace>

Note. If you need to restart a large number of pods, you can delete all pods with a specific label:

kubectl delete pod -l “app:myapp” -n &LTnamespace>

Or delete a replica set:

kubectl delete replicaset &LTname> -n &LTnamespace>

3. Scaling Replicas

You can also 'restart' pods using the scale command.

First, you need to set the number of replicas to 0. Kubernetes will terminate all existing replicas.

Example:

kubectl scale deployment &LTdeployment name> -n &LTnamespace> --replicas=0

Then, you can create the desired number of replicas, and Kubernetes will create new pods.

Example:

kubectl scale deployment &LTdeployment name> -n &LTnamespace> --replicas=&LTnew replica count>

Note. This method may cause application downtime while the new pods are being created.

4. Changing Environment Variables

You can also restart a pod by changing its associated environment variables with the kubectl set env command. Kubernetes will automatically restart the pod for the changes to take effect.

Example:

kubectl set env deployment &LTdeployment name> -n &LTnamespace> &LTenv name>=&LTnew value>

Conclusions

There are a few widespread methods for restarting Kubernetes pods. So, it is possible to choose the most appropriate method based on the required speed of pod restart and the acceptable downtime for the application.

Overall, the most preferred option is to use kubectl rollout restart, as this method helps avoid application downtime.

Request more information today
Discover how our services can benefit your business. Leave your contact information and our team will reach out to provide you with detailed information tailored to your specific needs. Take the next step towards achieving your business goals.
Contact form:
Our latest news