K8s(二)Pod资源——node调度策略、node亲和性、污点与容忍度

news2024/9/24 9:18:27

目录

node调度策略nodeName和nodeSelector

指定nodeName

指定nodeSelector

node亲和性

node节点亲和性

硬亲和性

软亲和性

污点与容忍度


本文主要介绍了在pod中,与node相关的调度策略,亲和性,污点与容忍度等的内容

node调度策略nodeName和nodeSelector

在创建pod等资源时,可以通过调整字段进行node调度,指定资源调度到满足何种条件的node

指定nodeName

vim testpod1.yaml
apiVersion: v1
kind: Pod
metadata:
	name: testpod1
	namespace: default 
	labels:
		app: tomcat
spec:
	nodeName: ws-k8s-node1 #增加字段,将这个pod调度到node1
	containers: 
		- name: test
		  image: docker.io/library/tomcat
			imagePullPolicy: IfNotPresent
kubectl apply -f testpod1.yaml
kubectl get pods #可以看到已经调度到node1上了
testpod1    1/1     Running   0    116s   10.10.179.9    ws-k8s-node1   <none>           <none>

指定nodeSelector

vim testpod2.yaml
apiVersion: v1
kind: Pod
metadata:
	name: testpod2
	namespace: default 
	labels:
		app: tomcat
spec:
	nodeSelector: #添加nodeSelector选项,
		admin: ws  #调度到具有admin=ws标签的node上
	containers: 
		- name: test
		  image: docker.io/library/tomcat
			imagePullPolicy: IfNotPresent
kubectl apply -f testpod2.yaml
但因为我没有admin=ws标签的node,所以应用后pod处于pending状态

#现在我给node1的节点打个标签
#kubectl --help | grep -i label
#kubectl label --help
Examples:
  # Update pod 'foo' with the label 'unhealthy' and the value 'true'
  #kubectl label pods foo unhealthy=true
kubectl label nodes ws-k8s-node1 admin=ws
#node/ws-k8s-node1 labeled
#调度情况恢复正常
kubectl get pods | grep testpod2
testpod2                      1/1     Running   0              11m
#删除node标签
kubectl label nodes ws-k8s-node1 admin-
#删除testpod2
kubectl delete pods testpod2

如果同时使用nodeName和nodeSelector,则会报错亲和性错误,无法正常部署;
如果nodeName和nodeSelector指定的node同时满足这两项的条件,就可以部署

node亲和性

        亲和性在Kubernetes中起着重要作用,通过定义规则和条件,它允许我们实现精确的Pod调度、资源优化、高性能计算以及容错性和可用性的增强。通过利用亲和性,我们可以更好地管理和控制集群中的工作负载,并满足特定的业务需求。

#查看帮助
kubectl explain pods.spec.affinity
RESOURCE: affinity <Object>
DESCRIPTION:
     If specified, the pod's scheduling constraints
     Affinity is a group of affinity scheduling rules.
FIELDS:
   nodeAffinity <Object> #node亲和性
     Describes node affinity scheduling rules for the pod.

   podAffinity  <Object> #pod亲和性
     Describes pod affinity scheduling rules (e.g. co-locate this pod in the
     same node, zone, etc. as some other pod(s)).

   podAntiAffinity      <Object> #pod反亲和性
     Describes pod anti-affinity scheduling rules (e.g. avoid putting this pod
     in the same node, zone, etc. as some other pod(s)).

node节点亲和性

在创建pod时,会根据nodeaffinity来寻找最适合该pod的条件的node

#查找帮助
kubectl explain pods.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>
   requiredDuringSchedulingIgnoredDuringExecution       <Object>

#软亲和性,如果所有都不满足条件,也会找一个节点将就
preferredDuringSchedulingIgnoredDuringExecution 
#硬亲和性,必须满足,如果不满足则不找节点,宁缺毋滥
requiredDuringSchedulingIgnoredDuringExecution

硬亲和性

kubectl explain pods.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution
#nodeSelectorTerms    <[]Object> -required-
#     Required. A list of node selector terms. The terms are ORed.
kubectl explain pods.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms
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.
#匹配表达式
kubectl explain pods.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms.matchExpressions
key  <string> -required-
operator     <string> -required-
values       <[]string>
#可用operator
     - `"DoesNotExist"`
     - `"Exists"`
     - `"Gt"`
     - `"In"`
     - `"Lt"`
     - `"NotIn"`

#
vim ying-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: ying-pod
	labels:
		app: tomcat
		user: ws
spec:
	affinity:
		nodeAffinity:
			requiredDuringSchedulingIgnoredDuringExecution:
				nodeSelectorTerms:
				- matchExpressions:
					- key: name         #去找key=name
						opertor: In       # name = ws或=wws       
						values:
							- ws
							- wss			
	containers:
		- name: test1
			namespace: default
			image: docker.io/library/tomcat
		  imagePullPolicy: IfNotPresent

kubectl apply -f ying-pod.yaml
#需要name=ws或name=wws,但是没有节点有标签,而且是硬亲和
#所以pod会处于pending状态
kubectl get pods | grep ying
ying-pod                      0/1     Pending       0          15m
#修改node标签
kubectl label nodes ws-k8s-node1 name=ws
#开始构建,并且已经到node1节点了
kubectl get pod -owide | grep ying
ying-pod      0/1     ContainerCreating   0       80s     <none>    ws-k8s-node1   <none>           <none>
#删除标签
kubectl label nodes ws-k8s-node1 name-

软亲和性

vim ruan-pod.yaml
apiVersion: v1
kind: Pod
metadata:
	name: ruan-pod
	namespace: default
spec:
	containers:
		- name: test
			image: docker.io/library/alpine
			imagePullPolicy: IfNotPresent
	affinity:
		nodeAffinity:
			preferredDuringSchedulingIgnoredDuringExecution: #必选preference和weight
			 - preference:
					matchExpressions:
						- key: name
							operate: In #还有Exists,Gt,Lt,NotIn等
							values:
								- ws
					weight: 50 #软亲和性有“权重”说法,权重更高的更优先,范围1-100
			 - preference:
					matchExpressions:
						- key: name
							operate: In
							values:
								- wws
					weight: 70 #设置的比上面的高,用以做测试
kubectl apply -f ruan-pod.yaml

#不满足条件,所以随机找一个进行调度,能看到调度到了node2上
kubectl get pod -owide | grep ruan
ruan-pod                      0/1     ContainerCreating   0          3m24s   <none>         ws-k8s-node2   <none>           <none>

#修改node1的标签name=ws
kubectl label nodes ws-k8s-node1 name=ws
kubectl delete -f ruan-pod.yaml  #删除再重新创建
kubectl apply -f ruan-pod.yaml
kubectl get pods -owide | grep ruan #调整到了node1上
ruan-pod          0/1     ContainerCreating   0       2s      <none>         ws-k8s-node1   <none>           <none>

#修改node2的标签name=wws,此时node2权重比node1高
kubectl label nodes ws-k8s-node2 name=wss
kubectl delete -f ruan-pod.yaml 
kubectl apply -f ruan-pod.yaml
kubectl get pods -owide | grep ruan #没有变化,还在node1
ruan-pod       0/1     ContainerCreating   0       4m29s   <none>      ws-k8s-node1   <none>           <none>
#因为yaml的匹配顺序,已经匹配到了name=ws,如果没有另外标签不同的则不会变化

#修改ruan-pod.yaml
...
			- preference:
					matchExpressions:
          - key: name
            operator: In
            values:
            - ws
        weight: 50
      - preference:
          matchExpressions:
          - key: names
            operator: In
            values:
            - wws
				weight: 70
...
#添加node2标签name1=wws,权重比node1高,且标签key不同
kubectl label nodes ws-k8s-node2 names=wws
kubectl delete -f ruan-pod.yaml 
kubectl apply -f ruan-pod.yaml
kubectl get po -owide | grep ruan #可以看到ruan-pod已经回到了node2上
ruan-pod     0/1     ContainerCreating   0    3m47s   <none>      ws-k8s-node2   <none>           <none>

#清理环境
kubectl label nodes ws-k8s-node1 name-
kubectl label nodes ws-k8s-node2 names-
kubectl delete -f ruan-pod.yaml
kubectl delete -f ying-pod.yaml --fore --grace-period=0 #强制删除

污点与容忍度

        污点类似于标签,可以给node打taints,如果pod没有对node上的污点有容忍,那么就不会调度到该node上。
        在创建pod时可以通过tolerations来定义pod对于污点的容忍度

#查看node上的污点
#master节点是默认有污点
kubectl describe node ws-k8s-master1 | grep -i taint
Taints:             node-role.kubernetes.io/control-plane:NoSchedule
#node默认没有污点
kubectl describe node ws-k8s-node1 | grep -i taint
Taints:             <none>

#kubectl explain nodes.spec.taints查看帮助
kubectl explain nodes.spec.taints.effect
1.NoExecute
对已调度的pod不影响,仅对新需要调度的pod进行影响
2.NoSchedule
对已调度和新调度的pod都会有影响
3.PreferNoSchedule
软性的NoSchedule,就算不满足条件也可以调度到不容忍的node上

#查看当前master节点pod容忍情况
kubectl get pods -n kube-system -owide
kubectl describe pods kube-proxy-bg7ck -n kube-system | grep -i tolerations -A 10
Tolerations:                 op=Exists
                             node.kubernetes.io/disk-pressure:NoSchedule op=Exists
                             node.kubernetes.io/memory-pressure:NoSchedule op=Exists
                             node.kubernetes.io/network-unavailable:NoSchedule op=Exists
                             node.kubernetes.io/not-ready:NoExecute op=Exists
                             node.kubernetes.io/pid-pressure:NoSchedule op=Exists
                             node.kubernetes.io/unreachable:NoExecute op=Exists
                             node.kubernetes.io/unschedulable:NoSchedule op=Exists
Events:                      <none>

#给node1打一个污点,使其不接受
kubectl taint node ws-k8s-node1 user=ws:NoSchedule
#创建wudian.yaml进行测试
cat > wudian.yaml << EOF
apiVersion: v1
kind: Pod
metadata:
  name: wudain-pod
  namespace: default
  labels:
    app: app1
spec:
  containers:
  - name: wudian-pod
    image: docker.io/library/tomcat
    imagePullPolicy: IfNotPresent
EOF
kubectl apply -f wudian.yaml
#wudian-pod调度到了node2
kubectl get pods -owide
NAME         READY   STATUS    RESTARTS   AGE   IP             NODE           NOMINATED NODE   READINESS GATES
wudain-pod   1/1     Running   0          18s   10.10.234.72   ws-k8s-node2   <none>           <none>
#给node2添加污点
kubectl taint node ws-k8s-node2 user=xhy:NoExecute
#再查看发现wudain-pood已经被删除
kubectl get pods -owide
No resources found in default namespace.
#再次创建变成离线状态
kubectl apply -f wudian.yaml
kubectl get pods -owide
NAME         READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
wudain-pod   0/1     Pending   0          3s    <none>   <none>   <none>           <none>

#查看当前node污点状态
kubectl describe node ws-k8s-node1 | grep -i taint
Taints:             user=ws:NoSchedule
kubectl describe node ws-k8s-node2 | grep -i taint
Taints:             user=xhy:NoExecute

#创建带有容忍度的pod wudian2.yaml
cat > wudian2.yaml << EOF
apiVersion: v1
kind: Pod
metadata:
  name: wudain2-pod
  namespace: default
  labels:
    app: app1
spec:
  containers:
  - name: wudian2-pod
    image: docker.io/library/tomcat
    imagePullPolicy: IfNotPresent
  tolerations:  #容忍度
  - key: "user"
    operator: "Equal"    #equal表示等于,exists代表存在
    value: "ws"          #根据字段,表示能容忍user=ws的污点
#如果operator为exists且value为空则代表容忍所有key相同的
    effect: "NoSchedule" #需要准确匹配容忍等级,如果不匹配则不会生效
#   tolerationSeconds: 1800  effect为NoExecute时才能使用,表示容忍污染的时间,默认是0,即永远容忍
EOF
#现在wudian2是能容忍node1的污点的
kubectl apply -f wudian2.yaml
kubectl get pods -owide
NAME          READY   STATUS    RESTARTS   AGE   IP             NODE           NOMINATED NODE   READINESS GATES
wudain-pod    0/1     Pending   0          21m   <none>         <none>         <none>           <none>
wudain2-pod   1/1     Running   0          15s   10.10.179.13   ws-k8s-node1   <none>           <none>

#创建带有容忍度的pod wudian3.yaml
cat > wudian3.yaml << EOF
apiVersion: v1
kind: Pod
metadata:
  name: wudain3-pod
  namespace: default
  labels:
    app: app1
spec:
  containers:
  - name: wudian3-pod
    image: docker.io/library/tomcat
    imagePullPolicy: IfNotPresent
  tolerations:  #容忍度
  - key: "user"
    operator: "Exists"    #equal表示等于,exists代表存在
    value: ""          #根据字段,表示能容忍user=ws的污点
#如果operator为exist且value为空则代表容忍所有key相同的
    effect: "NoExecute" #需要准确匹配容忍等级,如果不匹配则不会生效
    tolerationSeconds: 1800  #effect为NoExecute时才能使用,表示容忍污染的时间,默认是0,即永远容忍
EOF
kubectl apply -f wudian3.yaml
#wudian3运行在node2上
kubectl get pods -owide | grep -i node2
wudain3-pod   1/1     Running   0          59s   10.10.234.73   ws-k8s-node2   <none>           <none>

#清理环境
kubectl delete -f wudian.yaml
kubectl delete -f wudian2.yaml
kubectl delete -f wudian3.yaml
kubectl taint node ws-k8s-node1 user-
kubectl taint node ws-k8s-node2 user-

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

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

相关文章

深度学习中指定特定的GPU使用

目录 前言1. 问题所示2. 解决方法 前言 老生常谈&#xff0c;同样的问题&#xff0c;主要来源于&#xff1a;RuntimeError: CUDA error: out of memory 当使用完之后&#xff0c;想从其他方式调试&#xff0c;具体可看我这篇文章的&#xff1a;出现 CUDA out of memory 的解决…

Ps:认识路径

在 Photoshop 中&#xff0c;路径 Path广泛地应用于创建精确的图像边界&#xff08;包括精准抠图&#xff09;以及复杂的图形设计之中。 路径又称为“矢量路径”&#xff0c;或者“贝塞尔曲线” Bezier Curves路径。 路径本身只是一种基于数学方程的“轮廓指示”&#xff0c;并…

Python数据分析案例31——中国A股的月份效应研究(方差分析,虚拟变量回归)

案例背景 本次案例是博主本科在行为金融学课程上做的一个小项目&#xff0c;最近看很多经管类的学生作业都很需要&#xff0c;我就用python来重新做了一遍。不弄那些复杂的机器学习模型了&#xff0c;经管类同学就用简单的统计学方法来做模型就好。 研究目的 有效市场假说是现…

论文复现|tightly focused circularly polarized ring Airy beam

请尊重原创的劳动成果 如需要转载&#xff0c;请后台联系 前言 采用MATLAB复现一篇论文里面的插图&#xff0c;涡旋光束的聚焦的仿真方式有很多种&#xff0c;这里采用MATLAB进行仿真&#xff0c;当然也有其他的很多方式&#xff0c;不同的方式各有千秋。 论文摘要 本文证明…

Sqoop安全性:确保安全的数据传输

确保数据传输的安全性在大数据处理中至关重要。Sqoop作为一个用于数据传输的工具&#xff0c;也提供了多种安全性措施&#xff0c;以确保数据在传输过程中的机密性和完整性。本文将深入探讨Sqoop的安全性特性&#xff0c;提供详细的示例代码和全面的内容&#xff0c;以帮助大家…

压缩编码之不同缩放参数对重建图像质量的影响的python实现——JPEG变换编码不同压缩率的模拟

原理 JPEG&#xff08;Joint Photographic Experts Group&#xff09;是一种常用的图像压缩标准&#xff0c;它通过采用离散余弦变换&#xff08;DCT&#xff09;和量化来实现图像的压缩。 离散余弦变换&#xff08;DCT&#xff09;&#xff1a; JPEG首先将图像分割成8x8的块…

彝族民居一大特色——土掌房

彝族民居一大特色——土掌房在彝区&#xff0c;各地、各支系传承的居室建筑形式是多种多样的&#xff0c;并与当地的居住习俗有密切关联&#xff0c;从村寨的聚落到住宅的地址&#xff1b;从房间的分置到什物的堆放&#xff1b;从建筑结构到民居信仰和禁忌&#xff0c;都表现出…

U-Boot学习(3):.config、defconfig文件对比及图形化配置Kconfig

在上一节中&#xff0c;我们介绍了U-Boot编译和.config配置文件生成分析&#xff0c;我们可以通过make xxx__defconfig来进行一些配置&#xff0c;其中xxx__defconfig对应config目录下的基于不同开发板的一些配置&#xff0c;指令执行完后会根据对应的配置在根目录下生成一个.c…

基于java web的机票管理系统设计与实现设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用JSP技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

Redis主从架构、哨兵集群原理实战

1.主从架构简介 背景 单机部署简单&#xff0c;但是可靠性低&#xff0c;且不能很好利用CPU多核处理能力生产环境必须要保证高可用&#xff0c;一般不可能单机部署读写分离是可用性要求不高、性能要求较高、数据规模小的情况 目标 读写分离&#xff0c;扩展主节点的读能力&…

多选下拉框数据溢出父页面高度被截断

问题 解决 给父页面div一个最小高度minHeight。 <div style{{minHeight: 300,marginTop: 20}}><Row><Select...</Row> </div>&#xff08;在我的具体实例中&#xff0c;下拉框与表格页面都为Tabs ——> TabPane 所包含。故下拉框高度超出页面高…

【Spring实战】29 @Value 注解

文章目录 1. 定义2. 好处3. 示例1&#xff09;注入基本类型2&#xff09;注入集合类型3&#xff09;使用默认值4&#xff09;注入整数和其他类型 总结 在实际的应用中&#xff0c;我们经常需要从外部配置文件或其他配置源中获取参数值。Spring 框架提供了 Value 注解&#xff0…

基于SSM的网上购物商城设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用JSP技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

任务13:使用MapReduce对天气数据进行ETL(获取各基站ID)

任务描述 知识点&#xff1a; 天气数据进行ETL 重 点&#xff1a; 掌握MapReduce程序的运行流程熟练编写MapReduce程序使用MapReduce进行ETL 内 容&#xff1a; 编写MapReduce程序编写Shell脚本&#xff0c;获取MapReduce程序的inputPath将生成的inputPath文件传入到Wi…

【Debian】非图形界面Debian10.0.0安装xfce和lxde桌面

一、安装 1. Debian10.0.0安装xfce桌面 sudo apt update sudo apt install xfce4 startxfce4 2. Debian10.0.0安装lxde桌面 sudo apt-get install lxde安装后重启电脑。 二、说明 XFCE、LXDE 和 GNOME 是三个流行的桌面环境&#xff0c;它们都是为类 Unix 操作系统设计…

【数位dp】【C++算法】600. 不含连续1的非负整数

作者推荐 【矩阵快速幂】封装类及测试用例及样例 涉及知识点 数位dp LeetCode600. 不含连续1的非负整数 给定一个正整数 n &#xff0c;请你统计在 [0, n] 范围的非负整数中&#xff0c;有多少个整数的二进制表示中不存在 连续的 1 。 示例 1: 输入: n 5 输出: 5 解释: 下…

浏览器打印无法显示单选框选中效果

上面是原代码&#xff0c;我点击打印&#xff0c;出现打印页面&#xff0c;但单选框并未勾选中&#xff0c;我在外部放了一模一样的代码是能勾选上的&#xff0c;于是我对打印页的input单选框进行分析&#xff0c;发现他丢失了checked属性。然后通过gpt分析原因。得知了default…

RK3399平台入门到精通系列讲解(外设篇)热成像传感器MLX90640 JNI控制程序

文章目录 JNI回调函数回调函数的实现驱动可以详看:链接 JNI 文件:native-lib.cpp

领域特定语言(Domain-Specific Language, DSL)在 Visual Studio 2022中的实验——建立领域模型

一、环境 dotnet --version 8.0.101 Microsoft Visual Studio Enterprise 2022 (64 位) - Current 版本 17.8.4 已安装组件 ComponentLinkVisual Studiohttp://go.microsoft.com/fwlink/?LinkId185579Visual Studio SDKhttps://go.microsoft.com/fwlink/?li…

RabbitMQ的安装使用

RabbitMQ是什么&#xff1f; MQ全称为Message Queue&#xff0c;消息队列&#xff0c;在程序之间发送消息来通信&#xff0c;而不是通过彼此调用通信。 RabbitMQ 主要是为了实现系统之间的双向解耦而实现的。当生产者大量产生数据时&#xff0c;消费者无法快速消费&#xff0c;…