k8s deployment Strategy 更新策略

k8s更新策略
https://kubernetes.io/zh/docs/concepts/workloads/controllers/deployment/

Strategy
.spec.strategy specifies the strategy used to replace old Pods by new ones.
.spec.strategy.type can be “Recreate” or “RollingUpdate”. “RollingUpdate” is the default value.

Recreate Deployment
All existing Pods are killed before new ones are created when .spec.strategy.type==Recreate.

Rolling Update Deployment
The Deployment updates Pods in a rolling update fashion when .spec.strategy.type==RollingUpdate.
You can specify maxUnavailable and maxSurge to control the rolling update process.

Max Unavailable
.spec.strategy.rollingUpdate.maxUnavailable is an optional field that specifies
the maximum number of Pods that can be unavailable during the update process.
The value can be an absolute number (for example, 5) or a percentage of desired Pods
(for example, 10%). The absolute number is calculated from percentage by rounding down.
The value cannot be 0 if .spec.strategy.rollingUpdate.maxSurge is 0. The default value is 25%.

For example, when this value is set to 30%, the old ReplicaSet can be scaled down to 70% of
desired Pods immediately when the rolling update starts. Once new Pods are ready,
old ReplicaSet can be scaled down further, followed by scaling up the new ReplicaSet,
ensuring that the total number of Pods available at all times during the update is
at least 70% of the desired Pods.

Max Surge
.spec.strategy.rollingUpdate.maxSurge is an optional field that specifies the maximum number
of Pods that can be created over the desired number of Pods. The value can be an absolute
number (for example, 5) or a percentage of desired Pods (for example, 10%). The value cannot
be 0 if MaxUnavailable is 0. The absolute number is calculated from the percentage by rounding up.
The default value is 25%.

For example, when this value is set to 30%, the new ReplicaSet can be scaled up immediately
when the rolling update starts, such that the total number of old and new Pods does not exceed
130% of desired Pods. Once old Pods have been killed, the new ReplicaSet can be scaled up further,
ensuring that the total number of Pods running at any time during the update is at most 130%
of desired Pods.

maxSurge:滾動更新時最多可以多啓動多少個pod
maxUnavailable:滾動更新時最大可以刪除多少個pod
maxSurge和maxUnavailable可以用來決定更新是的最大pod數和最小pod數
是先啓動一個pod再刪除一個pod還是先刪除一個pod再啓動一個pod
例如
replicas是5
maxSurge: 1
maxUnavailable: 0
更新時 最大的pod數是 replicas+ maxSurge = 5+1 =6,最大的個數是6
最小pod數是 replicas - maxUnavailable = 5-0 = 5,最小pod數是5,所以只能先啓動一個pod,再刪除一個pod

demo

apiVersion: apps/v1
kind: Deployment
metadata:
  name: testStrategy
  labels:
    app: testStrategy
spec:
  replicas: 5
  strategy:        #升級策略(默認爲滾動升級,不需要修改)
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%        #滾動升級中,容器副本的最大數量(默認值,可根據實際情況修改)
      maxUnavailable: 25%        #滾動升級中,容器副本停止的最大數量(默認值,可根據實際情況修改)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章