Search code examples
kuberneteskubernetes-pod

How can we assign one of the pod's of StatefulSet to a specific node of a Kubernetes cluster in pod affinity / antiaffinity


I have a 7 node cluster. I have an app 'A' having 3 replicas running in random 3 nodes. I have another app 'B' having 5 replicas, now I have to assign only 1 of the replicas of 'B' to the same node as 'A'. Other 4 replicas can run in different nodes.

Node 1  -   A1
Node 2  -   B2
Node 3  -   B3
Node 4  -   A2, B1
Node 5  -   B4
Node 6  -   A3
Node 7  -   B5

Solution

    • Affinity Rule 1: Same type of pods will push each other
    • Affinity Rule 2: Different type of pods will attract each other

    By those rules, you can achieve something like bellow:

    Node 1: A1, B1
    Node 2: A2, B2
    Node 3: A3, B3
    Node 4: B4
    Node 5: B5
    ... ...
    

    But if you add taint to Node:1,2 such that no B pods will be able to be scheduled on those nodes. And add taint to Node:4,5,6,7 such that no A will be able to be scheduled on those nodes.

    Then you will end up having the following distribution:

    Node 1  -   A1
    Node 2  -   A2
    Node 3  -   A3, B1
    Node 4  -   B2
    Node 5  -   B3
    Node 6  -   B4
    Node 7  -   B5