所谓的污点,是给k8s集群中的节点设置的,通过设置污点,来规划资源创建是所在的节点
污点的类型 | 解释说明 |
---|---|
PreferNoshedule | 节点设置这个污点类型后; 表示,该节点接收调度,但是会降低调度的概率 |
NoSheule | 表示,该节点不接收新的调度,以前有的资源,也依然存在 |
NoExecute | 表示,不接收信息的调度,驱逐以前的资源调度 |
根据节点设置污点
查看过滤数据的前后两行
[root@master deployment-demo]# kubectl describe nodes | grep -i taint -A 2 -B 2
volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp: Fri, 29 Mar 2024 16:03:58 +0800
Taints: node-role.kubernetes.io/master:NoSchedule
Unschedulable: false
Lease:
--
volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp: Fri, 29 Mar 2024 16:09:38 +0800
Taints:
Unschedulable: false
Lease:
--
volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp: Fri, 29 Mar 2024 16:39:55 +0800
Taints:
Unschedulable: false
Lease:
[root@master deployment-demo]# cat taint.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: taint-demo
spec:
replicas: 5
selector:
matchLabels:
k8s: dolphin
template:
metadata:
name: pod01
labels:
k8s: dolphin
spec:
containers:
- name: c1
image: nginx
ports:
- containerPort: 80[root@master deployment-demo]# kubectl apply -f taint.yaml
deployment.apps/taint-demo created
我们可以看到资源是随机分布的
创建污点有两种方式:
创建污点
[root@master deployment-demo]# kubectl taint node node1 dolphin:NoExecute
查看状态,全都被驱离到node2节点上了
[root@master deployment-demo]# kubectl taint node node1 dolphin:NoExecute-
node/node1 untainted我们看到删除pod,node1节点又能重新分配pod资源了
[root@master deployment-demo]# kubectl taint node node1 dolphin:NoExecute
[root@master deployment-demo]# kubectl taint node node1 dolphin2=123:NoExecute --overwrite
node/node1 modified
通过上文,我们知道可以通过给k8s集群节点设置不同类型的“污点”,来控制资源创建的节点范围;
那么,k8s也同时提供了“污点容忍”,就是即便你的节点设置了“污点”,我的资源也可以创建在这个节点上的能力;
假设:一个节点上有两个污点,但是你创建pod的时候,还想在这个节点上创建,那么你就需要在资源清单中,写如“容忍这两个污点”,才会创建成功
上边案例中我们已经创建了2个污点,且创建资源时,node1节点不允许分配资源
[root@master deployment-demo]# cat taint.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: taint-demo
spec:
replicas: 5
selector:
matchLabels:
k8s: dolphin
template:
metadata:
name: pod01
labels:
k8s: dolphin
spec:
# 设置污点容忍
tolerations:
- key: node-role.kubernetes.io/master # master节点的污点
effect: NoSchedule #指定污点类型
operator: Exists #只要匹配到key就满足这个条件
- key: dolphin
effect: NoExecute
operator: Equal
- key: dolphin2
value: "123"
effect: NoExecute
operator: Equal #key和value都要满足匹配条件
containers:
- name: c1
image: nginx
ports:
- containerPort: 80[root@master deployment-demo]# kubectl apply -f taint.yaml
deployment.apps/taint-demo created
通过以上污点容忍配置,我们可以看到三个节点都能分配pod资源了
[root@master deployment-demo]# cat taint.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: taint-demo
spec:
replicas: 5
selector:
matchLabels:
k8s: dolphin
template:
metadata:
name: pod01
labels:
k8s: dolphin
spec:
# 设置污点容忍
tolerations:# 不写污点key的相关属性表示匹配所有key
- operator: Exists
containers:
- name: c1
image: nginx
ports:
- containerPort: 80