kubernetes-调度器-调度亲和性

节点的亲和性

pod.spec.nodeAffinity

  • preferredDuringSchedulinglgnoredDuringExecution:软策略
  • requireDuringSchedulinglgnoredDuringExecution:硬策略

requiredDuringSchedulinglgnoredDuringExecution

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: with-node-affinity
    image: harbor.bwingame8.com/library/myapp:v1
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: kubernetes.io/hostname
            operator: NotIn
            values:
            - k8s-node2

preferredDuringSchedulingIgnoredDuringExecution

apiVersion: v1
kind: Pod
metadata:
  name: affinity1
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: with-node-affinity
    image: harbor.bwingame8.com/library/myapp:v1
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: kubernetes.io/hostname
            operator: In
            values:
            - k8s-node3

合体

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: with-node-affinity
    image: harbor.bwingame8.com/library/myapp:v1
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: kubernetes.io/hostname
            operator: NotIn
            values:
            - k8s-node2
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: source
            operator: In
            values:
            - k8s-node2

键值运算关系

  • In: label的值在某个列表中
  • NotIn:label的值不在某个列表中
  • Gt: label的值大于某个值
  • Lt: label的值小于某个值
  • Exists:某个label存在
  • DoesNotExist:某个label不存在

Pod 亲和性

pod.spec.affinity.podAffinity/podAntiAffinity

  • preferredDuringSchedulingIgnoredDuringExecution: 软策略
  • requiredDuringSchedulingIgnoredDuringExecution:硬策略
apiVersion: v1
kind: Pod
metadata:
  name: pod-3
  labels:
    app: pod-3
spec:
  containers:
  - name: pod-3
    image: harbor.bwingame8.com/library/myapp:v1
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
        matchExpressions:
        - key: app
          operator: In
          values:
          - pod-1
        topologyKey: kubernetes.io/hostname
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: app
              operator: In
              values:
              - pod-2
          topologyKey: kubernetes.io/hostname

亲和性/反亲和性调度策略比较如下:

调度策略匹配标签操作符拓扑域支持调度目
nodeAffinity主机In,NotIn,Exists,DoesNotExist,Gt,Lt指定主机
podAffinityPODIn,NotIn,Exists,DoesNotExistPOD与指定POD同一拓扑域
podAnitAffinityPODIn,NotIn,Exists,DoesNotExistPOD与指定POD不再同一个拓扑域

文章作者: 阿培
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 阿培 !
 上一篇
kubernetes-调度器-污点 kubernetes-调度器-污点
Tain 和 Toleration节点亲和性,是 pod 的一种属性(偏好或硬性要求),它使 pod 被吸引到一类特定的节点。Taint 则相反,它使节点能够排斥一类特定的pod Taint 和 toleration 相互配合,可以用来避
2019-12-04
下一篇 
kubernetes-调度器-调度说明 kubernetes-调度器-调度说明
简介Scheduler 是kubernetes的调度器,主要的任务是把定义的Pod分配到集群的节点上。听起来非常简单,单有很多要考虑的问题 公平:如何保证每个节点都能被分配资源 资源高效利用:集群所有资源最大化被使用 效率:调度的性能要好
2019-12-04
  目录