Skip to main content

Affinity

Affinity is a set of Kubernetes scheduling rules that attract pods to certain nodes, or toward and away from other pods. It comes in two kinds, node affinity (pod-to-node) and pod affinity or anti-affinity (pod-to-pod), and each rule is either required (hard) or preferred (soft).

What affinity does

Affinity lets a pod tell the scheduler where it wants to run. Node affinity matches against node labels, such as instance type or availability zone. Pod affinity and anti-affinity match against the labels of pods already running, so you can co-locate chatty services or spread replicas across failure domains.

Affinity is the pull side of scheduling. Taints are the push side: a taint repels pods from a node, while affinity draws a pod toward one. Dedicated node pools usually need both.

flowchart TD A["Control pod placement?"] -->|"By node labels"| B["Node affinity"] A -->|"By other pods"| C{"Together or apart?"} C -->|"Together"| D["Pod affinity"] C -->|"Apart"| E["Pod anti-affinity"] classDef start fill:#475569,color:#ffffff classDef node fill:#DBEAFE,color:#1E293B classDef pod fill:#D1FAE5,color:#1E293B class A,C start class B node class D,E pod

Required vs preferred rules

Rule Behaviour
requiredDuringSchedulingIgnoredDuringExecution Hard rule. The pod stays Pending if no node matches
preferredDuringSchedulingIgnoredDuringExecution Soft rule. The scheduler tries, then falls back, using a weight from 1 to 100

Example: GPU nodes with a zone preference

A market-data service in the style of Zerodha's must run on GPU nodes and should prefer the Mumbai zone ap-south-1a.

YAML
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node.kubernetes.io/instance-type
operator: In
values: ["p3.2xlarge", "p3.8xlarge"]
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 80
preference:
matchExpressions:
- key: topology.kubernetes.io/zone
operator: In
values: ["ap-south-1a"]

Example: spread replicas across nodes

YAML
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchLabels:
app: order-service
topologyKey: kubernetes.io/hostname # "same node" is the domain

Change topologyKey to topology.kubernetes.io/zone to spread across zones instead of nodes.

Tip

For simple spreading, topologySpreadConstraints is usually easier to reason about than anti-affinity, and it handles uneven replica counts gracefully.

Remember

IgnoredDuringExecution means a running pod is not evicted if node labels change later. Rules apply only at scheduling time.

Common Mistake

Using required anti-affinity with more replicas than nodes. With 3 nodes and 4 replicas, the fourth pod stays Pending forever. Use preferred rules unless you are certain about node count.

Debug a stuck pod with kubectl describe pod. The Events section names the rule that blocked scheduling.

Frequently Asked Questions

What is the difference between affinity and nodeSelector?

nodeSelector is a plain equality match on node labels, so a pod either lands on a matching node or stays Pending. Affinity adds set-based operators like In and NotIn, soft preferences with weights, and pod-to-pod rules that nodeSelector cannot express.

How do affinity and taints relate?

A taint repels pods from a node unless they tolerate it, while affinity attracts a pod to nodes or other pods. A toleration only permits a pod onto a tainted node, so dedicated node pools combine a taint, a toleration, and node affinity.

Why is my pod Pending with an affinity rule?

A required rule that no node can satisfy leaves the pod unschedulable, and this often appears during rolling updates when capacity is tight. The Events section of kubectl describe pod lists which rule failed and how many nodes it excluded.

Found a mistake, an outdated step or a broken link?