Kubernetes资源调度之节点亲和

news2024/7/6 18:10:04

Kubernetes资源调度之节点亲和

Pod节点选择器

nodeSelector指定的标签选择器过滤符合条件的节点作为可用目标节点,最终选择则基于打分机制完成。因此,后者也称为节点选择器。用户事先为特定部分的Node资源对象设定好标签,而后即可配置Pod通过节点选择器实现类似于节点的强制亲和调度。

可以通过下面的命令查看每个node上的标签

[root@k8s-01 ~]# kubectl get nodes --show-labels
NAME     STATUS   ROLES                  AGE   VERSION   LABELS
k8s-01   Ready    control-plane,master   26d   v1.22.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/fluentd-ds-ready=true,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-01,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-02   Ready    <none>                 26d   v1.22.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/fluentd-ds-ready=true,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-02,kubernetes.io/os=linux,type=kong,ype=kong
k8s-03   Ready    <none>                 26d   v1.22.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/fluentd-ds-ready=true,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-03,kubernetes.io/os=linux,type=kong
k8s-04   Ready    <none>                 8d    v1.22.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-04,kubernetes.io/os=linux
[root@k8s-01 ~]#

给node4节点打上标签

[root@k8s-01 ~]# kubectl label nodes k8s-04 type=test
node/k8s-04 labeled

测试的yaml

echo "
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: nginx-test
  name: nginx-test
spec:
  containers:
  - image: nginx
    name: nginx-test
  nodeSelector:
    type: test
" | kubectl apply -f -

查看pod的分配情况

[root@k8s-01 ~]# kubectl get pods  -o wide |grep nginx-test
nginx-test                                1/1     Running   0               58s     10.244.7.81      k8s-04   <none>           <none>
[root@k8s-01 ~]#

然后我们可以通过 describe 命令查看调度结果:

image-20220627165806382

事实上,多数情况下用户都无须关心Pod对象的具体运行位置,除非Pod依赖的特殊条件仅能由部分节点满足时,例如GPU和SSD等。即便如此,也应该尽量避免使用.spec.nodeName静态指定Pod对象的运行位置,而是应该让调度器基于标签和标签选择器为Pod挑选匹配的工作节点。另外,Pod规范中的.spec.nodeSelector仅支持简单等值关系的节点选择器,而.spec.affinity.nodeAffinity支持更灵活的节点选择器表达式,而且可以实现硬亲和与软亲和逻辑。

节点亲和调度

节点亲和性(nodeAffinity)主要是用来控制 Pod 要部署在哪些节点上,以及不能部署在哪些节点上的,它可以进行一些简单的逻辑组合了,不只是简单的相等匹配。在Pod上定义节点亲和规则时有两种类型的节点亲和关系:强制(required)亲和和首选(preferred)亲和,或分别称为硬亲和与软亲和。

  • 强制亲和限定了调度Pod资源时必须要满足的规则,无可用节点时Pod对象会被置为Pending状态,直到满足规则的节点出现。
  • 柔性亲和规则实现的是一种柔性调度限制,它同样倾向于将Pod运行在某类特定的节点之上,但无法满足调度需求时,调度器将选择一个无法匹配规则的节点,而非将Pod置于Pending状态。

在Pod规范上定义节点亲和规则的关键点有两个:

  • 一是给节点规划并配置合乎期望的标签;
  • 二是为Pod对象定义合理的标签选择器。

正如preferredDuringSchedulingIgnoredDuringExecution和requiredDuringSchedulingIgnoredDuringExecution字段名字中的后半段符串IgnoredDuringExecution隐含的意义所指,在Pod资源基于节点亲和规则调度至某节点之后,因节点标签发生了改变而变得不再符合Pod定义的亲和规则时,调度器也不会将Pod从此节点上移出,因而亲和调度仅在调度执行的过程中进行一次即时的判断,而非持续地监视亲和规则是否能够得以满足。

查看官方说明:

[root@k8s-01 ~]# kubectl explain pod.spec.affinity.nodeAffinity
KIND:     Pod
VERSION:  v1

RESOURCE: nodeAffinity <Object>

DESCRIPTION:
     Describes node affinity scheduling rules for the pod.

     Node affinity is a group of node affinity scheduling rules.

FIELDS:
   preferredDuringSchedulingIgnoredDuringExecution      <[]Object>
     The scheduler will prefer to schedule pods to nodes that satisfy the
     affinity expressions specified by this field, but it may choose a node that
     violates one or more of the expressions. The node that is most preferred is
     the one with the greatest sum of weights, i.e. for each node that meets all
     of the scheduling requirements (resource request, requiredDuringScheduling
     affinity expressions, etc.), compute a sum by iterating through the
     elements of this field and adding "weight" to the sum if the node matches
     the corresponding matchExpressions; the node(s) with the highest sum are
     the most preferred.

   requiredDuringSchedulingIgnoredDuringExecution       <Object>
     If the affinity requirements specified by this field are not met at
     scheduling time, the pod will not be scheduled onto the node. If the
     affinity requirements specified by this field cease to be met at some point
     during pod execution (e.g. due to an update), the system may or may not try
     to eventually evict the pod from its node.

[root@k8s-01 ~]#

强制亲和

查看官方说明:

[root@k8s-01 ~]# kubectl explain pod.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution
KIND:     Pod
VERSION:  v1

RESOURCE: requiredDuringSchedulingIgnoredDuringExecution <Object>

DESCRIPTION:
     If the affinity requirements specified by this field are not met at
     scheduling time, the pod will not be scheduled onto the node. If the
     affinity requirements specified by this field cease to be met at some point
     during pod execution (e.g. due to an update), the system may or may not try
     to eventually evict the pod from its node.

     A node selector represents the union of the results of one or more label
     queries over a set of nodes; that is, it represents the OR of the selectors
     represented by the node selector terms.

FIELDS:
   nodeSelectorTerms    <[]Object> -required-
     Required. A list of node selector terms. The terms are ORed.

[root@k8s-01 ~]# kubectl explain pod.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms
KIND:     Pod
VERSION:  v1

RESOURCE: nodeSelectorTerms <[]Object>

DESCRIPTION:
     Required. A list of node selector terms. The terms are ORed.

     A null or empty node selector term matches no objects. The requirements of
     them are ANDed. The TopologySelectorTerm type implements a subset of the
     NodeSelectorTerm.

FIELDS:
   matchExpressions     <[]Object>
     A list of node selector requirements by node's labels.

   matchFields  <[]Object>
     A list of node selector requirements by node's fields.

[root@k8s-01 ~]#
  • matchExpressions:标签选择器表达式,基于节点标签进行过滤;可重复使用以表达不同的匹配条件,各条件间为“或”关系。▪
  • matchFields:以字段选择器表达的节点选择器;可重复使用以表达不同的匹配条件,各条件间为“或”关系。

注意:每个匹配条件可由一到多个匹配规则组成,例如某个matchExpressions条件下可同时存在两个表达式规则,如下面的示例所示,同一条件下的各条规则彼此间为“逻辑与”关系。这意味着某节点满足nodeSelectorTerms中的任意一个条件即可,但满足某个条件指的是可完全匹配该条件下定义的所有规则。

下面开始测试,给node3打上标签

[root@k8s-01 ~]# kubectl label nodes k8s-03 disktype=ssd
node/k8s-03 labeled

测试yaml

apiVersion: v1
kind: Pod
metadata:
  name: "busy-affinity"
  labels:
    app: "busy-affinity"
spec:
  containers:
  - name: busy-affinity
    image: "busybox"
    command: ["/bin/sh","-c","sleep 600"]
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution: ## 硬标准,后面是调度期间有效,忽略执行期间
        nodeSelectorTerms:
        - matchExpressions:
            - key: disktype
              values: ["ssd","hdd"]
              operator: In

查看pod

[root@k8s-01 ~]# kubectl get pods -o wide |grep busy-affinity
busy-affinity                             1/1     Running   0               76s     10.244.165.209   k8s-03   <none>           <none>
[root@k8s-01 ~]#

从结果可以看出 Pod 被部署到了 node3 节点上,因为我们的硬策略就是部署到含有"ssd","hdd"标签的node节点上,所以会尽量满足。这里的匹配逻辑是 label 标签的值在某个列表中,现在 Kubernetes 提供的操作符有下面的几种:

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

下面给node3和node4加上所有的标签,用于测试

[root@k8s-01 ~]#  kubectl label nodes k8s-04 disktype=hdd
node/k8s-04 labeled
[root@k8s-01 ~]# kubectl label nodes k8s-04 disk=60
node/k8s-04 labeled
[root@k8s-01 ~]# kubectl label nodes k8s-04 gpu=3080
node/k8s-04 labeled
[root@k8s-01 ~]# kubectl label nodes k8s-03 gpu=3090
node/k8s-03 labeled
[root@k8s-01 ~]# kubectl label nodes k8s-03 disk=30
node/k8s-03 labeled
[root@k8s-01 ~]#

柔性亲和

节点首选亲和机制为节点选择机制提供了一种柔性控制逻辑,被调度的Pod对象不再是“必须”,而是“应该”放置到某些特定节点之上,但条件不满足时,该Pod也能够接受被编排到其他不符合条件的节点之上。另外,多个柔性亲和条件并存时,它还支持为每个条件定义weight属性以区别它们优先级,取值范围是1~100,数字越大优先级越高。

修改后的yaml,加了柔性亲和

apiVersion: v1
kind: Pod
metadata:
  name: "busy-affinity"
  labels:
    app: "busy-affinity"
spec:
  containers:
  - name: busy-affinity
    image: "busybox"
    command: ["/bin/sh","-c","sleep 600"]
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution: ## 硬标准,后面是调度期间有效,忽略执行期间
        nodeSelectorTerms:
        - matchExpressions:
            - key: disktype
              values: ["ssd","hdd"]
              operator: In
      preferredDuringSchedulingIgnoredDuringExecution:  ## 软性分
      - preference: ## 指定我们喜欢的条件 
          matchExpressions:
          - key: disk
            values: ["40"]
            operator: Gt   ## node3的disk不满足这个条件,node4满足
        weight: 70 ## 权重 0-100
      - preference: ## 指定我们喜欢的条件
          matchExpressions:
          - key: gpu
            values: ["3080"]
            operator: Gt  ## node4的gpu不满足这个条件,node3满足
        weight: 30 ## 权重 0-100

因为在硬性条件中,node3和node4都满足硬性条件,在查看一下软性条件,其中node3满足第二个,node4满足第一个,但是第一个的权重高一些,所以最终选择了node4,查看运行的pods

[root@k8s-01 ~]# kubectl get pods -o wide
NAME                                      READY   STATUS    RESTARTS        AGE     IP               NODE     NOMINATED NODE   READINESS GATES
busy-affinity                             1/1     Running   0               34s     10.244.7.82      k8s-04   <none>           <none>
counter                                   1/1     Running   0               8d      10.244.165.198   k8s-03   <none>           <none>
nfs-client-provisioner-69b76b8dc6-ms4xg   1/1     Running   1 (14d ago)     26d     10.244.179.21    k8s-02   <none>           <none>
nginx-5759cb8dcc-t4sdn                    1/1     Running   0               7d6h    10.244.179.50    k8s-02   <none>           <none>
nginx-liveness                            1/1     Running   2 (5d17h ago)   5d17h   10.244.61.218    k8s-01   <none>           <none>
nginx-nginx                               1/1     Running   0               5d17h   10.244.179.3     k8s-02   <none>           <none>
nginx-readiness                           1/1     Running   0               5d17h   10.244.61.219    k8s-01   <none>           <none>
nginx-test                                1/1     Running   0               45m     10.244.7.81      k8s-04   <none>           <none>
post-test                                 1/1     Running   0               5d18h   10.244.179.62    k8s-02   <none>           <none>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/47532.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

YOLO X 改进详解

YOLO X 主要改进&#xff1a; Anchor-Free: FCOSDecoupled detection headAdvanced label assigning strategy Network structure improvement Decoupled detection head 对比于YOLO V5, YOLO X 在detection head上有了改进。YOLO V5中&#xff0c;检测头是通过卷积同时预…

ROS2--概述

ROS2概述1 ROS2对比ROS12 ROS2 通信3 核心概念4 ros2 安装5 话题、服务、动作6 参数参考1 ROS2对比ROS1 多机器人系统&#xff1a;未来机器人一定不会是独立的个体&#xff0c;机器人和机器人之间也需要通信和协作&#xff0c;ROS2为多机器人系统的应用提供了标准方法和通信机…

时间序列:时间序列模型---自回归过程(AutoRegressive Process)

本文是Quantitative Methods and Analysis: Pairs Trading此书的读书笔记。 这次我们构造一个由无限的白噪声实现&#xff08;white noise realization) 组成的时间序列&#xff0c;即。这个由无限数目的项组成的值却是一个有限的值&#xff0c;比如时刻的值为&#xff0c; 而…

基于PHP+MySQL校园餐饮配送系统的设计与实现

随着我国国民经济的稳步发展,我国的大学生也越来越多,但是大部分学生都是没有时间和环境去自己做饭的,有很多也不会做,而很多食堂的菜品有难以下咽,所以很多人就采取了订餐的方式来进行购买美食,但是现在很多网站都是只能进行点餐,而没有智能推荐功能,本系统在原来的外卖基础上…

【Linux】文件系统

目录&#x1f308;前言&#x1f337;1、磁盘的组成&#x1f361;1.1、磁盘的物理结构&#x1f362;1.2、磁盘的存储结构&#x1f363;1.3、磁盘的逻辑结构&#x1f338;2、文件系统&#x1f364;2.1、文件系统的结构&#x1f365;2.2、inode如何与数据块建立联系&#x1f366;2…

2021年全国研究生数学建模竞赛华为杯C题帕金森病的脑深部电刺激治疗建模研究求解全过程文档及程序

2021年全国研究生数学建模竞赛华为杯 C题 帕金森病的脑深部电刺激治疗建模研究 原题再现&#xff1a; 一、背景介绍   帕金森病是一种常见的神经退行性疾病&#xff0c;临床表现的特征是静止性震颤&#xff0c;肌强直&#xff0c;运动迟缓&#xff0c;姿势步态障碍等运动症…

R语言生存分析可视化分析

生存分析指的是一系列用来探究所感兴趣的事件的发生的时间的统计方法。 生存分析被用于各种领域&#xff0c;例如&#xff1a; 癌症研究为患者生存时间分析&#xff0c; “事件历史分析”的社会学 在工程的“故障时间分析”。 在癌症研究中&#xff0c;典型的研究问题如下…

Java中如何处理时间--Date类

文章目录0 写在前面1 介绍Date类2 构造方法举例2.1 Date()2.2 Date(long date)3 Date类中常用方法4 写在最后0 写在前面 在实际业务中&#xff0c;总会碰到关于时间的问题&#xff0c;例如收集当年的第一季度的数据。第一季度也就是当年的一月一日到三月三十一日。如何处理时间…

使用markdown画流程图、时序图等

概述 能表示的图类型还有很多&#xff0c;比如&#xff1a; sequenceDiagram时序图 classDiagram类图 stateDiagram:状态图 erDiagram&#xff1a;ER图 gantt&#xff1a; 甘特图 pie&#xff1a;饼图 requirementDiagram: 需求图 流程图 流程图代码以「graph 《布局…

【毕业设计】12-基于单片机的电子体温计(原理图工程+源码工程+仿真工程+答辩论文)

【毕业设计】12-基于单片机的电子体温计&#xff08;原理图工程源码工程仿真工程答辩论文&#xff09; 文章目录【毕业设计】12-基于单片机的电子体温计&#xff08;原理图工程源码工程仿真工程答辩论文&#xff09;任务书设计说明书摘要设计框架架构设计说明书及设计文件源码展…

Efficient Large-Scale Language Model Training on GPU ClustersUsing Megatron-LM

Efficient Large-Scale Language Model Training on GPU ClustersUsing Megatron-LM 1 INTRODUCTION 在这篇文章中展示了 如何将 tensor &#xff0c;pipeline&#xff0c; data 并行组合&#xff0c;扩展到数千个GPU上。 提出了一个新的交错流水线调度&#xff0c;可以提升1…

卷积神经网络的工程技巧总结

参考 卷积神经网络的工程技巧(tricks) - 云社区 - 腾讯云 要成功地使用深度学习算法&#xff0c;仅仅知道存在哪些算法和解释它们为何有效的原理是不够的。一个优秀的机器学习实践者还需知道如何针对具体应用挑选一个合适的算法以及如何监控&#xff0c;并根据实验反馈改进机器…

基于 Hive 的 Flutter 文档类型存储

基于 Hive 的 Flutter 文档类型存储 原文 https://medium.com/gytworkz/document-type-storage-in-flutter-using-hive-a18ea9659d84 前言 长久以来&#xff0c;我们一直使用共享首选项以键对格式在本地存储中存储数据&#xff0c;或者使用 SQLite 在 SQL 数据库中存储数据。 存…

JSP | JSP原理深度剖析、基础语法

目录 一&#xff1a;分析使用纯粹Servlet开发web应用的缺陷 二&#xff1a;JSP原理深度剖析 三&#xff1a;JSP的基础语法 一&#xff1a;分析使用纯粹Servlet开发web应用的缺陷 &#xff08;1&#xff09;在Servlet当中编写HTML/CSS/JavaScript等前端代码存在什么问题&…

基于ATX自动化测试解决方案

在整车开发中&#xff0c;诊断功能实现后需要进行测试验证。测试验证主要分为两个方面&#xff1a;诊断协议层测试和诊断功能测试。诊断协议层测试&#xff1a;需要对服务层服务定义、传输层相关时间参数进行测试验证&#xff1b;诊断功能测试&#xff1a;需要对各诊断功能项&a…

国产操作系统之银河麒麟服务器版V10安装

一、银河麒麟操作系统简介 银河麒麟是目前国内国产化操作系统主流产品之一。银河麒麟高级服务器操作系统V10是针对企业级关键业务&#xff0c;适应虚拟化、云计算、大数据、工业互联网时代对主机系统可靠性、安全性、性能、扩展性和实时性等需求&#xff0c;依据CMMI5级标准研制…

Java中的引用

Java中的引用强引用软引用弱引用虚引用终结器引用&#xff08;FinalReference)JDK 1.2版本之后&#xff0c;Java对引用的概念进行了扩充&#xff0c;将引用分为强引用&#xff08;Strongly Reference&#xff09;、软引用&#xff08;Soft Reference&#xff09;、弱引用&#…

时间序列:时间序列模型---移动平均过程(Moving Average Process)

本文是Quantitative Methods and Analysis: Pairs Trading此书的读书笔记。 我们从白噪声生成另一种时间序列。如下式&#xff1a; 这种时间序列的值由此刻的白噪声实现&#xff08;white noise realization)加上beta倍的前一刻的白噪声实现。注意这个beta跟CAPM模型的beta没有…