Kubernetes Affinity

A-Dem
3 min readNov 6, 2023

One important concept that can greatly influence how your pods are scheduled on nodes is Kubernetes Affinity. In this article, we’ll take a deep dive into Kubernetes Affinity, explaining it in plain English and providing detailed examples, including code snippets.

What Is Kubernetes Affinity?

At its core, Kubernetes Affinity is all about influencing the placement of pods on specific nodes based on conditions you define. This can be incredibly useful when you want to ensure that your pods are scheduled on nodes with specific characteristics or to distribute workloads evenly across your cluster.

There are two types of Kubernetes Affinity:

Node Affinity: With node affinity, you can specify the conditions that nodes in the cluster must satisfy to host your pods. This allows you to control where your workloads are placed, considering factors like hardware, labels, or other node attributes.

Pod Affinity: Pod affinity, on the other hand, allows you to influence the placement of pods in relation to other pods. You can ensure that pods are co-located or separated based on various criteria, such as labels, namespaces, or other pod attributes.

Node Affinity: Node Affinity is further divided into two parts…

--

--

A-Dem

Writing for fun! Tech, non-tech, funny, self improvement, devops, developing, coding etc...