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: testpod1namespace: default labels:app: tomcat
spec:nodeName: ws-k8s-node1 #增加字段,将这个pod调度到node1containers: - name: testimage: docker.io/library/tomcatimagePullPolicy: 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: testpod2namespace: default labels:app: tomcat
spec:nodeSelector: #添加nodeSelector选项,admin: ws  #调度到具有admin=ws标签的node上containers: - name: testimage: docker.io/library/tomcatimagePullPolicy: 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 constraintsAffinity 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 thesame node, zone, etc. as some other pod(s)).podAntiAffinity      <Object> #pod反亲和性Describes pod anti-affinity scheduling rules (e.g. avoid putting this podin 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-podlabels:app: tomcatuser: ws
spec:affinity:nodeAffinity:requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:- matchExpressions:- key: name         #去找key=nameopertor: In       # name = ws或=wws       values:- ws- wss			containers:- name: test1namespace: defaultimage: docker.io/library/tomcatimagePullPolicy: IfNotPresentkubectl 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-podnamespace: default
spec:containers:- name: testimage: docker.io/library/alpineimagePullPolicy: IfNotPresentaffinity:nodeAffinity:preferredDuringSchedulingIgnoredDuringExecution: #必选preference和weight- preference:matchExpressions:- key: nameoperate: In #还有Exists,Gt,Lt,NotIn等values:- wsweight: 50 #软亲和性有“权重”说法,权重更高的更优先,范围1-100- preference:matchExpressions:- key: nameoperate: Invalues:- wwsweight: 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: nameoperator: Invalues:- wsweight: 50- preference:matchExpressions:- key: namesoperator: Invalues:- wwsweight: 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=Existsnode.kubernetes.io/disk-pressure:NoSchedule op=Existsnode.kubernetes.io/memory-pressure:NoSchedule op=Existsnode.kubernetes.io/network-unavailable:NoSchedule op=Existsnode.kubernetes.io/not-ready:NoExecute op=Existsnode.kubernetes.io/pid-pressure:NoSchedule op=Existsnode.kubernetes.io/unreachable:NoExecute op=Existsnode.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-podnamespace: defaultlabels:app: app1
spec:containers:- name: wudian-podimage: docker.io/library/tomcatimagePullPolicy: 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-podnamespace: defaultlabels:app: app1
spec:containers:- name: wudian2-podimage: docker.io/library/tomcatimagePullPolicy: IfNotPresenttolerations:  #容忍度- 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-podnamespace: defaultlabels:app: app1
spec:containers:- name: wudian3-podimage: docker.io/library/tomcatimagePullPolicy: IfNotPresenttolerations:  #容忍度- 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.hqwc.cn/news/411391.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android车载系统Car模块架构链路分析

一、模块主要成员 CarServiceHelperService SystemServer 中专门为 AAOS 设立的系统服务&#xff0c;用来管理车机的核心服务 CarService。该系统服务的具体实现在 CarServiceHelperServiceUpdatableImpl CarService Car模块核心服务APP&#xff0c;Android 13版本开始分为…

Kubernetes网络模型概述

Kubernetes网络模型设计的一个基础原则是&#xff1a;每个Pod都拥有一个独立的IP地址&#xff0c;并假定所有Pod都在一个可以直接连通的、扁平的网络空间中。所以不管这些Pod是否运行在同一个Node中&#xff0c;都要求它们可以直接通过对方的IP进行访问。由于Kubernetes的网络模…

【每周AI简讯】GPT-5将有指数级提升,GPT Store正式上线

AI7 - Chat中文版最强人工智能 OpenAI的CEO奥特曼表示GPT-5将有指数级提升 GPT奥特曼参加Y-Combinator W24启动会上表示&#xff0c;我们已经非常接近AGI。GPT-5将具有更好的推理能力、更高的准确性和视频支持。 GPT Store正式上线 OpenAI正式推出GPT store&#xff0c;目前…

HUAWEI华为MateStation S台式机电脑12代PUC-H7621N,H5621N原装出厂Windows11.22H2系统

链接&#xff1a;https://pan.baidu.com/s/1QtjLyGTwMZgYiBO5bUVPYg?pwd8mx0 提取码&#xff1a;8mx0 原厂WIN11系统自带所有驱动、出厂主题壁纸、系统属性专属联机支持标志、Office办公软件、华为电脑管家等预装程序 文件格式&#xff1a;esd/wim/swm 安装方式&#xf…

ES自动补全

安装IK分词器 要实现根据字母做补全&#xff0c;就必须对文档按照拼音分词。在GitHub上恰好有elasticsearch的拼音分词插件。地址&#xff1a;GitHub - medcl/elasticsearch-analysis-pinyin: This Pinyin Analysis plugin is used to do conversion between Chinese characte…

如何在CentOS 7 中基于OpenSSL 3.0 搭建Python 3.0 环境

1、OpenSSL 1.1 原因 [rootlocalhost ~]# openssl version OpenSSL 1.0.2k-fips 26 Jan 2017 [rootlocalhost ~]#通过执行openssl version可知Linux系统已经安装了OpenSSL&#xff0c;但该版本较低&#xff1b;Python 3 要求 OpenSSL版本不能低于1.1.1&#xff0c;否则安装P…

VMware虚拟机自定义网段及物理机ping不通虚拟机问题解决

Vmware网络介绍&#x1f6dc; VMware虚拟机提供了几种网络模式&#xff0c;其中包括桥接模式&#xff08;Bridged Mode&#xff09;、NAT模式&#xff08;Network Address Translation Mode&#xff09;和仅主机模式&#xff08;Host-Only Mode&#xff09;。这些模式允许虚拟…

陶瓷碗口缺口检测-图像分割

图像分割 由于对碗口进行缺口检测&#xff0c;因此只需要碗口的边界信息。得到陶瓷碗区域填充后的图像&#xff0c;对图像进行边缘检测。这是属于图像分割中的内容&#xff0c;在图像的边缘中&#xff0c;可以利用导数算子对数字图像求差分&#xff0c;将边缘提取出来。 本案…

Java关键字static和final

一、final关键字是什么&#xff1f; 1、final可以用来修饰的结构&#xff1a;类、方法、变量 2、final用来修饰一个类&#xff1a;此类不能被其它类继承。当我们需要让一个类永远不被继承&#xff0c;此时就可以用final修饰&#xff0c;但要注意&#xff1a;final类中所有的成…

惠州材料生产企业MES系统一期项目圆满成功,二期项目值得期待

前言 惠州某材料生产企业各车间已经陆续用上MES生产系统&#xff0c;MES系统一期项目从启动到上线使用&#xff0c;经过需求调研与方案评审、内部开发测试、试运行、用户培训等工作&#xff0c;仅用了不到3个月时间&#xff0c;二期项目也在紧锣密鼓地进行中。 项目概况 该公…

Qt/QML编程之路:使用camera摄像头(35)

汽车应用中,camera起到了越来越多的作用,数字化的作用,这点无可争议,而作为GUI设计工具,如何让Camera类的应用能更好的发挥作用呢? You can use Camera to capture images and movies from a camera, and manipulate the capture and processing settings that get appl…

【c++】栈(satck)和队列(queue)

目录 一、stack 1.stack的介绍 2.stack的使用 3.stack的模拟实现 二、queue 1.queue的介绍 2.queue的使用 3.queue的模拟实现 三、priority_queue 1.priority_queue的介绍 2.priority_queue的使用 一、stack 1.stack的介绍 &#xff08;1&#xff09;stack是一种容…