8-k8s-污点与容忍

本文详细介绍了Kubernetes中的污点(taints)和容忍(tolerations)概念,包括它们的作用、在节点和Pod上的应用、以及NoSchedule和NoExecute的区别。通过实操演示了如何在节点上添加和移除污点,以及如何配置Pod的容忍度以决定是否调度到有污点的节点。
摘要由CSDN通过智能技术生成

一、概念

  1. 污点与容忍

    污点taints定义在节点之上的键值型属性数据。当节点被标记为有污点,那么意味着不允许pod调度到该节点。

    容忍tolerations是定义在 Pod对象上的键值型属性数据。被打上污点的节点,如果同时被pod标记为可以容忍污点的节点,则允许pod调度到该节点。。

    ps:在使用kubeadm部署的k8s集群的时候应该会发现,通常情况下,应用不会调度到master节点。因为默认给master节点加了污点。

    1)污点一般打在节点上,且一个节点可以配置使用多个污点

    2)容忍是标注在pod(资源控制器)上的。一个Pod(资源控制器)也可以有多个容忍度。启动pod一般不会调度在有污点的节点上,除非该pod标注了这些污点的容忍,才可以被调度。

  2. 污点配置类型

    1)Noschdule:如果Pod不能忍受这类污点,则该污点节点后续不会进行pod调度,已经创建的pod不会受到影响。
    2)NoExecute:如果Pod不能忍受这类污点,则该污点节点会马上驱除该节点上所有非kube-system空间的所有pod节点。

    3)PreferNoSchedule: NoSchedule的软约束,即该污点节点后续基本不会进行pod调度,但是无其他节点可供调度时,才允许调度到该节点。已经创建的pod不会受到影响。

二、相关操作

  1. 为节点打上污点:kubectl taint node worker1 key=value:NoSchedule

  2. 移除污点:kubectl taint node k8s-master key=value:Noschedule-

  3. 查看污点指令:kubectl taint -h

  4. pod配置容忍

    # pod的 spec下面配置容忍
    tolerations:
    - key: "污点的 key"
      value: "污点的value"
      offect: "NoSchedule" #污点产生的影响
      operator: "Equal" #1.Equal:pod和节点的key+value都要相等。2.Exists:pod和节点的key相等即可。
      tolerationSeconds: 时间(s)  #tolerationSeconds不设置,则Pod会一直在满足容忍的节点上一直执行。
    

三、实操污点NoSchedule

  1. 给worker1节点打上污点:kubectl taint no worker1 key1=value1:NoSchedule

  2. 查看节点: kubectl describe no worker1
    在这里插入图片描述

  3. 编写yaml清单:vi deployment-nginx.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: deployment-nginx     #Deployment 的名称
      labels:
        app: nginx
    spec:
      replicas: 3       # 创建 Pod 的副本数
      selector:         #定义 Deployment 如何找到要管理的 Pod,与 template 的 label(标签)对应
        matchLabels:
          app: nginx
      template:          #字段包含以下字段:
        metadata:
          labels:
            app: nginx    #使用 label(标签)标记 Pod
        spec:             #表示 Pod 运行一个名字为 nginx 的容器
          containers:
          - name: nginx
            image: nginx:1.15       #表示 Pod 运行一个名字为 nginx 的容器
            ports:                  #容器用于发送和接收流量的端口
            - containerPort: 80
    
  4. 创建:kubectl apply -f deployment-nginx.yaml

  5. 查看,发现worker1上面没有调度pod:kubectl get pod -o wide
    在这里插入图片描述

  6. 移除污点:kubectl taint no worker1 key1=value1:NoSchedule-

四、实操污点NoExecute

  1. 删除所有pod:kubectl delete -f deployment-nginx.yaml

  2. 重新部署:kubectl apply -f deployment-nginx.yaml

  3. 查看:kubectl get pod -o wide
    在这里插入图片描述

  4. 设置NoExecute:kubectl taint no worker1 key1=value1:NoExecute

  5. 查看节点:kubectl get pod -o wide

    ps:可以看到,除了kube-system空间以外的其他空间,所有worker1上的pod都被驱除
    在这里插入图片描述

五、实操容忍

  1. 在deployment-nginx上设置污点容忍,然后再次查看:vi deployment-nginx.yaml

    tolerations:
    - key: "key1"
      operator: "Equal"
      value: "value1"
      effect: "NoExecute"
    

    完整配置

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: deployment-nginx
      labels:
        app: nginx
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          tolerations:
          - key: "key1"
            operator: "Equal"
            value: "value1"
            effect: "NoExecute"
          containers:
          - name: nginx
            image: nginx:1.15
            ports:
            - containerPort: 80
    
  2. 更新:kubectl apply -f deployment-nginx.yaml

  3. 查看pod:kubectl get pod -o wide
    在这里插入图片描述

  4. 测试完还原,删除污点:kubectl taint no worker1 key1=value1:NoExecute-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值