介绍
Taints(污点)和 tolerations(容忍)在 Kubernetes 中用于指定哪些节点适合调度特定的 Pod。Taints 应用于节点以排斥 Pod,而 tolerations 应用于 Pod 以吸引它们到特定节点。在本实验中,我们将学习如何使用 taints 和 tolerations 在特定节点上调度 Pod。
Taints(污点)和 tolerations(容忍)在 Kubernetes 中用于指定哪些节点适合调度特定的 Pod。Taints 应用于节点以排斥 Pod,而 tolerations 应用于 Pod 以吸引它们到特定节点。在本实验中,我们将学习如何使用 taints 和 tolerations 在特定节点上调度 Pod。
在创建资源之前,你需要一个正在运行的 Kubernetes 集群。Minikube 是一个轻量级的 Kubernetes 环境,可以在你的本地机器上运行。
导航到工作目录:
打开终端并导航到默认的项目文件夹:
cd /home/labex/project
启动 Minikube:
启动 Minikube 以初始化一个 Kubernetes 集群:
minikube start
验证 Minikube 是否正在运行:
检查 Minikube 集群的状态:
minikube status
kubelet
和 apiserver
等组件是否显示为 Running
。minikube start
。如果启动 Minikube 时遇到问题,可以使用 minikube delete
重置环境(如有需要)。
在这一步骤中,我们将创建一个带有污点的节点,该污点将排斥某些 Pod。
kubectl label nodes minikube disk-type=ssd
kubectl taint nodes minikube disk-type=ssd:NoSchedule
NoSchedule
效果将阻止没有容忍配置的 Pod 被调度到该节点上。
在这一步骤中,我们将创建一个没有容忍配置的 Pod,并验证它无法被调度到带有污点的节点上。
pod-without-toleration.yaml
的文件,内容如下:apiVersion: v1
kind: Pod
metadata:
name: pod-without-toleration
spec:
containers:
- name: nginx
image: nginx:latest
kubectl apply -f pod-without-toleration.yaml
kubectl describe pod pod-without-toleration | grep -i taint
输出应显示 Pod 未被调度到带有污点的节点上。
在这一步骤中,我们将创建一个带有容忍配置的 Pod,使其能够被调度到带有污点的节点上。
pod-with-toleration.yaml
的文件,内容如下:apiVersion: v1
kind: Pod
metadata:
name: pod-with-toleration
spec:
containers:
- name: nginx
image: nginx:latest
tolerations:
- key: "disk-type"
operator: "Equal"
value: "ssd"
effect: "NoSchedule"
kubectl apply -f pod-with-toleration.yaml
kubectl get pod pod-with-toleration -o wide
在这一步骤中,我们将创建一个带有多个容忍配置的 Pod,使其能够被调度到带有多个污点的节点上。
pod-with-multiple-tolerations.yaml
的文件,内容如下:apiVersion: v1
kind: Pod
metadata:
name: pod-with-multiple-tolerations
spec:
containers:
- name: nginx
image: nginx:latest
tolerations:
- key: "disk-type"
operator: "Equal"
value: "ssd"
effect: "NoSchedule"
- key: "gpu"
operator: "Equal"
value: "true"
effect: "NoSchedule"
kubectl apply -f pod-with-multiple-tolerations.yaml
kubectl get pod pod-with-multiple-tolerations -o wide
在本实验中,我们学习了如何使用污点(taints)和容忍(tolerations)将 Pod 调度到特定节点上。我们首先创建了一个带有污点的节点,并验证了没有容忍配置的 Pod 无法被调度到该节点上。接着,我们创建了一个带有容忍配置的 Pod,并验证了它能够被调度到带有污点的节点上。最后,我们创建了一个带有多个容忍配置的 Pod,并验证了它能够被调度到带有多个污点的节点上。
污点和容忍是 Kubernetes 的强大功能,可用于确保某些工作负载仅被调度到特定节点上。