In this guide on kubectl rollout restart, we will talk about what is this command, 5 ways to use it, and a few commonly used kubectl rollout restart commands.

kubectl rollout restart, kubectl rollout restart banner

You surely might have experienced that moment when you update your favorite app on your cellphone and suddenly everything feels snappy. 

Well, just like your apps need an occasional restart to bring in the latest improvements, your applications running on Kubernetes also benefit from a similar tinge of freshness. 

That's where the power of the kubectl rollout restart command comes into the limelight!

And so, in this article, we'll see into the world of kubectl rollout restart and explore its use cases that suit our purpose. 

We'll discuss real-life scenarios where this command can come to the rescue, along with practical examples that we can apply in our day-to-day Kubernetes adventures. 

What is Kubectl Rollout Restart?

Kubectl Rollout Restart is a command in Kubernetes that allows you to restart pods or resources in a rolling manner (one after the other).

Let's look at the syntax of the kubectl rollout restart command. 

kubectl rollout restart deployment/[deployment-name] -n [namespace]

It initiates a smooth update process by gracefully terminating and recreating the pods, one at a time, to minimize disruptions and maintain application availability. 

It's a very convenient way to refresh (or update) deployments, StatefulSets, ReplicaSets, or specific pods based on labels, ensuring butter-smooth updates in your Kubernetes cluster.

Also Read: A Complete List of Kubectl Commands

5 Different Ways to Use Kubectl Rollout Restart

The kubectl rollout restart command is a handy feature in Kubernetes that allows developers like you and me to restart a deployment or a specific set of resources (pod, StatefulSet, Daemonset, etc.). 

It starts a rolling update, which gracefully terminates and recreates the pods associated with the specified resources. 

So, let's explore some real-life deployment scenarios where you can leverage this command.

Note: kubectl rollout restart operates at a cluster level and this command does not cause downtime.

Humalect IDP CTA banner, kubectl rollout restart

Using Kubectl Rollout Restart to Restart a Deployment

Suppose that you have a deployment called `myapp` running in your Kubernetes cluster, and you want to restart all the pods associated with it. 

You can use the following command:

kubectl rollout restart deployment myapp

This command starts a rolling update for the `myapp` deployment, which terminates and recreates the pods (one at a time) ensuring high availability during the process till all the pods are recreated.

Also Read: Kubectl Proxy Command - A Tutorial

Using Kubectl Rollout Restart to Restart a StatefulSet

In another scenario, you may have a StatefulSet named `mydatabase` responsible for managing a database with multiple replicas. 

If you want to restart all the pods within the StatefulSet, you can use this command:

kubectl rollout restart statefulset mydatabase

Executing this command starts a smooth rolling update for the `mydatabase` StatefulSet, sequentially restarting each replica pod while maintaining the correct order.

Also Read: Port Forwarding in Kubernetes

How to Use Kubectl Rollout Restart to Restart a ReplicaSet?

Now, assume you have a ReplicaSet named `myreplicaset` that manages a set of pods for a specific application. 

If you need to restart all the pods controlled by this ReplicaSet, you can make use of the following command:

kubectl rollout restart replicaset myreplicaset

By running this command, Kubernetes performs a rolling update for the `myreplicaset` ReplicaSet and thus restarts each pod to maintain a smooth service without hiccups.

Also Read: A Complete Tutorial on Kubectl Exec Command

Using Kubectl Rollout Restart to Restart Pods based on Labels

In some cases, depending on the type of your project, you may want to restart pods based on specific labels rather than restarting an entire deployment or ReplicaSet. 

You can do that by using a selector with the kubectl rollout restart command. 

For example, if you have pods with the label `app=myapp`, you can restart them using the following command:

kubectl rollout restart pod -l app=myapp

Running this command fires a rolling restart of all pods that have the label `app=myapp`, for a smooth transition.

How to Start a Daemonset using Kubectl Rollout Restart?

kubectl rollout restart daemonset daemonset-name

It helps to restart all the pods managed by the specified daemonset in Kubernetes. The pods will be terminated and new pods will be created with the updated configuration.

Note: This kubectl rollout restart command is only and only available from Kubernetes version 1.15 and onwards. If you are using an older version (which you should not), either update your K8s version or you may need to use alternative approaches to achieve a rolling restart, like updating the deployment's pod template or scaling the deployment all the way to zero and then back up to the needed replicas for your project.

Also Read: Namespaces in Kubernetes - A Tutorial

How to Use Kubectl Rollout Restart

Follow these easy steps to use kubectl rollout restart.

Step 1. Open up a terminal of your choice, 

Step 2. Next, set the context for your Kubernetes cluster using the command:

kubectl config use-context [context-name]

Step 3. Then, list all the deployments in your cluster with the command:

kubectl get deployments

Step 4. Find a deployment of your choice that you wish to restart.

Step 5. Finally, restart the deployment by running the following command. 

kubectl rollout restart deployment/[deployment-name]

That's it! Now you're good to go.

Note: Keep in mind that when using the 'kubectl rollout restart' command, you can restart the Deployment and recreate the Pods without needing to modify the Deployment file.

Humalect IDP CTA banner, Kubectl rollout restart

Commonly used Kubectl Rollout Commands

1. Rolling update "www" containers of "backend" deployment, updating the image

kubectl set image deployment/backend www=image:v3               

2. Here's how to use kubectl rollout to check out the history of deployments including the revision.

kubectl rollout history deployment/frontend        

3. To rollback to the previous deployment (undo it), run the command below.

kubectl rollout undo deployment/frontend 

4. Here's how to rollback to a specific revision using the kubectl rollout command.

kubectl rollout undo deployment/frontend --to-revision=2         

5. Here's how to use the kubectl rollout command to watch the rolling update status of "frontend" deployment until completion.

kubectl rollout status -w deployment/frontend 

6. Here's how to use the kubectl rollout command to watch the rolling restart of the "backend" deployment

kubectl rollout restart deployment/backend

Also Read: The Only Kubeadm Tutorial You'll Ever Need

Kubectl Rollout Restart - Final Words

Remember, with the kubectl rollout restart command, you're in control. 

You can restart specific resources or the entire deployment, ensuring a seamless experience for your users. 

So go ahead, embrace the power of kubectl rollout restart, and keep your Kubernetes world spinning flawlessly!