学习 Kubernetes(十九):Pod 调度

选择器

Node Selector 用于将 Pod 调度到指定 Node 标签的 Node 上。

为 Node 打标签:

kubectl label nodes node-tom role=database  

为 Pod 添加 Node Selector:

apiVersion: v1  
kind: Pod  
metadata:  
  name: database-pod
spec:  
  nodeSelector:
    role: database
  ...

选择器就是这么的简单粗暴,👇介绍比 Node Selector 更具表达力的亲和性。

亲和性

亲和性分为以下三类:

  • Node Affinity 用于 Node 之间亲和性调度规则;
  • Pod Affinity 用于 Pod 之间情和性调度规则;
  • Pod Anti-affinity 用于 Pod 之间反亲和性调度规则。

Node Affinity

为 Pod 添加 Node Affinity:

apiVersion: v1  
kind: Pod  
metadata:  
  name: database
spec:  
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
        - weight: 1
          preference:
            matchExpressions:
              - key: role
                operator: In
                values: [ "database-prefer" ]
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
            - key: role
              operator: In
              values: [ "database-require" ]
  ...

配置项 spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution 定义了 Pod 必须调度到满足条件的 Node 上。

配置项 spec.affinity.nodeAffinity.preferredDuringSchedulingIgnoredDuringExecution 定义了 Pod 尽量调度到权重最高的 Node 上。

Pod Affinity

为 Pod 添加 Pod Affinity 的🌰:

apiVersion: v1  
kind: Pod  
metadata:  
  name: database
spec:  
  affinity:
    podAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
        - weight: 1
          preference:
            labelSelector:
              matchExpressions:
                - key: app
                  operator: In
                  values: [ "database-prefer" ]
      requiredDuringSchedulingIgnoredDuringExecution:
        - labelSelector:
          matchExpressions:
            - key: app
              operator: In
              values: [ "database-require" ]
  ...

Pod Anti-affinity

为 Pod 添加 Pod Anti-affinity 的🌰

apiVersion: v1  
kind: Pod  
metadata:  
  name: database
spec:  
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
        - weight: 1
          preference:
            labelSelector:
              matchExpressions:
                - key: app
                  operator: In
                  values: [ "database-not-prefer" ]
      requiredDuringSchedulingIgnoredDuringExecution:
        - labelSelector:
          matchExpressions:
            - key: app
              operator: In
              values: [ "database-not-require" ]
  ...

污点和容忍

Traint(污点)和 Toleration(容忍)互相配合可用于避免 Pod 调度到 Node 上。

污点

污点由三部分组成:key、value 和 effect,格式为 key=value:effect

Effect 值包含:

  • NoSchedule 一定不能被调度
  • PreferNoSchedule 尽量不要调度
  • NoExecute 不仅不会调度,还会驱逐 Node 上已有的 Pod

为 Node 添加污点:

kubectl taint nodes <node_name> <key>=<value>:NoSchedule  

为 Node 移除污点:

kubectal taint nodes <node_name> <key>-  

容忍

为 Pod 添加容忍:

apiVersion: v1  
kind: Pod  
metadata:  
  name: database
spec:  
  tolerations:
    - key: <key>
      operator: "Equal"
      value: <value>
      effect: "NoSchedule"
  ...

则 Pod 可以容忍该污点,可以被调度到有该污点的 Pod 上。

优先级

TODO

参考