04 - Rolling Updates and Rollbacks

Q: Inspect the deployment and identify the number of PODs deployed by it

Ans: 4

$ kubectl get deploy                                              

$ kubectl describe deployment | grep -i Desired        

Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable

Q: Inspect the deployment and identify the current strategy

$ kubectl describe deployment frontend | grep -i StrategyType             

Ans: StrategyType: RollingUpdate


Q: If you were to upgrade the application now what would happen?

Ans: PODs are upgraded few at a time


Q: Let us try that. Upgrade the application by setting the image on the deployment to `kodekloud/webapp-color:v2`. 

Do not delete and re-create the deployment. Only set the new image name for the existing deployment.

  • Deployment Name: frontend
  • Deployment Image: kodekloud/webapp-color:v2

Run the command `kubectl edit deployment frontend` and modify the image to `kodekloud/webapp-color:v2`. 

Next, save and exit. The pods should be recreated with the new image.

方法一:

$ kubectl edit deployment frontend                

方法二:

$ kubectl set image deployment frontend simple-webapp=kodekloud/webapp-color:v2     


過程如下:

原本建立的:kodekloud/webapp-color:v1

RollingUpdate:把原本的 pods 一個一個地慢慢替換,實現 Zero Downtime

所有 Pods 更新完成:kodekloud/webapp-color:v2


Q: Up to how many PODs can be down for upgrade at a time

Consider the current strategy settings and number of PODs - 4

Look at the Max Unavailable value under RollingUpdateStrategy in deployment details

Ans: maxUnavailable: 25% x 4 pods = 1

strategy.rollingUpdate.maxUnavailable: 25%

Q: Change the deployment strategy to `Recreate`

Delete and re-create the deployment if necessary. Only update the strategy type for the existing deployment.

  • Deployment Name: frontend
  • Deployment Image: kodekloud/webapp-color:v2
  • Strategy: Recreate

$ kubectl edit deployment frontend                  

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
  namespace: default
spec:
  replicas: 4
  selector:
    matchLabels:
      name: webapp
  strategy:
    type: Recreate           # 修改 (原本 strategy.RollingUpdate 相關設定要記得刪除)
  template:
    metadata:
      labels:
        name: webapp
    spec:
      containers:
      - image: kodekloud/webapp-color:v2
        name: simple-webapp
        ports:
        - containerPort: 8080
          protocol: TCP
```


結論

  • Recreate:把原本的 pods 全部刪除,接著全部重新建立,有 downtime(502 Bad Gateway)
  • RollingUpdate:把原本的 pods 一個一個地慢慢替換,實現 Zero Downtime



$ kubectl set image --help                 

Usage:
  kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1
... CONTAINER_NAME_N=CONTAINER_IMAGE_N [options]

留言