污点(Taint)使节点能够排斥/驱逐一类特定的 Pod,通过给 Node 打一些污点,来限制 Pod 调度到某些 Node 上。
容忍度(Toleration) 是应用于 Pod等资源上的,容忍度允许调度器调度带有对应容忍度的 Pod到带有污点的Node上。
taints 内容包括 key、value、effect:
目前 Kubernetes 里面有三个 taints 行为:
污点使用kubectl命令即可,容忍写到yaml中更合适
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
tolerations:
- key: "example-key"
operator: "Exists"
value: "value1"
effect: "NoSchedule"
tolerationSeconds: 60
operator 的默认值是 Equal。
一个容忍度和一个污点相“匹配”是指它们有一样的键名和效果,并且:
toletationSeconds是容忍的时间,默认是永久,就是不驱逐。可以写上,一般和NoExecute搭配,可以在有污点的node上存在一会儿再被驱逐,单位是秒
给某节点xxx上添加污点 ,key为master,value为system,effect是NoSchedule
命令
kubectl taint node xxx master=system:NoSchedule

验证
在xxx节点上创建pod
busybox-tainttest.yaml
apiVersion: v1
kind: Pod
metadata:
name: busybox-tainttest
spec:
nodeSelector:
kubernetes.io/hostname: xxx
containers:
- name: busybox
image: busybox:latest
imagePullPolicy: IfNotPresent
command: ['sh','-c','sleep 3600']
ports:
- containerPort: 80
命令
kubectl create -f busybox-tainttest.yaml
kubectl describe po busybox-tainttest
结果

会看到该Pod处于Pending状态,describe时显示该pod不能容忍污点master:system,部分显示结果如上图,
apiVersion: v1
kind: Pod
metadata:
name: busybox-tainttest
spec:
tolerations:
- key: master
value: system
effect: NoSchedule
nodeSelector:
kubernetes.io/hostname: xxx
containers:
- name: busybox
image: busybox:latest
imagePullPolicy: IfNotPresent
command: ['sh','-c','sleep 3600']
ports:
- containerPort: 80
命令
kubectl apply -f busybox-tainttest.yaml
结果

可以看到,已经可以调度到这个节点了
命令
kubectl taint node xxx master=NoSchedule-
结果

可以看到该节点没有污点了