Kubernetes 調度器和調度算法

簡介

Scheduler 是 kubernetes 的調度器,主要的任務是把定義的 pod 分配到集群的節點上。聽起來非常簡單,但有很多要考慮的問題:

  • 公平:如何保證每個節點都能被分配資源
  • 資源高效利用:集群所有資源最大化被使用
  • 效率:調度的性能要好,能夠儘快地對大批量的 pod 完成調度工作
  • 靈活:允許用戶根據自己的需求控制調度的邏輯

Sheduler 是作為單獨的程序運行的,啟動之後會一直堅挺 API Server,獲取 PodSpec.NodeName 為空的 pod,對每個 pod 都會創建一個 binding,表明該 pod 應該放到哪個節點上


Kubernetes 調度器和調度算法

調度過程

調度分為幾個部分:首先是過濾掉不滿足條件的節點,這個過程稱為 predicate;然後對通過的節點按照優先級排序,這個是 priority;最後從中選擇優先級最高的節點。如果中間任何一步驟有錯誤,就直接返回錯誤

Predicate 有一系列的算法可以使用:

  • PodFitsResources:節點上剩餘的資源是否大於 pod 請求的資源
  • PodFitsHost:如果 pod 指定了 NodeName,檢查節點名稱是否和 NodeName 匹配
  • PodFitsHostPorts:節點上已經使用的 port 是否和 pod 申請的 port 衝突
  • PodSelectorMatches:過濾掉和 pod 指定的 label 不匹配的節點
  • NoDiskConflict:已經 mount 的 volume 和 pod 指定的 volume 不衝突,除非它們都是隻讀

如果在 predicate 過程中沒有合適的節點,pod 會一直在 pending 狀態,不斷重試調度,直到有節點滿足條件。經過這個步驟,如果有多個節點滿足條件,就繼續 priorities 過程: 按照優先級大小對節點排序

優先級由一系列鍵值對組成,鍵是該優先級項的名稱,值是它的權重(該項的重要性)。這些優先級選項包括:

  • LeastRequestedPriority:通過計算 CPU 和 Memory 的使用率來決定權重,使用率越低權重越高。換句話說,這個優先級指標傾向於資源使用比例更低的節點
  • BalancedResourceAllocation:節點上 CPU 和 Memory 使用率越接近,權重越高。這個應該和上面的一起使用,不應該單獨使用
  • ImageLocalityPriority:傾向於已經有要使用鏡像的節點,鏡像總大小值越大,權重越高

通過算法對所有的優先級項目和權重進行計算,得出最終的結果

自定義調度器

除了 kubernetes 自帶的調度器,你也可以編寫自己的調度器。通過 spec:schedulername 參數指定調度器的名字,可以為 pod 選擇某個調度器進行調度。比如下面的 pod 選擇 my-scheduler 進行調度,而不是默認的 default-scheduler:

<code>

apiVersion

: v1

kind

: Pod

metadata

:

name

: annotation-second-scheduler

labels

:

name

: multischeduler-example

spec

:

schedulername

: my-scheduler

containers

: -

name

: pod-with-second-annotation-container

image

: gcr.io/google_containers/

pause

:

2.0

/<code>

節點親和性

pod.spec.nodeAffinity

  • preferredDuringSchedulingIgnoredDuringExecution:軟策略
  • requiredDuringSchedulingIgnoredDuringExecution:硬策略

requiredDuringSchedulingIgnoredDuringExecution

<code>

apiVersion

: v1

kind

: Pod

metadata

:

name

: affinity

labels

:

app

: node-affinity-pod

spec

:

containers

: -

name

: with-node-affinity

image

: hub.atguigu.com/library/

myapp

:v1

affinity

:

nodeAffinity

:

requiredDuringSchedulingIgnoredDuringExecution

:

nodeSelectorTerms

: -

matchExpressions

: -

key

: kubernetes.io/hostname

operator

: NotIn

values

: - k8s-node02 /<code>

preferredDuringSchedulingIgnoredDuringExecution

<code>

apiVersion

: v1

kind

: Pod

metadata

:

name

: affinity

labels

:

app

: node-affinity-pod

spec

:

containers

: -

name

: with-node-affinity

image

: hub.atguigu.com/library/

myapp

:v1

affinity

:

nodeAffinity

:

preferredDuringSchedulingIgnoredDuringExecution

: -

weight

:

1

preference

:

matchExpressions

: -

key

: source

operator

: In

values

: - qikqiak /<code>

合體

<code>

apiVersion

: v1

kind

: Pod

metadata

:

name

: affinity

labels

:

app

: node-affinity-pod

spec

:

containers

: -

name

: with-node-affinity

image

: hub.atguigu.com/library/

myapp

:v1

affinity

:

nodeAffinity

:

requiredDuringSchedulingIgnoredDuringExecution

:

nodeSelectorTerms

: -

matchExpressions

: -

key

: kubernetes.io/hostname

operator

: NotIn

values

: - k8s-node02

preferredDuringSchedulingIgnoredDuringExecution

: -

weight

:

1

preference

:

matchExpressions

: -

key

: source

operator

: In

values

: - qikqiak /<code>

鍵值運算關係

  • In:label 的值在某個列表中
  • NotIn:label 的值不在某個列表中
  • Gt:label 的值大於某個值
  • Lt:label 的值小於某個值
  • Exists:某個 label 存在
  • DoesNotExist:某個 label 不存在

Pod 親和性

pod.spec.affinity.podAffinity/podAntiAffinity

  • preferredDuringSchedulingIgnoredDuringExecution:軟策略
  • requiredDuringSchedulingIgnoredDuringExecution:硬策略
<code>

apiVersion

: v1

kind

: Pod

metadata

:

name

: pod-

3

labels

:

app

: pod-

3

spec

:

containers

: -

name

: pod-

3

image

: hub.atguigu.com/library/

myapp

:v1

affinity

:

podAffinity

:

requiredDuringSchedulingIgnoredDuringExecution

: -

labelSelector

:

matchExpressions

: -

key

: app

operator

: In

values

: - pod-

1

topologyKey

: kubernetes.io/hostname

podAntiAffinity

:

preferredDuringSchedulingIgnoredDuringExecution

: -

weight

:

1

podAffinityTerm

:

labelSelector

:

matchExpressions

: -

key

: app

operator

: In

values

: - pod-

2

topologyKey

: kubernetes.io/hostname /<code>

親和性/反親和性調度策略比較如下:

調度策略匹配標籤操作符拓撲域支持調度目標nodeAffinity主機In, NotIn, Exists, DoesNotExist, Gt, Lt否指定主機podAffinityPODIn, NotIn, Exists, DoesNotExist是POD與指定POD同一拓撲域podAnitAffinityPODIn, NotIn, Exists, DoesNotExist是POD與指定POD不在同一拓撲域

Taint 和 Toleration

節點親和性,是 pod 的一種屬性(偏好或硬性要求),它使 pod 被吸引到一類特定的節點。Taint 則相反,它使 節點 能夠 排斥 一類特定的 pod

Taint 和 toleration 相互配合,可以用來避免 pod 被分配到不合適的節點上。每個節點上都可以應用一個或多個 taint ,這表示對於那些不能容忍這些 taint 的 pod,是不會被該節點接受的。如果將 toleration 應用於 pod 上,則表示這些 pod 可以(但不要求)被調度到具有匹配 taint 的節點上

汙點(Taint)

Ⅰ、 汙點 ( Taint ) 的組成

使用 kubectl taint 命令可以給某個 Node 節點設置汙點,Node 被設置上汙點之後就和 Pod 之間存在了一種相斥的關係,可以讓 Node 拒絕 Pod 的調度執行,甚至將 Node 已經存在的 Pod 驅逐出去

每個汙點的組成如下:

<code>

key

=value:effect /<code>

每個汙點有一個 key 和 value 作為汙點的標籤,其中 value 可以為空,effect 描述汙點的作用。當前 taint effect 支持如下三個選項:

  • NoSchedule:表示 k8s 將不會將 Pod 調度到具有該汙點的 Node 上
  • PreferNoSchedule:表示 k8s 將盡量避免將 Pod 調度到具有該汙點的 Node 上
  • NoExecute:表示 k8s 將不會將 Pod 調度到具有該汙點的 Node 上,同時會將 Node 上已經存在的 Pod 驅逐出去

Ⅱ、汙點的設置、查看和去除

<code> 

kubectl

taint nodes node1 key1=value1:NoSchedule

kubectl

describe pod pod-name

kubectl

taint nodes node1 key1:NoSchedule-

/<code>

容忍(Tolerations)

設置了汙點的 Node 將根據 taint 的 effect:NoSchedule、PreferNoSchedule、NoExecute 和 Pod 之間產生互斥的關係,Pod 將在一定程度上不會被調度到 Node 上。 但我們可以在 Pod 上設置容忍 ( Toleration ) ,意思是設置了容忍的 Pod 將可以容忍汙點的存在,可以被調度到存在汙點的 Node 上

pod.spec.tolerations

<code>

tolerations

: -

key

:

"key1"

operator

:

"Equal"

value

:

"value1"

effect

:

"NoSchedule"

tolerationSeconds

:

3600

-

key

:

"key1"

operator

:

"Equal"

value

:

"value1"

effect

:

"NoExecute"

-

key

:

"key2"

operator

:

"Exists"

effect

:

"NoSchedule"

/<code>
  • 其中 key, vaule, effect 要與 Node 上設置的 taint 保持一致
  • operator 的值為 Exists 將會忽略 value 值
  • tolerationSeconds 用於描述當 Pod 需要被驅逐時可以在 Pod 上繼續保留運行的時間

Ⅰ、當不指定 key 值時,表示容忍所有的汙點 key:

<code>

tolerations

: -

operator

:

"Exists"

/<code>

Ⅱ、當不指定 effect 值時,表示容忍所有的汙點作用

<code>

tolerations

: -

key

:

"key"

operator

:

"Exists"

/<code>

Ⅲ、有多個 Master 存在時,防止資源浪費,可以如下設置

<code>kubectl taint nodes Node-Name node-role.kubernetes.

io

/master=:PreferNoSchedule /<code>

指定調度節點

Ⅰ、Pod.spec.nodeName 將 Pod 直接調度到指定的 Node 節點上,會跳過 Scheduler 的調度策略,該匹配規則是強制匹配

<code>

apiVersion:

extensions/v1beta1

kind:

Deployment

metadata:

name:

myweb

spec:

replicas:

7

template:

metadata:

labels:

app:

myweb

spec:

nodeName:

k8s-node01

containers:

-

name:

myweb

image:

hub.atguigu.com/library/myapp:v1

ports:

-

containerPort:

80

/<code>

Ⅱ、Pod.spec.nodeSelector:通過 kubernetes 的 label-selector 機制選擇節點,由調度器調度策略匹配 label,而後調度 Pod 到目標節點,該匹配規則屬於強制約束

<code>

apiVersion:

extensions/v1beta1

kind:

Deployment

metadata:

name:

myweb

spec:

replicas:

2

template:

metadata:

labels:

app:

myweb

spec:

nodeSelector:

type:

backEndNode1

containers:

-

name:

myweb

image:

harbor/tomcat:8.5-jre8

ports:

-

containerPort:

80

/<code>


分享到:


相關文章: