节点选择器

nodeSelector

如下:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: pod-nodeselector
  5. spec:
  6. containers:
  7. - name: myapp
  8. image: ikubernetes/myapp:v1
  9. nodeSelector:
  10. disktype: ssd

上面定义只有调度到符合标签为disktype=ssd得node。

nodeName

当确认要让某个Pod允许在指定的节点上,如下:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: pod-nodename
  5. spec:
  6. containers:
  7. - name: myapp
  8. image: ikubernetes/myapp:v1
  9. nodeName: 172.16.1.130 # 节点名字

节点亲和性

nodeAffinity

nodeAffinity有两种,优选和必选,其为:preferredDuringSchedulingIgnoredDuringExecution和requiredDuringSchedulingIgnoredDuringExecution。

requiredDuringSchedulingIgnoredDuringExecution的例子如下:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: pod-nodeaffinity-required
  5. spec:
  6. containers:
  7. - name: myapp
  8. image: ikubernetes/myapp:v1
  9. affinity:
  10. nodeAffinity:
  11. requiredDuringSchedulingIgnoredDuringExecution:
  12. nodeSelectorTerms:
  13. - matchExpressions:
  14. - key: disktype
  15. operator: In
  16. values: ["ssd", "harddisk"]

其中operator支持In,NotIn, Exists, DoesNotExist. Gt, and Lt。

preferredDuringSchedulingIgnoredDuringExecution的例子如下:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: pod-nodeaffinity-preferred
  5. spec:
  6. containers:
  7. - name: myapp
  8. image: ikubernetes/myapp:v1
  9. affinity:
  10. nodeAffinity:
  11. preferredDuringSchedulingIgnoredDuringExecution:
  12. - preference:
  13. matchExpressions:
  14. - key: disktype
  15. operator: In
  16. values: ["ssd", "harddisk"]
  17. weight: 60

podAffinity

podAffinity也有preferredDuringSchedulingIgnoredDuringExecution和requiredDuringSchedulingIgnoredDuringExecution,其定义方式和nodeAffinity一样

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: pod-first
  5. labels:
  6. app: myapp
  7. row: fronted
  8. spec:
  9. containers:
  10. - name: myapp
  11. image: ikubernetes/myapp:v1
  12. ---
  13. apiVersion: v1
  14. kind: Pod
  15. metadata:
  16. name: pod-second
  17. labels:
  18. app: db
  19. row: backend
  20. spec:
  21. containers:
  22. - name: db
  23. image: busybox
  24. imagePullPolicy: IfNotPresent
  25. command:
  26. - "/bin/sh"
  27. - "-c"
  28. - "sleep 3600"
  29. affinity:
  30. podAffinity:
  31. requiredDuringSchedulingIgnoredDuringExecution:
  32. - labelSelector:
  33. matchExpressions:
  34. - key: app
  35. operator: In
  36. values: ["myapp"]
  37. topologyKey: kubernetes.io/hostname

podAntiAffinity

pod的反亲和性。

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: pod-first
  5. labels:
  6. app: myapp
  7. row: fronted
  8. spec:
  9. containers:
  10. - name: myapp
  11. image: ikubernetes/myapp:v1
  12. ---
  13. apiVersion: v1
  14. kind: Pod
  15. metadata:
  16. name: pod-second
  17. labels:
  18. app: db
  19. row: backend
  20. spec:
  21. containers:
  22. - name: db
  23. image: busybox
  24. imagePullPolicy: IfNotPresent
  25. command:
  26. - "/bin/sh"
  27. - "-c"
  28. - "sleep 3600"
  29. affinity:
  30. podAntiAffinity:
  31. requiredDuringSchedulingIgnoredDuringExecution:
  32. - labelSelector:
  33. matchExpressions:
  34. - key: app
  35. operator: In
  36. values: ["myapp"]
  37. topologyKey: kubernetes.io/hostname

污点调度方式

taints是Node级别的,可以通过kubectl explain node.spec.taints来查看。

  1. # kubectl explain node.spec.taints
  2. KIND: Node
  3. VERSION: v1
  4. RESOURCE: taints <[]Object>
  5. DESCRIPTION:
  6. If specified, the node's taints.
  7. The node this Taint is attached to has the "effect" on any pod that does
  8. not tolerate the Taint.
  9. FIELDS:
  10. effect <string> -required-
  11. Required. The effect of the taint on pods that do not tolerate the taint.
  12. Valid effects are NoSchedule, PreferNoSchedule and NoExecute.
  13. key <string> -required-
  14. Required. The taint key to be applied to a node.
  15. timeAdded <string>
  16. TimeAdded represents the time at which the taint was added. It is only
  17. written for NoExecute taints.
  18. value <string>
  19. Required. The taint value corresponding to the taint key.

其中effect定义对Pod的排斥效果:

  • NoSchdule:仅影响调度过程,对现存在的Pod不产生影响;
  • NoExecute:不仅影响调度,而且还影响现存Pod,不容忍的Pod对象将被驱逐;
  • PreferNoSchedule:

管理污点用kubectl taint.

tolerations容忍度是定义在Pod上的。

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. labels:
  6. app: nginx
  7. spec:
  8. replicas: 2
  9. selector:
  10. matchLabels:
  11. app: nginx
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx
  16. spec:
  17. containers:
  18. - name: nginx
  19. image: nginx:1.7.9
  20. imagePullPolicy: IfNotPresent
  21. ports:
  22. - containerPort: 80
  23. tolerations:
  24. - key: "node-type"
  25. operator: Equal
  26. value: dev
  27. effect: NoSchedule
  28. tolerationSeconds: 20