Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 221
Matthew van Boheemen Avatar asked Oct 08 '20 21:10

Matthew van Boheemen


People also ask

How do you restart a deployment pod?

1. Run the kubectl set env command below to update the deployment by setting the DATE environment variable in the pod with a null value ( =$() ). As soon as you update the deployment, the pods will restart.

What is kubectl rollout restart deployment?

Rolling Restart MethodKubernetes now allows you to execute a rolling restart of your deployment as of version 1.15. This is the quickest restart mechanism in Kubernetes, as it is a new addition. The command given above shuts down and restarts each container in your deployment one by one.

Is it possible to restart Kubernetes deployment?

On current Kubernetes, you can kubectl rollout restart deployment .... If you have a strategy of RollingUpdate on your deployments you can delete the pods in order to replace the pod and refresh it. Users expect applications to be available all the time and developers are expected to deploy new versions of them several times a day.

How to restart deployment in kubectl rollout?

kubectl get deployments -n <NameSpace Name> -o custom-columns=NAME:.metadata.name|grep -iv NAME|while read LINE; do kubectl rollout restart deployment $LINE -n <NameSpace Name> ; done; You just have to replace the Namespace value after -n in the command it would traverse through the list of deployments in the namespace and restart them for you.

How to rolling restart pods without changing deployment YAML in Kubernetes?

How to rolling restart pods without changing deployment yaml in kubernetes? On current Kubernetes, you can kubectl rollout restart deployment .... If you have a strategy of RollingUpdate on your deployments you can delete the pods in order to replace the pod and refresh it.

What's wrong with deployment retrying to restart the pod?

Deployment is there to ensure Pod restarts when it gets evicted by DiskPressureEviction . The problem I'm facing is caused by Deployment retrying to restart the Pod too fast. As the Pod is set to be in specific Node that hasn't cleaned up DiskPressure yet, restarting Pod fails sequentially before Node is ready to accept new Pod:


2 Answers

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>

like image 51
Daniel Marques Avatar answered Oct 19 '22 06:10

Daniel Marques


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) }'
like image 32
George Cimpoies Avatar answered Oct 19 '22 06:10

George Cimpoies