Junior — Middle
Explain the concept of Affinity in Kubernetes and how it affects pod distribution across nodes.
sobes.tech AI
Answer from AI
Affinity in Kubernetes is a mechanism that allows you to set rules for scheduling pods on cluster nodes.
There are two main types of affinity:
- Node Affinity — sets preferences or requirements for nodes where pods can run, for example, based on node labels.
- Pod Affinity/Anti-Affinity — sets rules for pods to be placed near other pods (affinity) or on different nodes (anti-affinity).
This affects pod distribution so that the scheduler considers these rules when selecting a node, ensuring, for example, high availability, load balancing, or resource compliance.
Example of Node Affinity in a manifest:
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd