Kubernetes
pod distribution
nodes
preferred mode
scheduling

Kubernetes pod distribution amongst nodes with preferred mode

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In Kubernetes, preferred scheduling rules influence placement without making it mandatory. That matters when you want pods spread across nodes for resilience, but you still want the scheduler to place the pod somewhere even if the ideal layout is temporarily impossible.

Preferred Versus Required Rules

Kubernetes offers both hard and soft scheduling constraints. Hard constraints must be satisfied or the pod stays pending. Preferred constraints are scoring hints: the scheduler tries to honor them, but it can ignore them if necessary.

For node affinity and pod anti-affinity, the preferred form is named preferredDuringSchedulingIgnoredDuringExecution. The long name is worth remembering because it explains the behavior exactly.

Preferred Pod Anti-Affinity Example

If you want replicas of the same app to avoid landing on the same node when possible, preferred pod anti-affinity is a common tool.

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: web
5spec:
6  replicas: 3
7  selector:
8    matchLabels:
9      app: web
10  template:
11    metadata:
12      labels:
13        app: web
14    spec:
15      affinity:
16        podAntiAffinity:
17          preferredDuringSchedulingIgnoredDuringExecution:
18            - weight: 100
19              podAffinityTerm:
20                labelSelector:
21                  matchExpressions:
22                    - key: app
23                      operator: In
24                      values:
25                        - web
26                topologyKey: kubernetes.io/hostname
27      containers:
28        - name: web
29          image: nginx:1.27

This tells the scheduler to prefer nodes that do not already run another pod with label app=web on the same hostname. It improves distribution, but it does not guarantee perfect balancing.

What "Preferred" Really Means in Practice

The scheduler turns preferred rules into scores. Higher weights increase the rule's influence, but scoring is still part of a larger decision process that includes resource availability and other scheduling plugins.

That means if the cluster has only one eligible node with enough CPU and memory, Kubernetes will place the pod there even when the anti-affinity preference is violated. That is the entire point of preferred mode: better placement when possible, successful placement when necessary.

Node Affinity Can Also Be Preferred

You can apply the same idea to node labels. For example, maybe you would like pods to land on SSD-backed nodes but still allow them to run elsewhere.

yaml
1affinity:
2  nodeAffinity:
3    preferredDuringSchedulingIgnoredDuringExecution:
4      - weight: 50
5        preference:
6          matchExpressions:
7            - key: disk
8              operator: In
9              values:
10                - ssd

This is useful for cost or performance preferences that should not block scheduling completely.

Use Topology Spread Constraints for Better Evenness

If your real goal is balanced replica distribution, topologySpreadConstraints often expresses that intent more directly than anti-affinity.

yaml
1topologySpreadConstraints:
2  - maxSkew: 1
3    topologyKey: kubernetes.io/hostname
4    whenUnsatisfiable: ScheduleAnyway
5    labelSelector:
6      matchLabels:
7        app: web

ScheduleAnyway behaves like a soft rule. Kubernetes tries to keep the skew low across nodes but still schedules when a perfect spread cannot be achieved.

For many modern workloads, topology spread constraints are easier to reason about than anti-affinity because they describe the distribution goal directly.

Choosing the Right Tool

Use preferred pod anti-affinity when you want replicas separated from each other and the rule is fundamentally about not colocating similar pods. Use preferred node affinity when you prefer certain nodes based on labels. Use topology spread constraints when you care most about even placement across a domain such as nodes or zones.

These tools can be combined, but too many soft preferences can make scheduling behavior harder to predict.

Common Pitfalls

A common mistake is expecting preferred rules to guarantee one pod per node. They do not. If you need a hard guarantee, use required constraints and accept that pods may remain pending.

Another issue is using a weak weight and assuming the preference will dominate scheduling. Preferred rules compete with other scoring inputs.

Finally, remember that IgnoredDuringExecution means running pods are not evicted later just because a better node becomes available.

Summary

  • Preferred scheduling rules are soft hints, not guarantees.
  • 'preferredDuringSchedulingIgnoredDuringExecution improves placement without blocking scheduling.'
  • Pod anti-affinity helps separate similar pods across nodes.
  • Topology spread constraints are often the clearest way to express even distribution.
  • Soft rules are best when availability matters more than perfect placement.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.