上一节学习的Affinity是在pod创建的时候设置的,但是随着pod的数目越来越多,更有效率的方式应该是在node端设置一些记号,pod默认就不会调度在这个node上,而只有一些经过特殊设置的pod才可以在该node调度。这就是这一节要学习的Taint和Toleration。
我是T型人小付,一位坚持终身学习的互联网从业者。喜欢我的博客欢迎在csdn上关注我,如果有问题欢迎在底下的评论区交流,谢谢。
Taint
Taint,污点,是在node上的一个设置,格式如下
key=value:effect
其中value可以为空,effect是该污点所对应的对pod的动作,可以为如下的一种
- NoSchedule - 表示k8s将不会将pod调度到具有该污点的node
- PreferNoSchedule - 表示k8s尽量避免将pod调度到具有该污点的node
- NoExecute - 表示k8s将不会将pod调度到具有该污点的node,并且该node上已存在的pod还会被驱逐(如果是控制器创建的pod会在别的node被重新创建)
污点设置和删除命令举例如下
# Update node 'foo' with a taint with key 'dedicated' and value 'special-user' and effect 'NoSchedule'.
# If a taint with that key and effect already exists, its value is replaced as specified.
kubectl taint nodes foo dedicated=special-user:NoSchedule
# Remove from node 'foo' the taint with key 'dedicated' and effect 'NoSchedule' if one exists.
kubectl taint nodes foo dedicated:NoSchedule-
# Remove from node 'foo' all the taints with key 'dedicated'
kubectl taint nodes foo dedicated-
# Add a taint with key 'dedicated' on nodes having label mylabel=X
kubectl taint node -l myLabel=X dedicated=foo:PreferNoSchedule
# Add to node 'foo' a taint with key 'bar' and no value
kubectl taint nodes foo bar:NoSchedule
如果要查看污点,直接用kubectl describe node NODE_NAME即可。
k8s的master在创建的时候默认就带着一个污点,如下
[root@k8s-master ~]# kubectl describe node k8s-master | grep Taints
Taints: node-role.kubernetes.io/master:PreferNoSchedule
我这里在安装集群的时候因为只有两个node,已经修改过master的这个污点为PreferNoSchedule,这也就是为什么上一节中最后的那个例子,pod在软策略下无法被调度到master的原因。
Toleration
针对某个Taint设置了Toleration,也就是容忍,的pod,可以被调度到带污点的node上。注意,只是表示可能,而并不是一定会调度到该带污点的node上。
Toleration在pod的pod.spec.tolerations字段设置,几个字段含义如下
| 字段 | 类型 | 说明 |
|---|---|---|
| effect | string | 和污点的effect设置对应,如果为空表示所有的effect |
| key | string | 和污点的key设置对应,如果为空表示所有的key |
| opertor | string | 表示key和下面value的关系,可选Exists和Equal |
| value | string | 如果operator是Exists,这里需要为空 |
| tolerationSeconds | integer | 只在NoExecute下有效,秒为单位,之后容忍失效,不设置表示永不失效 |
实际操作
以下所有yaml文件托管在我的Github仓库
现有的两个node如下
[root@k8s-master ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 15d v1.15.11
k8s-node1 Ready <none> 15d v1.15.11
其中node1是没有任何污点的,首先用下面的yaml文件test-taint.yaml起一个3个pod的deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: deployment-app
spec:
containers:
- name: mynginx
image: mynginx:v2
ports:
- containerPort: 80
起来以后2个在node1,一个在master
[root@k8s-master taint]# kubectl apply -f test-taint.yaml
deployment.extensions/test-deployment created
[root@k8s-master taint]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-deployment-64484c94f7-kjhpq 1/1 Running 0 5s 10.244.1.136 k8s-node1 <none> <none>
test-deployment-64484c94f7-tlhh5 1/1 Running 0 5s 10.244.0.21 k8s-master <none> <none>
test-deployment-64484c94f7-xjth6 1/1 Running 0 5s 10.244.1.137 k8s-node1 <none> <none>
这时候给node1打一个污点
[root@k8s-master taint]# kubectl taint nodes k8s-node1 xiaofu=good:NoExecute
node/k8s-node1 tainted
会发现所有的pod都跑到master上去了
[root@k8s-master taint]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-deployment-64484c94f7-6r5nc 1/1 Running 0 14s 10.244.0.22 k8s-master <none> <none>
test-deployment-64484c94f7-tlhh5 1/1 Running 0 9m16s 10.244.0.21 k8s-master <none> <none>
test-deployment-64484c94f7-zt9lt 1/1 Running 0 14s 10.244.0.23 k8s-master <none> <none>
之后新创建的pod如果不指定Toleration就都会跑到master上去。
用下面的yaml文件test-toleration.yaml新建deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test-toleration
spec:
replicas: 3
template:
metadata:
labels:
app: deployment-app
spec:
containers:
- name: mynginx
image: mynginx:v2
ports:
- containerPort: 80
tolerations:
- key: xiaofu
operator: Exists
这里配置的是只要key为xiaofu的污点存在就会容忍。
创建之后发现3个新pod都在node1下
[root@k8s-master taint]# kubectl apply -f test-toleration.yaml
deployment.extensions/test-toleration created
[root@k8s-master taint]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-deployment-64484c94f7-6r5nc 1/1 Running 0 22m 10.244.0.22 k8s-master <none> <none>
test-deployment-64484c94f7-tlhh5 1/1 Running 0 31m 10.244.0.21 k8s-master <none> <none>
test-deployment-64484c94f7-zt9lt 1/1 Running 0 22m 10.244.0.23 k8s-master <none> <none>
test-toleration-5c757467fd-5hqfn 1/1 Running 0 8s 10.244.1.139 k8s-node1 <none> <none>
test-toleration-5c757467fd-cp56b 1/1 Running 0 8s 10.244.1.138 k8s-node1 <none> <none>
test-toleration-5c757467fd-w9kv4 1/1 Running 0 8s 10.244.1.140 k8s-node1 <none> <none>
总结
污点是node上配置的用来排斥pod的信息,pod可以用容忍去抵消污点。
NoExecute是个很重要的污点,可以让某台node上的pod都转移到别的node上去,从而方便对node进行升级维护等运维操作。
除了Affinity和Taint,还有第三种影响调度的方法,我们下一节再学。
2031



被折叠的 条评论
为什么被折叠?



