容忍和污點Taint和Toleration設計理念:
Taint在一類服務器上打上污點,讓不能容忍這個污點的Pod不能部署在打了污點的服務器上。(鎖)
Toleration是讓Pod容忍節點上配置的污點,可以讓一些需要特殊配置的Pod能夠調用到具有污點和特殊配置的節點上。(鑰匙)
官方文檔:https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/
Taint命令示例
NoSchedule:禁止調度到該節點,已經在該節點上的Pod不受影響
NoExecute:禁止調度到該節點,如果不符合這個污點,會立馬被驅逐(或在一段時間后驅逐,默認300s,本文章后面可單獨設置驅逐時間)
PreferNoSchedule:盡量避免將Pod調度到指定的節點上,如果沒有更合適的節點,可以部署到該節點
創建一個污點(一個節點可以有多個污點):
kubectl taint nodes NODE_NAME TAINT_KEY=TAINT_VALUE:EFFECT
比如:
kubectl taint nodes k8s-node01 ssd=true:PreferNoSchedule
查看一個節點的污點:
kubectl get node k8s-node01 -o go-template --template {{.spec.taints}}
kubectl describe node k8s-node01 | grep Taints -A 10
刪除污點(和label類似):
基于Key刪除: kubectl taint nodes k8s-node01 ssd-
基于Key+Effect刪除: kubectl taint nodes k8s-node01 ssd:PreferNoSchedule-
修改污點(Key和Effect相同):
kubectl taint nodes k8s-node01 ssd=true:PreferNoSchedule --overwrite
Toleration配置解析
示例1(完全匹配)
key和value必須一致,且Taint為NoSchedule才被匹配
Equal:等于
tolerations:
- key: "taintKey"
operator: "Equal"
value: "taintValue"
effect: "NoSchedule"
示例2(不完全匹配)
所有包含這個key的node,且Taint為NoSchedule都有可能被匹配
Exists:包含
tolerations:
- key: "taintKey"
operator: "Exists"
effect: "NoSchedule"
示例3(大范圍匹配)
不能使用key為內置Taint
Exists:包含
tolerations:
- key: "taintKey"
operator: "Exists"
Taint和Toleration使用案例
需求:Node01是純SSD硬盤的節點,把需要高性能存儲的Pod調度到該節點上。
1、給節點打上污點和標簽
kubectl taint nodes k8s-node01 ssd=true:NoExecute(此時會驅逐沒有容忍該污點的Pod)
kubectl taint nodes k8s-node01 ssd=true:NoSchedule(禁止所有pod調度到該節點)
kubectl label node k8s-node01 ssd=true(給node01打標簽)
2、配置pod
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
ssd: "true"
tolerations:
- key: "ssd"
operator: "Exists" # 包含key為ssd的所有節點
k8s內置污點
內置污點解析
node.kubernetes.io/not-ready:節點未準備好,相當于節點狀態Ready的值為False。
node.kubernetes.io/unreachable:Node Controller訪問不到節點,相當于節點狀態Ready的值為Unknown。node.kubernetes.io/out-of-disk:節點磁盤耗盡。
node.kubernetes.io/memory-pressure:節點存在內存壓力。
node.kubernetes.io/disk-pressure:節點存在磁盤壓力。
node.kubernetes.io/network-unavailable:節點網絡不可達。
node.kubernetes.io/unschedulable:節點不可調度。
node.cloudprovider.kubernetes.io/uninitialized:如果Kubelet啟動時指定了一個外部的cloudprovider,它將給當前節點添加一個Taint將其標記為不可用。在cloud-controller-manager的一個controller初始化這個節點后,Kubelet將刪除這個Taint。
如節點不健康,6000秒后再驅逐(默認是300秒):
tolerations:
- key: "node.kubernetes.io/unreachable"
operator: "Exists"
effect: "NoExecute"
tolerationSeconds: 6000
Taint&Toleration利用k8s內置污點,確保節點宕機后快速恢復業務應用(生產建議使用)
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: tolerations-second
name: tolerations-second
spec:
replicas: 1
selector:
matchLabels:
app: tolerations-second
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: tolerations-second
spec:
containers:
- image: registry.cn-beijing.aliyuncs.com/dotbalo/nginx
name: nginx
resources:
requests:
cpu: 10m
nodeSelector:
ssd: "true"
tolerations:
- key: ssd
operator: Equal
value: "true"
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 10
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 10