How to Restart Kubernetes Pods

August 27, 2020

Introduction

Kubernetes is an open-source system built for orchestrating, scaling, and deploying containerized apps. If you’ve spent any time working with Kubernetes, you know how useful it is for managing containers.

You’ll also know that containers don’t always run the way they are supposed to. If an error pops up, you need a quick and easy way to fix the problem.

This tutorial will explain how to restart pods in Kubernetes.

Article on how to restart Kubernetes pods

Prerequisites

  • Access to a terminal window/ command line
  • A Kubernetes cluster
  • The Kubernetes kubectl command line tool

Restarting Kubernetes Pods

Note: The kubectl command line tool does not have a direct command to restart pods. However, the following workaround methods can save you time, especially if your app is running and you don’t want to shut the service down.

Let’s say one of the pods in your container is reporting an error. Depending on the restart policy, Kubernetes might try to automatically restart the pod to get it working again. However, that doesn’t always fix the problem.

If Kubernetes isn’t able to fix the issue on its own, and you can’t find the source of the error, restarting the pod is the fastest way to get your app working again.

Note: Modern DevOps teams will have a shortcut to redeploy the pods as a part of their CI/CD pipeline. While this method is effective, it can take quite a bit of time. Your pods will have to run through the whole CI/CD process.

Method 1: Rolling Restart

As of update 1.15, Kubernetes lets you do a rolling restart of your deployment. As a new addition to Kubernetes, this is the fastest restart method.

kubectl rollout restart deployment [deployment_name]

The above-mentioned command performs a step-by-step shutdown and restarts each container in your deployment. Your app will still be available as most of the containers will still be running.

Input and result for rolling restart

Note: Learn how to monitor Kubernetes with Prometheus. Monitoring Kubernetes gives you better insight into the state of your cluster. To better manage the complexity of workloads, we suggest you read our article Kubernetes Monitoring Best Practices.

Method 2: Using Environment Variables

Another method is to set or change an environment variable to force pods to restart and sync up with the changes you made.

For instance, you can change the container deployment date:

kubectl set env deployment [deployment_name] DEPLOY_DATE="$(date)"

In the example above, the command set env sets up a change in environment variables, deployment [deployment_name] selects your deployment, and DEPLOY_DATE="$(date)" changes the deployment date and forces the pod restart.

Input and result for using environment variables

Note: Learn everything about using environment variables by referring to our tutorials on Setting Environment Variables In Linux, Setting Environment Variables In Mac, and Setting Environment Variables In Windows.

Method 3: Scaling the Number of Replicas

Finally, you can use the scale command to change how many replicas of the malfunctioning pod there are. Setting this amount to zero essentially turns the pod off:

kubectl scale deployment [deployment_name] --replicas=0

To restart the pod, use the same command to set the number of replicas to any value larger than zero:

kubectl scale deployment [deployment_name] --replicas=1

When you set the number of replicas to zero, Kubernetes destroys the replicas it no longer needs.

Once you set a number higher than zero, Kubernetes creates new replicas. The new replicas will have different names than the old ones. You can use the command kubectl get pods to check the status of the pods and see what the new names are.

Input adn result for scaling the number of replicas

Conclusion

Kubernetes is an extremely useful system, but like any other system, it isn’t fault-free.

When issues do occur, you can use the three methods listed above to quickly and safely get your app working without shutting down the service for your customers.

After restarting the pods, you will have time to find and fix the true cause of the problem.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
What is Kubernetes DaemonSet and How to Use It?
July 16, 2020

The article contains an in-depth analysis of DaemonSets and practical examples of how best to implement...
Read more
What is Kubernetes? Complete Guide
April 20, 2023

If you are using Docker, you need to learn about Kubernetes. It is an open-source container orchestration...
Read more
How to Monitor Kubernetes With Prometheus
August 27, 2020

This tutorial shows you how to create a series of .yml files to set up Prometheus Monitoring on your...
Read more
Introduction to Kubernetes Persistent Volumes
January 27, 2020

Persistent Volumes are used in Kubernetes orchestration when you want to preserve the data in the volume even...
Read more