cka-节点污点(taint)及pod的tolerations

了解污点

创建的pod只是调度到了两台node上,虽然master的状态也是Ready,但是并没有pod调度上去,这就是因为taint的问题了。

如果我们给某节点设置了taint(污点)的话,只有那些设置了tolerations(容忍污点)的pod才能运行在此节点上。

首先查看某节点是否设置了taint

[root@vms10 pod]#  kubectl describe nodes vms11 |grep -E '(Roles|Taints)'
Roles:              <none>
Taints:             <none>
[root@vms10 pod]#

可以看到此时vms11上并没有任何taint的设置。

为节点设置taint的语法:
kubectl taint nodes 节点名 key值=value值:effect effect的值一般是NoSchedule。
如果要对所有节点设置:

kubectl taint nodes --all key值=value值:NoSchedule

注:这里value的值是可以不写的,如果不写话语法就是这样的:
kubectl taint nodes 节点名 key值=:NoSchedule

删除taint
kubectl taint nodes 节点名 key-

现在为vms11设置taint:

[root@vms10 pod]# kubectl taint nodes vms11.rhce.cc keyxx=valuexx:NoSchedule
node/vms11.rhce.cc tainted
[root@vms10 pod]# kubectl describe nodes vms11 |grep -E '(Roles|Taints)'
Roles:              <none>
Taints:             keyxx=valuexx:NoSchedule
[root@vms10 pod]#

现在查看现有pod(上一节里创建):
file
可以发现,pod依然是在vms11上运行,说明如果对某节点设置taint的话,则是不影响当前正在运行的pod的。
删除此deployment,并重新创建一个新的deployment:

root@vms10 pod]# kubectl delete deployments. nginx 
deployment.extensions "nginx" deleted
[root@vms10 pod]# kubectl run nginx --image=nginx --replicas=4
...
deployment.apps/nginx created
[root@vms10 pod]# 

file
可以看到,现在所有的pod都是运行在vms12上了,因为vms11存在taint(污点)。
自行删除此deployment。

如果需要pod在含有taint的节点上运行,则定义pod的时候需要指定toleration属性。
在pod里定义toleration的格式为:

tolerations:
- key: "key值"
  operator: "Equal"
  value: "value值"
  effect: "值"

operator的值一般是
Equal 意思是value需要和taint的value值一样(默认)
Exists 意思是可以不指定value的值

在operator的值为Equal

首先删除vms12上diskxx=ssdxx标签,在vms11上设置此标签:

[root@vms10 ~]# kubectl label nodes vms12.rhce.cc diskxx-
node/vms12.rhce.cc labeled
[root@vms10 ~]# 
[root@vms10 ~]# kubectl label nodes vms11.rhce.cc diskxx=ssdxx
node/vms11.rhce.cc labeled
[root@vms10 ~]# 

创建podtaint.yaml:

[root@vms10 pod]# cat podtaint.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: web1
  labels:
    role: myrole
spec:
  nodeSelector:
    diskxx: ssdxx
  containers:
    - name: web
      image: nginx
      imagePullPolicy: Always
      ports:
        - name: web
          containerPort: 80
          protocol: TCP
[root@vms10 pod]#
[root@vms10 pod]# kubectl apply -f podtaint.yaml
pod/web1 created
[root@vms10 pod]# kubectl get pods -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
web1   0/1     Pending   0          5s    <none>   <none>   <none>           <none>
[root@vms10 pod]#

因为要求web1要在vms11上运行(因为vms11上有diskxx=ssdxx标签),但vms11上有污点,所以此时pod处于Pending状态。
删除此pod:

[root@vms10 pod]# kubectl delete -f podtaint.yaml
pod "web1" deleted
[root@vms10 pod]#

修改podtaint.yaml的内容:

[root@vms10 pod]# cat podtaint.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: web1
  labels:
    role: myrole
spec:
  nodeSelector:
    diskxx: ssdxx
  tolerations:
  - key: "keyxx"
    operator: "Equal"
    value: "valuexx"
    effect: "NoSchedule"
  containers:
    - name: web
      image: nginx
      imagePullPolicy: Always
      ports:
        - name: web
          containerPort: 80
          protocol: TCP
[root@vms10 pod]#

再次创建pod:
file
可以看到此时pod在vms11上正常运行了。
注:并不是节点设置了taint,pod设置了toleration 这个pod就一定会在此节点上运行,所以这里用label来指定pod在vms11上运行。

删除此pod:

[root@vms10 pod]# kubectl delete -f podtaint.yaml 
pod "web1" deleted
[root@vms10 pod]#

删除vms11的taint设置,然后给vms11重新设置多个taint:

[root@vms10 ~]# kubectl taint nodes vms11.rhce.cc keyxxx-
node/vms11.rhce.cc untainted
[root@vms10 ~]#
[root@vms10 ~]# kubectl taint nodes vms11.rhce.cc key123=value123:NoSchedule
node/vms11.rhce.cc tainted
[root@vms10 ~]# kubectl taint nodes vms11.rhce.cc keyxx=valuexx:NoSchedule
node/vms11.rhce.cc tainted
[root@vms10 ~]#
[root@vms10 ~]# kubectl describe nodes vms11 |grep -E -A1 '(Roles|Taints)'
Roles:              <none>
Labels:             beta.kubernetes.io/arch=amd64
--
Taints:             key123=value123:NoSchedule
                    keyxx=valuexx:NoSchedule
[root@vms10 ~]#

在podtaint.yaml不修改的情况下再次运行:

[root@vms10 pod]# kubectl apply -f podtaint.yaml
pod/web1 created
[root@vms10 pod]# kubectl get pods -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
web1   0/1     Pending   0          2s    <none>   <none>   <none>           <none>
[root@vms10 pod]#

pod的状态为Pending,说明pod的toleration设置只要有一个和节点的taint不匹配,那么就不允许创建pod。
删除此pod并修改podtaint.yaml文件:

[root@vms10 pod]# cat podtaint.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: web1
  labels:
    role: myrole
spec:
  nodeSelector:
    diskxx: ssdxx
  tolerations:
  - key: "keyxx"
    operator: "Equal"
    value: "valuexx"
    effect: "NoSchedule"
  - key: "key123"
    operator: "Equal"
    value: "value123"
    effect: "NoSchedule"
  containers:
    - name: web
      image: nginx
      imagePullPolicy: Always
      ports:
        - name: web
          containerPort: 80
          protocol: TCP
[root@vms10 pod]#

运行并查看pod:

[root@vms10 pod]# kubectl apply -f podtaint.yaml
pod/web1 created
[root@vms10 pod]#

file
说明,pod里的toleration必须要和节点所有的taint值匹配才可以。
自行删除此pod。

operator的值为Exists

如果在设置节点taint的时候,如果value
取消vms11的key123=value123这个taint值:

[root@vms10 ~]# kubectl taint nodes vms11.rhce.cc key123-
node/vms11.rhce.cc untainted
[root@vms10 ~]# kubectl taint nodes vms11.rhce.cc keyxx-
node/vms11.rhce.cc untainted
[root@vms10 ~]# 
[root@vms10 ~]# kubectl describe nodes vms11 |grep -E '(Roles|Taints)'
Roles:              <none>
Taints:             keyxx=valuexx:NoSchedule
[root@vms10 ~]#

修改podtaint.yaml,配置operator的值为Exists:

[root@vms10 pod]# cat podtaint.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: web1
  labels:
    role: myrole
spec:
  nodeSelector:
    diskxx: ssdxx
  tolerations:
  - key: "keyxx"
    operator: "Exists"
    value: "valuexx"
    effect: "NoSchedule"
  containers:
    - name: web
      image: nginx
      imagePullPolicy: Always
      ports:
        - name: web
          containerPort: 80
          protocol: TCP
[root@vms10 pod]#
[root@vms10 pod]# kubectl apply -f podtaint.yaml 
The Pod "web1" is invalid: 
* spec.tolerations[0].operator: Invalid value: core.Toleration{Key:"keyxx", Operator:"Exists", Value:"valuexx", Effect:"NoSchedule", TolerationSeconds:(*int64)(nil)}: value must be empty when `operator` is 'Exists'
* spec.tolerations: Forbidden: existing toleration can not be modified except its tolerationSeconds
[root@vms10 pod]#

说明,在value值不为空的情况下,operator不能选择Exists。
修改vms11的taint值:

[root@vms10 ~]# kubectl taint nodes vms11.rhce.cc keyxx=:NoSchedule --overwrite
node/vms11.rhce.cc modified
[root@vms10 ~]# 
[root@vms10 ~]# kubectl describe nodes vms11 |grep -E '(Roles|Taints)'
Roles:              <none>
Taints:             keyxx:NoSchedule
[root@vms10 ~]#

keyxx=后面直接是冒号:说明此时value的值为空,修改podtaint.yaml的值:

[root@vms10 pod]# cat podtaint.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: web1
  labels:
    role: myrole
spec:
  nodeSelector:
    diskxx: ssdxx
  tolerations:
  - key: "keyxx"
    operator: "Exists"
    effect: "NoSchedule"
  containers:
    - name: web
      image: nginx
      imagePullPolicy: Always
      ports:
        - name: web
          containerPort: 80
          protocol: TCP
[root@vms10 pod]#

此时并没有设置value的值,然后创建pod:

[root@vms10 pod]# kubectl apply -f podtaint.yaml 
pod/web1 configured
[root@vms10 pod]# 
[root@vms10 pod]# kubectl get pods
NAME   READY   STATUS    RESTARTS   AGE
web1   1/1     Running   0          10m
[root@vms10 pod]#

正常运行,说明在没有设置value的时候,可以用Exists,不过如果要使用Equal的话,应该按照如下格式写:

- key: "keyxx"
    operator: "Exists"
    value: ""
effect: "NoSchedule"

自行删除web1这个pod。

相关新闻

发表回复

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

                                                                                                                                    RHCE9学习指南连载,点击阅读