k8s 亲和性

k8s 亲和性

分为节点亲和性,pod亲和性和pod反亲和性

1.1 节点亲和性

pod.spec.nodeAffinity

  • preferredDuringSchedulingIgnoredDuringExecution:软策略,如果条件允许就在一台节点上,条件不允许就去别的节点
  • requiredDuringSchedulingIgnoredDuringExecution:硬策略,如果条件允许就在一台节点上,条件不允许就不活了,pending状态

键值运算关系

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

如果nodeSelectorTerms下面有多个选项的话,满足任何一个条件就可以了;如果matchExpressions有多个选项的话,则必须同时满足这些条件才能正常调度 POD

1.1.1 preferredDuringSchedulingIgnoredDuringExecution

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: with-node-affinity
    image: wangyanglinux/myapp:v1
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1			//权重,当有2-3个pod,更愿意跟谁在一起,权重越大数字越大
        preference:
          matchExpressions:
          - key: source
            operator: In
            values:
            - qikqiak

1.1.2 requiredDuringSchedulingIgnoredDuringExecution

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: with-node-affinity
    image: wangyanglinux/myapp:v1
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:		//节点选择
        - matchExpressions:
          - key: kubernetes.io/hostname			//匹配lable里key为kubernetes.io/hostname的
            operator: NotIn
            values:
            - k8s-node02		//匹配lable里key为kubernetes.io/hostname且value为k8s-node02的

1.1.3 合体

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: with-node-affinity
    image: wangyanglinux/myapp:v1
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: kubernetes.io/hostname
            operator: NotIn
            values:
            - k8s-node02
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: source
            operator: In
            values:
            - qikqiak
//对满足source=qikqiak,且kubernetes.io/hostname不等于k8s-node02 的特征节点选择

2.1 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: wangyanglinux/myapp:v1
  affinity:
    podAffinity:		//pod亲和性
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - pod-1
        topologyKey: kubernetes.io/hostname
    podAntiAffinity:		//pod反亲和性,不是硬策略
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: app
              operator: In
              values:
              - pod-2
          topologyKey: kubernetes.io/hostname
//topologyKey,拓扑域。他会从上到下筛选
//预选:会先找一个Pod,pod的key=app, value=pod-2,满足这个条件额都会被筛选出来
//优选:根据拓扑域的不同,结果也不同,如上所示,以kubernetes.io/hostname作为拓扑域,会看上面那个Pod的kubernetes.io/hostname是什么,然后把pod部署到和这个pod同一个节点上,kubernetes.io/hostname是唯一的,所以肯定会在一台机器上。同理,如果改成磁盘类型这类拓扑域,那么同理,磁盘类型为这个的就可以部署上去,上面即使没有key=app,values=pod-2的pod也可以,只要磁盘类型一样就可以部署上去

  • pod的亲和性主要用来解决pod可以和哪些pod部署在同一个集群里面,即拓扑域(由node组成的集群)里面;而pod的反亲和性是为了解决pod不能和哪些pod部署在一起的问题,二者都是为了解决pod之间部署问题。需要注意的是,Pod 间亲和与反亲和需要大量的处理,这可能会显著减慢大规模集群中的调度,不建议在具有几百个节点的集群中使用,而且Pod 反亲和需要对节点进行一致的标记,即集群中的每个节点必须具有适当的标签能够匹配 topologyKey。如果某些或所有节点缺少指定的 topologyKey 标签,可能会导致意外行为。

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

posted @ 2022-07-28 17:29  liwenchao1995  阅读(254)  评论(0编辑  收藏  举报