节点选择器
nodeSelector
如下:
apiVersion: v1
kind: Pod
metadata:
name: pod-nodeselector
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
nodeSelector:
disktype: ssd
上面定义只有调度到符合标签为disktype=ssd得node。
nodeName
当确认要让某个Pod允许在指定的节点上,如下:
apiVersion: v1
kind: Pod
metadata:
name: pod-nodename
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
nodeName: 172.16.1.130 # 节点名字
节点亲和性
nodeAffinity
nodeAffinity有两种,优选和必选,其为:preferredDuringSchedulingIgnoredDuringExecution和requiredDuringSchedulingIgnoredDuringExecution。
requiredDuringSchedulingIgnoredDuringExecution的例子如下:
apiVersion: v1
kind: Pod
metadata:
name: pod-nodeaffinity-required
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values: ["ssd", "harddisk"]
其中operator支持In,NotIn, Exists, DoesNotExist. Gt, and Lt。
preferredDuringSchedulingIgnoredDuringExecution的例子如下:
apiVersion: v1
kind: Pod
metadata:
name: pod-nodeaffinity-preferred
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- preference:
matchExpressions:
- key: disktype
operator: In
values: ["ssd", "harddisk"]
weight: 60
podAffinity
podAffinity也有preferredDuringSchedulingIgnoredDuringExecution和requiredDuringSchedulingIgnoredDuringExecution,其定义方式和nodeAffinity一样
apiVersion: v1
kind: Pod
metadata:
name: pod-first
labels:
app: myapp
row: fronted
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
---
apiVersion: v1
kind: Pod
metadata:
name: pod-second
labels:
app: db
row: backend
spec:
containers:
- name: db
image: busybox
imagePullPolicy: IfNotPresent
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values: ["myapp"]
topologyKey: kubernetes.io/hostname
podAntiAffinity
pod的反亲和性。
apiVersion: v1
kind: Pod
metadata:
name: pod-first
labels:
app: myapp
row: fronted
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
---
apiVersion: v1
kind: Pod
metadata:
name: pod-second
labels:
app: db
row: backend
spec:
containers:
- name: db
image: busybox
imagePullPolicy: IfNotPresent
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values: ["myapp"]
topologyKey: kubernetes.io/hostname
污点调度方式
taints是Node级别的,可以通过kubectl explain node.spec.taints来查看。
# kubectl explain node.spec.taints
KIND: Node
VERSION: v1
RESOURCE: taints <[]Object>
DESCRIPTION:
If specified, the node's taints.
The node this Taint is attached to has the "effect" on any pod that does
not tolerate the Taint.
FIELDS:
effect <string> -required-
Required. The effect of the taint on pods that do not tolerate the taint.
Valid effects are NoSchedule, PreferNoSchedule and NoExecute.
key <string> -required-
Required. The taint key to be applied to a node.
timeAdded <string>
TimeAdded represents the time at which the taint was added. It is only
written for NoExecute taints.
value <string>
Required. The taint value corresponding to the taint key.
其中effect定义对Pod的排斥效果:
- NoSchdule:仅影响调度过程,对现存在的Pod不产生影响;
- NoExecute:不仅影响调度,而且还影响现存Pod,不容忍的Pod对象将被驱逐;
- PreferNoSchedule:
管理污点用kubectl taint.
tolerations容忍度是定义在Pod上的。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
tolerations:
- key: "node-type"
operator: Equal
value: dev
effect: NoSchedule
tolerationSeconds: 20