Home > Software engineering >  How to distribute K8 Deployments evenly across nodes with Kubernetes?
How to distribute K8 Deployments evenly across nodes with Kubernetes?

Time:04-09

I have 12 K8 deployments that should be distributed somewhat evenly across 3 K8 nodes based on resource utilization (like the uptime command). I expected Kubernetes to automatically choose the node that is utilizing the least amount of resources at the time of pod creation, which I would think should result in a somewhat even distribution, but to my surprise Kubernetes is creating the majority of the Pods on the same single node that is barely handling the load, while the other nodes are not being utilized almost at all.

I heard about using topologySpreadConstraints like so

topologySpreadConstraints:
              - maxSkew: 1
                topologyKey: kubernetes.io/hostname
                whenUnsatisfiable: ScheduleAnyway
                labelSelector:
                    matchLabels:
                      type: wordpress-3 

But I cant get it to work properly, what is the correct way to achieve the even distribution behavior of deployments that I am looking for? thanks!

CodePudding user response:

Are you using bitnami's wordpress chart?

If so you can update the values.yaml you pass into the chart and set anti-affinity like this:


# Affinity
affinity:
  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - topologyKey: kubernetes.io/hostname
      labelSelector:
        matchLabels:
          app.kubernetes.io/instance: {name of your Wordpress release}

This will force kubernetes to only allow one Wordpress pod on one host (i.e. node). I use this setup on my own Wordpress installations and it means if one node goes down, it doesn't take out the site as the other replicas will still be running and on separate nodes

  • Related