kubernetes的node亲和&pod亲和

k8s的亲和性有node亲和和pod亲和3

1.亲和性的Deployment模板

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name:  my-name
  labels:
    name:  my-name
spec:
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        name:  my-name
    spec:
      containers:
      - image:  ipedrazas/docmock
        name:  my-name

提前说明

  • node亲和性是全局的
  • pod的亲和性是ns下的

2.node亲和

#示例1
#节点亲和nodeSelector 同时满足
      nodeSelector:
        project: test
        disktype: ssd
        
#示例2
#硬要求nodeSelectorTerms匹配多个matchExpressions 只要满足任意matchExpressions中的任意一个即可
      affinity:
        nodeAffinity: #node亲和
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions: #匹配条件1,多个values可以调度
              - key: disktype
                operator: In
                values:
                - ssd # 只有一个value是匹配成功也可以调度
                - hdd
            - matchExpressions: #匹配条件2,多个matchExpressions加上以及每个matchExpressions values只有其中一个value匹配成功就可以调度
              - key: project
                operator: In
                values:
                - mmm  #即使这俩条件2的都匹配不上也可以调度
                - nnn
#要求disktype=ssd/hdd project=aaa/bbb 4个条件满足一个即可

#示例3
#硬限制nodeSelectorTerms匹配单个matchExpressions 要满足 disktype=ssd+project=test或者disktype=hdd+project=test
      affinity:
        nodeAffinity: #node亲和
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions: #匹配条件1
              - key: disktype
                operator: In
                values:
                - ssd
                - hdd #同个key的多个value只有有一个匹配成功就行
              - key: project #条件1和条件2必须同时满足,否则不调度
                operator: In
                values:
                - test
#组合disktype=ssd+project=test disktype=hdd+project=test 即可 二选1不存在其他情况

#示例4
#软限制亲和性
      affinity:
        nodeAffinity: #node亲和
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 80  #优先权重大的80
            preference: 
              matchExpressions: 
              - key: project 
                operator: In 
                values: 
                  - test
          - weight: 60 #而后权重60
            preference: 
              matchExpressions: 
              - key: disktype
                operator: In 
                values: 
                  - ssdX
#如果都没有按照schedule自行调度

#示例5
#优先硬限制匹配 不成功软限制
      affinity:
        nodeAffinity: #node亲和  NotIn 否定亲和
          requiredDuringSchedulingIgnoredDuringExecution: #硬限制
            nodeSelectorTerms:
            - matchExpressions: #硬匹配条件1
              - key: "kubernetes.io/role" 
                operator: NotIn
                values:
                - "master" #硬性匹配key 的值kubernetes.io/role不包含master的节点,即绝对不会调度到master节点(node反亲和)
          preferredDuringSchedulingIgnoredDuringExecution: #软限制
          - weight: 80 
            preference: 
              matchExpressions: 
              - key: project 
                operator: In 
                values: 
                  - test
          - weight: 100 
            preference: 
              matchExpressions: 
              - key: disktype
                operator: In 
                values: 
                  - ssd

#示例6
#节点反亲和
      affinity:
        nodeAffinity: #node亲和 NotIn 否定亲和
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions: #匹配条件1
              - key: disktype
                operator: NotIn
                values:
                - hdd #如果匹配disktype=hdd则绝对不会调度到hdd的节点

3.pod亲和

#示例7
#pod亲和性
      affinity:
        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: project 
                  operator: In
                  values:
                    - python
              topologyKey: kubernetes.io/hostname 
              namespaces: 
                - test
#软限制权重如果将生成的pod调度到亲和性配置为project=python的pod宿主机上
#说明 topologyKey 官方文档
#Pod 反亲和性需要节点上存在一致性的标签。换言之, 集群中每个节点都必须拥有与 topologyKey 匹配的标签。 如果某些或者所有节点上不存在所指定的 topologyKey 标签,调度行为可能与预期的不同。

#示例8
#硬限制将pod调度到project=python的pod宿主机
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: project
                operator: In
                values:
                  - python
            topologyKey: "kubernetes.io/hostname"
            namespaces:
              - test

#示例9
#硬限制将pod不被调度到project=python的pod宿主机
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: project
                operator: In
                values:
                  - python
            topologyKey: "kubernetes.io/hostname"
            namespaces:
              - test

#示例10
#软限制反亲和 将要调度的pod匹配不被允许调度在project=python的宿主机
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: project 
                  operator: In
                  values:
                    - python
              topologyKey: kubernetes.io/hostname 
              namespaces: 
                - test