k8s调度器原理与实践
目的: 将把没有调度的pod调度到最合适的node上。
主要调度手段
- Predict 过滤不满足条件的节点
- Priority 调度器会对这些适合的节点进行打分排序,选择最优点。
可自定义调度的手段
- 节点选择器
- 亲和性/反亲和性
- 污点/容忍污点
调度器的高级特性
- NodeNaMe and NodeSelector
kubectl label nodes shifeinode6 llcb.com/role=game1
kubectl get nodes shifeinode6 --show-labels
# https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
namespace: default
labels:
app: my-nginx
spec:
selector:
matchLabels:
app: my-nginx
replicas: 1
strategy:
template:
metadata:
labels:
app: my-nginx
spec:
nodeSelector: # node选择器
llcb.com/role: game1 # 选择标签
containers:
- name: my-nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 100m
memory: 100Mi
restartPolicy: Always
- 亲和性/反亲和性(Affinity and anti-affinity)
对于不同的节点我们会有以下的需求:
- Pod固定调度到某些节点之上
- Pod不会调度到某些节点之上
- Pod的多副本调度到相同的节点之上
- Pod的多副本调度到不同的节点之上
2.1 pod.spec.nodeAffinity
- preferredDuringSchedulingIgnoredDuringExecution:软策略(尽可能调度到这儿)
- requiredDuringSchedulingIgnoredDuringExecution:硬策略(必须调度到这儿)
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
namespace: default
labels:
app: my-nginx
spec:
selector:
matchLabels:
app: my-nginx
replicas: 1
strategy:
template:
metadata:
labels:
app: my-nginx
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
– matchExpressions:
– key: disktype
operator: In
values:
– ssd
containers:
- name: my-nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 100m
memory: 100Mi
restartPolicy: Always
键值对的运算关系:
- In:label 的值在某个列表中
- NotIn:label 的值不在某个列表中
- Gt:label 的值大于某个值
- Lt:label 的值小于某个值
- Exists:某个 label 存在
- DoesNotExist:某个 label 不存在
- 污点/非污点
- 添加/删除
kubectl taint nodes shifeinode6 server=game1:NoSchedule
kubectl taint nodes shifeinode6 server=game1:NoSchedule-
- 容忍污点
tolerations:
- key: "server"
operator: "Equal" # 等于这个污点
value: "game1"
effect: "NoSchedule"
tolerations:
- key: "key1"
operator: "Exists" # 存在这个污点
effect: "NoSchedule"
# https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
namespace: default
labels:
app: my-nginx
spec:
selector:
matchLabels:
app: my-nginx
replicas: 1
strategy:
template:
metadata:
labels:
app: my-nginx
spec:
nodeSelector: # node选择器
llcb.com/role: game1 # 选择标签
containers:
- name: my-nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 100m
memory: 100Mi
tolerations:
- key: "server"
operator: "Equal"
value: "game1"
effect: "NoSchedule"
restartPolicy: Always
3.1 使用思考
- 专用节点
- 配置了特殊的设备GPU
- 不可调度节点设置
kubectl cordon node_name
kubectl uncordon node_name
4.1 思考使用节点
- node调度的开关。