podAffinity:主要解决POD可以和哪些POD部署在同一个拓扑域中的问题(拓扑域用主机标签实现,可以是单个主机,也可以是多个主机组成的cluster、zone等。),podAntiAffinity主要解决POD不能和哪些POD部署在同一个拓扑域中的问题。podAntiAffinity和podAffinity它们都是处理Kubernetes集群内部POD和POD之间的关系。比如一个 pod 在一个节点上了,那么我这个也得在这个节点,或者你这个 pod 在节点上了,那么我就不想和你待在同一个节点上。
[root@master-1 app]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODENOMINATEDNODEREADINESS GATES ... myapp 1/1 Running 014s10.244.2.7node-1<none><none> ...
[root@master-1 app]# kubectl explain pod.spec.affinity.nodeAffinity KIND: Pod VERSION: v1
RESOURCE: nodeAffinity <Object>
DESCRIPTION: Describes node affinity scheduling rules forthe pod.
Node affinity is a group of node affinity scheduling rules.
FIELDS: preferredDuringSchedulingIgnoredDuringExecution <[]Object> The scheduler will prefer to schedule pods to nodes that satisfy the affinity expressions specified by this field, butit may choose a node that violates one or more ofthe expressions. The node thatis most preferred is the one withthe greatest sum of weights, i.e. for each node that meets all ofthe scheduling requirements (resource request, requiredDuringScheduling affinity expressions, etc.), compute a sum by iterating throughthe elements of this field and adding "weight"tothe sum ifthe node matches the corresponding matchExpressions; the node(s) withthe highest sum are the most preferred.
requiredDuringSchedulingIgnoredDuringExecution <Object> If the affinity requirements specified by this field are not met at scheduling time, the pod will not be scheduled ontothe node. If the affinity requirements specified by this field cease to be met atsome point during pod execution (e.g. due to an update), the system may or may nottry to eventually evict the pod fromits node.
[root@master-1 app]# kubectl explain pod.spec.affinity.podAffinity KIND: Pod VERSION: v1
RESOURCE: podAffinity <Object>
DESCRIPTION: Describes pod affinity scheduling rules (e.g. co-locate this pod inthe same node, zone, etc. assome other pod(s)).
Pod affinity is a group of inter pod affinity scheduling rules.
FIELDS: preferredDuringSchedulingIgnoredDuringExecution <[]Object> The scheduler will prefer to schedule pods to nodes that satisfy the affinity expressions specified by this field, butit may choose a node that violates one or more ofthe expressions. The node thatis most preferred is the one withthe greatest sum of weights, i.e. for each node that meets all ofthe scheduling requirements (resource request, requiredDuringScheduling affinity expressions, etc.), compute a sum by iterating throughthe elements of this field and adding "weight"tothe sum ifthe node has pods which matches the corresponding podAffinityTerm; the node(s) withthe highest sum are the most preferred.
requiredDuringSchedulingIgnoredDuringExecution <[]Object> If the affinity requirements specified by this field are not met at scheduling time, the pod will not be scheduled ontothe node. If the affinity requirements specified by this field cease to be met atsome point during pod execution (e.g. due to a pod label update), the system may or may nottryto eventually evict the pod fromits node. When there are multiple elements, the lists of nodes corresponding to each podAffinityTerm are intersected, i.e. all terms must be satisfied.
[root@master-1 app]# vim pod1-affinity-demo.yaml apiVersion: v1 kind: Pod metadata: name: node-affinity-pod1 labels: name: podaffinity-myapp tier: service spec: containers: - name: myapp image: nginx:latest imagePullPolicy: IfNotPresent nodeSelector: kubernetes.io/hostname: node-0
[root@master-1 app]# kubectl apply -f pod1-affinity-demo.yaml pod/node-affinity-pod1 created
[root@master-1 app]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODENOMINATEDNODEREADINESS GATES ... node-affinity-pod11/1 Running 013s10.244.4.9node-0<none><none> node-affinity-pod21/1 Running 011m10.244.4.10node-0<none><none> ...
  我们这个地方使用的是kubernetes.io/hostname这个拓扑域,意思就是我们当前调度的 pod 要和目标的 pod 处于同一个主机上面,因为要处于同一个拓扑域下面kubernetes.io/hostname=node-0,为了说明这个问题,我们把拓扑域改成beta.kubernetes.io/os,同样的我们当前调度的 pod 要和目标的 pod 处于同一个拓扑域中,目标的 pod 是不是拥有beta.kubernetes.io/os=linux的标签,而我们这里3个节点都有这样的标签,这也就意味着我们3个节点都在同一个拓扑域中,所以我们这里的 pod 可能会被调度到任何一个节点(因为master节点设置了污点所以不会调度至master节点),判断他们是否在同一拓扑域中是根据topologyKey中指定的node标签的values是否相同,如果相同则表示在同一拓扑域中:
1 2 3 4 5
[root@master-1 app]# kubectl get node --show-labels NAME STATUS ROLES AGE VERSION LABELS master-1 Ready master44d v1.13.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master-1,node-role.kubernetes.io/master= node-0 Ready <none>44d v1.13.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node-0 node-1 Ready <none>44d v1.13.1 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node-1
1 2 3 4 5 6 7 8
[root@master-1 app]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODENOMINATEDNODEREADINESS GATES nginx-ye-5664f956f8-4z26d 1/1 Running 08s10.244.4.12node-0<none><none> nginx-ye-5664f956f8-htb86 1/1 Running 08s10.244.4.13node-0<none><none> nginx-ye-5664f956f8-pd445 1/1 Running 08s10.244.2.20node-1<none><none> nginx-ye-5664f956f8-sqsws 1/1 Running 08s10.244.4.11node-0<none><none> nginx-ye-5664f956f8-wns5w 1/1 Running 08s10.244.2.21node-1<none><none> node-affinity-pod11/1 Running 061m10.244.2.13node-1<none><none>
DESCRIPTION: Describes pod anti-affinity scheduling rules (e.g. avoid putting this pod in the same node, zone, etc. as some other pod(s)).
Pod anti affinity is a group of inter pod anti affinity scheduling rules.
FIELDS: preferredDuringSchedulingIgnoredDuringExecution <[]Object> The scheduler will prefer to schedule pods to nodes that satisfy the anti-affinity expressions specified by this field, but it may choose a node that violates one or more of the expressions. The node that is most preferred is the one with the greatest sum of weights, i.e. for each node that meets all of the scheduling requirements (resource request, requiredDuringScheduling anti-affinity expressions, etc.), compute a sum by iterating through the elements of this field and adding "weight" to the sum if the node has pods which matches the corresponding podAffinityTerm; the node(s) with the highest sum are the most preferred.
requiredDuringSchedulingIgnoredDuringExecution <[]Object> If the anti-affinity requirements specified by this field are not met at scheduling time, the pod will not be scheduled onto the node. If the anti-affinity requirements specified by this field cease to be met at some point during pod execution (e.g. due to a pod label update), the system may or may not try to eventually evict the pod from its node. When there are multiple elements, the lists of nodes corresponding to each podAffinityTerm are intersected, i.e. all terms must be satisfied.
  这里的意思就是如果一个节点上面有一个name=podaffinity-myapp这样的 pod 的话,那么我们的 pod 就别调度到这个节点上面来,上面我们把name=podaffinity-myapp这个 pod 固定到了 node-1这个节点上面来,所以正常来说我们这里的 pod 不会出现在 node-1 节点上:
1 2 3 4 5 6 7 8
[root@master-1 app]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODENOMINATEDNODEREADINESS GATES nginx-ye-79c4778f6c-4h5ft 1/1 Running 015m10.244.4.20node-0<none><none> nginx-ye-79c4778f6c-8tw9k 1/1 Running 015m10.244.4.16node-0<none><none> nginx-ye-79c4778f6c-gpgxb 1/1 Running 015m10.244.4.17node-0<none><none> nginx-ye-79c4778f6c-hbrfr 1/1 Running 015m10.244.4.19node-0<none><none> nginx-ye-79c4778f6c-rbw5r 1/1 Running 015m10.244.4.18node-0<none><none> node-affinity-pod11/1 Running 0147m10.244.2.13node-1<none><none>