主打一手「Pod」管理。
分散式系統的一大優勢:可以針對不同的服務制定不同的管理策略,比如釋出、更新、下線,會根據服務自身的特點採取相應的措施;
如果服務的流量小,通常採取單服務部署即可,並且可以限定其資源分配;
如果服務的流量大並且是核心的能力,通常會採用叢集的方式管理,資源分配上也會更加的傾斜,從而保證核心應用的穩定性;
這種管理策略雖然很合理,但是從技術角度來看,其實現的複雜程度度也相對較高,在Kubenetes中可以使用Deployment元件簡化服務的編排難度;
通過Deployment控制器,可以對應用進行快速的編排,比如宣告Pod的釋出方式,更新和回滾策略,維持Pod副本數量;
實際上控制器並不會直接管理Pod,而是通過管理ReplicaSet間接實現Pod管理,ReplicaSet是在後臺管理的Pod,在應用部署後可以檢視相關的組態檔來驗證該流程;
作為K8S的工作負載(執行的應用程式)資源,Deployment為Pod和ReplicaSet提供宣告式的管理能力;
這裡只是一個簡單的Deployment的yaml檔案,作為生產環境中最常用的部署方式,更多的細節可以參考K8S檔案;
Deployment資源指令碼,容器使用【auto-serve:latest】映象檔案;
apiVersion: apps/v1
kind: Deployment
metadata:
name: serve-deployment
labels:
app: auto-serve
spec:
replicas: 2
selector:
matchLabels:
app: auto-serve
template:
metadata:
labels:
app: auto-serve
spec:
containers:
- name: auto-serve
image: auto-serve:latest
imagePullPolicy: Never
ports:
- containerPort: 8082
執行建立,然後在命令列或者控制檯介面檢視相關結果;
kubectl apply -f serve-deployment.yaml
檢視指定【serve-deployment】資訊;
kubectl get deployment/serve-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
serve-deployment 2/2 2 2 5s
檢視指定【serve-deployment】描述資訊;
kubectl describe deployment/serve-deployment
Name: serve-deployment
Labels: app=auto-serve
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=auto-serve
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
Pod Template:
Labels: app=auto-serve
Containers:
auto-serve:
Image: auto-serve:latest
Port: 8082/TCP
Host Port: 0/TCP
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set serve-deployment-f6f6c5bbd to 2
可以在控制檯介面檢視【ReplicaSet】,或者直接從指令碼檔案檢視相關資訊;
檢視上線狀態
kubectl rollout status deployment/serve-deployment
deployment "serve-deployment" successfully rolled out
檢視【ReplicaSet】
kubectl get rs
NAME DESIRED CURRENT READY AGE
serve-deployment-f6f6c5bbd 2 2 2 12m
檢視【Pod】
kubectl get pods
NAME READY STATUS RESTARTS AGE
serve-deployment-f6f6c5bbd-d8k6v 1/1 Running 0 15m
serve-deployment-f6f6c5bbd-hs6h5 1/1 Running 0 15m
提供【auto-serve】多個版本的映象檔案,用來模擬最常見的映象更新動作;
更新為【1.1.1】映象版本
kubectl set image deployment/serve-deployment auto-serve=auto-serve:1.1.1
檢視描述資訊
kubectl describe deployment/serve-deployment
Name: serve-deployment
Labels: app=auto-serve
Annotations: 【deployment.kubernetes.io/revision: 2】對比此處和初次釋出的描述
Selector: app=auto-serve
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=auto-serve
Containers:
auto-serve:
Image: auto-serve:1.1.1 【映象已更新】
Port: 8082/TCP
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
NewReplicaSet: serve-deployment-6d9cd7f8c6 (2/2 replicas created)
kubectl delete -f serve-deployment.yaml
再次執行映象更新,這樣【serve-deployment】會有3個版本
kubectl set image deployment/serve-deployment auto-serve=auto-serve:2.2.2
檢視歷史版本
kubectl rollout history deployment/serve-deployment
deployment.apps/serve-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
檢視歷史版本2資訊,【auto-serve】對應的映象版本是【1.1.1】
kubectl rollout history deployment/serve-deployment --revision=2
deployment.apps/serve-deployment with revision #2
Pod Template:
Labels: app=auto-serve
pod-template-hash=6d9cd7f8c6
Containers:
auto-serve:
Image: auto-serve:1.1.1
Port: 8082/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
回滾到上個版本,即映象【auto-serve:1.1.1】
kubectl rollout undo deployment/serve-deployment
deployment.apps/serve-deployment rolled back
回滾到指定版本,即映象【auto-serve:latest】
kubectl rollout undo deployment/serve-deployment --to-revision=1
伸縮命令
kubectl scale deployment/serve-deployment --replicas=3
deployment.apps/serve-deployment scaled
檢視【ReplicaSet】資訊
kubectl get rs
NAME DESIRED CURRENT READY AGE
serve-deployment-6b47bf4db7 0 0 0 7m3s
serve-deployment-6d9cd7f8c6 0 0 0 7m17s
serve-deployment-f6f6c5bbd 3 3 3 8m10s
檢視【Pod】資訊
kubectl get pods
NAME READY STATUS RESTARTS AGE
serve-deployment-f6f6c5bbd-4rvhw 1/1 Running 0 4m13s
serve-deployment-f6f6c5bbd-bwg7s 1/1 Running 0 4m15s
serve-deployment-f6f6c5bbd-mv9wt 1/1 Running 0 2m21s
暫停釋出
kubectl rollout pause deployment/serve-deployment
deployment.apps/serve-deployment paused
檢視資訊描述
kubectl describe deployment/serve-deployment
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing Unknown DeploymentPaused 【注意此處的狀態】
執行更新
kubectl set image deployment/serve-deployment auto-serve=auto-serve:1.1.1
檢視Pod,未觸發上線
恢復釋出,會自動執行上面的更新動作
kubectl rollout resume deployment/serve-deployment
檢視【ReplicaSet】資訊
kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES
serve-deployment-6b47bf4db7 0 0 0 14m auto-serve auto-serve:2.2.2
serve-deployment-6d9cd7f8c6 3 3 3 15m auto-serve auto-serve:1.1.1
serve-deployment-f6f6c5bbd 0 0 0 16m auto-serve auto-serve:latest
檢視【Pod】資訊
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
serve-deployment-6d9cd7f8c6-m66cv 1/1 Running 0 2m9s 10.1.0.125 docker-desktop
serve-deployment-6d9cd7f8c6-sk2qv 1/1 Running 0 2m11s 10.1.0.124 docker-desktop
serve-deployment-6d9cd7f8c6-zj6p2 1/1 Running 0 2m8s 10.1.0.126 docker-desktop
Deployment有三個核心狀態來描述其生命週期的變化:「Progressing」進行中,「Complete」已完成,「Failed」失敗;
【Progressing】
Deployment處在部署或者伸縮過程中;
當Deployment執行這些任務期間:建立新的ReplicaSet;正在為其最新的ReplicaSet擴容;正在為其舊有的ReplicaSets縮容;新的Pod已經就緒或者可用;
【Complete】
具有以下特徵時會被標記為已完成狀態;
Deployment關聯的所有副本都已更新到指定的最新版本,意味著此前請求的所有更新都已完成;Deployment關聯的所有副本都可用;未執行Deployment的舊副本;
【Failed】
Deployment可能會在嘗試部署其最新的ReplicaSet受挫,一直處於未完成狀態;
造成這種情況的因素很多,可能是:配額不足,就緒探測失敗,映象拉取錯誤,許可權不足,限制範圍問題,應用程式執行時的設定錯誤;
檔案倉庫:
https://gitee.com/cicadasmile/butte-java-note
指令碼倉庫:
https://gitee.com/cicadasmile/butte-auto-parent