如何用三只兔子的故事来轻松理解 Kubernetes 污点和容忍,


前言

本文通过自身理解进行述说,如有不准确的地方,请指正。

在讲述一系列相关专业术语之前,先尝试用一个通俗易懂的故事来说明 Kubernetes 中 node 与 pod 之间的爱恨情仇。

雄性(node)| 雌性(pod)

在银河系以外的一个星球上,有着一群两性生物,分别是雌性(pod)和雄性(node)。雌性生物居多,而雄性生物由于优胜劣汰,只剩下 3 只优质的雄性生物(node)。雄雌在一起就容易产生吸引,就会有以下的情况产生:

(1)雄(node)少雌(pod)多,而这三只优质的雄性生物性格、优点都是一致的,雌性生物选谁都一样。于是,雌性生物就分为均等分为三列和三只雄性生物在一起。这种类似于平均分配的原则。

k8s 中的概念:在 k8s 中是最常见最普通的 pod 分布方式,常用与 deployment 和 daemonset 控制器。

(2)时间一长,生物开始了进化。三只雄性(node)生物各自有了不一样的地方,编号 1 的雄性学会了种植(node01 label skill='grow');编号 2 的雄性学会了打猎(node02 label skill='hunt');编号 3 的雄性啥也没学会(没学会就保持默认)。普通的雌性(pod)仍然保持着平均分配的原则。而一些进化快的雌性(pod)生物发现了三只优质雄性(node)生物各自的不同,各自也开始有了一些选择。一些雌性更加青睐学会种植的雄性生物(nodeSelector skill='grow') ;一些雌性更加青睐学打猎的雄性生物(nodeSelector skill='hunt') ;而有些雌性生物喜欢的特点很奇特,它们喜欢会飞的雄性,而仅存的三只雄性生物都无法满足这一特点。如果有天一只雄性生物进化会飞了,它们就会依附与会飞的雄性生物,如果始终没有进化出会飞的雄性生物,则它们一直等下去这在 k8s 中,就是 yaml 文件中 nodeSelector 的使用。

k8s 中的概念:当 node 打上特定的标签后,会出现如下情况:

(3)三只雄性(node)不仅优点增长了,缺点也随之而来。编号 1 的雄性喜欢打脸(node01 taint hobby='face');编号 2 的雄性喜欢打屁股(node01 taint hobby='hunkers');编号 3 的雄性喜欢踩脚(node01 taint hobby='foot');普通的雌性(pod)生物仍然保持平均分配的原则,而一些再次进化的雌性生物也有了自己的性格。能够容忍打脸的雌性则和编号 1 的雄性在一起(tolerations hobby='face'),但是这个容忍可能是永久,也可能是 1 天(tolerationSeconds=86400)。而这三只雄性生物偶尔会在一起鬼混,编号 1 的雄性生物说不定哪天就嗜好就变为了喜欢睡懒觉。而一些无法容忍它睡懒觉嗜好的雌性生物就会隔一段时间或者马上就离开它。

k8s 中的概念:这就是 污点 与 容忍。

污点和容忍还有其他的选项参数,后文展开解说。

nodeSelector

将 pod 分配给指定的节点。

集群如下:

  1. $ kubectl get nodes  
  2. NAME         STATUS   ROLES    AGE   VERSION  
  3. k8s-master   Ready    master   20h   v1.19.7  
  4. k8s-node01   Ready    <none>   20h   v1.19.7  
  5. k8s-node02   Ready    <none>   20h   v1.19.7 

为 k8s-node01 添加一个标签

  1. $ kubectl label nodes k8s-node01 disktype=ssd  
  2. node/k8s-node01 labeled 

查看标签

  1. $ kubectl get nodes --show-labels  
  2. NAME         STATUS   ROLES    AGE   VERSION   LABELS  
  3. k8s-master   Ready    master   20h   v1.19.7   ..., kubernetes.io/hostname=k8s-master  
  4. k8s-node01   Ready    <none>   20h   v1.19.7   ..., disktype=ssd,kubernetes.io/hostname=k8s-node01  
  5. k8s-node02   Ready    <none>   20h   v1.19.7   ..., kubernetes.io/hostname=k8s-node02 

可以看到 k8s-node01 节点标签:disktype=ssd

创建一个调度到 选择节点的 Pod 

  1. apiVersion: apps/v1  
  2. kind: Deployment  
  3. metadata:  
  4.   labels:  
  5.     app: ngx  
  6.   name: ngx  
  7. spec:  
  8.   replicas: 2  
  9.   selector:  
  10.     matchLabels:  
  11.       app: ngx  
  12.   template:  
  13.     metadata:  
  14.       labels: 
  15.         app: ngx  
  16.     spec:  
  17.       containers:  
  18.       - image: nginx:alpine-arm64  
  19.         name: nginx  
  20.       nodeSelector:  
  21.         disktype: ssd    #### 选择服务 key: value 的节点  

创建 pod 查看是否调度到指定的节点

  1. $ kubectl apply -f  ngx.yaml  
  2. deployment.apps/ngx created  
  3. $ kubectl get po -o wide  
  4. NAME                   READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES  
  5. ngx-5f4df66559-hjmzg   1/1     Running   0          10s   10.244.1.13   k8s-node01   <none>           <none>  
  6. ngx-5f4df66559-wqgdb   1/1     Running   0          10s   10.244.1.14   k8s-node01   <none>           <none> 

k8s 亲和性

说道亲和性,亲和性主要分为两类:nodeAffinity 和 podAffinity 。

nodeAffinity

nodeAffinity 就是节点亲和性,调度可以分成软策略和硬策略两种方式,软策略就是如果你没有满足调度要求的节点的话,POD 就会忽略这条规则,继续完成调度过程,说白了就是满足条件最好了,没有的话也无所谓了的策略;而硬策略就比较强硬了,如果没有满足条件的节点的话,就不断重试直到满足条件为止,简单说就是你必须满足我的要求,不然我就不干的策略。nodeAffinity就有两上面两种策略:

  •  requiredDuringSchedulingIgnoredDuringExecution :硬策略
  •  preferredDuringSchedulingIgnoredDuringExecution :软策略

硬策略

  1. apiVersion: apps/v1  
  2. kind: Deployment  
  3. metadata:  
  4.   labels:  
  5.     app: ngx  
  6.   name: ngx  
  7. spec:  
  8.   replicas: 2  
  9.   selector: 
  10.      matchLabs:  
  11.       app: ngx  
  12.   template:  
  13.     metadata:  
  14.       labels:  
  15.         app: ngx  
  16.     spec:  
  17.       affinity:  
  18.         nodeAffinity:  
  19.           requiredDuringSchedulingIgnoredDuringExecution:  
  20.             nodeSelectorTerms:  
  21.             - matchExpressions:  
  22.               - key: disktype ## key 的值  
  23.                 operator: In ## 包括  
  24.                 values:  
  25.                 - ssd ## value  
  26.       containers:  
  27.       - image: nginx:alpine-arm64  
  28.         name: nginx  
  29.       nodeSelector:  
  30.         disktype: ssd 

上面这两个 pod 只会运行在 满足 node label disktype=value 的节点上,如果没有节点满足这个条件,则一直处于 pending 状态。

  1. $ kubectl get po -o wide  
  2. NAME                  READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES  
  3. ngx-7b65b44bc-gff9x   1/1     Running   0          3m53s   10.244.1.15   k8s-node01   <none>           <none>  
  4. ngx-7b65b44bc-w7bsf   1/1     Running   0          3m53s   10.244.1.16   k8s-node01   <none>           <none>  
  5. $ kubectl get nodes k8s-node01 --show-labels  
  6. NAME         STATUS   ROLES    AGE   VERSION   LABELS  
  7. k8s-node01   Ready    <none>   22h   v1.19.7   ..., disktype=ssd,kubernetes.io/arch=arm64,kubernetes.io/hostname=k8s-node01 

软策略

  1. apiVersion: apps/v1  
  2. kind: Deployment  
  3. metadata:  
  4.   labels:  
  5.     app: ngx  
  6.   name: ngx 
  7. spec:  
  8.   replicas: 2  
  9.   selector:  
  10.     matchLabels:  
  11.       app: ngx  
  12.   template:  
  13.     metadata:  
  14.       labels:  
  15.         app: ngx  
  16.     spec:  
  17.       affinity:  
  18.         nodeAffinity:  
  19.           preferredDuringSchedulingIgnoredDuringExecution:  
  20.           - weight: 10  
  21.             preference:  
  22.               matchExpressions:  
  23.               - key: disktype  
  24.                 operator: In  
  25.                 values: 
  26.                 - hdd 
  27.       containers:  
  28.       - image: nginx:alpine-arm64  
  29.         name: nginx 

软策略就是,第一选择是 node label disktype=hdd 的节点,如果没有,就采用默认 scheduler 的调度策略,没有强制性。

  1. $ kubectl get po -o wide  
  2. NAME                  READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES  
  3. ngx-d4754b6fd-lr96g   1/1     Running   0          2m55s   10.244.2.13   k8s-node02   <none>           <none>  
  4. ngx-d4754b6fd-ns7hs   1/1     Running   0          2m55s   10.244.1.28   k8s-node01   <none>           <none> 

operator 提供如下几种操作:

  •  In:label 的值在某个列表中
  •  NotIn:label 的值不在某个列表中
  •  Gt:label 的值大于某个值
  •  Lt:label 的值小于某个值
  •  Exists:某个 label 存在
  •  DoesNotExist:某个 label 不存在

如果nodeSelectorTerms下面有多个选项的话,满足任何一个条件就可以了;如果matchExpressions有多个选项的话,则必须同时满足这些条件才能正常调度 POD。

污点和容忍

在 Kubernetes 中,节点亲和性 NodeAffinity 是 Pod 上定义的一种属性,能够使 Pod 按我们的要求调度到某个节点上,而 Taints(污点) 则恰恰相反,它是 Node 上的一个属性,可以让 Pod 不能调度到带污点的节点上,甚至会对带污点节点上已有的 Pod 进行驱逐。当然,对应的 Kubernetes 可以给 Pod 设置 Tolerations(容忍) 属性来让 Pod 能够容忍节点上设置的污点,这样在调度时就会忽略节点上设置的污点,将 Pod 调度到该节点。一般时候 Taints 通常与 Tolerations 配合使用。

污点(Taints)

查看污点

查看 node 的污点

  1. $ kubectl describe nodes k8s-master  
  2. ...  
  3. Taints:             node-role.kubernetes.io/master:NoSchedule  
  4. ...  
  5. # 也可通过下面操作查看:  
  6. $ kubectl get nodes k8s-master -o go-template={{.spec.taints}}  
  7. [map[effect:NoSchedule key:node-role.kubernetes.io/master]] 

污点内容一般组成为 key、value 及一个 effect 三个元素,表现为:

  1. <key>=<value>:<effect> 

这里的 value 可以为空,表现形式为:

  1. node-role.kubernetes.io/master:NoSchedule  
  2. - key: node-role.kubernetes.io/master  
  3. - value: 空 
  4. - effect: NoSchedule 

设置污点

一般我们需要想要设置某个节点只允许特定的 Pod 进行调度,这时候就得对节点设置污点,可以按 kubectl taint node [node] key=value[effect] 格式进行设置,其中 effect 可取值如下:

  •  PreferNoSchedule: 尽量不要调度。
  •  NoSchedule: 一定不能被调度。
  •  NoExecute: 不仅不会调度, 还会驱逐 Node 上已有的 Pod。

一般时候我们设置污点,就像下面例子一样对齐进行设置:

  1. ### 设置污点并不允许 Pod 调度到该节点  
  2. $ kubectl taint node k8s-master key1=value1:NoSchedule  
  3. ### 设置污点尽量阻止污点调度到该节点  
  4. $ kubectl taint node k8s-master key2=value2:PreferNoSchedule  
  5. ### 设置污点,不允许普通 Pod 调度到该节点,且将该节点上已经存在的 Pod 进行驱逐  
  6. $ kubectl taint node k8s-master key3=value3:NoExecute 

删除污点

上面说明了如何对 Node 添加污点阻止 Pod 进行调度,下面再说一下如何删除节点上的污点,可以使用下面命令:

  1. kubectl taint node [node] [key]- 

上面语法和创建污点类似,不过需要注意的是删除污点需要知道 key 和最后面设置一个 "-" 两项将污点删除,示例如下:

为了方便演示,先给节点设置污点:

  1. ### 设置污点1  
  2. $ kubectl taint node k8s-master key1=value1:PreferNoSchedule  
  3. node/k8s-master tainted  
  4. ### 设置污点2  
  5. $ kubectl taint node k8s-master key2=value2:NoSchedule  
  6. node/k8s-master tainted  
  7. ### 设置污点3,并且不设置 value  
  8. $ kubectl taint node k8s-master key2=:PreferNoSchedule  
  9. node/k8s-master tainted 

查看污点,可以看到上面设置的三个值:

  1. $ kubectl describe nodes k8s-master  
  2. ...  
  3. Taints:             key2=value2:NoSchedule  
  4.                     node-role.kubernetes.io/master:NoSchedule  
  5.                     key1=value1:PreferNoSchedule  
  6.                     key2:PreferNoSchedule 
  7.  ... 

然后删除污点

  1. ### 删除污点,可以不指定 value,指定 [effect] 值就可删除该 key[effect] 的污点  
  2. $ kubectl taint node k8s-master key1:PreferNoSchedule-  
  3. ### 也可以根据 key 直接将该 key2 的所有 [effect] 都删除:  
  4. $ kubectl taint node k8s-master key2- 

再次查看污点,可以看到以上污点都被删除:

  1. $ kubectl describe nodes k8s-master  
  2. ...  
  3. Taints:             node-role.kubernetes.io/master:NoSchedule  
  4. ...  

容忍 (toleratints)

Pod 设置容忍

为了使某些 Pod 禁止调度到某些特定节点上,就可以对节点设置污点 taints。当然,如果希望有些 Pod 能够忽略节点的污点,继续能够调度到该节点,就可以对 Pod 设置容忍,让 Pod 能够容忍节点上设置的污点,例如:

对一个节点设置污点:

  1. kubectl taint node k8s-node01 key=value:NoSchedule 

对于 Pod 设置容忍, 以下两种方式都可以:

  1. ### 容忍的 key、value 和对应 effect 也必须和污点 taints 保持一致  
  2. ......  
  3. tolerations:  
  4. - key: "key"  
  5.   operator: "Equal" 
  6.   value: "value"  
  7.   effect: "NoSchedule"  
  8. ### 容忍 tolerations 的 key 和要污点 taints 的 key 一致,且设置的 effect 也相同,不需要设置 value  
  9. ......  
  10. tolerations:  
  11. - key: "key"  
  12.   operator: "Exists"  
  13.   effect: "NoSchedule" 

Node 和 Pod 对于污点与容忍基本概念

概念

  •  一个 node 可以有多个污点;
  •  一个 pod 可以有多个容忍;
  •  kubernetes 执行多个污点和容忍方法类似于过滤器

如果一个 node 有多个污点,且 pod 上也有多个容忍,只要 pod 中容忍能包含 node 上设置的全部污点,就可以将 pod 调度到该 node 上。如果 pod 上设置的容忍不能够包含 node 上设置的全部污点,且 node 上剩下不能被包含的污点 effect 为 PreferNoSchedule,那么也可能会被调度到该节点。

注意

当 pod 总存在 容忍,首先 pod 会选择没有污点的节点,然后再次选择容忍污点的节点。

  •  如果 node 上带有污点 effect 为 NoSchedule,而 pod 上不带响应的容忍,kubernetes 就不会调度 pod 到这台 node 上。
  •  如果 Node 上带有污点 effect 为 PreferNoShedule,这时候 Kubernetes 会努力不要调度这个 Pod 到这个 Node 上。
  •  如果 Node 上带有污点 effect 为 NoExecute,这个已经在 Node 上运行的 Pod 会从 Node 上驱逐掉。没有运行在 Node 的 Pod 不能被调度到这个 Node 上。一般使用与当某个节点处于 NotReady 状态下,pod 迅速在其他正常节点启动。

Deployment 中设置容忍

在 kubernetes 中 deployment 设置容忍,示例如下:

  1. apiVersion: apps/vl  
  2. kind: Deployment  
  3. metadata:  
  4.   name: example  
  5. spec:  
  6.   replicas: 5  
  7.   template:  
  8.     spec:  
  9.       ......  
  10.       tolerations:  
  11.       - key: "key"  
  12.         operator: "Equal"  
  13.         value: "value"  
  14.         effect: "NoSchedule" 

设置容忍时间

正常情况下, 如果一个污点带有 effect=NoExecute 被添加到了这个 Node。那么不能容忍这个污点的所有 Pod 就会立即被踢掉。而带有容忍标签的 Pod 就不会踢掉。然而,一个带有 effect=Noexecute 的容忍可以指定一个 tolerationSeconds 来指定当这个污点被添加的时候在多长时间内不被踢掉。例如:

  1. tolerations:  
  2. - key: "key"  
  3.   operator: "Equal"  
  4.   value: "value"  
  5.   effect: "Noexecute"  
  6.   tolerationSeconds: 3600 

如果这个 Pod 已经在这个带污点且 effect 为 NoExecute 的 node 上。这个 pod 可以一直运行到 3600s 后再被踢掉。如果这时候 Node 的污点被移除了,这个 Pod 就不会被踢掉。

容忍示例

Operator 默认是 Equal,可设置为 Equal 与 Exists 两种,按这两种进行示例:

Operator 是 Exists

容忍任何污点

例如一个空的 key,将匹配所有的 key、value、effect。即容忍任何污点。

  1. tolerations:  
  2. - operator: "Exists" 

容忍某 key 值的污点

例如一个空的 effect,并且 key 不为空,那么将匹配所有与 key 相同的 effect:

  1. tolerations:  
  2. - key: "key"  
  3.   operator: "Exists" 

Operator 是 Equal

node 上有一个污点

Node 和 Pod 的 key 为 key1、value1 与 effect 相同则能调度:

  1. #污点  
  2. key1=value1:NoSchedule  
  3. #Pod设置  
  4. tolerations:  
  5. - key: "key1"  
  6.   operator: "Equal"  
  7.   value: "value1"  
  8.   effect: "NoSchedule" 

node 上有多个污点

Node 的污点的 key、value、effect 和 Pod 容忍都相同则能调度:

  1. ## 设置污点  
  2. key1=value1:NoSchedule  
  3. key2=value2:NoExecute  
  4. ## Pod设置容忍  
  5. tolerations:  
  6. - key: "key1"  
  7.   operator: "Equal"  
  8.   value: "value1"  
  9.   effect: "NoSchedule"  
  10. - key: "key2"  
  11.   operator: "Equal"  
  12.   value: "value2"  
  13.   effect: "NoExecute" 

Node 的污点和 Pod 的大部分都相同,不同的是 Node 污点 effect 为 PreferNoSchedule 的,可能会调度:

  1. ## 污点  
  2. key1=value1:NoSchedule  
  3. key2=value2:NoExecute  
  4. key3=value3:PreferNoSchedule  
  5. ## Pod设置容忍  
  6. tolerations:  
  7. - key: "key1"  
  8.   operator: "Equal"  
  9.   value: "value1"  
  10.   effect: "NoSchedule"  
  11. - key: "key2"  
  12.   operator: "Equal"  
  13.   value: "value2"  
  14.   effect: "NoExecute" 

Node 的污点和 Pod 的大部分都相同,不同的是 Node 污点 effect 为 NoSchedule 和 NoExecute 的,不会被调度:

  1. ## 污点  
  2. key1=value1:NoSchedule  
  3. key2=value2:NoExecute  
  4. key3=value3:PreferNoSchedule  
  5. ## Pod设置容忍  
  6. tolerations:  
  7. - key: "key1"  
  8.   operator: "Equal"  
  9.   value: "value1"  
  10.   effect: "NoSchedule"  
  11. - key: "key3"  
  12.   operator: "Equal"  
  13.   value: "value3"  
  14.   effect: "PreferNoSchedule" 

对比理解 Exists 和 Equal 之间的区别:

  •  Exists 是包含,Equal 是等于,Exists 使用范围更广,而 Equal 则是精准匹配。
  •  当污点中存在 NoExecute 时,而容忍中不存在 NoExecute 时,不会被调度到该节点。
  •  Exists 可以不写 value , 而 Equal 则一定要指定对应的 value

污点驱逐

在使用 kubernetes 时,会遇到 某个 node 节点明明已经 宕机了,查看 node 状态从 Ready 状态变为 NotReady 状态,但是 节点所在的 pod 却已经处于 running 状态,过了很长一段时间才会转为 Terminating 状态,这是为什么呢?

污点是对于 node 节点来讲,如果 node 节点 effect 设置为 NoExecute ,它会影响节点上已经运行的 Pod,如下所示:

  •  立即将没有匹配容忍的 pod 驱逐。
  •  设置容忍但是没有指定 tolerationSeconds 参数的,那么该容忍永久生效,不会被驱逐。
  •  设置容忍但是有指定 tolerationSeconds 参数的,那么在指定的时间内容忍有效,超过指定时间后将被剔除。(pod 默认设置,tolerationSeconds = 300s)

此外,当某些条件为 true 时,节点控制器会自动污染节点。内置以下污点:

key 注释
node.kubernetes.io/not-ready 节点尚未准备好。这对应于 NodeCondition Ready 为 false。
node.kubernetes.io/unreachable 无法从节点控制器访问节点。这对应于 NodeCondition Ready 为 Unknown。
node.kubernetes.io/out-of-disk 节点磁盘不足。
node.kubernetes.io/memory-pressure 节点有内存压力。
node.kubernetes.io/disk-pressure 节点有磁盘压力。
node.kubernetes.io/network-unavailable 节点的网络不可用。
node.kubernetes.io/unschedulable 节点不可调度。
node.cloudprovider.kubernetes.io/uninitialized 当 kubelet 从 "外部" 云提供程序开始时,此污点在节点上设置为将其标记为不可用。来自 cloud-controller-manager 的控制器初始化此节点后,kubelet 删除此污点。

通过上面知识的铺垫,当一个节点宕机时,kubernetes 集群会给它打上什么样的污点呢?

一个 Ready 状态的节点

  1. $ kubectl get node k8s-node02 -o go-template={{.spec.taints}}  
  2. <no value> 

一个 NotReady 状态的节点

  1. $ kubectl get node k8s-node02 -o go-template={{.spec.taints}}  
  2. [map[effect:NoSchedule key:node.kubernetes.io/unreachable timeAdded:2021-12-23T13:49:58Z] map[effect:NoExecute key:node.kubernetes.io/unreachable timeAdded:2021-12-23T13:50:03Z]] 

处于 NotReady 状态的节点被打上了下面两个污点:

  1. Taints:             node.kubernetes.io/unreachable:NoExecute  
  2.                     node.kubernetes.io/unreachable:NoSchedule 

接下来测试 kubernetes 集群会给 Pod 分配什么样的容忍。

  1. $ kubectl get po nginx-745b4df97d-mgdmp -o yaml  
  2. ...  
  3.   tolerations:  
  4.   - effect: NoExecute  
  5.     key: node.kubernetes.io/not-ready  
  6.     operator: Exists  
  7.     tolerationSeconds: 300  ## 300/60=5min  
  8.   - effect: NoExecute  
  9.     key: node.kubernetes.io/unreachable  
  10.     operator: Exists  
  11.     tolerationSeconds: 300 ## 300/60=5min  
  12. ... 

看到这里,Pod 的失效机制已经很明白了, 当 node 节点处于 NotReady 状态或者 unreachable 状态时,Pod 会容忍它 5 分钟,然后被驱逐。而这 5 分钟内就算 Pod 处于 running 状态,也是无法正常提供服务的。因此,可以在 yaml 清单中 手动指明 0 容忍,清单文件如下:

  1. apiVersion: apps/v1  
  2. kind: Deployment  
  3. metadata:  
  4.   labels:  
  5.     app: nginx  
  6.   name: nginx  
  7. spec:  
  8.   replicas: 4  
  9.   selector:  
  10.     matchLabels:  
  11.       app: nginx  
  12.   template:  
  13.     metadata:  
  14.       labels:  
  15.         app: nginx  
  16.     spec:  
  17.       tolerations:  
  18.       - effect: NoExecute  
  19.         key: node.kubernetes.io/not-ready  
  20.         operator: Exists  
  21.         tolerationSeconds: 0  
  22.       - effect: NoExecute  
  23.         key: node.kubernetes.io/unreachable  
  24.         operator: Exists  
  25.         tolerationSeconds: 0  
  26.       containers:  
  27.       - image: nginx:alpine  
  28.         name: nginx 

生成 Pod

  1. $ kubectl get po -o wide  
  2. NAME                    READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES  
  3. nginx-84f6f75c6-c76fm   1/1     Running   0          6s    10.244.3.16   k8s-node02   <none>           <none>  
  4. nginx-84f6f75c6-hsxq5   1/1     Running   0          6s    10.244.3.15   k8s-node02   <none>           <none>  
  5. nginx-84f6f75c6-wkt52   1/1     Running   0          6s    10.244.1.63   k8s-node01   <none>           <none>  
  6. nginx-84f6f75c6-xmkjs   1/1     Running   0          6s    10.244.3.17   k8s-node02   <none>           <none> 

接下来强制关闭 k8s-node02 节点,查看 Pod 是否转移。

  1. $ kubectl get po -o wide  
  2. NAME                    READY   STATUS        RESTARTS   AGE    IP            NODE         NOMINATED NODE   READINESS GATES  
  3. nginx-84f6f75c6-c76fm   1/1     Terminating   0          116s   10.244.3.16   k8s-node02   <none>           <none>  
  4. nginx-84f6f75c6-csqf4   1/1     Running       0          13s    10.244.1.66   k8s-node01   <none>           <none>  
  5. nginx-84f6f75c6-hsxq5   1/1     Terminating   0          116s   10.244.3.15   k8s-node02   <none>           <none>  
  6. nginx-84f6f75c6-r2v4p   1/1     Running       0          13s    10.244.1.64   k8s-node01   <none>           <none>  
  7. nginx-84f6f75c6-v4knq   1/1     Running       0          13s    10.244.1.65   k8s-node01   <none>           <none>  
  8. nginx-84f6f75c6-wkt52   1/1     Running       0          116s   10.244.1.63   k8s-node01   <none>           <none>  
  9. nginx-84f6f75c6-xmkjs   1/1     Terminating   0          116s   10.244.3.17   k8s-node02   <none>           <none> 

在 node 节点转为 NotReady 状态后,Pod 立刻进行了转移。这是通过 在 yaml 清单文件中明确指定 容忍时间。还可以直接修改 apiserver 配置来修改默认容忍时间。

  1. vim /etc/kubernetes/manifests/kube-apiserver.yaml  
  2. ...  
  3. spec:  
  4.   containers:  
  5.   - command:  
  6.     - kube-apiserver  
  7.     - --advertise-address=192.168.1.11  
  8.     - --default-not-ready-toleration-seconds=1    ## 新增行  
  9.     - --default-unreachable-toleration-seconds=1  ## 新增行  
  10. ... 

修改保存后, kube-apiserver-k8s-masterpod 会自动重载最新配置。

  1. $ kubectl get po nginx-84f6f75c6-wkt52 -o yaml  
  2. ...  
  3.   tolerations:  
  4.   - effect: NoExecute  
  5.     key: node.kubernetes.io/not-ready  
  6.     operator: Exists  
  7.     tolerationSeconds: 0  
  8.   - effect: NoExecute  
  9.     key: node.kubernetes.io/unreachable  
  10.     operator: Exists  
  11.     tolerationSeconds: 0  
  12. ... 

对于小型集群,可以直接设置全局变量。

注意:当 kubernetes 集群只有一个 node 节点时,无法做到 Pod 转移,因为 Pod 已经无路可退了。

相关内容