节点亲和性,是pod的一种属性(偏好或硬性要求),它使pod被吸引到一类特定的节点。Taint 则相反,它使节点能够排斥一类特定的 pod
Taint 和 toleration 相互配合,可以用来避免 pod 被分配到不合适的节点上。每个节点上都可以应用一个或多个taint ,这表示对于那些不能容忍这些 taint 的 pod,是不会被该节点接受的。如果将 toleration 应用于 pod上,则表示这些 pod 可以(但不要求)被调度到具有匹配 taint 的节点上
I、污点 ( Taint ) 的组成
使用kubectl taint命令可以给某个 Node 节点设置污点,Node 被设置上污点之后就和 Pod 之间存在了一种相斥的关系,可以让 Node 拒绝 Pod 的调度执行,甚至将 Node 已经存在的 Pod 驱逐出去
每个污点的组成如下:
key=value:effect
每个污点有一个 key 和 value 作为污点的标签,其中 value 可以为空,effect 描述污点的作用。当前 tainteffect 支持如下三个选项:
NoSchedule
:表示 k8s 将不会将 Pod 调度到具有该污点的Node 上PreferNoSchedule
:表示 k8s 将尽量避免将 Pod 调度到具有该污点的Node 上NoExecute
:表示 k8s 将不会将 Pod 调度到具有该污点的 Node 上,同时会将 Node 上已经存在的 Pod驱逐出去II、污点的设置、查看和去除
# 设置污点
kubectl taint nodes node1 key1=value1:NoSchedule
# 节点说明中,查找 Taints 字段
kubectl describe pod pod-name
#结果:
`Taints: key1=value1:NoSchedule`
# 去除污点
kubectl taint nodes node1 key1:NoSchedule-
测试启动三个副本的Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: myapp
release: stabel
template:
metadata:
labels:
app: myapp
release: stabel
env: test
spec:
containers:
- name: myapp
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
kubectl apply -f dep.yml
发现3个pod都调度到了node2节点上(node1有污点)
[root@master k8s]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-5db79b5cb6-c6phz 1/1 Running 0 26s 10.244.2.30 node2 <none> <none>
myapp-deploy-5db79b5cb6-gkqlc 1/1 Running 0 26s 10.244.2.29 node2 <none> <none>
myapp-deploy-5db79b5cb6-qkjb7 1/1 Running 0 26s 10.244.2.28 node2 <none> <none>
去除污点:
kubectl taint nodes node1 key1:NoSchedule-
删除一个pod后:
服务就可以调度到node1节点
[root@master k8s]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-5db79b5cb6-c6phz 1/1 Running 0 35m 10.244.2.30 node2 <none> <none>
myapp-deploy-5db79b5cb6-gkqlc 1/1 Running 0 35m 10.244.2.29 node2 <none> <none>
myapp-deploy-5db79b5cb6-qkjb7 1/1 Running 0 35m 10.244.2.28 node2 <none> <none>
[root@master k8s]# kubectl delete pod myapp-deploy-5db79b5cb6-c6phz
pod "myapp-deploy-5db79b5cb6-c6phz" deleted
[root@master k8s]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-5db79b5cb6-dpcjf 1/1 Running 0 6s 10.244.1.34 node1 <none> <none>
myapp-deploy-5db79b5cb6-gkqlc 1/1 Running 0 35m 10.244.2.29 node2 <none> <none>
myapp-deploy-5db79b5cb6-qkjb7 1/1 Running 0 35m 10.244.2.28 node2 <none> <none>
node1节点使用NoExecute:(发现原来在node1节点的pod被驱逐到node2)
[root@master k8s]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-5db79b5cb6-dpcjf 1/1 Running 0 6s 10.244.1.34 node1 <none> <none>
myapp-deploy-5db79b5cb6-gkqlc 1/1 Running 0 35m 10.244.2.29 node2 <none> <none>
myapp-deploy-5db79b5cb6-qkjb7 1/1 Running 0 35m 10.244.2.28 node2 <none> <none>
[root@master k8s]# kubectl taint nodes node1 key1=value1:NoExecute
node/node1 tainted
[root@master k8s]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-5db79b5cb6-gkqlc 1/1 Running 0 52m 10.244.2.29 node2 <none> <none>
myapp-deploy-5db79b5cb6-qkjb7 1/1 Running 0 52m 10.244.2.28 node2 <none> <none>
myapp-deploy-5db79b5cb6-x9kc9 1/1 Running 0 2s 10.244.2.31 node2 <none> <none>
设置了污点的 Node 将根据 taint 的 effect:NoSchedule、PreferNoSchedule、NoExecute 和 Pod 之间产生互斥的关系,Pod 将在一定程度上不会被调度到 Node 上。但我们可以在 Pod 上设置容忍 ( Toleration ) ,意思是设置了容忍的 Pod 将可以容忍污点的存在,可以被调度到存在污点的 Node 上
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"
1、当不指定 key 值时,表示容忍所有的污点 key(master节点也会部署):
tolerations:
- operator: "Exists"
2、当不指定 effect 值时,表示容忍所有的污点作用
tolerations:
- key: "key"
operator: "Exists"
3、有多个 Master 存在时,防止资源浪费,可以如下设置(还没有测试)
kubectl taint nodes Node-Name node-role.kubernetes.io/master=:PreferNoSchedule
例子1:
使用下面的容忍度:
tolerations:
- key: "key1"
operator: "Equal"
value: "value1"
effect: "NoExecute"
[root@master k8s]# cat dep-tolerator.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: myapp
release: stabel
template:
metadata:
labels:
app: myapp
release: stabel
env: test
spec:
tolerations:
- key: key1
operator: Equal
value: value1
effect: NoExecute
containers:
- name: myapp
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
查看结果会看到node1和node2都有部署
[root@master k8s]# kubectl apply -f dep-tolerator.yml
deployment.apps/myapp-deploy created
[root@master k8s]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-67d655bffc-7bn6n 1/1 Running 0 12s 10.244.2.32 node2 <none> <none>
myapp-deploy-67d655bffc-9fqc2 1/1 Running 0 12s 10.244.1.36 node1 <none> <none>
myapp-deploy-67d655bffc-vmmd4 1/1 Running 0 12s 10.244.1.35 node1 <none> <none>
例子2:
使用
tolerations:
- operator: "Exists"
[root@master k8s]# cat dep-tolerator-all.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: myapp
release: stabel
template:
metadata:
labels:
app: myapp
release: stabel
env: test
spec:
tolerations:
- operator: Exists
containers:
- name: myapp
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
kubectl apply -f dep-tolerator-all.yml
结果:会发现连master节点也调度 了
[root@master k8s]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-b9d684ff-2525s 1/1 Running 0 21m 10.244.2.33 node2 <none> <none>
myapp-deploy-b9d684ff-bsrbg 1/1 Running 0 21m 10.244.0.8 master <none> <none>
myapp-deploy-b9d684ff-fggcg 1/1 Running 0 21m 10.244.1.37 node1 <none> <none>
例子3:(容忍带有key: "key1"的)
tolerations:
- key: "key1"
operator: "Exists"