How to restart Pods in Kubernetes : a complete guide

ada_pow

Ada Powers

Posted on April 20, 2022

How to restart Pods in Kubernetes : a complete guide

Introduction

Kubernetes is a reliable container orchestration system that helps developers create, deploy, scale, and manage their apps. A Pod is the most basic deployable unit of computing that can be created and managed on Kubernetes.

If you want to restart your Pods without running your CI pipeline or creating a new image, there are several ways to achieve this. In this article, we will be discussing Kubernetes pods and how to restart them in the event of a failure.

Before moving on to the actual how-to process, it is important to address the question: what is pod and container in Kubernetes?

What is a Pod in Kubernetes terminology?

You can think of a Pod as a container for containers, or an abstract layer surrounding multiple containers that enables management of child elements in Kubernetes - without having access to individual containers.

A Pod usually holds one container - however, multiple containers can exist within a single Pod, a bit like a side-car attachment. These 'side-car' containers might be unit containers or ephemeral containers for debugging/logging etc.

Containers within a single pod share resources and network namespaces as well. You can think of a pod as a single PC where you can spin up multiple docker containers. These containers share the same resources such as hardware, RAM, filesystem, etc, and can communicate with each other through localhost.

Understanding the Pod lifecycle

In order to figure out how to restart Pods in Kubernetes, understanding their lifecycle is essential.

In Kubernetes, Pods are considered ephemeral. This means that Pods are short-lived and can be terminated at any time. They are designed to run stateless applications which do not need to have data persistence. For software like databases, etc that need to persist data even when terminated, it is advisable to use a Stateful set.

There are 5 stages in a pod's lifecycle:

  • Pending: This indicates that containers inside the pod have not yet been created.
  • Running: Here, the containers in the Pod have been created without errors and are in the process of starting up
  • Succeeded: All the containers within the Pod have terminated successfully and will not be restarted (for example, a Pod whose containers just run a bash script)
  • Failed: The containers within the Pod have been terminated with a non-zero exit code
  • Unknown: Kubernetes cannot retrieve the status of the Pod.

When Is It a Good Idea to Restart Pods in Kubernetes?

Pods should only be restarted when necessary, for example - when the resource allotment to Pods is not defined/sufficient or when your software behaves abnormally. To restart Pods successfully, you must ensure that you are passing correct details and allotting enough resources or space to them.

It is possible to have a pod running when all its containers have been terminated. The reason for this could be that your pod is unable to terminate due to an issue such as unexpected termination/failure or a container, node, or service that cannot be controlled or cleaned by the respective manager service.

In summary, the most likely scenarios when you might need to restart a pod in Kubernetes are listed below but keep in mind that k8s will also automatically try to restart pods in any of these scenarios.

  • Timeout
  • Incorrect implementations
  • Unexpected errors that remained unfixed
  • Insufficient space to fulfill a resource allocation request

Now, to understand how to restart a Kubernetes pod, it is required to understand how a pod is typically created in Kubernetes.

Creating a Pod in Kubernetes

We can create a Pod in 2 ways: Using the declarative approach or using kubectl commands.

Here’s how both methods work -

Creating a Pod via the declarative approach

Create a file called pod.yaml and paste the following code.

apiVersion: v1
kind: Pod
metadata:
 name: nginx
spec:
 containers:
 - name: nginx
   image: nginx:alpine
   ports:
   - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

This creates a pod called nginx with the nginx:alpine docker image and exposes port 80. To create this pod, run the following command -

kubectl apply -f pod.yaml
Enter fullscreen mode Exit fullscreen mode

Note that pods are generally not created directly but are created by workload resources such as Deployments and statefulsets.

Creating a Pod using kubectl commands

Run the command as written below -

kubectl run –image nginx:alpine –restart=Never
Enter fullscreen mode Exit fullscreen mode

You can see all the running pods by running this command

kubectl get pod
Enter fullscreen mode Exit fullscreen mode

Restarting a Pod in Kubernetes

Now that you know how a Pod is created, here’s how to restart a Pod in Kubernetes. This section will cover the 4 most popular methods.

  1. Scaling Pod’s Replica Count Down and Up

This involves scaling down your pod replicas to zero, then scaling them back up to a non-zero number.

To see this in action, first, we would need to create a deployment.

Create a file called “deployment.yaml” and paste the following code -

apiVersion: apps/v1
kind: Deployment
metadata:
 name: myapp
spec:
 selector:
   matchLabels:
     app: myapp
 template:
   metadata:
     labels:
       app: myapp
   spec:
     containers:
     - name: myapp
       image: nginx
       ports:
       - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Apply it with the following command -

kubectl apply -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

On checking your cluster, you should find a new pod running.

Now, let’s scale the replicas down to zero. Run the command -

`kubectl scale deployment myapp -–replicas=0`.
Enter fullscreen mode Exit fullscreen mode

If you know how to stop Kubernetes Pods, you must have used this command at some point.

On checking your cluster again, you should find that Pod gone. Now scale the replicas back up.

With the easiest method covered, we’ll delve into other methods.

We can also use the kubectl rollout restart command to restart our pods. Here’s how to achieve this.

  1. Restarting Pods through Rollout

In your terminal, run the command:

kubectl rollout restart deployment myapp
Enter fullscreen mode Exit fullscreen mode

Once this command is run, Kubernetes systematically terminates and replaces your Pods while keeping some containers operational throughout. This method is effective if you want to keep serving traffic while performing the restart.

  1. Updating the Annotations

You can change Pod's annotations or add new ones, in order to force a Pod to restart. As Kubernetes requires replacing the Pod when any change occurs, this method can help you restart the pod without much difficulty. Note that this isn't true if the pod spec field restartPolicy is set to Never.

For example, you can create or modify “key_1” for your pod named “test-pod” and use the “-- overwrite” flag with it. This flag will let you modify the value of an existing parameter, while without it, you can only add a new annotation to your pod.

kubectl annotate pods test-pod key_1="2" --overwrite
Enter fullscreen mode Exit fullscreen mode
  1. Deletion

If restarts seem to prove unsuccessful, you might be interested in how to delete pods in Kubernetes. If so, this method can also help you re-initiate a Pod.

To restart a Pod, you can simply delete the existing unresponsive pod and let it be replaced by a new one. The command to use is -

kubectl delete pod my-pod
Enter fullscreen mode Exit fullscreen mode

Using Convox to manage your Kubernetes cluster

We have to admit that the above methods might not be very user-friendly - especially if you are new to Kubernetes and are concerned about further complicating your application development process. You might also need to occasionally add resources or migrate your Pod to another location in order to ensure better performance.

If you’re looking to significantly simplify your entire application development process, your best bet is to switch to a Platform-as-a-Service (PaaS) with a user-friendly GUI that helps you manage your Kubernetes cluster.

Convox for example offers your organization direct Kubernetes access and removes the complexity of managing and operating your Kubernetes cluster directly. Furthermore, Convox is a multi-cloud product that is compatible with several cloud service providers including DigitalOcean, AWS, Google Cloud, and Microsoft Azure. It is free to start simplifying your application development process with Convox and it only takes a few clicks to get started.

Apart from excellent uptime and amazing scalability, Convox delivers a smooth development experience and powerful infrastructure for your Pods.

Conclusion

In Kubernetes, Pods are designed to run until a task is completed and replaced by another deployment. So, there is no direct command or method to restart pods when you use this container orchestration platform. However, because it isn’t always possible to avoid unfavorable, stressful situations (in life or when working directly with Kubernetes) it is important to have a workable plan of action should things go wrong.

Whether your plan is to keep this guide handy and accessible, or whether it is to simplify the management of all your Kubernetes clusters by switching over to Convox, just don’t fail to plan!

In summary, you can restart your pods with downtime and without download. Oy you can use the replica scaling or annotation modification method if you can afford downtime but cannot delete the pod. Alternatively, when you have extra replicas of a pod, use the rollback or deletion method. It will ensure no downtime. Or just sign up to Convox and simplify everything once and for all.

💖 💪 🙅 🚩
ada_pow
Ada Powers

Posted on April 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related