简介

pod的亲和性主要用来解决pod可以和哪些pod部署在同一个node里面;

而pod的反亲和性是为了解决pod不能和哪些pod部署在一起的问题,

二者都是为了解决pod之间部署问题。

例子

pod亲和性

apiVersion: v1
kind: Pod
metadata:
  name: my-pod-affinity
spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: security
            operator: In
            values:
            - G1
        topologyKey: my-pod-node
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: security
              operator: In
              values:
              - G2
          topologyKey: my-pod-node
  containers:
  - name: my-pod-affinity
    image: tosomeone/hostname:v4``

pod反亲和性

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my2-pod-affinity
spec:
  selector:
    matchLabels:
      app: my2-pod
  replicas: 3
  template:
    metadata:
      labels:
        app: my2-pod
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - my2-pod
            topologyKey: "my2-pod-node"
      containers:
      - name: my2-pod-affinity
        image: tosomeone/hostname:v3

总结

需要注意的是,Pod 间亲和与反亲和需要大量的处理,这可能会导致大规模集群中的调度过慢,不建议在具有几百个节点的集群中使用。

而且Pod 反亲和需要对节点进行一致的标记,即集群中的每个节点必须具有适当的标签能够匹配 topologyKey。

如果某些或所有node缺少指定的标签,可能会导致调度异常。