# NAME: 列出了在目前 namespace 中的 deployment 清單 # DESIRED: 使用者所宣告的 desired status # CURRENT: 表示目前有多少個 pod 副本在運行 # UP-TO-DATE: 表示目前有多個個 pod 副本已經達到 desired status # AVAILABLE: 表示目前有多個 pod 副本已經可以開始提供服務 # AGE: 顯示目前 pod 運行的時間 $ kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment 3 3 3 0 15s
如果要即時監控 deployment 佈署的狀況,可以使用以下指令:
1 2 3 4 5
$ kubectl rollout status deployment/nginx-deployment Waiting for deployment "nginx-deployment" rollout to finish: 0 of 3 updated replicas are available... Waiting for deployment "nginx-deployment" rollout to finish: 1 of 3 updated replicas are available... Waiting for deployment "nginx-deployment" rollout to finish: 2 of 3 updated replicas are available... deployment "nginx-deployment" successfully rolled out
完成之後,AVAILABLE 的數字就會與 DESIRED 相同了:
1 2 3
$ kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment 3 3 3 3 32s
# 檢視 rollout status $ kubectl rollout status deployment/nginx-deployment Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination... deployment "nginx-deployment" successfully rolled out
# 新的變更已經成功套用 $ kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment 3 3 3 3 1d
$ kubectl describe deployment/nginx-deployment Name: nginx-deployment .... (略) RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=nginx Containers: nginx: Image: nginx:1.9.1 Port: 80/TCP .... (略) OldReplicaSets: <none> NewReplicaSet: nginx-deployment-6fdbb596db (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- # available pod = 4(new 1, old 3) Normal ScalingReplicaSet 20m deployment-controller Scaled up replica set nginx-deployment-6fdbb596db to 1 # available pod = 3(new 1, old 2) Normal ScalingReplicaSet 19m deployment-controller Scaled down replica set nginx-deployment-67594d6bf6 to 2 # available pod = 4(new 2, old 2) Normal ScalingReplicaSet 19m deployment-controller Scaled up replica set nginx-deployment-6fdbb596db to 2 # available pod = 3(new 2, old 1) Normal ScalingReplicaSet 19m deployment-controller Scaled down replica set nginx-deployment-67594d6bf6 to 1 # available pod = 3(new 3, old 1) Normal ScalingReplicaSet 19m deployment-controller Scaled up replica set nginx-deployment-6fdbb596db to 3 # available pod = 3(new 3, old 0) Normal ScalingReplicaSet 19m deployment-controller Scaled down replica set nginx-deployment-67594d6bf6 to 0
# 檢視 rollout status # 因為 container image version error,所以無法正確完成工作,因此更新會卡住,按下 Ctrl+C 跳離 $ kubectl rollout status deployments nginx-deployment Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated... ^C
# 取得 replicaset 狀態 # 新建的 replicaset 一直在無法完成工作的狀態 $ kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deployment-58c7645486 1 1 0 4m nginx-deployment-67594d6bf6 0 0 0 2d nginx-deployment-6fdbb596db 3 3 3 23h
# pod 則是明確的顯示遇到 image pull 的問題,時間一久就會開始進入 back off 的狀態 $ kubectl get pod NAME READY STATUS RESTARTS AGE nginx-deployment-58c7645486-xrgn8 0/1 ImagePullBackOff 0 4m nginx-deployment-6fdbb596db-27dmh 1/1 Running 0 23h nginx-deployment-6fdbb596db-67cwx 1/1 Running 0 23h nginx-deployment-6fdbb596db-r9qmv 1/1 Running 0 23h
# 檢視 deployment 的細節 $ kubectl describe deployment/nginx-deployment Name: nginx-deployment Namespace: default CreationTimestamp: Mon, 10 Sep 2018 20:13:20 +0000 Labels: app=nginx # 從 annotation 可以看出目前 deployment status 是由什麼指令造成的 Annotations: deployment.kubernetes.io/revision=3 kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{"kubernetes.io/change-cause":"kubectl apply --filename=nginx-deployment.yaml --r... kubernetes.io/change-cause=kubectl set image deployment/nginx-deployment nginx=nginx:1.91 --record=true Selector: app=nginx Replicas: 3 desired | 1 updated | 4 total | 3 available | 1 unavailable ....(略) OldReplicaSets: nginx-deployment-6fdbb596db (3/3 replicas created) NewReplicaSet: nginx-deployment-58c7645486 (1/1 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 1m deployment-controller Scaled up replica set nginx-deployment-67594d6bf6 to 3 Normal ScalingReplicaSet 1m deployment-controller Scaled up replica set nginx-deployment-6fdbb596db to 1 Normal ScalingReplicaSet 1m deployment-controller Scaled down replica set nginx-deployment-67594d6bf6 to 2 Normal ScalingReplicaSet 1m deployment-controller Scaled up replica set nginx-deployment-6fdbb596db to 2 Normal ScalingReplicaSet 59s deployment-controller Scaled down replica set nginx-deployment-67594d6bf6 to 1 Normal ScalingReplicaSet 59s deployment-controller Scaled up replica set nginx-deployment-6fdbb596db to 3 Normal ScalingReplicaSet 48s deployment-controller Scaled down replica set nginx-deployment-67594d6bf6 to 0 Normal ScalingReplicaSet 16s deployment-controller Scaled up replica set nginx-deployment-58c7645486 to 1
接著來看一下 rollout history:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 透過 rollout history 指令可以看出曾經下過什麼指令 $ kubectl rollout history deployment/nginx-deployment deployments "nginx-deployment" REVISION CHANGE-CAUSE 1 kubectl apply --filename=nginx-deployment.yaml --record=true 2 kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1 --record=true 3 kubectl set image deployment/nginx-deployment nginx=nginx:1.91 --record=true
# 檢視 rollout hsitory 的細節 $ kubectl rollout history deployment/nginx-deployment --revision=2 deployments "nginx-deployment" with revision #2 Pod Template: Labels: app=nginx pod-template-hash=2986615286 Annotations: kubernetes.io/change-cause=kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1 --record=true Containers: nginx: Image: nginx:1.9.1 ....(略)
# 重新檢視 deployment status => 正常 $ kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment 3 3 3 3 19m
# 重新檢視 replicaset status => 正常 $ kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deployment-58c7645486 0 0 0 17m nginx-deployment-67594d6bf6 0 0 0 19m nginx-deployment-6fdbb596db 3 3 3 18m
# 重新檢視 pod status => 正常 $ kubectl get pod NAME READY STATUS RESTARTS AGE nginx-deployment-6fdbb596db-7zqm6 1/1 Running 0 18m nginx-deployment-6fdbb596db-kcl8k 1/1 Running 0 18m nginx-deployment-6fdbb596db-rljdk 1/1 Running 0 18m
$ kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment 10 10 10 10 23h
# 將 container image version 指定到一個不存在的版本 $ kubectl set image deployment/nginx-deployment nginx=nginx:1.91
# k8s 調整後 (目前因為 image version 問題,所以已經卡住) $ kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment 10 13 5 8 23h
# 兩個不同的 version 同時存在(template hash code 為 "67594d6bf6" & "58c7645486") # 原本的 replicaset 中有 2(10-8) 個 pod 已經暫時被移除 (maxUnavailable < 25%) # 所有的 replicaset 的 pod 數量為 13(8+5),沒有超過 max surge 定義的 25% => (13 - 10) / 13 約莫 24% $ kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deployment-58c7645486 5 5 0 23h nginx-deployment-67594d6bf6 8 8 8 23h nginx-deployment-6fdbb596db 0 0 0 23h
# 可以再度看一下 deployment 細節 # 可以發現無論何時,所有 pod 的總和都沒超過 13 # 也就是表示 max surge 低於 25% $ kubectl describe deployment/nginx-deployment Name: nginx-deployment ....(略) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 9m deployment-controller Scaled up replica set nginx-deployment-67594d6bf6 to 10 Normal ScalingReplicaSet 2m deployment-controller Scaled up replica set nginx-deployment-58c7645486 to 3 Normal ScalingReplicaSet 2m deployment-controller Scaled down replica set nginx-deployment-67594d6bf6 to 8 Normal ScalingReplicaSet 2m deployment-controller Scaled up replica set nginx-deployment-58c7645486 to 5
# 檢視 deploy/nginx-deployment 的 rollout status $ kubectl rollout status deploy/nginx-deployment Waiting for rollout to finish: 2 out of 3 new replicas have been updated... error: deployment "nginx" exceeded its progress deadline
# 上一個指令回傳的 exit code 不等於 0 $ echo $? 1
輸出完整的 YAML 資訊
透過 kubectl describe 檢視 deployment 狀況時,可能會出現以下資訊:
1 2 3 4 5 6 7 8 9
$ kubectl describe deployment nginx-deployment .....(略) Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True ReplicaSetUpdated ReplicaFailure True FailedCreate .....(略)