1 常见的Pod调度方式:
1 自由调度: 默认的kube-scheduler调度
2 定向调度:nodeName, nodeSelector实现
3 亲和性调度:nodeAffinity, PodAffinity, PodAntiAffinity实现
4 污点和容忍调度:taint,tolerations实现
2 实践
2.1 定向调度
nodeName, NodeSelector:
1 首先node结点打标签,disk=ssd
结点打标签
kubectl label nodes k8s-node1 disk=ssd
查看结点标签
kubectl get nodes --show-labels 2 pod模板文件设定NodeSelector的值: disk: ssd apiVersion: apps/v1 kind: Deployment metadata: name: testnginx-deployment spec: selector: matchLabels: app: testnginx replicas: 3 template: metadata: labels: app: testnginx spec: containers: - name: testnginx image: nginx:latest ports: - containerPort: 80 nodeSelector: disk: ssd 3 预定义标签 kubernetes.io/hostname 2.2 亲和性调度 2.2.1 nodeAffinity pod与node关系 RequiredDuringSchedulingIgnoredDuringExecution: 必须满足指定规则才能调度pod到Node上 PreferredDuringSchedulingIgnoredDuringExecution:强调优先满足指定规则,软限制 apiVersion: apps/v1 kind: Deployment metadata: name: testnginx-deployment spec: selector: matchLabels: app: testnginx replicas: 3 template: metadata: labels: app: testnginx spec:
必须满足指定规则 硬限制
affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: disk operator: In values: - ssd
尽量优先满足指定规则 软限制
affinity: nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 1 preference: matchExpressions: - key: disk operator: In values: - ssd containers: - name: testnginx image: nginx:latest ports: - containerPort: 80
2.2.2 PodAffinity
pod与pod的关系
硬,软亲和性
requiredDuringSchedulingIgnoredDuringExecution 硬性要求,必须要满足条件,保证部署在一起 spec: affinity: podAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: -key: app operator: In values: - testnginx topologyKey: “kubernetes.io/hostname” preferredDuringSchedulingIgnoredDuringExecution 软性要求,尽量满足条件,保证部署在一起 affinity: podAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 60 podAffinityTerm: labelSelector: matchExpressions: - {key: app, operator: In, values: [“testnginx”]} topologyKey: kubernetes.io/hostname - weight: 30 podAffinityTerm: labelSelector: matchExpressions: - {key: app, operator: In, values: [“backend”]} topologyKey: kubernetes.io/hostname 2.2.2 PodAntiAffinity pod与pod的关系 硬,软反亲和性 requiredDuringSchedulingIgnoredDuringExecution 硬性要求,必须满足条件,保证分散部署的最佳实践 #如果节点上的pod标签存在满足app=nginx,则不能部署到节点上 spec: affinity: podAntiAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app operator: In values: - nginx topologyKey: “kubernetes.io/hostname” preferredDuringSchedulingIgnoredDuringExecution 软性要求,可以不完全满足,可同一node跑多个副本
软性要求
如果节点上的pod标签存在满足app=nginx,也可以部署到节点上,尽可能先部署到其它节点,如果没有满足也可以部署到此节点(大概是这么理解吧)
spec: affinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: - labelSelector: matchExpressions: - key: app operator: In values: - nginx topologyKey: "kubernetes.io/hostname"
2.3 污点容忍调度
污点(Taint) 是应用在节点之上的,为了排斥pod 所存在的
容忍度(Toleration)是应用于 Pod 上的,允许(但并不要求)Pod 调度到带有与之匹配的污点的节点上。
结点亲和性,是pod的一种属性(偏好或硬件要求),它使pod被吸引到一类特点的结点
Taint则相反,它使结点能够排斥一类特点的Pod,Taint和Toleration相互配合使用
2.3.1 污点 taint
设置污点
kubectl taint nodes node1 xtz=value1:NoSchedule
去除污点
kubectl taint nodes node1 xtz:NoSchedule-
#节点说明中,查找 Taints 字段
kubectl describe node node-name
三种调度策略:
PreferNoSchedule(软策略,尽量不调度到污点结点)
NoExecute(不会将 Pod 调度到具有该污点的 Node 上,同时会将 Node 上已经存在的 Pod驱逐出去)
NoSchedule(不会将Pod调度到具有该污点的Node上)
2.3.2 容忍度tolerations
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
tolerations: - key: “key1”
operator: “Equal”
value: “value1”
effect: “NoSchedule”
3 参考资料
https://blog.csdn.net/weixin_45310323/article/details/130718456
https://zhuanlan.zhihu.com/p/188514195
https://blog.csdn.net/qq_20042935/article/details/128442108
https://www.cnblogs.com/ygbh/p/17290915.html#_label1
https://blog.csdn.net/manwufeilong/article/details/124821748
https://www.cnblogs.com/ygbh/p/17290915.html#_lab2_1_1