Kubernetes Pod Affinity and Anti-Affinity are scheduling rules that tell Kubernetes where to place pods based on the labels of other pods already running in the cluster. Pod affinity places pods near each other, while pod anti-affinity keeps pods apart. In practical terms, they help you control workload placement for latency, availability, cost, and operational safety.
By default, the Kubernetes scheduler places pods on nodes that have enough available resources and satisfy basic constraints. Pod affinity and anti-affinity add another layer of intent: they let you say, “run this pod close to those pods” or “do not run this pod near those pods.”
These rules are useful when Kubernetes resource requests alone are not enough to express how your workloads should be placed.
Pod affinity and anti-affinity are configured in a pod spec under spec.affinity. The scheduler evaluates these rules during pod placement.
The rules usually depend on three parts:
app=redis or tier=frontend.kubernetes.io/hostname for a node or topology.kubernetes.io/zone for an availability zone.requiredDuringSchedulingIgnoredDuringExecution means the scheduler must satisfy the rule when placing the pod. If no valid node exists, the pod stays pending.
This is useful for hard requirements, such as keeping replicas of the same critical service on different nodes.
preferredDuringSchedulingIgnoredDuringExecution means the scheduler tries to satisfy the rule, but it can still place the pod elsewhere if needed.
This is safer for many production workloads because it reduces the chance of pods getting stuck during node pressure, upgrades, or scaling events.
For example, if you run Apache Airflow on EKS, you may want schedulers, workers, and supporting services placed with different rules depending on their failure tolerance and traffic patterns. A guide like deploying Apache Airflow on AWS Elastic Kubernetes Service shows the kind of real workload where placement decisions can matter.
This example uses pod anti-affinity to prefer placing replicas of the same app on different nodes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchLabels:
app: api
topologyKey: kubernetes.io/hostname
containers:
- name: api
image: example/api:1.0.0
With this configuration, Kubernetes tries to avoid placing multiple app=api pods on the same node. If the cluster has fewer available nodes than replicas, the scheduler can still place more than one replica on a node because the rule is preferred, not required.
The key question is whether proximity helps or hurts the workload. If being close improves latency or reduces data transfer, affinity may help. If being together increases outage risk, anti-affinity is usually the better fit.
Pod affinity and node affinity solve different scheduling problems.
Use node affinity when the workload needs a specific kind of node. Use pod affinity or anti-affinity when the workload needs to be placed relative to other pods.
Pod anti-affinity and topology spread constraints can both spread pods, but they work differently.
For many modern Kubernetes clusters, topology spread constraints are easier to use for even replica distribution. Pod anti-affinity is still useful when you need stricter separation logic based on pod labels.
IgnoredDuringExecution behavior means Kubernetes does not evict running pods if labels or topology change later.During Kubernetes upgrades, strict anti-affinity can also affect how quickly workloads reschedule when nodes are drained. If you manage production clusters, review placement rules as part of your upgrade planning. These practical tips for Kubernetes upgrades cover related operational concerns.
Pod affinity and anti-affinity rules often live in Helm charts, Kustomize overlays, Terraform-managed manifests, or GitOps repositories. If your team manages Kubernetes resources through Terraform, you can define these rules alongside deployments, services, and config maps. See this guide on how to deploy Kubernetes resources using Terraform for a related workflow.
Teams that manage cloud resources through Kubernetes APIs may also combine scheduling rules with platform tools such as Crossplane. For example, you might provision cloud infrastructure and deploy workloads through Kubernetes-native definitions, as shown in this guide to deploy AWS resources using Crossplane on Kubernetes.
preferredDuringSchedulingIgnoredDuringExecution unless you have a strict availability or compliance requirement.topology.kubernetes.io/zone when you care about zone-level failure separation.kubernetes.io/hostname when you care about node-level separation.app, component, tier, and release.Kubernetes Pod Affinity and Anti-Affinity give you label-based control over where pods run relative to other pods. Use affinity to place related workloads close together. Use anti-affinity to spread replicas or conflicting workloads apart. Start with preferred rules, choose the right topology key, and avoid hard constraints unless your workload truly needs them.