Taints: 避免Pod调度到特定Node上
Tolerations: 允许Pod调度到持有Taints的Node上
下面是一个简单的示例:在 node1 上加一个 Taint,该 Taint 的键为 key,值为 value,Taint 的效果是 NoSchedule。这意味着除非 pod 明确声明可以容忍这个 Taint,否则就不会被调度到 node1 上
kubectl taint nodes node1 key=value:NoSchedule
然后需要在 pod 上声明 Toleration。下面的 Toleration 设置为可以容忍具有该 Taint 的 Node,使得 pod 能够被调度到 node1 上:
apiVersion: v1
kind: Pod
metadata:
name: pod-taints
spec:
tolerations:
\- key: "key"
operator: "Equal"
value:"value" effect: "NoSchedule"
containers:
- name: pod-taints
image: busybox:latest
也可以写成如下:
tolerations: \- key: "key" operator: "Exists" effect: "NoSchedule"
参考之前文章,腾讯云上配置一主两从,主机master,从机node1和node2
kubectl taint node node2 type=calculate:NoSchedule
kubectl describe nodes node2 | grep Taints
apiVersion: apps/v1
kind: Deployment
metadata:
name: taint-deploy
spec:
replicas: 3
selector:
matchLabels:
app: taint-pod
template:
metadata:
labels:
app: taint-pod
spec:
containers:
- image: busybox:latest
name: taint-pod
command: [ "/bin/sh", "-c", "tail -f /etc/passwd" ]
[root@master demo]# kubectl apply -f taint-pod.yaml
deployment.apps/taint-deploy created
[root@master demo]# kubectl get pods -o wide | grep taint
taint-deploy-69f9b6874c-2pbkd 1/1 Running 0 8m22s 10.40.0.4 node1 <none> <none>
taint-deploy-69f9b6874c-h6kpj 1/1 Running 0 8m22s 10.40.0.3 node1 <none> <none>
taint-deploy-69f9b6874c-qblnx 1/1 Running 0 8m22s 10.40.0.5 node1 <none> <none>
[root@master demo]# kubectl scale --replicas=9 deploy/taint-deploy -n default
deployment.apps/taint-deploy scaled
[root@master demo]# kubectl get pods -o wide | grep taint
taint-deploy-69f9b6874c-2pbkd 1/1 Running 0 11m 10.40.0.4 node1 <none> <none>
taint-deploy-69f9b6874c-5j8rx 0/1 ContainerCreating 0 14s <none> node1 <none> <none>
taint-deploy-69f9b6874c-5ws25 0/1 ContainerCreating 0 14s <none> node1 <none> <none>
taint-deploy-69f9b6874c-f7lck 0/1 ContainerCreating 0 14s <none> node1 <none> <none>
taint-deploy-69f9b6874c-h6kpj 1/1 Running 0 11m 10.40.0.3 node1 <none> <none>
taint-deploy-69f9b6874c-l686n 0/1 ContainerCreating 0 14s <none> node1 <none> <none>
taint-deploy-69f9b6874c-qblnx 1/1 Running 0 11m 10.40.0.5 node1 <none> <none>
taint-deploy-69f9b6874c-r2nln 0/1 ContainerCreating 0 14s <none> node1 <none> <none>
taint-deploy-69f9b6874c-vjbqq 0/1 ContainerCreating 0 14s <none> node1 <none> <none>
[root@master demo]# kubectl scale --replicas=1 deploy/taint-deploy -n default
[root@master demo]# kubectl get pods -o wide | grep taint
taint-deploy-69f9b6874c-2pbkd 1/1 Running 0 24m 10.40.0.4 node1 <none> <none>
[root@master demo]# kubectl scale --replicas=9 deploy/taint-deploy -n default
deployment.apps/taint-deploy scaled
[root@master demo]# kubectl get pods -o wide | grep taint
taint-deploy-69f9b6874c-2pbkd 1/1 Running 0 38m 10.40.0.4 node1 <none> <none>
taint-deploy-69f9b6874c-8cq67 1/1 Running 0 8m29s 10.32.0.6 node2 <none> <none>
taint-deploy-69f9b6874c-bskp5 1/1 Running 0 8m29s 10.32.0.7 node2 <none> <none>
taint-deploy-69f9b6874c-gbv66 1/1 Running 0 8m29s 10.40.0.6 node1 <none> <none>
taint-deploy-69f9b6874c-jrz79 1/1 Running 0 8m29s 10.40.0.5 node1 <none> <none>
taint-deploy-69f9b6874c-k7cvp 1/1 Running 0 8m29s 10.40.0.7 node1 <none> <none>
taint-deploy-69f9b6874c-pj6d9 1/1 Running 0 8m29s 10.32.0.4 node2 <none> <none>
taint-deploy-69f9b6874c-rqw8k 1/1 Running 0 8m29s 10.32.0.5 node2 <none> <none>
taint-deploy-69f9b6874c-zdtxz 1/1 Running 0 8m29s 10.40.0.3 node1 <none> <none>
kubectl label nodes <your-node-name> disktype=ssd
其中 <your-node-name>
是你所选节点的名称。
disktype=ssd
标签:kubectl get nodes --show-labels
输出类似于此:
NAME STATUS ROLES AGE VERSION LABELS
worker0 Ready <none> 1d v1.13.0 ...,disktype=ssd,kubernetes.io/hostname=worker0
worker1 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=worker1
worker2 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=
下面清单描述了一个 Pod,它有一个节点亲和性配置 requiredDuringSchedulingIgnoredDuringExecution
, disktype=ssd
。 这意味着 pod 只会被调度到具有 disktype=ssd
标签的节点上。
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
kubectl apply -f https://k8s.io/examples/pods/pod-nginx-required-affinity.yaml
kubectl get pods --output=wide
输出类似于此:
NAME READY STATUS RESTARTS AGE IP NODE
nginx 1/1 Running 0 13s 10.200.0.4 worker0
本清单描述了一个Pod,它有一个节点亲和性设置 preferredDuringSchedulingIgnoredDuringExecution
, disktype: ssd
。 这意味着 pod 将首选具有 disktype=ssd
标签的节点。
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
kubectl apply -f https://k8s.io/examples/pods/pod-nginx-preferred-affinity.yaml
kubectl get pods --output=wide
输出类似于此:
NAME READY STATUS RESTARTS AGE IP NODE
nginx 1/1 Running 0 13s 10.200.0.4 worker0
kubectl label nodes <your-node-name> disktype=ssd
其中 <your-node-name>
是你所选节点的名称。
disktype=ssd
标签:kubectl get nodes --show-labels
输出类似于此:
NAME STATUS ROLES AGE VERSION LABELS
worker0 Ready <none> 1d v1.13.0 ...,disktype=ssd,kubernetes.io/hostname=worker0
worker1 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=worker1
worker2 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=
下面清单描述了一个 Pod,它有一个节点亲和性配置 requiredDuringSchedulingIgnoredDuringExecution
, disktype=ssd
。 这意味着 pod 只会被调度到具有 disktype=ssd
标签的节点上。
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
kubectl apply -f https://k8s.io/examples/pods/pod-nginx-required-affinity.yaml
kubectl get pods --output=wide
输出类似于此:
NAME READY STATUS RESTARTS AGE IP NODE
nginx 1/1 Running 0 13s 10.200.0.4 worker0
本清单描述了一个Pod,它有一个节点亲和性设置 preferredDuringSchedulingIgnoredDuringExecution
, disktype: ssd
。 这意味着 pod 将首选具有 disktype=ssd
标签的节点。
pods/pod-nginx-preferred-affinity.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
kubectl apply -f https://k8s.io/examples/pods/pod-nginx-preferred-affinity.yaml
输出类似于此:
NAME READY STATUS RESTARTS AGE IP NODE
nginx 1/1 Running 0 13s 10.200.0.4 worker0
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有