為了方便管理服務的版本,Kubernetes提供了滾動更新(RollingUpdate)和回復舊版(RollBack)的功能,以更新 Container Image 版本為更新要點,透過 kubenetes 上 apply、edit、set-image 的功能進行服務更新,並藉由 record 進行指令紀錄 (版本號),提供後續滾動更新、滾動回復版本使用,滾動更新狀態紀錄使用 rollout history 進行檢視。
nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
minReadySeconds: 10
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
hostPort: 80
kubectl create -f nginx.yaml --record
kubectl get deployment nginx -o wide
更新服務 yaml 檔案nginx.yaml
派送修改後 yaml 檔案
kubectl apply -f nginx.yaml --record
kubectl rollout status deployment nginx
kubectl get deployment nginx -o wide
kubectl set image deployment nginx nginx=nginx:1.11.5 --record
kubectl rollout status deployment nginx
kubectl get deployment nginx -o wide
更新服務 yaml 檔案
派送修改後 yaml 檔案
kubectl replace -f nginx.yaml
kubectl rollout status deployment nginx
kubectl get deployment nginx -o wide
kubectl edit deployment nginx --record
kubectl rollout status deployment nginx
kubectl get deployment nginx -o wide
kubectl rollout history deployment nginx
kubectl rollout undo deployment nginx
3. 觀看 回復後 服務狀態
kubectl get deployment nginx -o wide
kubectl rollout history deployment nginx
kubectl rollout undo deployment nginx --to-revision=5
kubectl get deployment nginx -o wide