Matthew van Boheemen
Matthew van Boheemen

Reputation: 1335

kubectl - How to restart a deployment (or all deployment)

We have an AKS cluster and sometimes we end up with an issue where a deployment needs a restart (e.g. cached data has been updated and we want to refresh it or there is corrupt cache data we want to refresh).

I've been using the approach of scaling the deployment to 0 and then scaling it back up using the commands below:

kubectl scale deployments/<deploymentName> --replicas=0
kubectl scale deployments/<deploymentName> --replicas=1

This does what I expect it to do, but it feels hacky and it means we're not running any deployments while this process is taking place.

What's a better approach to doing this? For either a specific deployment and for all the deployments?

Upvotes: 19

Views: 58620

Answers (6)

Kr1SEl
Kr1SEl

Reputation: 1

To restart all deployments in a namespace use:

kubectl rollout restart deployment -n <namespace>

Upvotes: 0

A simple script to rollout restart all deployments in a namespace. This will also wait for the rollout of each individual deployment to succeed and logs the same in console

#!/bin/bash

# Define the namespace
NAMESPACE="your-namespace"

# Get a list of deployments in the specified namespace
DEPLOYMENTS=$(kubectl get deployments -n $NAMESPACE -o=jsonpath='{.items[*].metadata.name}')

# Iterate through the deployments and trigger a rollout restart
for DEPLOYMENT in $DEPLOYMENTS; do
    echo "Rolling out restart for deployment: $DEPLOYMENT"
    kubectl rollout restart deployment $DEPLOYMENT -n $NAMESPACE

    # Wait for the rollout to complete
    while ! kubectl rollout status deployment $DEPLOYMENT -n $NAMESPACE | grep "successfully rolled out"; do
        echo "Waiting for rollout of $DEPLOYMENT to complete..."
        sleep 5
    done

    echo "Rollout for $DEPLOYMENT completed"
done

Upvotes: 0

prudhvireddy
prudhvireddy

Reputation: 226

deploys=`kubectl get deployments -n <namespace> --no-headers=true| awk '{print $1}'`
for deploy in $deploys; do
  kubectl rollout restart deployments/$deploy -n <namespace>
done

Upvotes: 0

sa.as
sa.as

Reputation: 461

if you change any of these values in the new deployment, current pods will recreate/restart

  • annotations
  • cpu limit/request
  • Memory limit/request

Upvotes: 0

George Cimpoies
George Cimpoies

Reputation: 1004

How to restart all deployments in a cluster (multiple namespaces):

kubectl get deployments --all-namespaces | tail +2 | awk '{ cmd=sprintf("kubectl rollout restart deployment -n %s %s", $1, $2) ; system(cmd) }'

Upvotes: 14

Daniel Marques
Daniel Marques

Reputation: 1405

If you have a strategy of RollingUpdate on your deployments you can delete the pods in order to replace the pod and refresh it.

About the RollingUpdate strategy:

Users expect applications to be available all the time and developers are expected to deploy new versions of them several times a day. In Kubernetes this is done with rolling updates. Rolling updates allow Deployments' update to take place with zero downtime by incrementally updating Pods instances with new ones.

RollingUpdate config:

spec:
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate

maxSurge specifies the maximum number of Pods that can be created over the desired number of Pods.

maxUnavailable specifies the maximum number of Pods that can be unavailable during the update process.

Delete the pod:

kubectl delete pod <pod-name>

Edit:

Also, you can rollout the deployment, which will restart the pod but will create a new revision of the deployment as well.

Ex: kubectl rollout restart deployments/<deployment-name>

Upvotes: 27

Related Questions