调度相关
Kubernetes 的调度配置有 nodeSelector
和 nodeAffinity
两种方式,其中 nodeSelector
是最简单的,而 nodeAffinity
则更灵活强大,本文涉及的例子,两种方式均会提供。
只调度到超级节点
如果集群中有超级节点和非超级节点,又希 望将 Pod 只调度到超级节点上,可以这样配置:
- nodeSelector 写法
- nodeAffinity 写法
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
# 使用 nodeSelector 显式指定调度到超级节点
nodeSelector:
node.kubernetes.io/instance-type: eklet
containers:
- name: nginx
image: nginx:latest
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
affinity:
nodeAffinity: # 使用 nodeAffinity 显式指定调度到超级节点
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node.kubernetes.io/instance-type
operator: In
values:
- eklet
containers:
- name: nginx
image: nginx:latest
只调度到原生节点
- 方式一:编辑 YAML
- 方式二:控制台操作
编辑调度策略:
kubectl -n kube-system edit cm crane-scheduler-apply-scope
namespaceScope
限制指定命名空间的 Pod 强制调度到原生节点:
apiVersion: v1
data:
namespaceScope: '{"default":true}'
kind: ConfigMap
metadata:
name: crane-scheduler-apply-scope
namespace: kube-system
default
改为要限制的命名空间名称,*
表示所有命名空间。
在集群的【组件管理】中找到 cranescheduler
,点【更新配置】:
根据需求配置:
调度到指定机型
节点可能有多种机型,如果希望将 Pod 调度到指定机型的节点上,可以这样配置:
nodeAffinity
写法支持配置多种机型。
- nodeSelector 写法
- nodeAffinity 写法
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
nodeSelector:
node.kubernetes.io/instance-type: SA2.MEDIUM8 # 显式指定调度到 SA2.MEDIUM8 机型的节点
containers:
- name: nginx
image: nginx:latest
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node.kubernetes.io/instance-type
operator: In
values:
- SA2.MEDIUM8 # 显式指定调度到 SA2.MEDIUM8 机型的节点
containers:
- name: nginx
image: nginx:latest
前面说的节点不包含超级节点,因为超级节点是个虚拟的节点,并不是实体节点,可以认为底层是个无限大的计算资源池,调度到超级节点上的 Pod,分配的机器是随机的,机型也是随机的。
如果 Pod 一定要在超级节点,且业务对机型又有要求,也可以用如下注解显式声明超级节点中的 Pod 的机 型:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
annotations:
eks.tke.cloud.tencent.com/cpu-type: "SA5,SA4,amd" # 优先使用 SA5,如果没资源就尝试 SA4,如果都没有再尝试其他所有 AMD 系列 CPU 的机型
spec:
nodeSelector:
node.kubernetes.io/instance-type: eklet
containers:
- name: nginx
image: nginx:latest
指定置放群组(超级节点)
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
annotations:
eks.tke.cloud.tencent.com/spread-placement-group-id: ps-xxxxxxxx
spec:
nodeSelector:
node.kubernetes.io/instance-type: eklet
containers:
- name: nginx
image: nginx:latest