Same node affinity on Kubernetes

5/12/2020

I have nginx deployment pods as front that communicates to uwsgi deployment pods as back with ClusterIP service.

I want the nginx pod to use in priority the uwsgi pod that's running on its node.

Is it possible to do that with node affinity without naming nodes?

-- Vincent J
kubernetes

3 Answers

5/13/2020

From what I understand you have nginx pods and uwsgi pods, and nginx pods proxy traffic to uwsgi pods.

And you are trying to make nxinx pods proxy traffic to uwsgi pods that are on the same node.

Previously posted answers are only partialy valid. Let me explain why.

Using PodAffinity will indeed schedule nginx and uwsgi pods together but it won't affect loadBalancing.
Nginx <-> uwsgi loadbalancing will stay unchanged (will be random).

The easiest thing you can do is to run nginx container and uwsgi container in the same pod and make them communicate with localhost. In such way you are making sure that:

  1. nginx and uwsgi always get scheduled on the same node
  2. connection over localhost forces traffic to stay inside of a pod.

Let me know if this approach solves your problem or maybe for some reason we should try different approach.

-- HelloWorld
Source: StackOverflow

5/12/2020

If you want to run nginx pod on the same node as uwsgi pod, use pod affinity.

        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - uwsgi

For more details about pod affinity and anti-affinity click here

-- hoque
Source: StackOverflow

5/12/2020

The provisioning of pods and there order/scheduling of on the same node can be achieved via node affinity. However, if you want Kubernetes to decide it for you will have to use inter-pod affinity.

Also just to verify if you are doing everything the right way please refer pod-affinity example.

-- redzack
Source: StackOverflow