污點、容忍度
- 污點、容忍度
- 管理節點污點
- 把k8snode2當成是生產環境專用的,其他node是測試的
- 給k8snode1也打上污點
污點、容忍度
- 給了節點選則的主動權,我們給節點打一個污點,不容忍的pod就運行不上來,污點就是定義在節點上的鍵值屬性數據,可以定決定拒絕那些pod;
- taints是鍵值數據,用在節點上,定義污點;
- tolerations是鍵值數據,用在pod上,定義容忍度,能容忍哪些污點
- pod親和性是pod屬性;但是污點是節點的屬性,污點定義在k8s集群的節點上的一個字段
kubectl explain node.spec.taints
KIND: Node
VERSION: v1
RESOURCE: taints <[]Object>
DESCRIPTION:If specified, the node's taints.The node this Taint is attached to has the "effect" on any pod that doesnot tolerate the Taint.
FIELDS:effect <string> -required-key <string> -required-timeAdded <string>value <string>taints的effect用來定義對pod對象的排斥等級(效果):NoSchedule:
僅影響pod調度過程,當pod能容忍這個節點污點,就可以調度到當前節點,后來這個節點的污點改了,加了一個新的污點,使得之前調度的pod不能容忍了,那這個pod會怎么處理,對現存的pod對象不產生影響NoExecute:
既影響調度過程,又影響現存的pod對象,如果現存的pod不能容忍節點后來加的污點,這個pod就會被驅逐PreferNoSchedule:
最好不,也可以,是NoSchedule的柔性版本
查看master這個節點是否有污點,顯示如下:
kubectl describe nodes k8smaster1
Taints: node-role.kubernetes.io/control-plane:NoSchedule
上面可以看到master這個節點的污點是Noschedule
所以我們創建的pod都不會調度到master上,因為我們創建的pod沒有容忍度
kubectl describe pods kube-apiserver-k8smaster1 -n kube-system
顯示如下:
Tolerations: :NoExecute op=Exists
可以看到這個pod的容忍度是NoExecute,則可以調度到k8smaster1上
管理節點污點
kubectl taint –help
把k8snode2當成是生產環境專用的,其他node是測試的
給k8snode2打污點,pod如果不能容忍就不會調度過來
kubectl taint node k8snode2 node-type=production:NoSchedule
vim pod-taint.yaml
apiVersion: v1
kind: Pod
metadata:name: taint-podnamespace: defaultlabels:tomcat: tomcat-pod
spec:containers:- name: taint-podports:- containerPort: 8080image: tomcat:8.5-jre8-alpine
imagePullPolicy: IfNotPresent
kubectl apply -f pod-taint.yaml
kubectl get pods -o wide
顯示如下:
taint-pod running k8snode1
可以看到都被調度到k8snode1上了,因為k8snode2這個節點打了污點,而我們在創建pod的時候沒有容忍度,所以k8snode2上不會有pod調度上去的
給k8snode1也打上污點
kubectl taint node k8snode1 node-type=dev:NoExecute
kubectl get pods -o wide
顯示如下:可以看到已經存在的pod節點都被攆走了
taint-pod termaitering
vim pod-demo-1.yaml
apiVersion: v1
kind: Pod
metadata:name: myapp-deploynamespace: defaultlabels:app: myapprelease: canary
spec:containers:- name: myappimage: ikubernetes/myapp:v1imagePullPolicy: IfNotPresentports:- name: httpcontainerPort: 80tolerations:- key: "node-type"operator: "Equal"value: "production"effect: "NoExecute"tolerationSeconds: 3600
kubectl apply -f pod-demo-1.yaml
kubectl get pods
myapp-deploy 1/1 Pending 0 11s k8snode2
還是顯示pending,因為我們使用的是equal(等值匹配),所以key和value,effect必須和node節點定義的污點完全匹配才可以,把上面配置effect: "NoExecute"變成effect: “NoSchedule”;
tolerationSeconds: 3600這行去掉
修改后重新生成pod
kubectl delete -f pod-demo-1.yaml
kubectl apply -f pod-demo-1.yaml
kubectl get pods
myapp-deploy 1/1 running 0 11s k8snode2
上面就可以調度到k8snode2上了,因為在pod中定義的容忍度能容忍node節點上的污點
刪除污點:
kubectl taint nodes xianchaonode1 node-type:NoExecute-
kubectl taint nodes xianchaonode2 node-type-