← Back to list

Pod affinity and Pod anti-affinity

What is Pod affinity and Pod anti-affinity? 🥸

Khushi Malhotra · 2024-08-16 15:07 · 0 claps · 2.0 min read
#kubernetes #pod-affinity #pod-anti-affinity #affinity #kubectl
Open on Medium ↗
Wiki topics: TLS · Design Tools & Workflow ☁️ · DevOps & Cloud

Pod affinity and Pod anti-affinity

What is Pod affinity and Pod anti-affinity? 🥸

Pod affinity allow you to constrain which node your pod is eligible to be scheduled based on labels on pods that are already running on the node.

Pod anti-affinity is the opposite which ensures that certain pods donot run on the same nodes.

Scenario: Imagine you’re organizing a party, and you want to make sure certain groups of friends sit together or at least at nearby tables. You might also want to make sure that people who don’t get along sit far apart.

Pod Affinity:

  1. Pod Affinity (Friends Sitting Together):
  • You have a group of friends who are close, and you want them to sit at the same table or nearby tables. In Kubernetes, if you have two services (let’s say a web server and a caching server) that work closely together, you can use pod affinity to ensure that these pods are scheduled on the same node or close to each other. This can help reduce latency or improve communication between these services.
  • Example: You have a web server and a database that communicate frequently. You want them to be scheduled on the same node to minimize network latency. So, you use pod affinity to ensure that wherever the web server pod is scheduled, the database pod should also be scheduled there.

Pod Anti-Affinity:

Pod Anti-Affinity (Enemies Sitting Apart):

  • Now, think of two people at your party who don’t get along. You want to make sure they sit as far apart as possible. Similarly, in Kubernetes, you might want to prevent certain pods from being scheduled on the same node to ensure redundancy or to avoid resource contention. This is where pod anti-affinity comes in.
  • Example: You have two replicas of the same application (for example, two instances of a web server). To ensure high availability, you don’t want them on the same node because if that node fails, both replicas would go down. You can use pod anti-affinity to make sure that each replica is scheduled on a different node.

How we can define them and what all attributes are included in it, we will be discussing this in our next blog. Thanks for reading.😉 See you again. 🫠


메타데이터
post_id
7adffbf994bc
slug
pod-affinity-and-pod-anti-affinity-7adffbf994bc
url
https://medium.com/@khushimalhoz/pod-affinity-and-pod-anti-affinity-7adffbf994bc
canonical_url
https://medium.com/@khushimalhoz/pod-affinity-and-pod-anti-affinity-7adffbf994bc
author_url
https://medium.com/@khushimalhoz
status
ok
fetched_at
2026-08-29 00:13:12