• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
gys001
博客园    首页    新随笔    联系   管理    订阅  订阅

Pod详解之Pod调度(污点和容忍)

污点和容忍

污点(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驱离

image-20200605021606508

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

# 设置污点
kubectl taint nodes node1 key=value:effect

# 去除污点
kubectl taint nodes node1 key:effect-

# 去除所有污点
kubectl taint nodes node1 key-
查询所有节点的污点:
wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
chmod +x ./jq
cp jq /usr/bin

列出所有节点的污点
kubectl get nodes -o json | jq '.items[].spec'
或
kubectl get nodes -o json | jq '.items[].spec.taints'

查看指定节点上的污点
kubectl describe node 节点名称
案例

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

1. 准备节点node1(为了演示效果更加明显,暂时停止node2节点,可直接关闭node2的虚拟机)
2. 为node1节点设置一个污点: `tag=heima:PreferNoSchedule`;然后创建pod1( 结果:pod1 可以 )
3. 修改为node1节点设置一个污点: `tag=heima:NoSchedule`;然后创建pod2(结果: pod1 正常 pod2 失败 )
4. 修改为node1节点设置一个污点: `tag=heima:NoExecute`;然后创建pod3 ( 结果:3个pod都失败 )
kubectl cordon NODE [options] 将节点标记为不可调度。

kubectl uncordon NODE [options]

先关停node2(直接关机)

# 为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 taint1

# 为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

注:此时taint2状态为Pending,taint1状态仍为Running
# 为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 taint1 taint2 taint3

注:此时taint1 taint2均消失, taint3状态为Pending

查看报错信息
[root@master ~]# kubectl describe pod taint3 -n dev

查看此时node1的污点
[root@master ~]# kubectl describe node node1 | grep -i taint

小提示:

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

容忍(Toleration)

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

image-20200514095913741

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

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

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

2. 本小节,可以通过给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"   # 添加容忍的规则,这里必须和标记的污点规则相同

注:此时容忍 匹配的只是node1的污点
# 添加容忍之前的pod
[root@master ~]# kubectl get pods -n dev -o wide
[root@master ~]# vim pod-toleration.yaml
[root@master ~]# kubectl create -f pod-toleration.yaml

# 添加容忍之后的pod

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

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

[root@master ~]# kubectl explain pod.spec.tolerations
FIELDS:
   key       # 对应着要容忍的污点的键,空意味着匹配所有的键
   value     # 对应着要容忍的污点的值
   operator  # key-value的运算符,支持Equal和Exists(默认)

注:Exists只作用在key上,若是Exists,则不用配置value     

   effect    # 对应污点的effect,空意味着匹配所有影响
   tolerationSeconds   # 容忍时间, 当effect为NoExecute时生效,表示pod在Node上的停留时间(一般不做配置)

参考

黑马B站k8s课程https://www.bilibili.com/video/BV1Qv41167ck/
https://gitee.com/yooome/golang/blob/main/k8s%E8%AF%A6%E7%BB%86%E6%95%99%E7%A8%8B-%E8%B0%83%E6%95%B4%E7%89%88/k8s%E8%AF%A6%E7%BB%86%E6%95%99%E7%A8%8B.md
https://www.yuque.com/fairy-era/yg511q/xyqxge

 

posted @ 2022-11-17 17:56  gys001  阅读(603)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3