Understanding Kubernetes: Part 25 Affinity and Anti-Affinity

📢 If you’ve been following our Kubernetes series 2025, welcome back! For new readers, check out Part 24: Taints and Tolerations
What is Node Affinity?
Node Affinity is a rule that ensures Pods are scheduled on specific nodes based on defined labels.
What is Pod Affinity and Anti-Affinity?
Pod Affinity ensures that Pods are scheduled close to other Pods based on label selectors.
Pod Anti-Affinity prevents Pods from being scheduled on the same nodes or within a failure domain.
Use Cases
Affinity and Anti-Affinity are useful in various scenarios:
1. Node Affinity
Ensuring that workloads requiring SSD storage run on nodes labeled with
storage=ssd
.Running GPU-intensive workloads on nodes labeled
gpu=true
.
2. Pod Affinity
Ensuring that microservices communicate efficiently by scheduling them on the same node.
Keeping interdependent applications (e.g., frontend and backend) close together to reduce latency.
3. Pod Anti-Affinity
Distributing replicas of an application across multiple nodes for high availability.
Ensuring that critical workloads are not placed on the same node to prevent single points of failure.
Node Affinity Syntax
Node Affinity rules are defined in the Pod specification under affinity.nodeAffinity
.
Example: Scheduling a Pod on SSD Nodes
apiVersion: v1
kind: Pod
metadata:
name: ssd-pod
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "storage"
operator: "In"
values:
- "ssd"
containers:
- name: app
image: my-app
This ensures that the Pod is scheduled only on nodes labeled storage=ssd
.
Pod Affinity Syntax
Pod Affinity rules are defined under affinity.podAffinity
.
Example: Placing Pods Close Together
apiVersion: v1
kind: Pod
metadata:
name: web-pod
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
labelSelector:
matchLabels:
app: frontend
topologyKey: "kubernetes.io/hostname"
containers:
- name: web
image: nginx
This ensures that the Pod is scheduled on the same node as other Pods labeled app=frontend
.
Pod Anti-Affinity Syntax
Pod Anti-Affinity is defined under affinity.podAntiAffinity
.
Example: Spreading Replicas Across Nodes
apiVersion: v1
kind: Pod
metadata:
name: backend-pod
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
labelSelector:
matchLabels:
app: backend
topologyKey: "kubernetes.io/hostname"
containers:
- name: backend
image: my-backend
This ensures that backend
Pods are scheduled on different nodes to improve reliability.
In My Previous Role
As a Senior DevOps Engineer, I leveraged Affinity and Anti-Affinity for:
Optimizing Microservice Deployments: Ensured interdependent services were scheduled on the same node to reduce latency.
High Availability Strategies: Used Anti-Affinity to distribute replicas across nodes to prevent single points of failure.
Performance Tuning: Enforced Node Affinity rules to assign workloads to high-memory nodes for better performance.
🚀 Ready to Master Kubernetes?
Take your Kubernetes journey to the next level with the Master Kubernetes: Zero to Hero course! 🌟 Whether you’re a beginner or aiming to sharpen your skills, this hands-on course covers:
✅ Kubernetes Basics — Grasp essential concepts like nodes, pods, and services. ✅ Advanced Scaling — Learn HPA, VPA, and resource optimization. ✅ Monitoring Tools — Master Prometheus, Grafana, and AlertManager. ✅ Real-World Scenarios — Build production-ready Kubernetes setups.
🎓 What You’ll Achieve
💡 Confidently deploy and manage Kubernetes clusters. 🛡️ Secure applications with ConfigMaps and Secrets. 📈 Optimize and monitor resources for peak performance.
🔥 Start Learning Now: [Join the Master Kubernetes Course](https://cloudops0.gumroad.com/l/k8s)
Don’t miss your chance to become a Kubernetes expert! 💻✨
🚀 Stay ahead in DevOps and SRE! 🔔 Subscribe now and never miss a beat on Kubernetes and more. 🌟
🚀 Master Terraform: Infrastructure as Code
🔥 Start Learning Now: Join the Master Terraform Course
Last updated