污点(Taint)和容忍(Toleration)

节点亲和性,是Pod的一种属性(偏好或硬性要求),它使Pod被吸引到一类特定的节点,Taint则相反,它使节点能够 排斥 一类特定的Pod
Taint与Toleration相互配合,可以用来避免Pod被分配到不合适的节点上,每个节点上都可以应用一个或两个taint,这表示对那些不能容忍这些污点(taint)的pod,是不会被该节点接受的,如果将容忍(toleration)应用于pod上,则表示这些pod可以(但不要求)被调度到具有匹配taint的节点上

注意,以下所有的测试都是1Master、1Node的情况下:
[root@Centos8 scheduler]
# kubectl get node
NAME          STATUS   ROLES    AGE    VERSION

centos8       Ready    master   134d   v1.15.1

testcentos7   Ready    <none>   133d   v1.15.1

污点(Taint)

1、污点的组成

使用kubectl taint 命令可以给某个node节点设置污点,Node被设置上污点之后就和Pod之间存在了一种相斥的关系,可以让Node拒绝Pod的调度执行,甚至将已经存在得Pod驱逐出去
每个污点的组成如下:
key=value:effect
每个污点有一个 key 和 value 作为污点标签,其中 value 可以为空,effect描述污点的作用,当前 effect 支持如下三个选项:
  • NoSchedule:表示 k8s 不会将Pod调度到具有该污点的Node上
  • PreferNoSchedule:表示 k8s 将尽量避免将Pod调度到具有该污点的Node上
  • NoExecute:表示 k8s 将不会将Pod调度到具有该污点的Node上,同时会将Node上已有的Pod驱逐出去

2、污点的设置、查看和去除

k8s的master节点本身就带有effect类型为NoSchedule的污点,这也是为什么k8s在调度Pod时,不会调度到master节点的原因,具体查看如下:
[root@Centos8 scheduler]
# kubectl describe node centos8
Taints:             node-role.kubernetes.io/master:NoSchedule

过程介绍
## 设置污点
kubectl taint nodes [node name] key1=value:NoSchedule


## 节点说明中,查看Taint字段
kubectl describe node [node name]


## 去除污点
kubectl taint nodes [node name] key1:NoSchedule-

演示

## 查看当前节点所拥有Pod,都在testcentos7中
[root@Centos8 scheduler]
# kubectl get pod -o wide
NAME                READY   STATUS    RESTARTS   AGE   IP             NODE     

affinity-required   1/1     Running   0          68m   10.244.3.224 testcentos7

pod-1               1/1     Running   0          78m   10.244.3.223 testcentos7

required-pod2       0/1     Pending   0          59m   <none>         <none>   


## 给testcentos7设置NoExecute污点,使已经落在本节点的Pod被驱赶出去
[root@Centos8 scheduler]
# kubectl taint nodes testcentos7 check=vfan:NoExecute
node/testcentos7 tainted


## 查看Pod有没被驱逐出去
[root@Centos8 scheduler]
# kubectl get pod 
NAME            READY   STATUS    RESTARTS   AGE

required-pod2   0/1     Pending   0          62m


## 只剩一个Pending状态的Pod,因为他还没创建,所以还未分配Node
查看下testcentos7的污点信息:
[root@Centos8 scheduler]
# kubectl describe node  testcentos7
Taints:             check=vfan:NoExecute

目前所有的节点都被打上了污点,新建Pod测试下效果:
[root@Centos8 scheduler]
# kubectl create -f pod.yaml 
pod/pod-1 created


[root@Centos8 scheduler]
# kubectl get pod 
NAME            READY   STATUS    RESTARTS   AGE

pod-1           0/1     Pending   0          4s

required-pod2   0/1     Pending   0          7h18m

新建的Pod会一直处于Pending状态,因为没有可用的Node节点,这时候就可以使用容忍(Toleration)了

容忍(Toleration)

设置了污点的Node将根据 taint 的 effect:NoSchedule、PreferNoSchedule、NoExecute和Pod之间产生互斥的关系,Pod将在一定程度上不会被调度到 Node 上。但我们可以在 Pod 上设置容忍(Toleration),意思是设置了容忍的 Pod 将可以在有相对应污点的 Node 中存在。
容忍(Toleration)的策略规则如下:
Pod.spec.tolerations
tolerations:

- key: 
"key1"
  operator: 
"Equal"
  value: 
"value1"
  effect: 
"NoSchedule"
  tolerationSeconds: 3600

- key: 
"key1"
  operator: 
"Equal"
  value: 
"value1"
  effect: 
"NoExecute"
- key: 
"key2"
  operator: 
"Exists"
  effect: 
"NoSchedule"
  • 其中 key、value、effect 要与Node中的 taint 保持一致
  • operator 的值为 Exists 将会忽略 value 的值,值为 Equal 时则必须匹配相同的value
  • tolerationSeconds 用于描述当Pod需要驱逐时可以在Node上继续保留运行的时间
1、当不指定key时,表示容忍所有污点的key:
tolerations:

- operator: 
"Exists"
例如:
vim pod3.yaml
apiVersion:v1
kind:Pod
metadata:
name:pod-3
labels:
app:nginx
type:web
spec:
containers:
-name:pod-3
image:nginx:1.2.1
imagePullPolicy:IfNotPresent
ports:
-name:web
containerPort:80
tolerations:
-operator:"Exists"
effect:"NoSchedule"
yaml策略为:可以容忍所有策略为NoSchedule的污点
[root@Centos8 scheduler]
# kubectl create -f pod3.yaml 
pod/pod-3 created


[root@Centos8 scheduler]
# kubectl get pod -o wide 
NAME    READY   STATUS    RESTARTS   AGE   IP             NODE          NOMINATED NODE  

pod-3   1/1     Running   0          9s    10.244.0.107   centos8       

因为centos8为master节点,污点默认为NoSchedule,所以Pod-3被调度到master节点
2、当不指定 offect 值时,表示容忍所有的污点类型
tolerations:

- key: 
"key1"
  value: 
"value1"
  operator: 
"Exists"
Pod容忍测试用例:
vim pod2.yaml
apiVersion:v1
kind:Pod
metadata:
name:pod-2
labels:
app:nginx
type:web
spec:
containers:
-name:pod-2
image:nginx:1.2.1
imagePullPolicy:IfNotPresent
ports:
-name:web
containerPort:80
tolerations:
-key:"check"
operator:"Equal"
value:"vfan"
以上策略表示:能够容忍所有key:value为check:vfan的污点类型
[root@Centos8 scheduler]
# kubectl create -f pod2.yaml 
pod/pod-2 created


[root@Centos8 scheduler]
# kubectl get pod 
NAME    READY   STATUS    RESTARTS   AGE

pod-1   0/1     Pending   0          3m25s

pod-2   1/1     Running   0          4s

设置容忍的Pod,可以正常调度到Node节点,而没有设置容忍的,还一直处于Pending状态

最后将Node污点去除:

kubectl taint nodes testcentos7 check=vfan:NoExecute-

去除node污点仅需在创建命令的最后加一个 - 即可

指定调度节点

注意,以下所有的测试都是1Master、1Node的情况下:
[root@Centos8 scheduler]
# kubectl get node
NAME          STATUS   ROLES    AGE    VERSION

centos8       Ready    master   134d   v1.15.1

testcentos7   Ready    <none>   133d   v1.15.1

1、Pod.spec.nodeName 将 Pod 直接调度到指定的 Node 节点上,会跳过 Schedule 的调度策略,该匹配规则是强制匹配
vim nodeName1.yaml
apiVersion:extensions/v1beta1
kind:Deployment
metadata:
name:nodename-1
labels:
app:web
spec:
replicas:3
template:
metadata:
labels:
app:web
spec:
nodeName:testcentos7
containers:
-name:nodename-1
image:nginx:1.2.1
imagePullPolicy:IfNotPresent
ports:
-containerPort:80
以上策略表示,所有Deployment所有的副本,均要调度到testcentos7节点上,开始创建:
[root@Centos8 scheduler]
# kubectl apply -f nodeName1.yaml 
deployment.extensions/nodename-1 created


[root@Centos8 scheduler]
# kubectl get pod -o wide 
NAME                 READY   STATUS    RESTARTS   AGE   IP             NODE

nodename-1-7f4c7db4d4-hdcjv   1/1  Running   0   92s   10.244.3.240 testcentos7

nodename-1-7f4c7db4d4-xxrj8   1/1  Running   0   93s   10.244.3.238 testcentos7

nodename-1-7f4c7db4d4-zkt2c   1/1  Running   0   92s   10.244.3.239 testcentos7

为了对比效果,修改yaml文件中Node节点为centos8
nodeName: centos8

再次创建测试
[root@Centos8 scheduler]
# kubectl delete -f nodeName1.yaml 

[root@Centos8 scheduler]
# kubectl apply -f nodeName1.yaml 
deployment.extensions/nodename-1 created


NAME                          READY   STATUS    RESTARTS   AGE    IP     NODE

nodename-1-7d49bd7849-ct9w5   1/1     Running   0  2m2s   10.244.0.112  centos8

nodename-1-7d49bd7849-qk9mm   1/1     Running   0  2m2s   10.244.0.113  centos8

nodename-1-7d49bd7849-zdphd   1/1     Running   0  2m2s   10.244.0.111  centos8

全部落在了centos8节点中
2、Pod.spec.nodeSelector:通过 kubernetes 的 label-selector 机制选择节点,由调度策略匹配 label,而后调度 Pod 到目标节点,该匹配规则属于强制约束
vim nodeSelect1.yaml
apiVersion:extensions/v1beta1
kind:Deployment
metadata:
name:node-1
labels:
app:web
spec:
replicas:3
template:
metadata:
labels:
app:myweb
spec:
nodeSelector:
type:ssd# Node包含的标签
containers:
-name:myweb
image:nginx:1.2.1
ports:
-containerPort:80
以上策略表示:将Deployment的三个副本全部调度到标签为type:ssd的Node节点上
[root@Centos8 scheduler]
# kubectl apply -f nodeSelect1.yaml 
deployment.extensions/node-1 created


[root@Centos8 scheduler]
# kubectl get pod 
NAME                      READY   STATUS    RESTARTS   AGE

node-1-684b6cc685-9lzbn   0/1     Pending   0          3s

node-1-684b6cc685-lwzrm   0/1     Pending   0          3s

node-1-684b6cc685-qlgjq   0/1     Pending   0          3s


[root@Centos8 scheduler]
# kubectl get node --show-labels
NAME          STATUS   ROLES    AGE    VERSION   LABELS

centos8       Ready    master   135d   v1.15.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=centos8,kubernetes.io/os=linux,node-role.kubernetes.io/master=

testcentos7   Ready    <none>   134d   v1.15.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=testcentos7,kubernetes.io/os=linux

因为没有Node节点有此标签,所以一直处于Pending状态
下面我们将testcentos7节点打上标签type:ssd
[root@Centos8 scheduler]
# kubectl label node testcentos7 type=ssd
node/testcentos7 labeled


[root@Centos8 scheduler]
# kubectl get pod
NAME                      READY   STATUS    RESTARTS   AGE

node-1-684b6cc685-9lzbn   1/1     Running   0          4m30s

node-1-684b6cc685-lwzrm   1/1     Running   0          4m30s

node-1-684b6cc685-qlgjq   1/1     Running   0          4m30s

打上标签后,Pod恢复Running状态
公众号专属福利 1 | 
| 2020全新专题实战教程限时1.99元 | 
|别获取课程邀请好友听课即可返现99%|
|如不分享也可识别海报中二维码单独购买|
公众号专属福利 2 | 
| 长按识别免费领取实战手册|

继续阅读
阅读原文