NodeAffinity:节点亲和力

  • RequiredDuringSchedulingIgnoredDuringExecution:硬亲和力,既支持必须部署在指定的节点上,也支持必须不部署在指定的节点上。
  • PreferredDuringSchedulingIgnoredDuringExecution:软亲和力,尽量部署在满足条件的节点上,或者是尽量不要部署在被匹配的节点。

PodAffinity:Pod亲和力

  • A应用B应用C应用,将A应用根据某种策略尽量或者部署在一块。Label
  • A:app=a B:app=b
  • RequiredDuringSchedulingIgnoredDuringExecution:将A应用和B应用部署在一块
  • PreferredDuringSchedulingIgnoredDuringExecution:尽量将A应用和B应用部署在一块

PodAntiAffinity:Pod反亲和力

  • A应用B应用C应用,将A应用根据某种策略尽量或不部署在一块。Label
  • RequiredDuringSchedulingIgnoredDuringExecution:不要将A应用与与之匹配的应用部署在一块
  • PreferredDuringSchedulingIgnoredDuringExecution:尽量。。。

给节点打上label

kubectl label node k8s-node01 kubernetes.io/e2e-az-name=e2e-az1
kubectl label node k8s-node02 kubernetes.io/e2e-az-name=e2e-az2
kubectl label node k8s-node01 another-node-label-key=another-node-label-value
kubectl label node k8s-master01 another-node-label-key=another-node-label-value
复制代码

编辑Deployment

spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: kubernetes.io/e2e-az-name
            operator: In
            values:
            - e2e-az1
            - e2e-az2
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: another-node-label-key
            operator: In
            values:
            - another-node-label-value
复制代码

In:部署在满足多个条件的节点上

NotIn:不要部署在满足这些条件的节点上

Exists:部署在具有某个存在key为指定的值的Node节点上

DoesNotExist:和Exists相反

Gt:大于指定的条件(条件为number,不能为string)

Lt:小于指定的条件

编辑Deployment,修改pod亲和性

spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: region
            operator: In
            values:
            - beijing
        topologyKey: kubernetes.io/hostname
复制代码

编辑Deployment,修改namspace的pod亲和性

spec:
# 把demo-nginx和kube-system namespace下的符合label为k8s-app=calico-kube-controllers的Pod部署在同一个节点上(拓扑域)
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: k8s-app
            operator: In
            values:
            - calico-kube-controllers
        # 如果写了namespace的字段,但是留空,它是匹配所有namespace下的指定label的Pod,如果写了namespace并且制定了值,就是匹配指定namespace下的指定label的Pod。
        # 如果没有写namespace,匹配当前namespace
        namespaces:
          - kube-system
        topologyKey: kubernetes.io/hostname
复制代码

topologyKey:拓扑域,首先说明一点,不同的key不同的value是属于不同的拓扑域。

kube-system->k8s-app=calico-kube-controllers

拓扑域划分演示:

kubectl label node k8s-master01 k8s-node01 jigui=1
kubectl label node k8s-master02 k8s-node02 jigui=2
kubectl label node k8s-master03 jigui=3

kubectl edit deploy demo-nginx
spec:
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoreDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - demo-nginx
       topologyKey: jigui
复制代码

扩展到4个pod,其中3个可以找到部署节点,最后一个处于pending状态,因为配置了3个拓扑域,最后一个找不到部署节点

k8s nfs可扩展 k8s nodeaffinity_kubernetes

配置为软亲和力

spec:
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoreDuringExecution:
      - weight: 1
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: app
              operator: In
              values:
              - demo-nginx
          topologyKey: jigui
复制代码