How to Restart Kubernetes Pod

HINT: There is no ‘kubectl restart’ command

Kubernetes (k8s) is an open-source container-orchestration system for automating deployment, scaling and management of containerized applications.

This article demonstrates how to restart your running pods with kubectl (a command line interface for running commands against Kubernetes clusters).


Problem with your Pod?

Suppose one of your pods running on k8s is having an issue.

It was working fine earlier, but for some reason it’s breaking now. You have no idea what’s causing it so you check the health of the pods with

kubectl get pods -n service
NAME                     READY     STATUS             RESTARTS   AGE
api-7996469c47-d7zl2 1/1 Running 0 77d
api-7996469c47-tdr2n 1/1 Running 0 77d
chat-5796d5bc7c-2jdr5 0/1 Error 0 5d
chat-5796d5bc7c-xsl6p 0/1 Error 0 5d

You see that one of your pods is having an issue since its status is Error. You might encounter another status CrashLoopBackOff, which is k8s trying to restart the pods automatically (default).

kubectl describe pod chat -n service

You attempt to debug by running the describe command, but can’t find anything useful in the output. You do the next logical thing and check the pod’s tailing logs with with stern and can’t seem to find any useful error messages. Perhaps someone from your team pushed a bad commit so you’re checking the git logs to see if there are no recent pushes to the repo. There isn’t any.

You scratch your head and wonder what the issue is. Your users are impatiently waiting for the app to work again so you don’t want to waste any time.

Restarting your Kubernetes Pod

Let’s just restart the pod!

Typically, for modern dev teams, you have a CI/CD system where you can simply press a button to redeploy your pods.

The problem with this, however, is that it could take a long time to reboot your pod since it has to rerun through the entire continuous integration (e.g. unit tests, compiling code, building docker image, etc) and continuous deployment (e.g. spin up environment, checkout code, download packages, etc). Why do you have to run through this process again when you know that it’s going to pass?

The problem with [CI/CD] is that it could take a long time to reboot your pod since it has to rerun through the entire process again.

A quicker solution would be to use kubectl built in command scale and set the replicas to zero.

kubectl scale deployment chat --replicas=0 -n service
kubectl get pods -n service
NAME                     READY     STATUS             RESTARTS   AGE
api-7996469c47-d7zl2 1/1 Running 0 77d
api-7996469c47-tdr2n 1/1 Running 0 77d
chat-5796d5bc7c-2jdr5 0/1 Terminating 0 5d
chat-5796d5bc7c-xsl6p 0/1 Terminating 0 5d

The command scale sets the amount of replicas that should be running for the respective pod. When you set it to zero, it affectively shuts down the process.

kubectl get pods -n service
NAME                     READY     STATUS             RESTARTS   AGE
api-7996469c47-d7zl2 1/1 Running 0 77d
api-7996469c47-tdr2n 1/1 Running 0 77d

To start the pod again, set the replicas to more than 0.

kubectl scale deployment chat --replicas=2 -n service
kubectl get pods -n service
NAME                     READY     STATUS             RESTARTS   AGE
api-7996469c47-d7zl2 1/1 Running 0 77d
api-7996469c47-tdr2n 1/1 Running 0 77d
chat-5796d5bc7c-2jdr5 1/1 Running 0 3s
chat-5796d5bc7c-xsl6p 1/1 Running 0 3s

That’s it!

Conclusion

Kubernetes is great for container orchestration. However, as with all systems, problems do occur. Restarting your pods with kubectl scale --replicas=0is a quick and easy way to get your app running again. Nevertheless, restarting your pod will not fix the underlying issue that caused the pod to break in the first place, so please make sure to find the core problem and fix it!

P.S. We’re hiring! Panya Studios is always on the lookout for talented and passionate individuals to join our growing team in Bangkok, Thailand. Explore our current openings at https://studios.panya.me/


If you have another way of doing this or you have any problems with examples above let me know in the comments.

Don’t forget to follow me here on Medium for more interesting software engineering articles.

Thanks for reading!

Resources