Course contentsShow
← Cloud and Infrastructure
Lesson 1352 of 2,98927. Kubernetes Scheduling and ResourcesFree lesson

Pod Anti-Affinity Basics

Learn how pod anti-affinity spreads pods apart for high availability and fault tolerance.

Pod Anti-Affinity Basics

You'll learn how pod anti-affinity spreads your pods across different nodes or zones to prevent single points of failure.

What Is Pod Anti-Affinity?

Where pod affinity attracts pods to run together, pod anti-affinity does the opposite—it pushes pods apart. This is your high-availability and fault-tolerance tool. By telling Kubernetes "don't schedule these pods on the same node" or "keep these pods in different zones," you ensure that if one node or zone fails, your application remains available.

Think of it like fire exits in a building: you don't put all the emergency exits on the same wall. You spread them out so that no matter where the problem occurs, people have options.

How It Works

Pod anti-affinity uses label selectors to identify which pods should be kept apart, and a topology key to define the boundary. Common topology keys include:

  • kubernetes.io/hostname — keep pods on different nodes
  • topology.kubernetes.io/zone — keep pods in different availability zones

You can specify anti-affinity as:

  • Required (requiredDuringSchedulingIgnoredDuringExecution) — hard rule, scheduling fails if it can't be satisfied
  • Preferred (preferredDuringSchedulingIgnoredDuringExecution) — soft rule, scheduler tries its best

Use Cases

  • Database replicas: Spread primary and replicas across nodes so a node failure doesn't take down your entire database
  • Multi-zone deployments: Distribute pods across availability zones for regional resilience
  • Noisy neighbor prevention: Keep resource-intensive pods away from each other

Key Takeaway: Pod anti-affinity strategically separates pods based on labels and topology constraints, ensuring your workloads survive node or zone failures by avoiding single points of failure.