Kubernetes Tip: How To Run Different Replicas Of A Deployment On Different Nodes?

Consider a use case where one has to run replicas of the same deployment on different nodes from the HA perspective.

Let’s mimic as real scenario with an example. I have a single kubernetes cluster having 4 nodes with 3 worker nodes & a master node.There is a deployment manifest for web-server having 4 replicas. The requirement is to spread the replicas as evenly as possible from a HA perspective thereby reducing blast radius for a service when a node goes down.

One master and three worker nodes.

To achieve this requirement, the web-server deployment manifest looks as in the image below. Note the important parameters podAntiAffinity, preferredDuringSchedulingIgnoredDuringExecution & topologyKey.

manifest of web-server deployment for replicas to be distributed across nodes.

In plain English, this essentially means that when scheduling web-server replicas, it is preferred not to co-locate replicas on the same host. Note the word preferred one could replace preferred to required where preferred is soft scheduling while required is strict scheduling.

Let’s apply this manifest.

Apply the manifest

Boom Done!.

Let’s look at how the scheduler has distributed these replicas.

Pods distributed across available nodes.

How cool, the scheduler has been fair in distributing the replicas across different available nodes. If there are more replicas than available nodes in example considered, scheduler made sure to schedule them as well because of *preferred* parameter.

One can use this in the HPA scenario as well. For more info on scheduling, refer to k8s docs.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store