12. kubernetes调度——污点Taint和容忍Toleration

一、通过节点属性调度

1、节点名称

[root@k8s-master ~]# kubectl get nodes
NAME                   STATUS   ROLES           AGE    VERSION
k8s-master.linux.com   Ready    control-plane   127d   v1.29.1
k8s-node01.linux.com   Ready    <none>          127d   v1.29.1
k8s-node02.linux.com   Ready    <none>          127d   v1.29.1
apiVersion: apps/v1
kind: Deployment
metadata:
    name: test1
spec:
    replicas: 2
    selector:
        matchLabels:
            app: test1
    template:
        metadata:
            labels:
                app: test1
        spec:
            nodeName: k8s-node02.linux.com					// 指定工作节点名称
            containers:
            - name: test1
              image: centos:7
              imagePullPolicy: IfNotPresent
              command:
              - sleep
              - "3600"
[root@k8s-master schedulerTest]# kubectl create -f test1.yaml 
deployment.apps/test1 created
[root@k8s-master schedulerTest]# 
[root@k8s-master schedulerTest]# kubectl get pod -o wide
NAME                         READY   STATUS                   RESTARTS   AGE   IP              NODE                   NOMINATED NODE   READINESS GATES
test1-d69854cd5-jgpvm        1/1     Running                  0          7s    10.88.242.139   k8s-node02.linux.com   <none>           <none>
test1-d69854cd5-tzx6l        1/1     Running                  0          7s    10.88.242.138   k8s-node02.linux.com   <none>           <none>

2、节点标签

2.1 查看节点标签

[root@k8s-master schedulerTest]# kubectl get nodes --show-labels 
NAME                   STATUS   ROLES           AGE    VERSION   LABELS
k8s-master.linux.com   Ready    control-plane   127d   v1.29.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master.linux.com,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node01.linux.com   Ready    <none>          127d   v1.29.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01.linux.com,kubernetes.io/os=linux
k8s-node02.linux.com   Ready    <none>          127d   v1.29.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02.linux.com,kubernetes.io/os=linux

2.2 添加标签

[root@k8s-master schedulerTest]# kubectl label node k8s-node01.linux.com disk=ssd 
node/k8s-node01.linux.com labeled
[root@k8s-master schedulerTest]# kubectl get nodes --show-labels
NAME                   STATUS   ROLES           AGE    VERSION   LABELS
k8s-master.linux.com   Ready    control-plane   127d   v1.29.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master.linux.com,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node01.linux.com   Ready    <none>          127d   v1.29.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disk=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01.linux.com,kubernetes.io/os=linux
k8s-node02.linux.com   Ready    <none>          127d   v1.29.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02.linux.com,kubernetes.io/os=linux

2.3 修改标签

[root@k8s-master schedulerTest]# kubectl label node k8s-node01.linux.com disk=full --overwrite 
node/k8s-node01.linux.com labeled
[root@k8s-master schedulerTest]# kubectl get node --show-labels
NAME                   STATUS   ROLES           AGE    VERSION   LABELS
k8s-master.linux.com   Ready    control-plane   127d   v1.29.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master.linux.com,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node01.linux.com   Ready    <none>          127d   v1.29.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disk=full,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01.linux.com,kubernetes.io/os=linux
k8s-node02.linux.com   Ready    <none>          127d   v1.29.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02.linux.com,kubernetes.io/os=linux
[root@k8s-master schedulerTest]# 

2.4 删除标签

[root@k8s-master schedulerTest]# kubectl label node k8s-node01.linux.com disk-
node/k8s-node01.linux.com unlabeled

2.5 通过节点标签进行调度

apiVersion: apps/v1
kind: Deployment
metadata:
    name: test2
spec:
    replicas: 2
    selector:
        matchLabels:
            app: test2
    template:
        metadata:
            labels:
                app: test2
        spec:
            nodeSelector:								// 节点标签 
                ram: higher
            containers:
            - name: test2
              image: centos:7
              imagePullPolicy: IfNotPresent
              command:
              - sleep
              - "3600"

二、污点Taint和容忍Toleration

污点、容忍配合使用,避免pod被分配不合适的机器

1、污点Taint

污点,本质上就是个key-value

1.1 查看Master节点的污点

[root@k8s-master schedulerTest]# kubectl describe node k8s-master.linux.com | grep -i taint
Taints:             node-role.kubernetes.io/control-plane:NoSchedule

1.2 添加污点

格式:

# kubectl taint node <节点名称> key=value:{NoSchedule|NoExecute|PreferNoSchedule}

污点策略:

  • NoSchedule
    新建的POD不会再向该节点调度
    已经运行在该节点的POD不会受影响

  • NoExecute
    新建的POD不会再向该节点调度
    已经运行在该节点的POD同时也会被驱逐

  • PreferNoSchedule
    尽量不向该节点调度新建的POD

[root@k8s-master schedulerTest]# kubectl taint node k8s-node02.linux.com fan=error:NoExecute 
node/k8s-node02.linux.com tainted

[root@k8s-master schedulerTest]# kubectl describe node k8s-node02.linux.com | grep -i taint
Taints:             fan=error:NoExecute

1.3 删除污点

格式:

# kubectl taint node <节点名称> key:{NoSchedule|NoExecute|PreferNoSchedule}-

2、容忍Toleration

apiVersion: apps/v1
kind: Deployment
metadata:
    name: test5
spec:
    replicas: 2
    selector:
        matchLabels:
            app: test5
    template:
        metadata:
            labels:
                app: test5
        spec:
            containers:
            - name: test5
              image: centos:7
              imagePullPolicy: IfNotPresent
              command:
              - sleep
              - "3600"
            tolerations:									// 容忍fan=error这个污点
              - key: "fan"
                operator: "Equal"
                value: "error"
                effect: NoExecute
[root@k8s-master ~]# kubectl get pod -o wide
NAME                         READY   STATUS                   RESTARTS   AGE         IP              NODE                   NOMINATED NODE   READINESS GATES
test5-7575ffc867-hs9hf       1/1     Running                  0          2m1s        10.88.242.129   k8s-node02.linux.com   <none>           <none>
test5-7575ffc867-lp4k2       1/1     Running                  0          2m1s        10.88.201.193   k8s-node01.linux.com   <none>           <none>

  • 20
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误提示是因为 Kubernetes 集群中的节点出现污点 (Taint)。污点是指节点上的一些标记,用于表明节点的一些特属性或状态,例如节点上硬盘容量不足。当一个 Pod 需要调度到某个节点上时,Kubernetes 会根据 Pod 的需求和节点的状态来判断是否可以将其调度到该节点上。如果节点上的污点与 Pod 的需求不匹配,那么该节点就会被排除在调度的范围之外,从而导致 Pod 调度失败。 根据这个错误提示,您的 Kubernetes 集群中有两个节点出现了污点:一个是控制平面节点上的 node-role.kubernetes.io/control-plane 污点,另一个是某个节点上的 node.kubernetes.io/disk-pressure 污点。其中,node-role.kubernetes.io/control-plane 污点用于标记控制平面节点,通常不应该将其他类型的 Pod 调度在控制平面节点上。而 node.kubernetes.io/disk-pressure 污点则表示该节点上的硬盘容量不足。 为了解决这个问题,您可以采取以下几个步骤: 1. 查看节点状态:使用 kubectl get nodes 命令查看节点的状态,确认哪些节点上出现了污点。 2. 解除污点:使用 kubectl taint nodes 命令解除节点上的污点,例如:kubectl taint nodes node1 node-role.kubernetes.io/control-plane=:NoSchedule 可以解除节点 node1 上的控制平面污点,从而允许其他类型的 Pod 调度到该节点上。 3. 节点维护:如果污点是由于节点上的硬盘容量不足导致的,那么您可以考虑对该节点进行维护,扩容硬盘容量或清理无用数据。在进行节点维护之前,需要使用 kubectl drain 命令将该节点上的 Pod 调度到其他节点上,避免数据丢失或服务中断。 希望这些信息能够帮助您解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值