K8s 集群容量 - kluster capacity
背景
容器平臺的三個價值:穩(wěn)定性、效率、成本,都離不開容量管理。容量管理是 Kubernetes 集群管理中非常重要的一部分。它可以確保系統(tǒng)中的資源得到合理的分配和使用,避免了因資源不足或浪費導(dǎo)致的系統(tǒng)運行異?;蛐实拖碌膯栴}。通過容量管理,可以更好地控制和優(yōu)化系統(tǒng)資源的利用,保證 Kubernetes 集群的穩(wěn)定性和可靠性。容量管理還可以幫助管理員對系統(tǒng)進行更好的規(guī)劃和預(yù)測,避免因資源不足而需要緊急擴容的情況出現(xiàn),從而提高了系統(tǒng)的可維護性和可靠性。
面臨的問題
K8s 集群管理員或多或少會被如下問題所困擾:
- 不清楚當(dāng)前集群資源使用或者剩余容量處于什么水位;
- 不清楚當(dāng)前集群資源浪費情況如何;
- 不清楚當(dāng)前集群資源碎片程度如何;
- 不清楚如何設(shè)置調(diào)度策略配置值以提升資源利用效率;
- ...
資源是一個典型的可量化的指標,上述問題均可以做到可量化,我們?nèi)鄙俚闹皇且粋€好用的工具。
項目介紹
kluster-capacity[1] 旨在通過模擬線上真實調(diào)度器的能力來解決上述問題,目前已經(jīng)支持三個能力:容量評估,調(diào)度模擬,集群壓縮。
容量評估
介紹
隨著集群中節(jié)點上新的 Pod 被調(diào)度,消耗的資源越來越多。監(jiān)控集群中可用的資源非常重要,因為運維人員可以及時增加當(dāng)前的資源,以免所有資源都耗盡?;蛘?,采取不同的步驟來增加可用資源。
集群容量包括單個集群節(jié)點的容量。容量涵蓋了 CPU、內(nèi)存、磁盤空間和其他資源。
整體剩余可分配容量是一個估計值。目標是分析剩余可分配的資源并估計可用容量,即可以在集群中安排給定資源需求的 Pod 實例數(shù)量。
增強
以下是對原集群容量的一些增強功能:
- 支持直接從集群中使用現(xiàn)有的 Pod 作為 Pod 模板。
- 支持針對不同的 Pod 模板進行批量模擬。
運行
# 直接使用指定的 pod 模板
$ ./kluster-capacity ce --kubeconfig <path to kubeconfig> --schedulerconfig= <path to schedulerconfig> --pods-from-template <path to pod templates>
# 使用集群中指定的 pod 作為模板
$ ./kluster-capacity ce --kubeconfig <path to kubeconfig> --schedulerconfig= <path to schedulerconfig> --pods-from-cluster <namespace/name key of the pod>
更多運行參數(shù)及功能,請執(zhí)行如下命令:
$ ./kluster-capacity ce --help
演示
假設(shè)集群運行有 4 個節(jié)點和 1 個主節(jié)點,每個節(jié)點有 2 個 CPU 和 4GB 內(nèi)存。而每個 Pod 所需的資源為 150m CPU 和 100Mi 內(nèi)存。
$ ./kluster-capacity ce --kubeconfig <path to kubeconfig> --schedulerconfig= <path to schedulerconfig> --pods-from-template <path to pod templates> --verbose
Pod requirements:
- cpu: 150m
- memory: 100Mi
The cluster can schedule 52 instance(s) of the pod.
Termination reason: FailedScheduling: pod (small-pod-52) failed to fit in any node
fit failure on node (kube-node-1): Insufficient cpu
fit failure on node (kube-node-4): Insufficient cpu
fit failure on node (kube-node-2): Insufficient cpu
fit failure on node (kube-node-3): Insufficient cpu
Pod distribution among nodes:
- kube-node-1: 13 instance(s)
- kube-node-4: 13 instance(s)
- kube-node-2: 13 instance(s)
- kube-node-3: 13 instance(s)
隨著集群中運行的 pod 數(shù)量增加,再次運行分析時,可調(diào)度的 pod 數(shù)量也會減少。
$ ./kluster-capacity ce --kubeconfig <path to kubeconfig> --schedulerconfig= <path to schedulerconfig> --pods-from-template <path to pod templates> --verbose
Pod requirements:
- cpu: 150m
- memory: 100Mi
The cluster can schedule 46 instance(s) of the pod.
Termination reason: FailedScheduling: pod (small-pod-46) failed to fit in any node
fit failure on node (kube-node-1): Insufficient cpu
fit failure on node (kube-node-4): Insufficient cpu
fit failure on node (kube-node-2): Insufficient cpu
fit failure on node (kube-node-3): Insufficient cpu
Pod distribution among nodes:
- kube-node-1: 11 instance(s)
- kube-node-4: 12 instance(s)
- kube-node-2: 11 instance(s)
- kube-node-3: 12 instance(s)
輸出格式
ce 命令有一個 --output (-o) 標志,可以將其輸出格式化為 json 或 yaml。
$ ./kluster-capacity ce --kubeconfig <path to kubeconfig> --schedulerconfig= <path to schedulerconfig> --pods-from-template <path to pod templates> -o json|yaml
調(diào)度模擬
介紹
調(diào)度器模擬以當(dāng)前集群中的所有 node、pod 等相關(guān)資源為輸入,模擬從沒有 pod 到創(chuàng)建并調(diào)度所有 pod 的過程。這可以用來計算集群壓縮率比,以評估調(diào)度效果或衡量調(diào)度算法的質(zhì)量。
與集群壓縮相比,其結(jié)果更加激進和理想化。
運行
./kluster-capacity ss --kubeconfig <path to kubeconfig> --schedulerconfig= <path to schedulerconfig>
更多運行參數(shù)及功能,請執(zhí)行如下命令:
$ ./kluster-capacity ss --help
它支持兩種終止條件:AllSucceed 和 AllScheduled。前者是指所有pod調(diào)度成功后程序結(jié)束,后者是指所有 pod 至少被調(diào)度一次后程序退出。默認值為 AllSucceed。可以使用 --exit-condition 標志設(shè)置退出條件。
演示
假設(shè)集群運行有 4 個節(jié)點和 1 個主節(jié)點,每個節(jié)點有 2 個 CPU 和 4GB 內(nèi)存。有 40 個資源需求是 100m CPU 和 200Mi 內(nèi)存的 Pod 需要被調(diào)度。
如果調(diào)度器使用 LeastAllocated 策略,調(diào)度結(jié)果可能如下所示:
$ ./kluster-capacity ss --kubeconfig <path to kubeconfig> --schedulerconfig= <path to schedulerconfig>
Termination reason: AllSucceed: 40 pod(s) have been scheduled successfully.
Pod distribution among nodes:
- kube-node-1: 10 instance(s)
- kube-node-2: 10 instance(s)
- kube-node-3: 10 instance(s)
- kube-node-4: 10 instance(s)
如果調(diào)整調(diào)度器使用 MostAllocated 策略,調(diào)度結(jié)果可能如下所示:
$ ./kluster-capacity ss --kubeconfig <path to kubeconfig> --schedulerconfig= <path to schedulerconfig>
Termination reason: AllSucceed: 40 pod(s) have been scheduled successfully.
Pod distribution among nodes:
- kube-node-1: 20 instance(s)
- kube-node-2: 20 instance(s)
可以分析上面的調(diào)度結(jié)果來評估調(diào)度策略的有效性和集群容量壓縮比。例如,上面的結(jié)果表示集群壓縮比為2,這意味著在理想情況下有50%的資源浪費。
集群壓縮
介紹
集群壓縮以集群的當(dāng)前狀態(tài),包括所有 node、pod 和其他相關(guān)資源作為輸入,模擬通過移除節(jié)點來壓縮集群的過程。它可用于計算集群的壓縮比,這是衡量資源利用效率的指標。
與模擬調(diào)度相比,集群壓縮的結(jié)果通常更顯示,可操作性更強。
運行
./kluster-capacity cc --kubeconfig <path to kubeconfig> --schedulerconfig= <path to schedulerconfig> --verbose
更多運行參數(shù)及功能,請執(zhí)行如下命令:
$ ./kluster-capacity cc --help
演示
假設(shè)集群運行有 4 個節(jié)點和 1 個主節(jié)點,每個節(jié)點有 2 個 CPU 和 4GB 內(nèi)存。運行有 40 個資源需求是 100m CPU 和 200Mi 內(nèi)存的 Pod。
./kluster-capacity cc --kubeconfig <path to kubeconfig> --schedulerconfig= <path to schedulerconfig> --verbose
2 node(s) in the cluster can be scaled down.
Termination reason: FailedSelectNode: could not find a node that satisfies the condition, 1 master node(s); 2 node(s) can't be scale down because of insufficient resource in other nodes;
nodes selected to be scaled down:
- kube-node-1
- kube-node-3
上面的結(jié)果表明,給定 40 個 pod 的資源需求,在保證所有 pod 都能被調(diào)度的情況下,集群可以去掉 2 個節(jié)點,壓縮比為 2,也就是有 50% 的資源浪費。
演進
當(dāng)前已經(jīng)支持上述三種能力,后續(xù)會繼續(xù)完善其他容量、資源管理相關(guān)能力,如
- 基于 snapshot 的模擬
- 資源碎片分析
幫助我們基于集群歷史某一時刻的狀態(tài)來進行相關(guān)模擬操作,以及分析資源碎片情況等,歡迎體驗并提出您的寶貴意見,謝謝!
參考資料
[1]kluster-capacity: https://github.com/k-cloud-labs/kluster-capacity