16

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?

Matthew van Boheemen
  • 1,087
  • 3
  • 13
  • 21
  • 1
    assuming you dont want to actually do a new deployment, I think this is the right approach. You certainly don't want do deleting deployments willy nillly, as you might lose something important like a PVC. If anything I'd say the 'hack' feeling might come from something like a latest tag on the image.... – stringy05 Oct 08 '20 at 22:55
  • Does this answer your question? [How to rolling restart pods without changing deployment yaml in kubernetes?](https://stackoverflow.com/questions/57559357/how-to-rolling-restart-pods-without-changing-deployment-yaml-in-kubernetes) – David Maze Oct 08 '20 at 23:02
  • 4
    On current Kubernetes, you can `kubectl rollout restart deployment ...`. – David Maze Oct 08 '20 at 23:03

5 Answers5

21

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>

Daniel Marques
  • 1,249
  • 8
  • 17
9

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) }'
George Cimpoies
  • 884
  • 2
  • 14
  • 26
0

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

  • annotations
  • cpu limit/request
  • Memory limit/request
sa.as
  • 381
  • 2
  • 4
0
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
prudhvireddy
  • 226
  • 1
  • 2
  • 7
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