Search code examples
algorithmkubernetesdynamic-programminggraph-algorithmhungarian-algorithm

Which algorithim does Kubernetes use to assign pods to nodes?


This is more of cost estimation question than how to use features like node affinity.

So basically there are m pods with some constraints like:

  • each pod of specific Deployments / StatefulSets should be on a different kubernetes node
  • pods of specific Deployments / StatefulSets should be balanced over 3 availability zones

Now, I want to find how many nodes (all same types) I will need to host given set of Deployments / StatefulSets.

I first thought this of more like an assignment problem to be solved using Hungarian Algorithim but this seems much more complex in term like multi dimensional constraints.


Solution

  • By my knowledge the algorithm used by default by the kube-scheduler is described on github here.

    It explains how it works. It first filters nodes that do not meet the requirements of the pods, e.g. resource requests > available resources on nodes, affinity etc.

    Then uses a ranking algorithm to determine the best fitting node. For deeper insights on the