How to Restart Kubernetes Pod

How to Restart Kubernetes Pod

HINT: There is no ?kubectl restart? command

Image for post

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).

Update April 9, 2020

As of kubernetes 1.15, you can now do a rolling restart of all pods for a deployment, so that you don?t take the service down.

kubectl -n service rollout restart deployment <name>Image for postThanks Steve Dillon!

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 serviceNAME READY STATUS RESTARTS AGEapi-7996469c47-d7zl2 1/1 Running 0 77dapi-7996469c47-tdr2n 1/1 Running 0 77dchat-5796d5bc7c-2jdr5 0/1 Error 0 5dchat-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

Image for post

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 servicekubectl get pods -n serviceNAME READY STATUS RESTARTS AGEapi-7996469c47-d7zl2 1/1 Running 0 77dapi-7996469c47-tdr2n 1/1 Running 0 77dchat-5796d5bc7c-2jdr5 0/1 Terminating 0 5dchat-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 serviceNAME READY STATUS RESTARTS AGEapi-7996469c47-d7zl2 1/1 Running 0 77dapi-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 servicekubectl get pods -n serviceNAME READY STATUS RESTARTS AGEapi-7996469c47-d7zl2 1/1 Running 0 77dapi-7996469c47-tdr2n 1/1 Running 0 77dchat-5796d5bc7c-2jdr5 1/1 Running 0 3schat-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. If you?re looking for help getting started or configuring your kubernetes cluster, reach out to our backend experts at www.advisurs.com (San Francisco, Bangkok, and Remote).

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

  • https://kubernetes.io/docs/reference/kubectl/cheatsheet/
  • https://sysdig.com/blog/debug-kubernetes-crashloopbackoff/
  • https://github.com/wercker/stern

Image for post

Join our community Slack and read our weekly Faun topics ?

Image for post

If this post was helpful, please click the clap ? button below a few times to show your support for the author! ?

26