节点选择器
nodeSelector
如下:
apiVersion: v1kind: Podmetadata:name: pod-nodeselectorspec:containers:- name: myappimage: ikubernetes/myapp:v1nodeSelector:disktype: ssd
上面定义只有调度到符合标签为disktype=ssd得node。
nodeName
当确认要让某个Pod允许在指定的节点上,如下:
apiVersion: v1kind: Podmetadata:name: pod-nodenamespec:containers:- name: myappimage: ikubernetes/myapp:v1nodeName: 172.16.1.130 # 节点名字
节点亲和性
nodeAffinity
nodeAffinity有两种,优选和必选,其为:preferredDuringSchedulingIgnoredDuringExecution和requiredDuringSchedulingIgnoredDuringExecution。
requiredDuringSchedulingIgnoredDuringExecution的例子如下:
apiVersion: v1kind: Podmetadata:name: pod-nodeaffinity-requiredspec:containers:- name: myappimage: ikubernetes/myapp:v1affinity:nodeAffinity:requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:- matchExpressions:- key: disktypeoperator: Invalues: ["ssd", "harddisk"]
其中operator支持In,NotIn, Exists, DoesNotExist. Gt, and Lt。
preferredDuringSchedulingIgnoredDuringExecution的例子如下:
apiVersion: v1kind: Podmetadata:name: pod-nodeaffinity-preferredspec:containers:- name: myappimage: ikubernetes/myapp:v1affinity:nodeAffinity:preferredDuringSchedulingIgnoredDuringExecution:- preference:matchExpressions:- key: disktypeoperator: Invalues: ["ssd", "harddisk"]weight: 60
podAffinity
podAffinity也有preferredDuringSchedulingIgnoredDuringExecution和requiredDuringSchedulingIgnoredDuringExecution,其定义方式和nodeAffinity一样
apiVersion: v1kind: Podmetadata:name: pod-firstlabels:app: myapprow: frontedspec:containers:- name: myappimage: ikubernetes/myapp:v1---apiVersion: v1kind: Podmetadata:name: pod-secondlabels:app: dbrow: backendspec:containers:- name: dbimage: busyboximagePullPolicy: IfNotPresentcommand:- "/bin/sh"- "-c"- "sleep 3600"affinity:podAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: appoperator: Invalues: ["myapp"]topologyKey: kubernetes.io/hostname
podAntiAffinity
pod的反亲和性。
apiVersion: v1kind: Podmetadata:name: pod-firstlabels:app: myapprow: frontedspec:containers:- name: myappimage: ikubernetes/myapp:v1---apiVersion: v1kind: Podmetadata:name: pod-secondlabels:app: dbrow: backendspec:containers:- name: dbimage: busyboximagePullPolicy: IfNotPresentcommand:- "/bin/sh"- "-c"- "sleep 3600"affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: appoperator: Invalues: ["myapp"]topologyKey: kubernetes.io/hostname
污点调度方式
taints是Node级别的,可以通过kubectl explain node.spec.taints来查看。
# kubectl explain node.spec.taintsKIND: NodeVERSION: v1RESOURCE: taints <[]Object>DESCRIPTION:If specified, the node's taints.The node this Taint is attached to has the "effect" on any pod that doesnot 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 onlywritten 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/v1kind: Deploymentmetadata:name: nginx-deploymentlabels:app: nginxspec:replicas: 2selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.7.9imagePullPolicy: IfNotPresentports:- containerPort: 80tolerations:- key: "node-type"operator: Equalvalue: deveffect: NoScheduletolerationSeconds: 20
