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 <deployment_name> -n <namespace>
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 <pod_name> -n <namespace>
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 <namespace>
Or delete a replica set:
kubectl delete replicaset <name> -n <namespace>
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 <deployment name> -n <namespace> --replicas=0
Then, you can create the desired number of replicas, and Kubernetes will create new pods.
Example:
kubectl scale deployment <deployment name> -n <namespace> --replicas=<new 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 <deployment name> -n <namespace> <env name>=<new 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.
Automate resource provisioning in Azure DevOps CI/CD pipelines using Terraform
Streamline CORS for your APIs on AWS Gateway with Terraform and Lambda secure scale done
Cut your Kubernetes cloud bill with these 5 hacks for smarter scaling and resource tuning