$linuxjunkies
>

pod affinity

also: pod affinity rule, pod co-location

A Kubernetes scheduling constraint that attracts pods to run on the same nodes as other pods, based on label selectors. Pod affinity ensures related workloads are colocated for performance or operational reasons.

Pod affinity is a Kubernetes feature that influences pod placement decisions, pulling pods toward nodes where other pods matching certain criteria are already running. It's the opposite of pod anti-affinity, which repels pods away from each other.

Pod affinity rules are defined in pod specifications using label selectors to identify "target" pods. When a pod with affinity rules is scheduled, the Kubernetes scheduler preferentially places it on nodes where those target pods exist, improving data locality or keeping dependent services close together.

Example: A web application pod might specify affinity for pods labeled app=database, ensuring the web tier runs on the same node as its database for lower latency. Affinity can be set as requiredDuringSchedulingIgnoredDuringExecution (hard requirement) or preferredDuringSchedulingIgnoredDuringExecution (soft preference).

Related terms