Kubernetes - pod FailedScheduling due to "No nodes are available that match all of the predicates: MatchInterPodAffinity (1)."

4/26/2018

I am trying to create a deployment which create two pods whose node IP's match with two exisiting pods. For this I am defining PodAffinity as below

affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - {{ .Values.albId }}
        topologyKey: "{{ .Values.topologyKey }}"
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - {{ .Values.name }}
        topologyKey: "{{ .Values.topologyKey }}"

Since the namespace in which I want to create new pods is different from namespace in which I am referring exisitng pods, PodAffinity is failing. Pods remain in pending state and when I do describe pod I get below error

Events:
  FirstSeen LastSeen    Count   From            SubObjectPath   Type        Reason          Message
  --------- --------    -----   ----            -------------   --------    ------          -------
  28s       13s     6   default-scheduler           Warning           FailedScheduling  No nodes are available that match all of the predicates: MatchInterPodAffinity (1).

From k8s docs- https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#inter-pod-affinity-and-anti-affinity-alpha-feature found that, I should define namespaces in PodAffinity and initialse it to empty list in order to allow cross namespace PodAffinity.

But I did not get any example from net on example of how to initialise namespaces to emplty list.

Please need help on this.

-- Bhagyashree
kubernetes
kubernetes-pod
namespaces

1 Answer

4/26/2018

From the documentation you linked:

In addition to labelSelector and topologyKey, you can optionally specify a list namespaces of namespaces which the labelSelector should match against (this goes at the same level of the definition as labelSelector and topologyKey). If omitted, it defaults to the namespace of the pod where the affinity/anti-affinity definition appears. If defined but empty, it means “all namespaces”

Here is the example based on your yaml:

affinity:
podAffinity:
  requiredDuringSchedulingIgnoredDuringExecution:
  - labelSelector:
      matchExpressions:
      - key: app
        operator: In
        values:
        - {{ .Values.albId }}
    topologyKey: "{{ .Values.topologyKey }}"
    namespaces: [] # empty array
podAntiAffinity:
  requiredDuringSchedulingIgnoredDuringExecution:
  - labelSelector:
      matchExpressions:
      - key: app
        operator: In
        values:
        - {{ .Values.name }}
    topologyKey: "{{ .Values.topologyKey }}"
    namespaces: []
-- Anton Kostenko
Source: StackOverflow