Restart pods without taking the service down

As of kubernetes 1.15, you can do a rolling restart of all pods for a deployment without taking the service down. To achieve this we'll have to use kubectl rollout restart.

Let's asume you have a deployment with two replicas:

$ kubectl get pods
NAME                                     READY   STATUS      RESTARTS   AGE
pet2cattle-5454555dbb-6jtnz              1/1     Running     0          131m
pet2cattle-5454555dbb-czw42              1/1     Running     0          19s

To be able to restart the pods without causing downtime we can use kubectl rollout restart on the deployment:

$ kubectl rollout restart deployment pet2cattle
deployment.apps/pet2cattle restarted

This command will the the deployment controller to deploy another set of pods like it would do with an update. Taking a look at the pods we will be able to notice the different ID (5454555dbb vs 5c74f6dd58)

$ kubectl get pods
NAME                                     READY   STATUS      RESTARTS   AGE
pet2cattle-5454555dbb-6jtnz              1/1     Running     0          131m
pet2cattle-5454555dbb-czw42              1/1     Running     0          43s
pet2cattle-5c74f6dd58-dl7q4              0/1     Running     0          4s

Once the new deployment set (5c74f6dd58) is ready, the deployment controller will take down the old one (5454555dbb)

$ kubectl get pods
NAME                                     READY   STATUS        RESTARTS   AGE
pet2cattle-5c74f6dd58-dl7q4              1/1     Running       0          34s
pet2cattle-5c74f6dd58-b2nxb              1/1     Running       0          27s
pet2cattle-5454555dbb-6jtnz              0/1     Terminating   0          132m

We can also keep track of the operation using kubectl rollout status

$ kubectl rollout status deploy pet2cattle
Waiting for deployment "pet2cattle" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "pet2cattle" rollout to finish: 1 old replicas are pending termination...
deployment "pet2cattle" successfully rolled out

Posted on 01/03/2021