kubernetes 的调度

kubernetes 的调度

标签(空格分隔): kubernetes系列


  • 一: kubernetes的调度
  • 二: kubernetes的节点的亲和性
  • 三: kubernetes的污点与容忍
  • 四: kubernetes的固定节点

一:kubernetes的调度

1.1 scheduler 的介绍

Scheduler 是 kubernetes 的调度器,主要的任务是把定义的 pod 分配到集群的节点上。听起来非常简单,但有很多要考虑的问题: 1.公平:如何保证每个节点都能被分配资源 2. 资源高效利用:集群所有资源最大化被使用 3. 效率:调度的性能要好,能够尽快地对大批量的 pod 完成调度工作 4. 灵活:允许用户根据自己的需求控制调度的逻辑Sheduler 是作为单独的程序运行的,启动之后会一直坚挺 API Server,获取 PodSpec.NodeName 为空的 pod,对每个 pod 都会创建一个 binding,表明该 pod 应该放到哪个节点上

1.2 调度过程

调度分为几个部分:首先是过滤掉不满足条件的节点,这个过程称为 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 调度的亲和性

键值运算关系In:label 的值在某个列表中NotIn:label 的值不在某个列表中Gt:label 的值大于某个值Lt:label 的值小于某个值Exists:某个 label 存在DoesNotExist:某个 label 不存在

2.1 节点亲和性

节点亲和性pod.spec.nodeAffinitypreferredDuringSchedulingIgnoredDuringExecution:软策略requiredDuringSchedulingIgnoredDuringExecution:硬策略requiredDuringSchedulingIgnoredDuringExecution vim affit1.yaml---apiVersion: v1kind: Pod metadata: name: affinity labels: app: node-affinity-podspec: containers: - name: with-node-affinity image: wangyanglinux/myapp:v1 affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: NotIn values: - node02.flyfish---kubectl get pod --show-labes 排除operator:NotIn 就是不在 node02.flyfish 运行kubectl apply -f affit1.yamlkubectl get pod kubernetes.io/hostname: kubectl get node --show-labels


vim aifft2.yaml----apiVersion: v1kind: Podmetadata: name: affinity1 labels: app: node-affinity-pod1spec: containers: - name: with-node-affinity1 image: wangyanglinux/myapp:v1 affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: In values: - node03.flyfish----在node03.flyfish 节点上 运行kubectl apply -f aifft2.yaml

vim aifft3.yaml-----apiVersion: v1kind: Podmetadata: name: affinity2 labels: app: node-affinity-pod2spec: containers: - name: with-node-affinity2 image: wangyanglinux/myapp:v1 affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: NotIn values: - node03.flyfish-----不在node03.flyfish 运行kubectl apply -f aifft3.yaml


preferredDuringSchedulingIgnoredDuringExecution:软策略vim prefer.yaml---apiVersion: v1kind: Podmetadata: name: affinity labels: app: node-affinity-podspec: containers: - name: with-node-affinity image: wangyanglinux/myapp:v1 affinity: nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 1 preference: matchExpressions: - key: source operator: In values: - node04.flyfish---软策略 如果节点 存在 者 在这个节点上面运行,如果节点不存在者会随便选择一个节点去运行---kubectl apply -f perfer.yamlkubectl get pod -o wide 

vim perfer1.yaml---apiVersion: v1kind: Podmetadata: name: affinity1 labels: app: node-affinity-pod1spec: containers: - name: with-node-affinity1 image: wangyanglinux/myapp:v1 affinity: nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 1 preference: matchExpressions: - key: source operator: In values: - node03.flyfish---因为node3.flyfish 这个节点存在 所以 就会在node03.flyfish 这个节点上面运行kubectl apply -f perfer1.yamlkubectl get pod -o wide


合体使用---vim perfer3.yaml-----apiVersion: v1kind: Podmetadata: name: affinity2 labels: app: node-affinity-pod2spec: containers: - name: with-node-affinity2 image: wangyanglinux/myapp:v1 affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: NotIn values: - node02.flyfish preferredDuringSchedulingIgnoredDuringExecution: - weight: 1 preference: matchExpressions: - key: source operator: In values: - node03.flyfish----从上到下 进行亲和性 匹配 先满足硬 匹配在满足软匹配kubectl apply -f perfer3.yamlkubectl get pod -o wide 

2.2 pod 亲和性

pod.spec.affinity.podAffinity/podAntiAffinitypreferredDuringSchedulingIgnoredDuringExecution:软策略requiredDuringSchedulingIgnoredDuringExecution:硬策略新建一个node01 的 pod-----vim node01.yaml----apiVersion: v1kind: Podmetadata: name: node01 labels: app: node01spec: containers: - name: with-node-affinity1 image: wangyanglinux/myapp:v1----kubectl apply -f node01.yamlkubectl get pod -o wide 在 node02.flyfish 上面运行


vim pod1.yaml-----apiVersion: v1kind: Podmetadata: name: pod-3 labels: app: pod-3spec: containers: - name: pod-3 image: wangyanglinux/myapp:v1 affinity: podAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app operator: In values: - node01 topologyKey: kubernetes.io/hostname----pod-3 这个pod 必须和node01 的 pod 在一个节点上面kubectl apply -f pod1.yamlkubectl get pod -o wide 

vim pod2.yaml-----apiVersion: v1kind: Podmetadata: name: pod-4 labels: app: pod-4spec: containers: - name: pod-4 image: wangyanglinux/myapp:v1 affinity: podAntiAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app operator: In values: - node01 topologyKey: kubernetes.io/hostname------必须和 node01 的pod 不在同一个节点kubectl apply -f pod2.yamlkubectl get pod -o wide 

2.3 亲和性/反亲和性调度策略比较如下:

三:kubernetes 的污点与容忍

3.1 Taint 和 Toleration

节点亲和性,是 pod 的一种属性(偏好或硬性要求),它使 pod 被吸引到一类特定的节点。Taint 则相反,它使节点 能够 排斥 一类特定的 podTaint 和 toleration 相互配合,可以用来避免 pod 被分配到不合适的节点上。每个节点上都可以应用一个或多个taint ,这表示对于那些不能容忍这些 taint 的 pod,是不会被该节点接受的。如果将 toleration 应用于 pod上,则表示这些 pod 可以(但不要求)被调度到具有匹配 taint 的节点上

3.2 污点(Taint)

Ⅰ、 污点 ( Taint ) 的组成使用 kubectl taint 命令可以给某个 Node 节点设置污点,Node 被设置上污点之后就和 Pod 之间存在了一种相斥的关系,可以让 Node 拒绝 Pod 的调度执行,甚至将 Node 已经存在的 Pod 驱逐出去每个污点的组成如下:每个污点有一个 key 和 value 作为污点的标签,其中 value 可以为空,effect 描述污点的作用。当前 taintkey=value:effecteffect 支持如下三个选项:NoSchedule :表示 k8s 将不会将 Pod 调度到具有该污点的 Node 上PreferNoSchedule :表示 k8s 将尽量避免将 Pod 调度到具有该污点的 Node 上NoExecute :表示 k8s 将不会将 Pod 调度到具有该污点的 Node 上,同时会将 Node 上已经存在的 Pod 驱逐出去

kubectl get node kubectl describe node node01.flyfish |grep Taints 

3.3 污点的设置、查看和去除

# 设置污点kubectl taint nodes node1.flyfish key1=value1:NoSchedule# 节点说明中,查找 Taints 字段kubectl describe pod pod-name# 去除污点kubectl taint nodes node1 key1:NoSchedule-

驱逐 node03.flyfish 节点上面的 podkubectl taint nodes node03.flyfish check=flyfish:NoExecute因为 此处的pod 不是使用deployment 创建的 自主式pod 驱逐就没有了

3.4 容忍(Tolerations)

设置了污点的 Node 将根据 taint 的 effect:NoSchedule、PreferNoSchedule、NoExecute 和 Pod 之间产生互斥的关系,Pod 将在一定程度上不会被调度到 Node 上。 但我们可以在 Pod 上设置容忍 ( Toleration ) ,意思是设置了容忍的 Pod 将可以容忍污点的存在,可以被调度到存在污点的 Node 上

pod.spec.tolerations------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"------

vim pod5.yaml----apiVersion: v1kind: Podmetadata: name: pod-5 labels: app: pod-5spec: containers: - name: pod-5 image: wangyanglinux/myapp:v1 tolerations: - key: "check" operator: "Equal" value: "flyfish" effect: "NoExecute" tolerationSeconds: 3600 容忍 3600s 在节点上面运行-----驱逐node02.flyfish 上面的podkubectl taint nodes node02.flyfish check=flyfish:NoExecute 

kubectl apply -f pod5.yaml 

其中 key, vaule, effect 要与 Node 上设置的 taint 保持一致operator 的值为 Exists 将会忽略 value 值tolerationSeconds 用于描述当 Pod 需要被驱逐时可以在 Pod 上继续保留运行的时间

1.当不指定 key 值时,表示容忍所有的污点 key:tolerations:- operator: "Exists"2.当不指定 effect 值时,表示容忍所有的污点作用tolerations:- key: "key"operator: "Exists"3. 有多个 Master 存在时,防止资源浪费,可以如下设置kubectl taint nodes node01.flyfish node-role.kubernetes.io/master=:PreferNoSchedule这样主节点上面 也可以 运行pod 了

去除node02.flyfish 与node03.flyfish的污点kubectl taint nodes node02.flyfish check=flyfish:NoExecute-kubectl taint nodes node03.flyfish check=flyfish:NoExecute-


四: kubernetes的固定节点

1、Pod.spec.nodeName 将 Pod 直接调度到指定的 Node 节点上,会跳过 Scheduler 的调度策略,该匹配规则是强制匹配----vim pod.yaml ----apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: mywebspec: replicas: 3 template: metadata: labels: app: myweb spec: nodeName: node02.flyfish containers: - name: myweb image: wangyanglinux/myapp:v1 ports: - containerPort: 80----kubectl apply -f pod.yamlkubectl get pod -o wide 强制pod 运行在node02.flyfish上面


Pod.spec.nodeSelector:通过 kubernetes 的 label-selector 机制选择节点,由调度器调度策略匹配 label,而后调度 Pod 到目标节点,该匹配规则属于强制约束----vim pod1.yaml -----apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: myweb222spec: replicas: 2 template: metadata: labels: app: myweb spec: nodeSelector: disk: ssd containers: - name: myweb image: wangyanglinux/myapp:v1 ports: - containerPort: 80----kubectl apply -f pod1.yaml必须存在labels disk:ssd 才能运行pod

将 node03.flyfish 打上 disk:ssd 标签kubectl label node node03.flyfish disk=sshd kubectl get pod -o wide 


相关文章