Shubham Gupta
Shubham Gupta

Reputation: 39

Restart n number of pods in K8s

I am working on an application which is running on the Kubernetes cluster. I want to restart the n number of pods manually in a sequence. Can we do that? Would kubectl scale <options> work here?

Upvotes: 0

Views: 1734

Answers (3)

Shubham Gupta
Shubham Gupta

Reputation: 39

We can also use the bash script for this case. This shell script asks you for the replica_set-id, No of pods to be restarted and this script will delete/restart the pods in sequence.

#! /bin/bash
read -p "Replicaset-id: " p
pods=$(kubectl get pods | grep "$p" | awk '{print $1}')
read -p "No of pods to be restarted: " n
m=0
for pod in $pods
    do 
        ((m++))
        echo "$m"
        kubectl delete pod "$pod"
        if [[ "$m" -eq "$n" ]]; then
            break
        fi
    done

Upvotes: 0

moonkotte
moonkotte

Reputation: 4181

The answer is yes, you can restart 5 out of 10 pods of a particular deployment. Though it won't be a single command for this.

As you correctly assumed kubectl scale will help you here.

Restart of 5 pods out of 10 contains 2 operations:

  1. Scaling down the deployment from 10 to 5 pods

    kubectl scale deployment deployment-name --replicas=5
    
  2. Scaling up the deployment from 5 to 10 pods back:

    kubectl scale deployment deployment-name --replicas=10
    

Also you can delete exact pods, kube-controller-manager with deployment/replicaset controllers within will make sure that desired state will match the exact state and therefore missing pods will be automatically rescheduled.


However following best practice (thanks to @DavidMaze), ideal scenario is restart the whole deployment. This can be done with following command:

kubectl rollout restart deployment deployment-name

This is safer option and it allows to roll back easily in case of any mistakes/errors.

Also it's possible to restart pods 1 by 1 within the deployment when rollout restart is requested.

.spec.strategy.rollingUpdate.maxUnavailable should be set to 1 which means only 1 pods at most will be unavailable during the restart - reference to max unavailable.

Kubernetes Deployments

Upvotes: 2

Chandra Sekar
Chandra Sekar

Reputation: 763

With replicaSet in place you can always scale up/down 'N' number of pods which will restart them and if you need to restart specific one simply delete them and RS will spin up a new one for you.

Upvotes: 1

Related Questions