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

目录

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.mfbz.cn/a/329292.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【AI】RTX2060 6G Ubuntu 22.04.1 LTS (Jammy Jellyfish) 部署Chinese-LLaMA-Alpaca-2

下载源码 cd ~/Downloads/ai git clone --depth1 https://gitee.com/ymcui/Chinese-LLaMA-Alpaca-2 创建venv python3 -m venv venv source venv/bin/activate安装依赖 pip install -r requirements.txt 已安装依赖列表 (venv) yeqiangyeqiang-MS-7B23:~/Downloads/ai/Chi…

Lazada不懂英文能做吗?Lazada国内店铺好做吗?-站斧浏览器

Lazada不懂英文可以做吗&#xff1f; Lazada作为一个国际化的电商平台&#xff0c;为了方便用户来自不同国家和地区的购物需求&#xff0c;提供了多语言支持。对于不懂英文的用户来说&#xff0c;他们同样可以在Lazada上进行购物。 首先&#xff0c;Lazada平台上的界面和商品…

【Linux】文件系统与软硬连接

欢迎来到Cefler的博客&#x1f601; &#x1f54c;博客主页&#xff1a;折纸花满衣 &#x1f3e0;个人专栏&#xff1a;题目解析 &#x1f30e;推荐文章&#xff1a;【LeetCode】winter vacation training 目录 &#x1f449;&#x1f3fb; 磁盘HDD的物理存储结构磁盘的逻辑抽象…

252:vue+openlayers 绘制锥形渐变填充色的圆形

第252个 点击查看专栏目录 本示例的目的是介绍如何在vue+openlayer中绘制带有锥形渐变填充色的圆形。 直接复制下面的 vue+openlayers源代码,操作2分钟即可运行实现效果 文章目录 示例效果配置方式示例源代码(共131行)相关API参考专栏目标示例效果 </

基于Pixhawk和ROS搭建自主无人车(三):ROS通信篇

参考 ArduPilot Development超维空间科技 基于Pixhawk和ROS搭建自主无人车&#xff08;文章链接汇总&#xff09; 1. 硬件接线一览 2. 安装 Mavros 2.1 简介 Mavros 是一个用于与无人机通信的 ROS 功能包&#xff0c;它借助 MAVLink 协议来与 PX4 Autopilot 进行通信&#x…

Vue加载序列帧动图

解读方法 使用<img :src"currentFrame" alt"加载中" /> 加载图片动态更改src的值使用 requestAnimationFrame 定时更新在需要的页面调用封装的组件 <LoadToast v-if"showLoading" /> 封装组件 <template><div class"…

C++力扣题目47--全排列II

47.全排列 II 力扣题目链接(opens new window) 给定一个可包含重复数字的序列 nums &#xff0c;按任意顺序 返回所有不重复的全排列。 示例 1&#xff1a; 输入&#xff1a;nums [1,1,2]输出&#xff1a; [[1,1,2], [1,2,1], [2,1,1]] 示例 2&#xff1a; 输入&#xf…

视觉检测系统:工厂生产零部件的智能检测

在工厂的生产加工过程中&#xff0c;工业视觉检测系统被广泛应用&#xff0c;并且起着重要的作用。它能够对不同的零部件进行多功能的视觉检测&#xff0c;包括尺寸和外观的缺陷。随着制造业市场竞争越来越激烈&#xff0c;对产品质检效率的要求不断提高&#xff0c;传统的人工…

部署YUM仓库及NFS共享存储

引言&#xff1a; 学习YUM 软件仓库&#xff0c;可以完成安装、卸载、自动升级 rpm 软件包等任务&#xff0c;能够自动 查找并解决 rpm 包之间的依赖关系&#xff0c;而无须管理员逐个、手工地去安装每个 rpm 包&#xff0c;使管理员在维护大量 Linux 服务器时更加轻松自如。特…

洗地机如何选择?一篇教会你挑到好用的洗地机

要说当下哪款清洁设备最好用&#xff0c;当数洗地机!洗地机单个操作中能够同时完成扫地和拖地&#xff0c;不仅清洁效果高&#xff0c;还节省力气&#xff0c;甚至处理墙角垃圾灰尘也无需我们蹲下来摩擦地板。好的配置加上性能真的是能帮助我们更快、更有效清洁地面&#xff0c…

【嘉立创EDA-PCB设计指南】2.详解BOM表+C0603封装绘制流程+元件封装其它注意点总结+原理图转到PCB流程

前言&#xff1a;本文详解BOM表C0603封装绘制流程元件封装其它注意点总结原理图转到PCB流程。最终会实现如下图所示的PCB初态。对于封装绘制的流程是一样的&#xff0c;所以只在第2章节对C0603进行详细的封装流程描述&#xff0c;对该PCB的其它元件在第3章节-元件封装的其它注意…

线上研讨会 | 智能供应链计划与高级排程APS助力转型变革

行业背景 近年来&#xff0c;市场环境的快速变化对我国产业链和供应链的要求不断提升&#xff0c; “十四五”规划纲要提到要提升产业链供应链现代化水平。数字化供应链是促进产业链供应链稳定&#xff0c;畅通国民经济循环的重要一环。亟须探寻数字化供应链的核心要义和发展路…

栈(顺序存储、链式存储)

栈的定义 栈&#xff08;Stack&#xff09;是只允许在一端进行插入或删除操作的线性表 栈的操作特性是后进先出LIFO&#xff08;Last In First Out&#xff09; 顺序存储 链式存储

核对表:基本数据类型CHECKLIST:Fundmental Data

核对表&#xff1a;基本数据类型CHECKLIST:Fundmental Data 数值概论 代码中避免使用神秘数值吗&#xff1f; 代码考虑了除零错误吗&#xff1f; 类型转换很明显吗&#xff1f; 如果在一条语句中存在两个不同类型的变量&#xff0c;那么这条语句会像你期望的那样求值吗&#x…

3、深入解析Redis Cluster集群运维与核心原理

在今天的大规模分布式系统中&#xff0c;Redis Cluster已经成为了许多企业选择的分布式缓存方案之一。了解Redis Cluster的运维及核心原理对于确保系统的高可用性和性能至关重要。本文将深入探讨Redis Cluster集群的运维细节和核心原理&#xff0c;以帮助读者更好地理解和优化R…

MT1155-1163总结

1. 首先&#xff0c;单位矩阵是主对角线全为1&#xff0c;其余位置为0的矩阵 主对角线如图&#xff08;虽然好丑&#xff09; 方法一&#xff1a;用一维数组表示&#xff0c;在a[0],a[4],a[8]位置上为一&#xff0c;其余为0 方法二&#xff1a;定义二维数组&#xff0c;在a[…

阿里云服务器怎么样?阿里云服务器优势、价格及常见问题

阿里云服务器ECS英文全程Elastic Compute Service&#xff0c;云服务器ECS是一种安全可靠、弹性可伸缩的云计算服务&#xff0c;阿里云提供多种云服务器ECS实例规格&#xff0c;如ECS经济型e实例、通用算力型u1、ECS计算型c7、通用型g7、GPU实例等&#xff0c;阿里云服务器网al…

uniapp 简易自定义日历

注&#xff1a;此日历是根据接口返回的日期自动对应星期的&#xff0c;返回的数据中也包含星期&#xff0c;其实就是一个div自定义&#xff0c;可根据自己需求更改&#xff1b; 1、组件代码 gy-calendar-self.vue <template><view class"calendar"><…

[Linux 进程(四)] 再谈环境变量,程序地址空间初识

文章目录 1、前言2、环境变量2.1 main函数第三个参数 -- 环境参数表2.2 本地环境变量和env中的环境变量2.3 配置文件与环境变量的全局性2.4 内建命令与常规命令2.5 环境变量相关的命令 3、程序地址空间 1、前言 上一篇我们讲了环境变量&#xff0c;如果有不明白的先读一下上一…

黑马程序员——html css基础——day01——HTML基础

目录&#xff1a; 今日课程介绍 核心技术点标签语法HTML骨架标签的关系注释标题标签段落标签换行和水平线文本格式化标签图像标签 图像属性属性语法路径 相对路径绝对路径超链接标签音频视频综合案例一个人简介综合案例二Vue简介 1.今日课程介绍 今日目标&#xff1a;掌握标…