kubectl - 如何重新启动部署(或所有部署)

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

我们有一个 AKS 集群,有时我们会遇到需要重新启动部署的问题(例如,缓存数据已更新,我们想要刷新它,或者缓存数据损坏,我们想要刷新)。

我一直在使用将部署扩展到 0,然后使用以下命令将其重新扩展的方法:

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

这符合我的预期,但感觉有点老套,这意味着我们不会运行在此过程中进行任何部署。

执行此操作的更好方法是什么?对于特定部署和所有部署?

如果您的部署有 RollingUpdate 策略,您可以删除 pods 以替换 pod 并刷新它。

关于滚动更新策略:

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.

滚动更新配置:

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

maxSurge 指定可以在所需数量的 Pods 上创建的最大数量 Pods。

maxUnavailable 指定在更新过程中可以不可用的最大 Pods 数量。

删除广告连播:

kubectl delete pod <pod-name>

编辑:

此外,您可以转出部署,这将重新启动 pod,但也会创建部署的新修订版。

例如:kubectl rollout restart deployments/<deployment-name>

如何重启集群中的所有部署(多个命名空间):

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