What if I told you there is no way to restart a Kubernetes Pod? Well, there isn't. But don't worry, there are ways to achieve a similar result.
Restarting a Pod is equivalent to duplicating a Pod, and then deleting the original one. While there's no command to restart a specific Pod, you can do that via resources that manage Pods, like a Deployment or StatefulSet.
Let's see how that works!
How to restart a Pod in Kubernetes
Restart Pods managed by a Controller — such as Deployment or DaemonSet — is possible via the
kubectl rollout restart command. Here's an example:
kubectl rollout restart -n default deployment/nginx
The command above will restart all Pods managed by the
nginx deployment in the
If you prefer using GUI instead of the terminal commands, you can also restart pods using Aptakube. Here's an example of using Aptakube to restart a deployment. Note now Kubernetes performs the restart in a way that is safe and does not cause downtime to your service. We'll explain more about this in the next section.
While this is not strictly a restart because it requires a Create + Delete, it technically looks like a restart, except that the Pod Name and IP will be different. If you're using a Service to access your Pods (which you should), then having a new IP would not be a problem as the service would transparently route traffic to new Pods.
Safely restarting Pods using Update Strategies
How Kubernetes handles rolling restarts depends on the update strategy defined in the parent resource. There are 3 update strategies to choose from
- RollingUpdate: With this strategy, Kubernetes will first create new Pods using the same template as the existing ones, After a number of Pods are in the
Readystate, Kubernetes will then start deleting older Pods. This is performed until all new Pods are Ready, and older Pods are deleted. There are additional parameters to control how quickly this update should happen. See video above for a visual example of rolling update. This is the default strategy for
StatefulSet, and the recommended strategy for most use cases.
- Recreate: This strategy will first delete all existing Pods before creating new ones. Use this strategy carefully as it may cause downtime to your service. This is only available for
- OnDelete: This strategy will wait until the Pods are manually deleted before creating new ones. This is especially useful to have a manual control over the update process. This is only available for
👋 That's all folks, I hope you find this short article useful.