调度规则
deployment全自动调度: 运行在哪个节点上完全由master的scheduler经过一系列的算法计算得出, 用户无法进行干预
nodeselector定向调度: 指定pod调度到一些node上, 通过设置node的标签和deployment的nodeSelctor属性相匹配
多个node有相同标签, scheduler 会选择一个可用的node进行调度
nodeselector定义的标签匹配不上任何node上的标签, 这个pod是无法调度
k8s给node预定义了一些标签, 通过kubectl describe node xxxx进行查看
用户可以使用k8s给node预定义的标签
NodeAffinity: node节点亲和性
硬限制 : 必须满足指定的规则才可以调度pod到node上
软限制 : 优先满足指定规则,调度器会尝试调度pod到node上, 但不强求
PodAffinity: pod亲和与互斥调度, 根据在节点上正在运行的pod标签而不是节点的标签进行判断和调度
亲和: 匹配标签两个pod调度到同一个node上
互斥: 匹配标签两个pod不能运行在同一个node上
nodeselector定向调度
- 给目标node打上一些标签
kubectl label nodes <node-name> <label-key>=<label-value>
kubectl label nodes 192.168.1.62 disk=ssd
- 在pod的定义加上nodeSelector设置
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.11
ports:
- containerPort: 80
nodeSelector:
disk: ssd
如果给多个node都定义了相同的标签, 则调度器会根据调度算法从这组node中挑选一个可用的node进行pod调度
如果指定了pod的nodeSelector条件, 且集群中不存在包含响应标签的Node, 则即使在集群中还有其它可供使用的Node,这个pod也无法被成功调度
用户可以自行给node添加标签, kubernetes也会给node预定义一些标签, 可以通过kubectl describe node
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
disk=ssd
kubernetes.io/hostname=192.168.1.62
uploading-image-951691.png
NodeAffinity
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 4
template:
metadata:
labels:
app: nginx
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: beta.kubernetes.io/arch
operator: In
values:
- amd64
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: disk
operator: In
values:
- ssd
containers:
- name: nginx
image: nginx:1.11
ports:
- containerPort: 80
requiredDuringSchedulingIgnoredDuringExecution: 硬限制, 要求只运行在amd64的节点上
preferredDuringSchedulingIgnoredDuringExecution: 软限制, 尽量运行在磁盘类型ssd的节点上
PodAffinity: pod亲和性调度
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-test1
spec:
selector:
matchLabels:
app: nginx-test1
replicas: 1
template:
metadata:
labels:
app: nginx-test1
security: s1
spec:
containers:
- name: nginx
image: nginx:1.11
ports:
- containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-test2
spec:
selector:
matchLabels:
app: nginx-test2
replicas: 4
template:
metadata:
labels:
app: nginx-test2
security: s1
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- nginx-test1
topologyKey: kubernetes.io/hostname
containers:
- name: nginx
image: nginx:1.11
ports:
- containerPort: 80