6. Pod调度之nodeSelector
1. 作用
nodeSelector
是 Kubernetes 的一种简单的节点调度策略,通过基于节点的标签来调度 Pod。每个节点可以拥有多个标签,nodeSelector
用来选择具有特定标签的节点。
2. 实战案例
2.1 给节点打标签
在这个案例中,我们给所有节点都打上了一个 school=oldboyedu
的标签,并且将 worker232
节点的标签移除。
[root@master231 scheduler]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master231 Ready control-plane,master 8d v1.23.17 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master231,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
worker232 Ready <none> 8d v1.23.17 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=worker232,kubernetes.io/os=linux
worker233 Ready <none> 8d v1.23.17 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=worker233,kubernetes.io/os=linux
然后我们给所有节点打上 school=oldboyedu
的标签:
kubectl label nodes --all school=oldboyedu
并且移除 worker232
节点的标签:
kubectl label nodes worker232 school-
2.2 使用 nodeSelector
调度 Pod 到特定节点
使用 nodeSelector
指定 Pod 只会调度到那些标签为 school=oldboyedu
的节点上。以下是 YAML 配置文件:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: scheduler-nodeselector
spec:
selector:
matchLabels:
apps: xiuxian
template:
metadata:
labels:
apps: xiuxian
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
nodeSelector:
school: oldboyedu # 设置Pod调度到带有指定标签的节点
containers:
- name: xiuxian
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
应用这个配置:
kubectl apply -f 09-scheduler-nodeSelector.yaml
2.3 查看 Pod 状态
使用 kubectl get pods -o wide
查看 Pod 是否成功调度到预期的节点:
kubectl get pods -o wide
7. Pod调度之nodeAffinity
1. 作用
nodeAffinity
是比 nodeSelector
更强大的节点调度策略。它不仅支持精确的匹配(硬限制),还支持多个条件匹配(软限制),让 Pod 调度更具灵活性。
nodeSelector
是硬限制,要求所有条件必须完全匹配,否则 Pod 不会被调度。nodeAffinity
允许软限制,可以根据权重调整 Pod 的调度优先级。
2. 实战案例使用 nodeAffinity
实现 nodeSelector
在这个示例中,使用 nodeAffinity
实现了基于 school
标签的 Pod 调度。
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: scheduler-nodeaffinity
spec:
selector:
matchLabels:
apps: xiuxian
template:
metadata:
labels:
apps: xiuxian
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: school
values:
- "oldboyedu"
operator: In
containers:
- name: xiuxian
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
应用该配置:
kubectl apply -f 10-scheduler-nodeAffinity.yaml
3. 实战案例使用 nodeAffinity
实现多值匹配
通过修改节点标签,增加多个匹配的值,Pod 可以调度到多个符合条件的节点。
kubectl label nodes worker233 school=laonanhai --overwrite
修改配置文件,将 nodeAffinity
中的 values
更改为多个值:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: school
values:
- "oldboyedu"
- "laonanhai"
operator: In
应用该配置:
kubectl apply -f 11-scheduler-deploy-nodeAffinity.yaml
8. Pod调度之podAffinity
podAffinity
是 Pod 的亲和性配置,允许你将 Pod 调度到某些具有特定标签的节点上。
1. 给节点打标签
kubectl label nodes master231 dc=jiuxianqiao
kubectl label nodes worker232 dc=lugu
kubectl label nodes worker233 dc=zhaowei
2. 配置 Pod 的亲和性
apiVersion: apps/v1
kind: Deployment
metadata:
name: scheduler-podaffinity
spec:
replicas: 5
selector:
matchLabels:
apps: xiuxian
template:
metadata:
labels:
apps: xiuxian
version: v1
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- topologyKey: dc
labelSelector:
matchLabels:
apps: xiuxian
containers:
- name: c1
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2
resources:
requests:
cpu: 0.5
memory: 200Mi
limits:
cpu: 4
memory: 8Gi
3. 应用配置并查看状态
kubectl apply -f 13-scheduler-podAffinity.yaml
kubectl get pods -o wide
9. Pod调度之podAntiAffinity
podAntiAffinity
是 Pod 的反亲和性配置,指的是防止 Pod 调度到某些已经有特定标签的节点上。
1. 实战案例
apiVersion: apps/v1
kind: Deployment
metadata:
name: scheduler-podantiaffinity
spec:
replicas: 5
selector:
matchLabels:
apps: xiuxian
template:
metadata:
labels:
apps: xiuxian
version: v1
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- topologyKey: dc
labelSelector:
matchExpressions:
- key: apps
values: ["xiuxian"]
operator: In
containers:
- name: c1
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2
resources:
requests:
cpu: 0.5
memory: 200Mi
limits:
cpu: 4
memory: 8Gi
2. 应用配置并查看状态
kubectl apply -f 14-scheduler-podAntiAffinity.yaml
kubectl get pods -o wide
总结
- Node Selector 是一个简单的节点调度机制,通过标签选择节点。
- Node Affinity 提供了比
nodeSelector
更灵活的调度策略,可以实现软限制和硬限制。 - Pod Affinity 和 Pod Anti-Affinity 用于控制 Pod 与其他 Pod 的调度关系,前者强调同类 Pod 的亲和性,后者则避免 Pod 跟特定的 Pod 一起调度到同一个节点。