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>
标签:Toleration,12,kubernetes,master,io,linux,k8s,com
From: https://blog.csdn.net/u010198709/article/details/140795773