Kubernetes调度器-Pod分配给节点(Taint污点和Toleration容忍)

Taint和Toleration
节点亲和性是pod的一种属性(偏好或硬性要求),它使pod被吸引到一类特定的节点。Taint则相反,它使节点能够排斥一类特定的pod。

Taint和toleration相互配合,可以用来避免pod被分配到不合适的节点上。每个节点上都可以应用一个或多个taint ,这表示对于那些不能容忍这些taint的pod,是不会被该节点接受的。如果将toleration应用于pod上,则表示这些pod可以(但不要求)被调度到具有匹配taint的节点上。

污点(Taint)
给节点增加一个taint,node节点被设置上污点之后就和pod之间存在了一种相斥的关系,可以让node节点拒绝pod的调度执行,甚至将node节点存在的pod驱逐出去。

添加污点的方式:kubectl taint nodes node1 key=value:NoSchedule
给节点node1增加一个taint,它的key是key,value是value,effect是NoSchedule。这表示只有拥有和这个taint相匹配的toleration的pod才能够被分配到node1这个节点。可以在PodSpec中定义pod的toleration。

删除上述命令添加的taint:
kubectl taint nodes node1 key:NoSchedule-

每个污点有一个key和value作为污点的标签,其中value可以为空,effect描述污点的作用,当前taint的effect支持如下三个选项:
1)NoScchedule:表示k8s将不会将pod调度到具有该污点的node上
2)PreferNoSchedule:表示k8s将尽量避免将pod调度到具有该污点的node上
3)NoExecute:表示k8s将不会将pod调度到具有该污点的node上,同时会将node上已经存在的pod驱逐出去。

污点查看、设置、去除操作
[root@k8smaster pod]# kubectl describe nodes k8smaster |grep Taints #查看污点
Taints:             node-role.kubernetes.io/master:NoSchedule
[root@k8smaster pod]# kubectl describe nodes k8snode01 |grep Taints #查看污点
Taints:             <none>
[root@k8smaster pod]# kubectl describe nodes k8snode02 |grep Taints #查看污点
Taints:             <none>
[root@k8smaster pod]# kubectl taint node k8snode01 key1=value1:NoSchedule #添加污点
node/k8snode01 tainted
[root@k8smaster pod]# kubectl taint node k8snode01 key2=value2:NoSchedule  #添加污点
node/k8snode01 tainted
[root@k8smaster pod]# kubectl describe nodes k8snode01 |grep NoSchedule  #查看污点
Taints:             key1=value1:NoSchedule
                    key2=value2:NoSchedule
[root@k8smaster pod]# 
[root@k8smaster pod]# kubectl taint node k8snode01 key2=value2:NoSchedule- #删除污点
node/k8snode01 untainted
[root@k8smaster pod]# kubectl describe nodes k8snode01|grep NoSchedule #查看污点
Taints:             key1=value1:NoSchedule

[root@k8smaster pod]# kubectl get nodes --show-labels 
NAME        STATUS   ROLES    AGE   VERSION   LABELS
k8smaster   Ready    master   9d    v1.15.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8smaster,kubernetes.io/os=linux,node-role.kubernetes.io/master=
k8snode01   Ready    <none>   9d    v1.15.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd2,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8snode01,kubernetes.io/os=linux
k8snode02   Ready    <none>   9d    v1.15.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8snode02,kubernetes.io/os=linux
[root@k8smaster pod]# 
[root@k8smaster pod]# more pod5.yaml  #k8snode01有污点,没有可用节点,pod无法调度起来
apiVersion: v1
kind: Pod
metadata:
  name: mypod5
  labels:
    app: mypod5
spec:
  containers:
  - name: mypod5-ctn
    image: 192.168.23.100:5000/tomcat:v2
    imagePullPolicy: IfNotPresent
  restartPolicy: Never
  nodeSelector:
    disktype: ssd2
[root@k8smaster pod]# kubectl create -f pod5.yaml 
pod/mypod5 created
[root@k8smaster pod]# kubectl get pod -o wide #调度失败
NAME     READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
mypod5   0/1     Pending   0          17s   <none>   <none>   <none>           <none>
[root@k8smaster pod]# kubectl describe pod mypod5 
Name:         mypod5
Namespace:    default
Priority:     0
Node:         <none>
Labels:       app=mypod5
Annotations:  <none>
Status:       Pending
IP:           
Containers:
  mypod5-ctn:
    Image:        192.168.23.100:5000/tomcat:v2
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-vt7pl (ro)
Conditions:
  Type           Status
  PodScheduled   False 
Volumes:
  default-token-vt7pl:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-vt7pl
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  disktype=ssd2
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason            Age                From               Message
  ----     ------            ----               ----               -------
  Warning  FailedScheduling  35s (x2 over 35s)  default-scheduler  0/3 nodes are available: 1 node(s) had taints that the pod didn't tolerate, 2 node(s) didn't match node selector.
[root@k8smaster pod]#


容忍(Tolerations)
设置了污点的node将根据taint的effect:NoSchedule、PreferNoSchedule、NoExecute和pod之间产生互斥的关系,pod将在一定程度上不会被调度到node上。但我们可以在pod上设置容忍Toleration,设置了容忍的pod将可以容忍污点的存在,可以被调度到存在污点的node上。

在PodSpec中为容器设定容忍标签。以下两个容忍标签都与上面的kubectl taint 创建的污点"匹配", 因此具有任一容忍标签的Pod都可以将其调度到"node1"上:

tolerations:
- key: "key"
  operator: "Equal"
  value: "value"
  effect: "NoSchedule"
tolerations:
- key: "key"
  operator: "Exists"
  effect: "NoSchedule"


一个toleration和一个taint相"匹配”"指它们有一样的key和effect ,并且:如果operator是Exists,此时toleration不能指定value。
如果operator是Equal,则它们的value应该相等。
注意:
存在两种特殊情况:
1)如果一个toleration的key为空且operator为Exists,表示这个toleration与任意的key、value和effect都匹配,即这个toleration能容忍任意taint。
tolerations:
- operator: "Exists"
2)如果一个toleration的effect为空,则key值与之相同的相匹配taint的effect可以是任意值。
tolerations:
- key: "key"
operator: "Exists"
上述例子使用到的effect的一个值 NoSchedule,您也可以使用另外一个值 PreferNoSchedule。这是“优化”或“软”版本的 NoSchedule ——系统会尽量避免将pod调度到存在其不能容忍 taint 的节点上,但这不是强制的。effect 的值还可以设置为 NoExecute ,下文会详细描述这个值。

可以给一个节点添加多个taint ,也可以给一个pod 添加多个toleration。Kubernetes处理多个taint和toleration的过程就像一个过滤器:从一个节点的所有taint开始遍历,过滤掉那些pod中存在与之相匹配的toleration的taint。余下未被过滤的taint的effect值决定了pod是否会被分配到该节点,特别是以下情况:如果未被过滤的taint中存在一个以上effect值为NoSchedule的taint,则k8s不会将 pod 分配到该节点。如果未被过滤的taint中不存在effect值为NoSchedule的taint,但是存在effect值为PreferNoSchedule的taint,则Kubernetes会尝试将pod分配到该节点。如果未被过滤的taint中存在一个以上effect值为NoExecute的taint,则Kubernetes不会将pod分配到该节点(如果 pod 还未在节点上运行),或者将pod从该节点驱逐(如果pod已经在节点上运行)。

通常情况下,如果给一个节点添加了一个effect值为NoExecute的taint,则任何不能忍受这个taint的pod都会马上被驱逐,任何可以忍受这个taint的pod都不会被驱逐。但是,如果pod存在一个effect值为NoExecute的toleration指定了可选属tolerationSeconds的值,则表示在给节点添加了上述taint之后,pod还能继续在节点上运行的时间。

设置容忍的pod
[root@k8smaster pod]# more pod6.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: mypod6
  labels:
    app: mypod6
spec:
  containers:
  - name: mypod6-ctn
    image: 192.168.23.100:5000/tomcat:v2
    imagePullPolicy: IfNotPresent
  restartPolicy: Never
  tolerations:
  - key: key1
    operator: Equal
    value: value1
    effect: NoSchedule

  nodeSelector:
    disktype: ssd2
[root@k8smaster pod]# kubectl create -f pod6.yaml 
pod/mypod6 created
[root@k8smaster pod]# kubectl get pod -o wide
NAME     READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
mypod6   1/1     Running   0          3m48s   10.244.1.32   k8snode01   <none>           <none>
[root@k8smaster pod]# 


 

发布了60 篇原创文章 · 获赞 20 · 访问量 4600

猜你喜欢

转载自blog.csdn.net/zhaikaiyun/article/details/104523637