1. 程式人生 > >Kubernetes Scheduler原理解析

Kubernetes Scheduler原理解析

本文是對Kubernetes Scheduler的演算法解讀和原理解析,重點介紹了預選(Predicates)和優選(Priorities)步驟的原理,並介紹了預設配置的Default Policies。接下來,我會分析Kubernetes Scheduler的原始碼,窺探其具體的實現細節以及如何開發一個Policy,見我下片博文吧。

Scheduler及其演算法介紹

Kubernetes Scheduler是Kubernetes Master的一個元件,通常與API Server和Controller Manager元件部署在一個節點,共同組成Master的三劍客。

一句話概括Scheduler的功能:將PodSpec.NodeName為空的Pods逐個地,經過預選(Predicates)和優選(Priorities)兩個步驟,挑選最合適的Node作為該Pod的Destination。

展開這兩個步驟,就是Scheduler的演算法描述:

  • 預選:根據配置的Predicates Policies(預設為DefaultProvider中定義的default predicates policies集合)過濾掉那些不滿足這些Policies的的Nodes,剩下的Nodes就作為優選的輸入。
  • 優選:根據配置的Priorities Policies(預設為DefaultProvider中定義的default priorities policies集合)給預選後的Nodes進行打分排名,得分最高的Node即作為最適合的Node,該Pod就Bind到這個Node。

    如果經過優選將Nodes打分排名後,有多個Nodes並列得分最高,那麼scheduler將隨機從中選擇一個Node作為目標Node。

因此整個schedule過程,演算法本身的邏輯是非常簡單的,關鍵在這些Policies的邏輯,下面我們就來看看Kubernetes的Predicates and Priorities Policies。

Predicates and Priorities Policies

Predicates Policies

Predicates Policies就是提供給Scheduler用來過濾出滿足所定義條件的Nodes,併發的(最多16個goroutine)對每個Node啟動所有Predicates Policies的遍歷Filter,看其是否都滿足配置的Predicates Policies,若有一個Policy不滿足,則直接被淘汰。

注意:這裡的併發goroutine number為All Nodes number,但最多不能超過16個,由一個queue控制。

Kubernetes提供了以下Predicates Policies的定義,你可以在kube-scheduler啟動引數中新增--policy-config-file來指定要運用的Policies集合,比如:

{
"kind" : "Policy",
"apiVersion" : "v1",
"predicates" : [
    {"name" : "PodFitsPorts"},
    {"name" : "PodFitsResources"},
    {"name" : "NoDiskConflict"},
    {"name" : "NoVolumeZoneConflict"},
    {"name" : "MatchNodeSelector"},
    {"name" : "HostName"}
    ],
"priorities" : [
    ...
    ]
}
  1. NoDiskConflict: Evaluate if a pod can fit due to the volumes it requests, and those that are already mounted. Currently supported volumes are: AWS EBS, GCE PD, ISCSI and Ceph RBD. Only Persistent Volume Claims for those supported types are checked. Persistent Volumes added directly to pods are not evaluated and are not constrained by this policy.

  2. NoVolumeZoneConflict: Evaluate if the volumes a pod requests are available on the node, given the Zone restrictions.

  3. PodFitsResources: Check if the free resource (CPU and Memory) meets the requirement of the Pod. The free resource is measured by the capacity minus the sum of requests of all Pods on the node. To learn more about the resource QoS in Kubernetes, please check QoS proposal.

  4. PodFitsHostPorts: Check if any HostPort required by the Pod is already occupied on the node.

  5. HostName: Filter out all nodes except the one specified in the PodSpec’s NodeName field.

  6. MatchNodeSelector: Check if the labels of the node match the labels specified in the Pod’s nodeSelector field and, as of Kubernetes v1.2, also match the scheduler.alpha.kubernetes.io/affinity pod annotation if present. See here for more details on both.

  7. MaxEBSVolumeCount: Ensure that the number of attached ElasticBlockStore volumes does not exceed a maximum value (by default, 39, since Amazon recommends a maximum of 40 with one of those 40 reserved for the root volume – see Amazon’s documentation). The maximum value can be controlled by setting the KUBE_MAX_PD_VOLS environment variable.

  8. MaxGCEPDVolumeCount: Ensure that the number of attached GCE PersistentDisk volumes does not exceed a maximum value (by default, 16, which is the maximum GCE allows – see GCE’s documentation). The maximum value can be controlled by setting the KUBE_MAX_PD_VOLS environment variable.

  9. CheckNodeMemoryPressure: Check if a pod can be scheduled on a node reporting memory pressure condition. Currently, no BestEffort should be placed on a node under memory pressure as it gets automatically evicted by kubelet.

  10. CheckNodeDiskPressure: Check if a pod can be scheduled on a node reporting disk pressure condition. Currently, no pods should be placed on a node under disk pressure as it gets automatically evicted by kubelet.

預設的DefaultProvider中選了以下Predicates Policies:

  1. NoVolumeZoneConflict
  2. MaxEBSVolumeCount
  3. MaxGCEPDVolumeCount
  4. MatchInterPodAffinity

    說明:Fit is determined by inter-pod affinity.AffinityAnnotationKey represents the key of affinity data (json serialized) in the Annotations of a Pod.

    AffinityAnnotationKey string = "scheduler.alpha.kubernetes.io/affinity"

  5. NoDiskConflict
  6. GeneralPredicates
    • PodFitsResources
      • pod, in number
      • cpu, in cores
      • memory, in bytes
      • alpha.kubernetes.io/nvidia-gpu, in devices。截止V1.4,每個node最多隻支援1個gpu
    • PodFitsHost
    • PodFitsHostPorts
    • PodSelectorMatches
  7. PodToleratesNodeTaints
  8. CheckNodeMemoryPressure
  9. CheckNodeDiskPressure

Priorities Policies

經過預選策略甩選後得到的Nodes,會來到優選步驟。在這個過程中,會併發的根據每個Node分別啟動一個goroutine,在每個goroutine中會根據對應的policy實現,遍歷所有的預選Nodes,分別進行打分,每個Node每一個Policy的打分為0-10分,0分最低,10分最高。待所有policy對應的goroutine都完成後,根據設定的各個priorities policies的權重weight,對每個node的各個policy的得分進行加權求和作為最終的node的得分。

finalScoreNodeA = (weight1 * priorityFunc1) + (weight2 * priorityFunc2)

注意:這裡的併發goroutine number為All Nodes number,但最多不能超過16個,由一個queue控制。

思考:如果經過預選後,沒有一個Node滿足條件,則直接返回FailedPredicates報錯,不會再觸發Prioritizing階段,這是合理的。但是,如果經過預選後,只有一個Node滿足條件,同樣會觸發Prioritizing,並且所走的流程和多個Nodes一樣。實際上,如果只有一個Node滿足條件,在優選階段,可以直接返回該Node作為最終scheduled結果,無需跑完整個打分流程。

如果經過優選將Nodes打分排名後,有多個Nodes並列得分最高,那麼scheduler將隨機從中選擇一個Node作為目標Node。

Kubernetes提供了以下Priorities Policies的定義,你可以在kube-scheduler啟動引數中新增--policy-config-file來指定要運用的Policies集合,比如:

{
"kind" : "Policy",
"apiVersion" : "v1",
"predicates" : [
    ...
    ],
"priorities" : [
    {"name" : "LeastRequestedPriority", "weight" : 1},
    {"name" : "BalancedResourceAllocation", "weight" : 1},
    {"name" : "ServiceSpreadingPriority", "weight" : 1},
    {"name" : "EqualPriority", "weight" : 1}
    ]
}
  • LeastRequestedPriority: The node is prioritized based on the fraction of the node that would be free if the new Pod were scheduled onto the node. (In other words, (capacity - sum of requests of all Pods already on the node - request of Pod that is being scheduled) / capacity). CPU and memory are equally weighted. The node with the highest free fraction is the most preferred. Note that this priority function has the effect of spreading Pods across the nodes with respect to resource consumption.
  • BalancedResourceAllocation: This priority function tries to put the Pod on a node such that the CPU and Memory utilization rate is balanced after the Pod is deployed.
  • SelectorSpreadPriority: Spread Pods by minimizing the number of Pods belonging to the same service, replication controller, or replica set on the same node. If zone information is present on the nodes, the priority will be adjusted so that pods are spread across zones and nodes.
  • CalculateAntiAffinityPriority: Spread Pods by minimizing the number of Pods belonging to the same service on nodes with the same value for a particular label.
  • ImageLocalityPriority: Nodes are prioritized based on locality of images requested by a pod. Nodes with larger size of already-installed packages required by the pod will be preferred over nodes with no already-installed packages required by the pod or a small total size of already-installed packages required by the pod.
  • NodeAffinityPriority: (Kubernetes v1.2) Implements preferredDuringSchedulingIgnoredDuringExecution node affinity; see here for more details.

預設的DefaultProvider中選了以下Priorities Policies

  1. SelectorSpreadPriority, 預設權重為1
  2. InterPodAffinityPriority, 預設權重為1

    • pods should be placed in the same topological domain (e.g. same node, same rack, same zone, same power domain, etc.)
    • as some other pods, or, conversely, should not be placed in the same topological domain as some other pods.
    • AffinityAnnotationKey represents the key of affinity data (json serialized) in the Annotations of a Pod.

    scheduler.alpha.kubernetes.io/affinity="..."

  3. LeastRequestedPriority, 預設權重為1

  4. BalancedResourceAllocation, 預設權重為1
  5. NodePreferAvoidPodsPriority, 預設權重為10000

    說明:這裡權重設定足夠大(10000),如果得分不為0,那麼加權後最終得分將很高,如果得分為0,那麼意味著相對其他得搞很高的,註定被淘汰,分析如下:

    如果Node的Anotation沒有設定key-value:

    scheduler.alpha.kubernetes.io/preferAvoidPods="..."

    則該node對該policy的得分就是10分,加上權重10000,那麼該node對該policy的得分至少10W分。

    如果Node的Anotation設定了

    scheduler.alpha.kubernetes.io/preferAvoidPods="..."

    如果該pod對應的Controller是ReplicationController或ReplicaSet,則該node對該policy的得分就是0分,那麼該node對該policy的得分相對沒有設定該Anotation的Node得分低的離譜了。也就是說這個Node一定會被淘汰!

  6. NodeAffinityPriority, 預設權重為1

  7. TaintTolerationPriority, 預設權重為1

scheduler演算法流程圖

這裡寫圖片描述

總結

  • kubernetes scheduler的任務就是將pod排程到最合適的Node。
  • 整個排程過程分兩步:預選(Predicates)和優選(Policies)
  • 預設配置的排程策略為DefaultProvider,具體包含的策略見上。
  • 可以通過kube-scheduler的啟動引數–policy-config-file指定一個自定義的Json內容的檔案,按照格式組裝自己Predicates and Priorities policies。

相關推薦

Kubernetes Scheduler原理解析

本文是對Kubernetes Scheduler的演算法解讀和原理解析,重點介紹了預選(Predicates)和優選(Priorities)步驟的原理,並介紹了預設配置的Default Policies。接下來,我會分析Kubernetes Scheduler的

Golang-Scheduler原理解析

本文主要分析Golang裡面對於協程的排程原理,本文與Golang的memory allocation、channel、garbage collection這三個主題是緊密相關的,本文scheduler作為系列的第一篇文章。 文章大體上的思路是這樣的: section1:主要圖示

Kubernetes網路原理解析

1. kubernetes網路模型1.1. 基礎原則每個Pod都擁有一個獨立的IP地址,而且假定所有Pod都在一個可以直接連通的、扁平的網路空間中,不管是否執行在同一Node上都可以通過Pod的IP來訪問。k8s中Pod的IP是最小粒度IP。同一個Pod內所有的容器共享一個網

[Architect] Abp 框架原理解析(5) UnitOfWork

框架 方法 src options nalu res actions cnblogs 一個數 本節目錄 介紹 分析Abp源碼 實現UOW 介紹 UOW(全稱UnitOfWork)是指工作單元. 在Abp中,工作單元對於倉儲和應用服務方法默認開啟。並在一次請求中,共享

angularjs工作原理解析

body oot 分隔 復制 抖動 修改 重新 接收 裏的 個人覺得,要很好的理解AngularJS的運行機制,才能盡可能避免掉到坑裏面去。在這篇文章中,我將根據網上的資料和自己的理解對AngularJS的在啟動後,每一步都做了些什麽,做一個比較清楚詳細的解析。 首

USB Type-C工作原理解析

說明 是否 forms dfp 其他 耗時 def 左右 del 自從蘋果發布了新MacBook,USB Type-C接口就成為了熱議對象。我來從硬件角度解析下這個USB Type-C,以便大家更好的了解USB Type-C的工作原理。特色尺寸小,支持正反插,速度快(10G

LocationManager(一)-定位方式原理解析

一段時間 接入點 work use npr roi 無線網 服務器 輔助 參考資源:android 4種定位原理及實現——1 android使用不同的方法為應用提供位置信息。 定位的方式有三種:GPS地位(A-GPSAssistedGPS:輔助全球衛星定位系統,或者是同步G

移動端使用rem同時適應安卓ios手機原理解析,移動端響應式開發

size screen bsp 應用 屏幕 來看 比例 忽略 基礎 rem單位大家可能已經很熟悉,rem是隨著html的字體大小來顯示代表寬度的方法,我們怎樣進行移動端響應式開發呢 瀏覽器默認的字體大小為16px 及1rem 等於 16px 如果我們想要使1rem等於 12

短信轟炸工具原理解析

圖形驗證碼 led 可能 https encoding 驗證碼生成 dex alt create 溫馨提示:本文文章緊作為學習探討,不能用於破壞攻擊用途,後果自負。文章後面有Demo源碼下載,使用C#語言開發。   相信不少人都莫名奇妙收過一些註冊驗證碼短信、登錄驗證碼短

【數據壓縮】JPEG標準與原理解析

round 高頻 切割 基於 大小 image 生成 p s pan 轉載請註明出處:http://blog.csdn.net/luoshixian099/article/details/50392230 CSDN-勿在浮沙築高臺 為了滿足不同應用的需求,J

數據庫水平切分(拆庫拆表)的實現原理解析(轉)

數字 一個數據庫 java ins 結果 都對 不同 com 嚴重 第1章 引言 隨著互聯網應用的廣泛普及,海量數據的存儲和訪問成為了系統設計的瓶頸問題。對於一個大型的互聯網應用,每天幾十億的PV無疑對數據庫造成了相當 高的負載。對於系統的穩定性和擴展性造成了極大的問題。

遊戲外掛原理解析與制作 - [內存數值修改類 篇一]

tle lin 篩選 分享 自己的 src 但我 以及 先來   本章旨在講解外掛實現原理,未深入涉及至代碼層面。希望能與對這方面感興趣的朋友多多交流,畢竟理論是死的,套路是固定的,只有破解經驗是花大量時間和心血積累的。 對於單機遊戲而言,遊戲中絕大部分的參數(比如血、藍

圍棋人機大戰中阿爾法狗原理解析,左右互搏,青出於藍而勝於藍?

重新 公園 mas 人機大戰 規律 .com boa beyond 暫時 —阿爾法狗原理解析 這些天都在沒日沒夜地關註一個話題,谷歌人工智能程序AlphaGo(國內網友親切地稱為“阿 爾法狗”)擊敗歐洲職業圍棋冠軍樊麾二段,在圍棋遊戲中達到了人類職業棋手的水平。什麽!!19

遊戲外掛原理解析與制作 - [內存數值修改類 篇二]

物理內存 one 很難 byte array private src 所有 計算   本章旨在講解如何利用高級語言根據變量數值尋找內存地址。涉及代碼以C#為例。   我用C#寫了一個WinForm形式的Demo,界面如下:      源代碼: //血量初始

Spring源碼:IOC原理解析(二)

main 節點 定義 nat ner multicast esp loading more 版權聲明:本文為博主原創文章,轉載請註明出處,歡迎交流學習! 接著上一章節的內容,我們來分析當new一個FileSystemXmlApplicationContext對

Giraph源代碼分析(九)—— Aggregators 原理解析

需要 which 詳細 當前 推斷 part waiting ted class HamaWhite 原創。轉載請註明出處!歡迎大家增加Giraph 技術交流群: 228591158 Giraph中Aggregator的基本使用方法請參考官方文檔:http

第九章 Servllet工作原理解析

jet 原理解析 如何工作 servlet容器 應用 創建 strong 1.2 ner 9.1 從Servlet容器說起   Servlet容器:Jetty, Tomcat等。   這裏以Tomcat為例, 真正管理Servlet的容器是Context容器

數據庫水平切分的實現原理解析——分庫,分表,主從,集群,負載均衡器(轉)

支付 讀取 dba 我們 課題研究 穩定性 存在 use 根據 第1章 引言 隨著互聯網應用的廣泛普及,海量數據的存儲和訪問成為了系統設計的瓶頸問題。對於一個大型的互聯網應用,每天幾十億的PV無疑對數據庫造成了相當高的負載。對於系統的穩定性和擴展性造成了極大的問題。通過數

MyBatis框架中Mapper映射配置的使用及原理解析(二) 配置篇 SqlSessionFactoryBuilder,XMLConfigBuilder

.cn 創建 ron 子節點 homepage 解析 調用 sco title 在 <MyBatis框架中Mapper映射配置的使用及原理解析(一) 配置與使用> 的demo中看到了SessionFactory的創建過程: SqlSessionFactory

laravel的源碼解析:PHP自動加載功能原理解析

完整 必須 開發人員 segment 多余 社區 目的 psr 完全 前言 這篇文章是對PHP自動加載功能的一個總結,內容涉及PHP的自動加載功能、PHP的命名空間、PHP的PSR0與PSR4標準等內容。 一、PHP自動加載功能 PHP自動加載功能的由來 在PHP開發