美文网首页
k8s-实战入门-Pod详解(十五)

k8s-实战入门-Pod详解(十五)

作者: 漫长的白日梦技术大佬 | 来源:发表于2022-10-01 22:50 被阅读0次

污点和容忍

污点(Taints)

    前面的调度方式都是站在Pod的角度上,通过在Pod上添加属性,来确定Pod是否要调度到指定的Node上,其实我们也可以站在Node的角度上,通过在Node上添加污点属性,来决定是否允许Pod调度过来。

    Node被设置上污点之后就和Pod之间存在了一种相斥的关系,进而拒绝Pod调度进来,甚至可以将已经存在的Pod驱逐出去。

污点的格式为:key=value:effect, key和value是污点的标签,effect描述污点的作用,支持如下三个选项:

PreferNoSchedule:kubernetes将尽量避免把Pod调度到具有该污点的Node上,除非没有其他节点可调度

NoSchedule:kubernetes将不会把Pod调度到具有该污点的Node上,但不会影响当前Node上已存在的Pod

NoExecute:kubernetes将不会把Pod调度到具有该污点的Node上,同时也会将Node上已存在的Pod驱离

使用kubectl设置和去除污点的命令示例如下:

# 设置污点

kubectltaintnodesnode1key=value:effect

# 去除污点

kubectltaintnodesnode1key:effect-

# 去除所有污点

kubectltaintnodesnode1key-

接下来,演示下污点的效果:

准备节点node1(为了演示效果更加明显,暂时停止node2节点)

为node1节点设置一个污点: tag=heima:PreferNoSchedule;然后创建pod1( pod1 可以 )

修改为node1节点设置一个污点: tag=heima:NoSchedule;然后创建pod2( pod1 正常  pod2 失败 )

修改为node1节点设置一个污点: tag=heima:NoExecute;然后创建pod3 ( 3个pod都失败 )

# 为node1设置污点(PreferNoSchedule)

[root@master~]# kubectl taint nodes node1 tag=heima:PreferNoSchedule

# 创建pod1

[root@master~]# kubectl run taint1 --image=nginx:1.17.1 -n dev

[root@master~]# kubectl get pods -n dev -o wide

NAMEREADYSTATUSRESTARTSAGEIPNODE

taint1-7665f7fd85-574h41/1Running02m24s10.244.1.59node1

# 为node1设置污点(取消PreferNoSchedule,设置NoSchedule)

[root@master~]# kubectl taint nodes node1 tag:PreferNoSchedule-

[root@master~]# kubectl taint nodes node1 tag=heima:NoSchedule

# 创建pod2

[root@master~]# kubectl run taint2 --image=nginx:1.17.1 -n dev

[root@master~]# kubectl get pods taint2 -n dev -o wide

NAMEREADYSTATUSRESTARTSAGEIPNODE

taint1-7665f7fd85-574h41/1Running02m24s10.244.1.59node1

taint2-544694789-6zmlf0/1Pending021s<none><none>

# 为node1设置污点(取消NoSchedule,设置NoExecute)

[root@master~]# kubectl taint nodes node1 tag:NoSchedule-

[root@master~]# kubectl taint nodes node1 tag=heima:NoExecute

# 创建pod3

[root@master~]# kubectl run taint3 --image=nginx:1.17.1 -n dev

[root@master~]# kubectl get pods -n dev -o wide

NAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED

taint1-7665f7fd85-htkmp0/1Pending035s<none><none><none>

taint2-544694789-bn7wb0/1Pending035s<none><none><none>

taint3-6d78dbd749-tktkq0/1Pending06s<none><none><none>

小提示:

使用kubeadm搭建的集群,默认就会给master节点添加一个污点标记,所以pod就不会调度到master节点上.

容忍(Toleration)

    上面介绍了污点的作用,我们可以在node上添加污点用于拒绝pod调度上来,但是如果就是想将一个pod调度到一个有污点的node上去,这时候应该怎么做呢?这就要使用到容忍

污点就是拒绝,容忍就是忽略,Node通过污点拒绝pod调度上去,Pod通过容忍忽略拒绝

下面先通过一个案例看下效果:

上一小节,已经在node1节点上打上了NoExecute的污点,此时pod是调度不上去的

本小节,可以通过给pod添加容忍,然后将其调度上去

创建pod-toleration.yaml,内容如下

apiVersion: v1

kind: Pod

metadata:

  name: pod-toleration

  namespace: dev

spec:

  containers:

  - name: nginx

   image: nginx:1.17.1

  tolerations:      # 添加容忍

  - key: "tag"# 要容忍的污点的key

   operator: "Equal"# 操作符

   value: "heima"# 容忍的污点的value

   effect: "NoExecute"# 添加容忍的规则,这里必须和标记的污点规则相同

# 添加容忍之前的pod

[root@master~]# kubectl get pods -n dev -o wide

NAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED

pod-toleration0/1Pending03s<none><none><none>

# 添加容忍之后的pod

[root@master~]# kubectl get pods -n dev -o wide

NAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED

pod-toleration1/1Running03s10.244.1.62node1<none>

下面看一下容忍的详细配置:

[root@master ~]# kubectl explain pod.spec.tolerations

......

FIELDS:

  key       # 对应着要容忍的污点的键,空意味着匹配所有的键

  value     # 对应着要容忍的污点的值

  operator  # key-value的运算符,支持Equal和Exists(默认)

  effect    # 对应污点的effect,空意味着匹配所有影响

  tolerationSeconds   # 容忍时间, 当effect为NoExecute时生效,表示pod在Node上的停留时间

相关文章

网友评论

      本文标题:k8s-实战入门-Pod详解(十五)

      本文链接:https://www.haomeiwen.com/subject/agfjartx.html