k8s的污点、容忍度

给了节点选则的主动权,我们给节点打一个污点,不容忍的 pod 就运行不上来,污点就是定义在节点上的键值属性数据,可以决定拒绝哪些 pod;

taints 是键值数据,用在节点上,定义污点;
tolerations 是键值数据,用在 pod 上,定义容忍度,能容忍哪些污点
pod 亲和性是 pod 属性;但是污点是节点的属性,污点定义nodeSelector 上

[root@master01 qinhe]# kubectl describe nodes  master01
Taints:             node-role.kubernetes.io/master:NoSchedule
[root@master01 qinhe]#  kubectl explain node.spec.taints
KIND:     Node
VERSION:  v1

RESOURCE: taints <[]Object>

DESCRIPTION:
     If specified, the node's taints.

     The node this Taint is attached to has the "effect" on any pod that does
     not tolerate the Taint.

FIELDS:
   effect       <string> -required-
     Required. The effect of the taint on pods that do not tolerate the taint.
     Valid effects are NoSchedule, PreferNoSchedule and NoExecute.

   key  <string> -required-
     Required. The taint key to be applied to a node.

   timeAdded    <string>
     TimeAdded represents the time at which the taint was added. It is only
     written for NoExecute taints.

   value        <string>
     The taint value corresponding to the taint key.

effect 用来定义对 pod 对象的排斥等级(效果):

  1. NoSchedule:
    仅影响 pod 调度过程,当 pod 能容忍这个节点污点,就可以调度到当前节点,对现存的 pod 对象不产生影响 。
  2. NoExecute:
    既影响调度过程,又影响现存的 pod 对象,如果现存的 pod 不能容忍节点后来加的污点,这个 pod就会被驱逐
  3. PreferNoSchedule:
    最好不调度,也可以调度,是 NoSchedule 的柔性版本

在 pod 对象定义容忍度的时候支持两种操作:
1.等值密钥:key 和 value 上完全匹配
2.存在性判断:key 和 effect 必须同时匹配,value 可以是空
在 pod 上定义的容忍度可能不止一个,在节点上定义的污点可能多个,需要琢个检查容忍度和污点能否匹配,
每一个污点都能被容忍,才能完成调度,如果不能容忍怎么办,那就需要看 pod 的容忍度了

[root@master01 qinhe]# kubectl describe nodes  master01
Taints:             node-role.kubernetes.io/master:NoSchedule
上面可以看到 master 这个节点的污点是 Noschedule 
所以我们创建的 pod 都不会调度到 master01 上,因为我们创建的 pod 没有容忍度

[root@master01 qinhe]#  kubectl describe pods kube-apiserver-master01 -n kube-system
Tolerations:       :NoExecute op=Exists
可以看到api-server的 pod 的容忍度是 NoExecute,则可以调度到master01 上 

实验

比如把 node02 当成是生产环境专用的,其他 node 是测试的
给 node02打污点,pod 如果不能容忍就不会调度过来

[root@master01 qinhe]#  kubectl taint node node02 node-type=production:NoSchedule
[root@master01 qinhe]# cat pod-taint.yaml
apiVersion: v1
kind: Pod
metadata:
  name: taint-pod
  namespace: default
  labels:
    tomcat: tomcat-pod
spec:
  containers:
  - name: taint-pod
    ports:
    - containerPort: 8080
    image: tomcat:8.5-jre8-alpine
    imagePullPolicy: IfNotPresent
[root@master01 qinhe]# kubectl apply -f pod-taint.yaml
pod/taint-pod created
[root@master01 qinhe]# kubectl get pods -o wide
taint-pod    1/1     Running   0          4s    10.244.196.140   node01   <none>           <none>
可以看到都被调度到 node01 上了,因为 node02 这个节点打了污点,
而我们在创建 pod 的时候没有容忍度,所以 node02 上不会有 pod 调度上去的 

给 xianchaonode1 也打上污点 :

[root@master01 qinhe]# kubectl taint node  node01 node-type=dev:NoExecute
node/node01 tainted
[root@master01 qinhe]# kubectl get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP              NODE     NOMINATED NODE   READINESS GATES
pod-first    1/1     Running   0          48m   10.244.140.79   node02   <none>           <none>
上面可以看到node01已经存在的 pod 节点都被撵走了 
[root@master01 qinhe]# cat pod-demo-1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-deploy
  namespace: default
  labels:
    app: myapp
    release: canary
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
  tolerations:   # Pod 可以容忍的污点列表,允许 Pod 被调度到具有相应污点的节点上。
  - key: "node-type"    #污点的键名。这里为 node-type
    operator: "Equal"   #用于匹配值的操作符。Equal 表示等同于
    value: "production"   #与键关联的值。这里为 production
    effect: "NoExecute"   #污点的效果,通常有 NoSchedule, PreferNoSchedule, 和 NoExecute。NoExecute 表示如果 Pod 不能容忍此污点,它将被驱逐。
    tolerationSeconds: 3600   #Pod 允许在节点上运行的时间(秒数),即使节点上有 NoExecute 效果的污点。这里为 3600 秒,即一个小时。

[root@master01 qinhe]#  kubectl apply -f pod-demo-1.yaml
pod/myapp-deploy created
You have new mail in /var/spool/mail/root
[root@master01 qinhe]# kubectl get pods
NAME           READY   STATUS    RESTARTS   AGE
myapp-deploy   0/1     Pending   0          8s

还是显示 pending,因为我们使用的是 equal(等值匹配),所以 key 和 value,effect 必须和
node 节点定义的污点完全匹配才可以,把上面配置 effect: "NoExecute"变成effect: "NoSchedule"成;  
tolerationSeconds: 3600 这行去掉 

[root@master01 qinhe]#  kubectl delete -f pod-demo-1.yaml
[root@master01 qinhe]# kubectl apply -f pod-demo-1.yaml
pod/myapp-deploy created
[root@master01 qinhe]# kubectl get pods -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP              NODE     NOMINATED NODE   READINESS GATES
myapp-deploy   1/1     Running   0          29s   10.244.140.80   node02   <none>           <none>
上面就可以调度到 node02 上了,因为在 pod 中定义的容忍度能容忍 node 节点上的污点 

再次修改修改如下部分: 实现存在性判断

  tolerations:
  - key: "node-type"
    operator: "Exists"
    value: ""
    effect: "NoSchedule"
只要对应的键是存在的,exists,其值被自动定义成通配符
[root@master01 qinhe]# kubectl delete -f pod-demo-1.yaml
pod "myapp-deploy" deleted
[root@master01 qinhe]# kubectl apply -f pod-demo-1.yaml
pod/myapp-deploy created
[root@master01 qinhe]# kubectl get pod  -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP              NODE     NOMINATED NODE   READINESS GATES
myapp-deploy   1/1     Running   0          8s    10.244.140.81   node02   <none>           <none>
发现还是调度到 xianchaonode2 上 


再次修改: 
  tolerations:
  - key: "node-type"
    operator: "Exists"
    value: ""
    effect: ""
有一个 node-type 的键,不管值是什么,不管是什么效果,都能容忍
[root@master01 qinhe]# kubectl delete -f pod-demo-1.yaml
pod "myapp-deploy" deleted
[root@master01 qinhe]# kubectl apply -f pod-demo-1.yaml
pod/myapp-deploy created
[root@master01 qinhe]# kubectl get pod -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
myapp-deploy   1/1     Running   0          5s    10.244.196.141   node01   <none>           <none>
可以看到node02 和 node01 节点上都有可能有 pod 被调度 

试验结束删除污点:

[root@master01 qinhe]# kubectl taint nodes node01 node-type:NoExecute-
node/node01 untainted
[root@master01 qinhe]# kubectl taint nodes  node02 node-type-
node/node02 untainted
污点容忍度Kubernetes中用来控制Pod调度的机制。污点(Taint)是指在节点上设置的一种属性,用于排斥一类特定的Pod。而容忍度(Toleration)是指在Pod上设置的属性,用于指定Pod是否可以被调度到具有匹配污点的节点上。 当一个节点上设置了污点时,只有那些在Pod的容忍度中定义了匹配该污点的规则的Pod才能被调度到该节点上。如果Pod没有定义容忍度或者容忍度不匹配节点上的污点,则该Pod将不会被调度到该节点上。 举个例子,假设我们在节点node1上设置了一个污点,key为k1,value为v1,effect为NoSchedule。如果一个Pod的容忍度中定义了匹配这个污点的规则,比如key为test,value为16,effect为NoSchedule,那么这个Pod就可以被调度到拥有这个污点的node1节点上。 总结来说,污点容忍度是相互匹配的关系。通过在节点上设置污点和在Pod上设置容忍度,我们可以控制Pod的调度行为,避免将Pod调度到不合适的节点上。\[2\]\[3\] #### 引用[.reference_title] - *1* *2* *3* [k8s--基础--20--污点容忍度](https://blog.csdn.net/zhou920786312/article/details/126241579)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

峰峰--

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值