Setting inter-pod affinity across namespaces in kubernetes

4/28/2020

I have a pod with the label app: jenkins-master on jenkins namespace.

I also want to create an nginx deployment.

(for reasons beyond the scope of this question, the nginx deployment will reside in a different namespace)

I want these two pods to be (softly) scheduled on the same node so I am using the following pod spec in the deployment manifest

spec:
  affinity:
    podAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - jenkins-master

How should I syntactically set the namespace value to jenkins in the above snipper?

(or even better state that I want this to be applicable across all namespaces?

-- pkaramol
kubernetes

1 Answer

4/28/2020

You can specify optional associated list of namespaces

spec:
  affinity:
    podAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - jenkins-master
        namespaces: 
        - namespacename1
        - namespacename2

As documented here

namespaces
string array    namespaces specifies which namespaces the labelSelector applies to (matches against); null or empty list means "this pod's namespace"
-- Arghya Sadhu
Source: StackOverflow