Pod的调度
Pod是通过schedule决定调度到哪个节点上运行
调度的优先级排序:定向 > 污点(NoSchedule) > 亲和度 > 自动
NoExcute的限制很严格,级别最高
调度算法
预选算法(Predicates)
优选算法(Priorities)
自动调度-schedule
k8s中的schedule会根据资源情况将pod自动调度到可用的node节点上
定向调度-nodeName, nodeSelector
nodeName实现
将pod定向调度到某个节点上,通过设置nodeName:主机名进行特定节点调度
直接启pod
spec.nodeName
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
nodeNode: k8s-node-1
deploy启动pod
spec.template.spec.nodeName
appVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-wang
labels:
app: nginx-wang
spec:
replicas: 5
selector:
matchLabels:
app: nginx-wang
template:
metadata:
labels:
app: nginx-wang
spec:
nodeName: k8s-node-1
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
nodeSelector标签选择器实现
spec.nodeSelector
1. 给node打标签
kubectl label nodes node-name nodeenv=pro # 自定义标签
# 查看node节点的标签
kubectl describe node k8s-node-1
kubectl get node -o yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
nodeSelector:
nodeenv: pro # 只调度到特定标签的节点
使用deployment启pod类似操作
亲和度调度
亲和性(Affinity)是一种调度策略,用于控制 Pod优先调度到哪些节点或与哪些 Pod 部署在一起。它是对早期 nodeSelector
的增强,提供了更灵活的调度规则。
亲和性的种类
- 节点亲和性(Node Affinity):让 Pod优先调度到匹配标签的节点(例如带 GPU 的节点)。
- Pod 间亲和性(Pod Affinity):让 Pod优先与某些 Pod 部署在同一拓扑域(如同一节点、同一区域)。
- Pod 间反亲和性(Pod Anti-Affinity):让 Pod避免与某些 Pod 部署在同一拓扑域(如分散部署提高可用性)。
- 会话亲和性(Session Affinity)也称为粘性会话(Sticky Session),是一种负载均衡策略,用于确保来自同一个客户端的请求始终被路由到同一个后端 Pod。
pod间亲和性和反亲和性示例
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- redis
topologyKey: "kubernetes.io/hostname" # 同一节点
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- db
topologyKey: "kubernetes.io/hostname" # 不同节点
节点亲和性示例
apiVersion: v1
kind: Pod
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution: # 必须满足的规则
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
preferredDuringSchedulingIgnoredDuringExecution: # 优先满足的规则
- weight: 100 # 权重(1-100)
preference:
matchExpressions:
- key: region
operator: In
values:
- east
会话亲和性示例
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- port: 80
sessionAffinity: ClientIP # 基于客户端IP实现会话亲和性
sessionAffinityConfig:
clientIP:
timeoutSeconds: 10800 # 会话保持时间(秒)
污点和容忍度(taint toleration)
污点
污点是节点的一种属性,用于标记节点 “不欢迎” 某些 Pod。当节点被设置了污点后,默认情况下所有 Pod 都不能调度到该节点,除非 Pod 有对应的容忍度。
# kubectl taint nodes <node-name> <key>=<value>:<effect>
# 设置污点
kubectl taint nodes node1 dedicated=teamA:NoSchedule
# 去除污点
kubectl taint nodes node1 key:effect-
# 去除所有污点
kubectl taint nodes node1 key-
# effect可选
# NoSchedule:没有对应容忍度的 Pod不能被调度到该节点。
# PreferNoSchedule:系统会尽量避免调度没有容忍度的 Pod,但不是强制的。
# NoExecute:已在节点上运行的 Pod 如果没有容忍度,会被驱逐。
容忍度
容忍度是Pod 的一种属性,用于声明 Pod “可以容忍” 某些节点的污点,从而允许 Pod 调度到带有这些污点的节点上。
配置方式:在 Pod 的 YAML 中添加 tolerations
字段,例如:
apiVersion: v1
kind: Pod
spec:
tolerations:
- key: "dedicated"
operator: "Equal"
value: "teamA"
effect: "NoSchedule"
# key 和 value 需与节点的污点匹配。
# effect 需与污点的效果(如 NoSchedule)匹配。
# operator 可以是 Equal(等于)或 Exists(存在)。【Equal需要kv相等,Exists存在即可,不需要kv】
驱逐节点资源
-
在节点上打污点(Taint)
kubectl taint nodes <节点名称> node.kubernetes.io/unreachable:NoExecute-
-
使用
kubectl drain
(推荐方式)(不能再被调度到该节点上来)标记为不可调度kubectl drain <节点名称> --ignore-daemonsets --delete-emptydir-data kubectl drain node-1 --ignore-daemonsets --delete-emptydir-data # --ignore-daemonsets:跳过 DaemonSet 创建的 Pod(因为 DaemonSet 会自动重建,无需驱逐)。 # --delete-emptydir-data:删除与 Pod 关联的 emptydir 临时存储。 # --force:强制驱逐不属于任何 ReplicationController、Deployment 等控制器的 Pod。 # --grace-period=-1:立即终止 Pod(不推荐,可能导致数据丢失)。 # 重新恢复node能够被调度 kubectl uncordon node-name kubectl get node
-
手动标记为不可调度(cordon)
kubectl cordon <节点名称> # 标记节点为不可调度