Node Affinity and Anti-Affinity in Kubernetes: Deciding Where Your Pods Run

Node Affinity and Anti-Affinity in Kubernetes: Deciding Where Your Pods Run

Understand how Kubernetes lets you attract pods to certain nodes, keep them away from others, and spread copies apart for resilience, using node affinity, pod affinity, and anti-affinity rules that you can reason about with confidence.

Picture a large hotel with hundreds of rooms on many floors and in several wings. Some guests ask for a sea view, some need a ground-floor room because of a wheelchair, and a group of friends wants rooms next to each other. A famous couple, on the other hand, wants to stay far away from the noisy wedding party. The receptionist has to honour all of these wishes while still filling the hotel efficiently. In Kubernetes, the scheduler is that receptionist, pods are the guests, nodes are the rooms, and affinity and anti-affinity rules are the wishes each guest writes on the booking form.

1Why Pod Placement Matters

By default Kubernetes places pods wherever there is room, and that is not always what you want.

When you create a pod, Kubernetes has to pick one machine from your cluster to run it. That machine is called a node. Out of the box, the scheduler looks for nodes with enough free CPU and memory and picks a good one. For many simple applications this is perfectly fine, and you never need to think about it again.

However, real systems quickly develop special needs. A machine-learning job needs a node with a graphics card. A database needs fast local disks. A payment service must run only on nodes that are certified for sensitive data. A web application with six copies should not have all six on the same machine, because one hardware failure would take the whole application down. These needs cannot be met by free CPU and memory alone, and that is where affinity and anti-affinity come in.

Simple Analogy

Think of a school bus company assigning children to buses. Most children can sit anywhere. But a child in a wheelchair must be on the bus with a lift, two siblings should sit together, and two children who quarrel should sit far apart. Affinity is the rule “put me with this,” and anti-affinity is the rule “keep me away from that.”

The two big families of rules

Kubernetes offers two families of placement rules that are often confused. Node affinity is about the relationship between a pod and the labels on a node, such as “run me only on nodes that have an SSD.” Pod affinity and pod anti-affinity are about the relationship between a pod and other pods that are already running, such as “run me near the cache pods” or “do not run me on a node that already has one of my siblings.” Knowing which family you need is the first step to using them well.

2
Rule strengths: required and preferred
6
Match operators for node labels
1-100
Weight range for preferred rules
i
Good to know

The phrase “node anti-affinity” is used in everyday talk, but the official node affinity feature expresses it by using negative operators. True anti-affinity as a named feature exists for pods. This guide covers both ideas clearly so the naming never confuses you.

2How the Scheduler Makes Decisions

Every placement rule you write is read by a component that works in two stages.

The Kubernetes scheduler is a control-plane component that watches for new pods that have no node assigned. For each such pod, it goes through a process with three main phases. First it filters out the nodes that cannot run the pod. Then it scores the remaining nodes to find the best one. Finally it binds the pod to the winner by writing the chosen node name onto the pod.

flowchart TD
    A[New pod with no node] --> B[Filtering phase
remove nodes that cannot run the pod] B --> C{Any node left?} C -->|No| D[Pod stays Pending
event explains why] C -->|Yes| E[Scoring phase
rank remaining nodes] E --> F[Pick highest score] F --> G[Bind pod to node] G --> H[Kubelet on node starts containers]
FIG 1 — The scheduler filters first, scores second, then binds the pod to one node

Where affinity plugs in

The two strengths of affinity rules map neatly onto the two phases. A required rule works during filtering: a node that does not satisfy it is removed from the list, and if no node satisfies it the pod waits. A preferred rule works during scoring: nodes that satisfy it get extra points, but nodes that do not are still allowed. This single idea, hard filter versus soft score, explains almost all the behaviour you will ever see.

Simple Analogy

Choosing a restaurant with friends works the same way. “It must be vegetarian-friendly” is a filter: restaurants without vegetarian dishes are crossed off. “I would like outdoor seating” is a score: restaurants with a terrace win extra points, but a good restaurant without one can still win.

!
Common Misconception

Many beginners believe the scheduler keeps watching and will move a pod if a rule stops being true. It does not. Scheduling happens once, at the moment the pod is placed. Afterwards the pod stays where it is, even if labels change. We look at this carefully in later chapters.

3Labels: The Language of Placement

Every affinity rule is just a question asked about labels.

A label is a simple key and value pair attached to a Kubernetes object. A node might carry a label saying its disk type is ssd, another saying its hardware team is gpu-pool, and another saying it lives in a certain zone. Pods carry labels too, for example an application name and a tier. Affinity rules do not know anything about hardware or software directly; they only compare these labels.

Term 1

Node label

A key and value on a node that describes it, such as disk type, zone, or machine family. Administrators or cloud providers set them.

Term 2

Pod label

A key and value on a pod that describes what it is, such as application name or environment. Pod affinity rules match against these.

Term 3

Selector

A question asked of labels, such as “does the label disk equal ssd?” Selectors are the building blocks of every rule.

Term 4

Topology domain

A group of nodes that share the same value for a label, for example all nodes in one zone. Pod affinity rules use domains to define what “near” means.

Well-known labels that already exist

Kubernetes and cloud providers automatically add several standard labels to nodes. These include the node’s own host name, its operating system, its processor architecture, its instance type, and the region and zone it lives in. Because they are standardized, rules written against them are portable between clusters. You can add your own labels on top for things such as disk type, hardware pools, compliance level, or team ownership.

Label PurposeWhat It Tells YouTypical Use in Rules
Host nameThe unique name of one nodeSpread copies so no two share a machine
ZoneThe data-centre zone of the nodeSurvive the loss of a single zone
RegionThe geographic region of the nodeKeep data in a legal region
Operating systemLinux or WindowsRun Windows workloads only on Windows nodes
ArchitectureThe processor family, such as amd64 or arm64Match container images to processors
Instance typeThe size or family of the machinePick memory-heavy or compute-heavy machines
Custom labelsAnything your team definesDisk type, hardware pool, compliance tier
i
Practical Tip

Before writing any rule, look at the labels on your nodes. A rule that refers to a label no node has will make every pod Pending, and this is one of the most common causes of “my pod never starts.”

4nodeSelector: The Simple Starting Point

The oldest and simplest way to steer a pod toward certain nodes.

Before affinity existed, Kubernetes offered a field called nodeSelector. You list one or more label pairs, and the pod can run only on nodes that have all of them. It is easy to read, easy to explain, and works exactly as it sounds. For many small clusters and quick needs, it is still the right tool.

Simple Analogy

nodeSelector is a hotel booking form with a single tick-box list: “sea view” and “non-smoking.” Every ticked box is mandatory. There is no way to say “I would like a sea view if possible” or “anything except the third floor.”

What nodeSelector cannot do

nodeSelector has firm limits. It can only demand an exact match of a label value. It cannot say “this or that,” it cannot say “anything but this,” it cannot express a soft preference, and it cannot refer to other pods. Every entry is a hard requirement, and all entries must be satisfied together. When your needs grow beyond that, you move on to node affinity, which is a strict superset of what nodeSelector can express.

Advantages

  • Very easy to read and to teach to new team members
  • Cheap for the scheduler to evaluate
  • Perfect for one-label needs such as operating system or hardware pool
  • Supported since the earliest versions of Kubernetes

Disadvantages / Trade-offs

  • Only exact matches, no alternatives or exclusions
  • No soft preferences, everything is mandatory
  • Cannot express spreading or co-location with other pods
  • Becomes clumsy as soon as requirements multiply
i
Using both together

If a pod specifies both nodeSelector and node affinity, a node must satisfy both to be eligible. They combine with a logical AND. This is useful for layering a broad organisational rule on top of an application-specific one.

5Node Affinity Explained

A richer way of telling the scheduler which nodes a pod likes, needs, or dislikes.

Node affinity is a set of rules inside the pod specification that describes how a pod relates to node labels. It grows nodeSelector in three ways. It supports several operators instead of only equality. It allows rules to be soft preferences as well as hard requirements. And it lets you combine conditions in flexible ways so that a pod may be happy with one of several kinds of node.

Strength 1

Required during scheduling

A hard rule. A node that does not satisfy it is filtered out. If no node qualifies, the pod stays Pending. The full field name says it is ignored during execution, which we explain soon.

Strength 2

Preferred during scheduling

A soft rule. Nodes that satisfy it gain extra score, but nodes that do not are still allowed. Each preference carries a weight from one to one hundred.

What “IgnoredDuringExecution” means

Both rule types have the words “IgnoredDuringExecution” at the end of their long field names. This is Kubernetes telling you that the rule is checked only when the pod is being scheduled. Once the pod is running, if the node labels change so that the rule is no longer satisfied, the pod is left alone and continues to run. A variant that would evict such pods was discussed by the community, but it is not implemented, so you should plan as though it does not exist.

Simple Analogy

Checking into a hotel is like scheduling. The receptionist confirms your room has a sea view when you check in. If the hotel later builds a wall in front of the window, you are not thrown out; you simply stay in the room you were given. The check happened once, at the door.

sequenceDiagram
    participant U as User
    participant API as API Server
    participant S as Scheduler
    participant N as Chosen Node
    U->>API: Create pod with node affinity rules
    API->>S: Pod is unscheduled
    S->>S: Filter using required rules
    S->>S: Score using preferred rules
    S->>API: Bind pod to best node
    API->>N: Kubelet starts containers
    Note over N: Later label changes do not move the pod
        
FIG 2 — Affinity is evaluated at scheduling time only, then the decision is final

6Required vs Preferred: Hard Rules and Soft Wishes

Choosing the right strength is the most important design decision you make.

A required rule is a promise that the pod will never run on an unsuitable node, but it comes with a risk: if no suitable node exists, the pod waits forever. A preferred rule keeps your pod running almost anywhere, but gives no guarantee. Good engineers use required rules only for things that would cause real harm if violated, and preferred rules for everything that is merely nicer.

How weights work

Each preferred rule has a weight between one and one hundred. For every node, the scheduler adds up the weights of all preferred rules that node satisfies. This sum becomes part of the node’s overall score, alongside scores from other scheduling considerations such as resource balance. A higher weight means the rule matters more when compared to other rules and other scoring factors.

1

Filter by required rules

Every node that fails a required rule is removed. Only survivors continue to the next stage.

2

Check each preferred rule per node

For every surviving node, the scheduler asks which preferences it satisfies and adds the weights of those that match.

3

Combine with other scores

The affinity points are merged with other scoring signals, such as how balanced the node’s resource usage would be.

4

Pick the winner

The node with the highest total is chosen. If two nodes tie, one is picked from among them.

AspectRequiredPreferred
Scheduler phaseFilteringScoring
If no node matchesPod stays PendingPod runs on a non-matching node
Uses weightsNoYes, one to one hundred
Best forHardware needs, compliance, isolationCost savings, locality, tidy layouts
RiskUnschedulable podsRule silently ignored under pressure
!
Common Mistake

Using a required rule for something that is only a preference is the number one cause of stuck pods. Ask yourself: if this rule cannot be met, would I rather the pod not run at all? If the honest answer is no, use a preferred rule.

7Operators and the Logic of Matching

Six operators, and two simple rules about combining them.

Node affinity rules are built from match expressions. Each expression has a label key, an operator, and, for most operators, a list of values. The operator decides what kind of question is being asked. Learning the six operators is like learning six verbs, and with them you can say almost anything about node labels.

OperatorMeaningExample in Plain Words
InThe label value is one of the listed valuesDisk type is ssd or nvme
NotInThe label value is none of the listed valuesZone is anything except the maintenance zone
ExistsThe label key is present, whatever its valueThe node has any gpu label at all
DoesNotExistThe label key is absentThe node has no spot-instance label
GtThe label value, read as a number, is greater than the given numberCore count label above sixteen
LtThe label value, read as a number, is less than the given numberGeneration label below three

How conditions combine: AND inside, OR outside

Node affinity has a two-level structure that trips up many learners. A required rule contains a list of node selector terms. Each term contains a list of match expressions. Expressions inside one term are combined with AND: all of them must be true. The terms themselves are combined with OR: it is enough for any single term to be satisfied.

flowchart TD
    R[Required node affinity] --> T1[Term 1]
    R --> T2[Term 2]
    T1 --> E1[Expression A
zone In east-1] T1 --> E2[Expression B
disk In ssd] T2 --> E3[Expression C
pool Exists] E1 -. AND .- E2 T1 -. OR .- T2 N{Node eligible if
Term 1 fully true
OR Term 2 fully true} T1 --> N T2 --> N
FIG 3 — Expressions within a term are ANDed, and separate terms are ORed
Simple Analogy

Think of a job advert that says “Applicants must have either (a law degree and five years of experience) or (a finance degree).” Each bracket is a term. Inside a bracket, every item is required. Between brackets, meeting one is enough.

Matching fields as well as labels

Besides labels, a term can match certain fields of the node object itself, most usefully the node name. This is rarely needed, because pinning a pod to one exact machine defeats the purpose of a scheduler, but it can be handy for debugging or for one-off maintenance tasks. Match fields work like match expressions and sit in the same terms.

!
Frequent Confusion

Putting two conditions in the same term when you meant “either one” makes the rule far stricter than intended, because both must then be true. If you want alternatives, use separate terms.

8Node Anti-Affinity: Saying “Not Here”

Keeping pods away from certain nodes using negative operators.

Kubernetes does not have a separate node anti-affinity field. Instead, you create the same effect by using the negative operators, NotIn and DoesNotExist, inside a node affinity rule. “Do not run on nodes labelled as experimental” becomes a rule that says the experimental label does not exist, or that its value is not in a list. The result is a pod that avoids those nodes while remaining free to use all others.

Simple Analogy

A guest booking a hotel says, “Anything except the rooms above the nightclub.” They have not chosen a room; they have only crossed a few off the list. Every other room is acceptable.

Required and preferred negatives

Negative operators can be used with either strength. A required negative is a strict exclusion: the pod will never run on a node with the unwanted label. A preferred negative simply lowers the appeal of such nodes, so the pod lands there only when better options are exhausted. This difference is important for cost control, where you might prefer to avoid expensive nodes without making the application fail when they are the only ones left.

Keep production away from experimental nodes

Nodes used for testing new kernel versions or drivers carry a special label. A required negative rule on production pods guarantees they never land on those nodes, even by accident.

Avoid interruptible machines for critical work

Cheap cloud machines that can be reclaimed at short notice are labelled by the provider or by administrators. A critical pod uses a negative rule so it never runs on such a node, while batch work happily uses them.

Steer clear of a zone under maintenance

A preferred negative rule on a zone label tells the scheduler to avoid that zone during planned work, without blocking pods if capacity elsewhere runs out.

i
Negatives and missing labels

A negative rule such as NotIn is satisfied by a node that lacks the label entirely, because the value is certainly not in the list. If you want to demand that the label exists and holds a safe value, combine an Exists condition with your In condition inside the same term.

i
Repelling versus excluding

If the goal is to keep nearly all pods away from a group of nodes, and only special pods should enter, node-side rules called taints are a better fit than adding a negative rule to every pod. We compare them in a later chapter.

9Pod Affinity and Pod Anti-Affinity

Rules about neighbours: who to sit next to, and who to avoid.

Node affinity looks at node labels. Pod affinity and pod anti-affinity look at the labels of pods that are already running. A pod affinity rule says “place me in the same area as pods that match this description.” A pod anti-affinity rule says “do not place me in the same area as pods that match this description.” The word area is precise: it is defined by a topology key, which we explore in the next chapter.

Rule 1

Pod affinity

Attracts a pod toward the same topology domain as certain other pods. Useful when two services talk a lot and benefit from short network paths.

Rule 2

Pod anti-affinity

Repels a pod from the same topology domain as certain other pods. Useful for spreading copies of one application so a single failure cannot hurt them all.

Field 1

Label selector

Describes which existing pods the rule refers to, such as all pods with a given application label.

Field 2

Topology key

Names the node label that defines what counts as the same place, such as the host name or the zone.

Field 3

Namespaces

By default, the rule looks only at pods in the same namespace as the incoming pod. You can widen it with a list of namespaces or a namespace selector.

Simple Analogy

At a wedding, the seating planner receives two kinds of notes. “Seat me at the same table as my cousins” is pod affinity. “Do not seat two of the best man’s relatives at the same table, in case a family dispute breaks out” is pod anti-affinity. The seating planner then decides what a table is: a single table, a whole section of the hall, or the entire room.

Required and preferred work the same way

Pod affinity and anti-affinity also come in required and preferred forms, with the same filtering and scoring meaning as before. A required anti-affinity rule is a strict guarantee of spreading, but it makes the number of schedulable copies depend on the number of available domains. A preferred anti-affinity rule spreads pods as much as reasonably possible, but happily doubles up when the cluster is small or busy.

!
Capacity Trap

With a required anti-affinity rule that uses the host name as the domain, you can run at most as many copies as you have nodes. Ask for six copies on a five-node cluster and one pod will stay Pending forever, which surprises many teams during scale-ups and rolling updates.

10The Topology Key: Defining “Together” and “Apart”

One label name decides the size of the neighbourhood for every pod affinity rule.

The topology key is the name of a node label. Nodes that share the same value for that label form one topology domain. When a pod affinity rule says “same domain as pods matching X,” it means “on a node whose value for the topology key equals that of a node running a pod matching X.” Changing the key changes the meaning of “together” without touching anything else in the rule.

graph TD
    subgraph Region
        subgraph Zone A
            N1[Node 1
host-1] N2[Node 2
host-2] end subgraph Zone B N3[Node 3
host-3] N4[Node 4
host-4] end end H[Key = host name
every node is its own domain] -.-> N1 Z[Key = zone
Node 1 and 2 share a domain
Node 3 and 4 share a domain] -.-> N2
FIG 4 — The same nodes form different domains depending on the topology key
Topology KeyWhat “Same Domain” MeansAnti-Affinity ResultAffinity Result
Host nameThe same single nodeNo two copies share a machineCopies land on the very same machine
ZoneThe same availability zoneNo two copies share a zoneCopies stay inside one zone, saving cross-zone traffic
RegionThe same regionCopies spread across regionsCopies stay inside one region
Custom label such as rackNodes on the same rackCopies spread across racksCopies stay on one rack

Choosing the right size of neighbourhood

Spreading over host names protects you from single-machine failures. Spreading over zones protects you from the loss of an entire data-centre zone, which is a rarer but more serious event. Many production services use both: a required or strongly weighted rule at the zone level for the big protection, and a softer rule at the host name level for tidy distribution within each zone.

i
Design Tip

Every node must carry the label named by the topology key. Nodes missing the label are treated as not belonging to any matching domain, which can produce surprising results. Confirm that the label is present on all nodes before relying on it.

“The topology key does not change what you ask for; it changes how big the neighbourhood is.”

11Comparing Placement Tools

Kubernetes offers several ways to influence placement, and each has its own strengths.

Affinity is only one member of a family of scheduling controls. Choosing the right member for the job keeps your configuration simple and predictable. The most common confusion is between affinity, which attracts pods, and taints and tolerations, which repel them from the node side.

ToolWho Defines ItDirectionStrengthBest For
nodeSelectorPodPod chooses nodesHard onlySimple single-label needs
Node affinityPodPod chooses nodesHard or softRich rules about node labels
Pod affinityPodPod chooses neighboursHard or softCo-locating chatty services
Pod anti-affinityPodPod avoids neighboursHard or softSpreading replicas for resilience
Taints and tolerationsNode and podNode repels podsHard or softReserving nodes for special workloads
Topology spread constraintsPodPod aims for even spreadHard or softBalanced distribution across domains

Taints versus affinity: a key difference

A taint is a mark placed on a node that repels every pod that does not carry a matching toleration. A toleration on a pod only says “I am allowed on that node.” It does not say “I want to be there.” So a toleration alone never pulls a pod toward a tainted node. To reserve a group of nodes for one workload, you normally use both: a taint on the nodes to keep everyone else out, and node affinity on the pods to attract them in.

Simple Analogy

A taint is a “staff only” sign on a door. A toleration is a staff badge that lets you through the door. Having the badge does not make you walk through the door. Node affinity is the instruction from your manager saying, “Go and work in that room.”

Topology spread constraints versus pod anti-affinity

Pod anti-affinity says “not with these pods,” which is a blunt instrument for spreading. Topology spread constraints say “keep the difference between the busiest and emptiest domain within a chosen limit,” which produces a balanced layout and allows more copies than there are domains. For pure even spreading, spread constraints are usually the better and cheaper tool, while anti-affinity remains ideal for saying “keep these specific workloads apart.”

i
Rule of Thumb

Use node affinity to answer “what kind of machine?”, pod affinity to answer “next to whom?”, pod anti-affinity to answer “away from whom?”, spread constraints to answer “how evenly?”, and taints to answer “who is allowed in at all?”

12When to Use Each Rule

Real situations and the rule that fits them best.

Situations that call for node affinity

Hardware-specific workloads

Machine-learning training needs accelerator cards, and video processing may need special encoders. A required node affinity rule on the hardware label sends these pods to the right machines and keeps them off nodes where they would fail.

Data locality and compliance

Some data must stay in a legal region or on nodes that passed a security audit. Required rules on region or compliance labels make this a technical guarantee rather than a hope.

Cost-aware placement

A preferred rule can lead batch jobs to cheaper interruptible machines, while a fallback to regular machines keeps work moving when the cheap ones run out.

Mixed processor architectures

When a cluster contains both amd64 and arm64 nodes, a required rule on the architecture label ensures that an image is only started on a processor it supports.

Situations that call for pod affinity

An application and its cache

A web tier that reads from an in-memory cache runs faster when both sit in the same zone, or even on the same node. A preferred pod affinity rule toward the cache pods reduces latency and cross-zone network costs.

Tightly coupled batch pipelines

Stages of a data pipeline that pass large volumes of data to each other benefit from running close together, which pod affinity at the zone level provides.

Situations that call for pod anti-affinity

Highly available web services

Copies of a service are spread across nodes and zones so that a single failure never removes all of them at once. This is the classic and most common use of anti-affinity.

Clustered databases and brokers

Members of a replicated database or message broker must not share a machine, otherwise one hardware fault could remove a majority and stop the cluster from working.

Noisy neighbours

A workload that uses disk or network heavily can hurt another. Anti-affinity between the two keeps them on separate machines and protects the sensitive one.

13Affinity During Updates, Scaling, and Maintenance

Rules that look perfect on a quiet day can misbehave when the cluster is busy changing.

Rolling updates and required anti-affinity

During a rolling update, Kubernetes may create extra pods before removing old ones. If a required anti-affinity rule forbids two copies on the same node, and every node already holds one copy, the extra pod has nowhere to go. It stays Pending, the rollout stalls, and the release stops making progress. The usual remedies are to keep some spare nodes, to allow the rollout to remove an old pod before adding a new one, or to use a preferred rule where strictness is not truly needed.

sequenceDiagram
    participant D as Deployment
    participant S as Scheduler
    participant N as Nodes (3 nodes, 3 pods)
    D->>S: Create surge pod for new version
    S->>N: Filter with required anti-affinity
    N-->>S: Every node already hosts a copy
    S-->>D: No node available
    Note over D: Surge pod stays Pending
    Note over D: Rollout waits until an old pod is removed or a node is added
        
FIG 5 — A surge pod cannot be placed when strict anti-affinity meets a full set of nodes

Autoscaling and node provisioning

Cluster autoscalers look at Pending pods and decide whether adding a node would help. A pod that is Pending because of a required affinity rule can trigger the creation of a new node, but only if that new node would satisfy the rule. If the rule asks for a label that new nodes do not receive, more machines will not help. Make sure your node pools carry the labels your rules depend on.

Maintenance and draining

When a node is drained for maintenance, its pods are evicted and recreated elsewhere, and the scheduler applies all rules again to find new homes. Tight rules can leave evicted pods with no place to go. Planning capacity with maintenance in mind, meaning always having some spare room in each zone, keeps drains from turning into outages.

!
Static Nature

Because rules are only evaluated when a pod is placed, a layout that was perfectly spread yesterday may become lopsided after several restarts and drains. Kubernetes will not rebalance on its own. A separate tool called the descheduler can evict badly placed pods so they are scheduled again under the current rules.

Event 1

Node failure

Pods from the failed node are recreated and rescheduled. Anti-affinity helps by having ensured that not all copies were on that node in the first place.

Event 2

Scale up

New replicas are placed using the same rules. Required anti-affinity limits how many can be added before running out of domains.

Event 3

Label change

Editing node labels affects only future scheduling decisions. Running pods are not moved.

14Performance and Scale Considerations

Not all rules cost the same for the scheduler to evaluate.

Node affinity is relatively cheap. For each pod, the scheduler looks at each node’s labels and checks a few conditions. The work grows in step with the number of nodes and is easy to handle even in very large clusters. This is why node affinity and nodeSelector are considered safe at almost any scale.

Pod affinity and anti-affinity are more expensive. To evaluate them, the scheduler must consider all existing pods that match the selector and work out which domains they occupy, for every candidate node. The cost grows with both the number of pods and the number of nodes, and the official guidance warns that these rules can slow scheduling noticeably in clusters with several hundred nodes. For very large clusters, topology spread constraints often achieve the same goal at lower cost.

Advantages of keeping rules lean

  • Faster scheduling during bursts of new pods
  • Easier debugging when something is Pending
  • Fewer accidental conflicts between rules
  • Less risk of stalled rollouts

Costs of heavy rule sets

  • Longer scheduling latency in large clusters
  • Rules that interact in ways nobody predicted
  • More pods stuck Pending when capacity is tight
  • Harder for new team members to understand the layout
Simple Analogy

Checking whether a room has a sea view is a quick glance at the room. Checking whether a room is far from every other guest in the wedding party means looking up where all those guests are staying. The second check is much slower when the hotel is large and full.

i
Practical Tip

Limit the label selector in pod affinity rules to the smallest, most specific group of pods. A narrow selector means fewer pods to consider and quicker decisions, as well as a clearer meaning for anyone reading the rule later.

15Security and Isolation Considerations

Placement rules can protect workloads, but only if the labels themselves can be trusted.

Affinity is frequently used to isolate sensitive workloads, for example keeping payment services on nodes reserved for that purpose. This only works if two conditions hold: no untrusted pod can land on those nodes, and no untrusted party can change the labels those rules rely on. Otherwise the rule is a polite suggestion rather than a security control.

Protect the isolation labels

A component on each node can normally set some labels on its own node. Kubernetes reserves a special label prefix for isolation purposes that node components are not allowed to modify when the relevant admission control is enabled. Use that prefix for labels that guard sensitive workloads so a compromised node cannot relabel itself to attract them.

Combine attraction with repulsion

Node affinity alone pulls your sensitive pods toward dedicated nodes, but it does not stop other pods from landing there. Add a taint on those nodes so that only pods with the matching toleration may enter, and you get a two-way guarantee.

Control who can write pod specifications

Anyone able to create pods can write affinity rules, including rules that target sensitive nodes or attempt to crowd out other workloads. Role-based access control and admission policies should limit which teams can use which labels and rules.

Watch for information leaks through anti-affinity

A pod anti-affinity rule that refers to another team’s pod labels can, in theory, reveal that those pods exist and where they run. Policies that limit cross-namespace affinity terms reduce this exposure.

!
Do Not Assume

Affinity is a scheduling hint, not a sandbox. Pods placed on the same node still share a kernel. For strong isolation between tenants, add stronger controls such as sandboxed runtimes or separate clusters, and treat placement rules as one layer among several.

16Troubleshooting, Monitoring, and Metrics

When a pod will not start, the cluster usually tells you exactly why, if you know where to look.

The most common symptom of an affinity problem is a pod stuck in the Pending state. The scheduler records a message in the pod’s events that lists how many nodes were rejected and for what reason. Reading that message carefully solves most cases quickly, because it tells you whether the problem is node labels, pod affinity, anti-affinity, taints, or plain lack of resources.

1

Read the scheduling event

The message typically says that none of the nodes are available and breaks the count down by reason, such as nodes that did not match the node affinity rules or nodes that conflicted with pod anti-affinity.

2

Compare labels with rules

List the labels on your nodes and check that the keys and values named in the rule really exist. A single typo in a label value is enough to make every node fail the rule.

3

Check the topology key

For pod affinity, confirm that all nodes carry the topology key label. Missing labels create unexpected domains.

4

Count domains against replicas

With required anti-affinity, compare the number of replicas with the number of domains. More replicas than domains guarantees Pending pods.

5

Consider taints and resources

Sometimes affinity is fine, and the real blocker is a taint without a matching toleration, or nodes without enough free CPU and memory.

What to monitor over time

Signal 1

Pending pod count

A steady number of Pending pods is the clearest sign that a rule cannot be satisfied. Alert on pods that stay Pending beyond a few minutes.

Signal 2

Scheduling latency

The time between pod creation and placement rises when heavy pod affinity rules meet a large cluster.

Signal 3

Pods per node and per zone

A simple chart of how many replicas of each service run in each zone reveals when spreading has drifted.

Signal 4

Failed scheduling events

Counting these events by reason shows which type of rule causes the most trouble across your teams.

17Design Patterns for Placement

Proven combinations that solve recurring problems.

Pattern: Zone-first, node-second spreading

Use a strongly weighted preferred anti-affinity at the zone level and a lighter preferred anti-affinity at the host name level. Copies spread across zones first, then across machines inside each zone, without ever blocking scheduling in a small cluster.

Pattern: Dedicated node pool

Label and taint a pool of nodes for one team or workload type. Give that workload a matching toleration and a required node affinity. Other pods are kept out by the taint, and yours are drawn in by the affinity.

Pattern: Cheap first, reliable fallback

Give batch pods a preferred affinity for interruptible nodes with a high weight, and no requirement at all. They use the cheap machines when available and quietly move to regular ones when not.

Pattern: Co-locate, but only softly

For an application and its cache, a preferred pod affinity at the zone level yields lower latency in the normal case without risking Pending pods if the cache is busy elsewhere.

Pattern: Anti-affinity for quorum members

For a replicated store that needs a majority to function, use required anti-affinity at the host name level, and size the cluster so that there are always more nodes than members.

Pattern: Architecture guard

In a cluster with mixed processors, add a required node affinity on the architecture label to every workload whose image supports only one, so a wrong start-up failure never happens.

flowchart TD
    Q1{What do you need?} -->|Right kind of machine| NA[Node affinity]
    Q1 -->|Near other pods| PA[Pod affinity]
    Q1 -->|Away from other pods| PAA[Pod anti-affinity]
    Q1 -->|Even distribution| TS[Topology spread constraints]
    Q1 -->|Reserve nodes| TT[Taints + tolerations + node affinity]
    NA --> S1{Is it truly mandatory?}
    PA --> S1
    PAA --> S1
    S1 -->|Yes| RQ[Use required]
    S1 -->|No| PR[Use preferred with weight]
        
FIG 6 — A quick decision guide for choosing a placement tool and its strength

18Anti-patterns to Avoid

Habits that create stuck pods, stalled rollouts, and confusing clusters.

ANTI-PATTERN-01 Avoid
Problem

Using required rules for preferences that are merely nice to have.

Why It’s Harmful

Any shortage of matching nodes leaves pods Pending, turning a capacity wobble into an outage.

Correct Approach

Reserve required rules for real needs such as hardware, legal boundaries, and isolation, and use weighted preferences for everything else.

ANTI-PATTERN-02 Avoid
Problem

Required host-level anti-affinity with more replicas than nodes.

Why It’s Harmful

Extra replicas can never be placed, and rolling updates that need surge pods stall.

Correct Approach

Keep replicas below the node count with headroom, use a preferred rule, or use topology spread constraints that tolerate doubling up.

ANTI-PATTERN-03 Avoid
Problem

Relying on a toleration alone to place pods on dedicated nodes.

Why It’s Harmful

A toleration only permits entry; it does not attract. The pods may still land elsewhere, and other pods may land on the dedicated nodes if there is no taint.

Correct Approach

Pair a taint on the nodes with a toleration and a node affinity on the pods.

ANTI-PATTERN-04 Avoid
Problem

Hard-coding a node name to pin a pod to one machine.

Why It’s Harmful

The pod cannot survive the loss of that node, and the scheduler’s flexibility is thrown away.

Correct Approach

Describe the properties you need through labels and let any suitable node be chosen.

ANTI-PATTERN-05 Avoid
Problem

Heavy pod anti-affinity across every workload in a very large cluster.

Why It’s Harmful

Scheduling slows dramatically as the scheduler compares many pods against many nodes.

Correct Approach

Prefer topology spread constraints for general spreading and keep anti-affinity for the few workloads that truly need strict separation.

ANTI-PATTERN-06 Avoid
Problem

Expecting Kubernetes to move running pods when labels change.

Why It’s Harmful

Layouts silently drift from the intended design, and teams believe protections exist that do not.

Correct Approach

Remember that rules apply at placement time only, and use a descheduler or planned restarts when a layout must be corrected.

19Best Practices and Common Mistakes

A short checklist to keep placement rules understandable and safe.

Best Practices

  • Start with the simplest tool that expresses your need
  • Use required rules only for genuine must-haves
  • Give preferred rules meaningful, well-separated weights
  • Verify that labels and topology keys exist on all nodes
  • Keep pod selectors narrow and specific
  • Plan spare capacity for rolling updates and drains
  • Document why each rule exists so future teams can judge it

Common Mistakes

  • Misspelling label keys or values and creating impossible rules
  • Putting alternatives in one term when they belong in separate terms
  • Forgetting that the same namespace is assumed for pod affinity
  • Assuming affinity evicts pods when labels change
  • Ignoring the interaction with taints and resource requests
  • Copying a rule from another cluster without checking its labels
!
Check Before You Ship

Before releasing a new rule to production, test it in a cluster that resembles production in node count and zone layout. A rule that works on a three-node test cluster with one zone can behave very differently when the real layout has several zones, each with a different number of nodes.

20Real-World Scenarios

How teams apply these ideas in everyday operations.

A retailer running checkout across three zones

An online retailer runs a dozen copies of its checkout service. A preferred pod anti-affinity at the zone level with a high weight spreads the copies across three zones, and a lighter host-level rule keeps them tidy inside each zone. When one zone has a network problem, the other two keep serving customers without interruption.

A research team sharing accelerator nodes

A company reserves its accelerator machines for training jobs. The nodes carry a taint that repels normal workloads, and the training pods carry a matching toleration plus a required node affinity for the accelerator label. General services never land there, and training jobs never end up on machines without cards.

A message broker cluster with three members

A team runs a broker that needs two of three members alive to keep working. They use required host-level anti-affinity so no two members share a machine, and they keep at least five nodes in the cluster so maintenance on one never leaves a member with no place to run.

A media company controlling cost

Transcoding jobs use a preferred node affinity for interruptible machines with a high weight. Most of the time the jobs run on cheap capacity. When the cloud reclaims those machines, the jobs restart on regular nodes automatically, because the rule never made them mandatory.

A regulated financial workload

A bank must keep customer records inside a specific region. A required node affinity on the region label, together with protected isolation labels that node components cannot edit, makes the placement rule reliable and auditable.

21Frequently Asked Questions

Q1What is the difference between nodeSelector and node affinity?

nodeSelector only supports exact label matches, and every entry is mandatory. Node affinity supports several operators, alternatives, and soft preferences with weights. Anything nodeSelector can do, node affinity can do too.

Q2Is there a separate node anti-affinity feature?

Not as a distinct field. You express it with the NotIn and DoesNotExist operators inside node affinity. Pod anti-affinity, on the other hand, is a named feature for keeping pods apart from other pods.

Q3What happens if no node satisfies a required rule?

The pod stays Pending and the scheduler keeps retrying. An event on the pod explains how many nodes were rejected and why. The pod starts when a suitable node appears.

Q4Will a running pod move if node labels change?

No. Rules are evaluated only when the pod is scheduled, which is what the words IgnoredDuringExecution mean. Running pods stay where they are until they are restarted or evicted for another reason.

Q5How do multiple node selector terms combine?

Terms are combined with OR, so a node needs to satisfy only one. Expressions within a single term are combined with AND, so all of them must be true.

Q6What does the weight in a preferred rule do?

It sets how many points a node earns for satisfying that rule, from one to one hundred. The scheduler sums the weights for each node and adds them to its other scores, so higher weights make the rule matter more.

Q7Why is my pod Pending even though nodes have free resources?

Placement rules, taints, or volume constraints may be rejecting those nodes. Read the scheduling event on the pod, which normally lists the reasons and how many nodes each affected.

Q8What is a topology key?

It is the name of a node label that defines what counts as the same place for pod affinity and anti-affinity. Using the host name means the same node, and using the zone means the same zone.

Q9Should I use pod anti-affinity or topology spread constraints?

For an even spread across many domains, topology spread constraints are usually simpler and cheaper. For keeping specific workloads apart from each other, or for strict one-per-domain rules, pod anti-affinity is the right fit.

Q10Does a toleration attract pods to a tainted node?

No. A toleration only allows a pod to run on a tainted node. To draw pods in, add a node affinity rule that prefers or requires that node group.

Q11Do pod affinity rules look at pods in other namespaces?

By default they consider only pods in the same namespace as the pod being scheduled. You can widen the scope with a namespace list or a namespace selector.

Q12Can affinity rules slow down my cluster?

Node affinity is cheap, but pod affinity and anti-affinity become costly in large clusters because the scheduler must compare many pods against many nodes. Keep selectors narrow and consider spread constraints at scale.

22Summary and Key Takeaways

Affinity and anti-affinity are the vocabulary Kubernetes gives you for telling the scheduler where pods belong. Node affinity speaks about node labels, letting you attract pods to the right kind of machine and, with negative operators, keep them away from the wrong kind. Pod affinity and pod anti-affinity speak about neighbours, letting you place related pods close together or spread copies apart, with the topology key defining how large a neighbourhood is.

The most important idea is the difference between required and preferred. Required rules filter nodes and can leave pods Pending, while preferred rules only adjust scores and always allow the pod to run. Because rules are applied only at scheduling time, they need to be paired with sensible capacity planning, taints for reserving nodes, protected labels for security, and monitoring for stuck pods. Used with care, these tools make your applications resilient, efficient, and predictable.

Key Takeaways

  • Node affinity matches node labels — it upgrades nodeSelector with six operators, alternatives, and soft preferences, and expresses “anti-affinity” through NotIn and DoesNotExist.
  • Required filters, preferred scores — a required rule can leave a pod Pending, while a preferred rule with a weight from one to one hundred only tilts the choice.
  • AND inside a term, OR between terms — getting this structure right prevents rules from being far stricter or looser than intended.
  • The topology key sets the neighbourhood — host name, zone, and region give protection from machine, zone, and region failures respectively.
  • Rules apply only at placement — running pods are never moved when labels change, so layouts can drift and may need a descheduler or restarts.
  • Plan capacity around strict rules — required anti-affinity limits replicas to the number of domains and can stall rolling updates that need surge pods.
  • Combine tools deliberately — use taints to repel, affinity to attract, spread constraints for balance, and protected labels so isolation rules can be trusted.