Is there any way to make kubernetes distribute pods as much as possible?
Yes, use topologyKey
it's more advanced and preferred.
I would like to maintain a situation where each deployment must spread its containers
CO-LOCATING PODS IN THE SAME AVAILABILITY ZONE
If one need to do to run the frontend pods in the same zone as the backend pod would be to change the topologyKey
property to failure-domain.beta.kubernetes.io/zone
.
CO-LOCATING PODS IN THE SAME GEOGRAPHICAL REGION
If one need the pods to be deployed in the same region instead of the same zone, the topologyKey
would be set to failure-domain.beta.kubernetes.io/region
.
UNDERSTANDING HOW TOPOLOGYKEY WORKS
It is simple. If you want, you can easily use your own topologyKey
, such as rack
, to have the pods scheduled to the same server rack
. The only prerequisite is to add a rack
label
to your nodes
.
For example, if you had 20 nodes
, with 10 in each rack, you’d label the first ten as rack=rack1
and the others as rack=rack2
. Then, when defining a pod’s podAffinity
, you’d set the toplogyKey
to rack
.
Working Process
When the Scheduler is deciding where to deploy a pod, it checks the pod’s pod- Affinity config, finds the pods that match the label selector, and looks up the nodes they’re running on. Specifically, it looks up the nodes’ label whose key matches the topologyKey field specified in podAffinity. Then it selects all the nodes whose label matches the values of the pods it found earlier. In figure 16.5, the label selector matched the backend pod, which runs on Node 12. The value of the rack label on that node equals rack2, so when scheduling a frontend pod, the Scheduler will only select among the nodes that have the rack=rack2 label.