What is the main purpose of pod affinity in Kubernetes?
Think about how pods can be placed close to similar pods for better communication.
Pod affinity lets you schedule pods close to other pods with matching labels, which helps reduce network latency and improve performance.
Given the following pod anti-affinity rule, what will happen when scheduling a new pod?
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- frontend
topologyKey: kubernetes.io/hostnameThe cluster has nodes with existing pods labeled app=frontend on some nodes.
Anti-affinity means avoiding nodes with certain pods.
This anti-affinity rule prevents scheduling the new pod on nodes that already have pods labeled app=frontend, ensuring they don't co-locate on the same host.
Which YAML snippet correctly configures requiredDuringSchedulingIgnoredDuringExecution pod affinity to schedule pods on nodes with pods labeled role=database?
Look for required affinity with matching labels and correct topology key.
Option C correctly uses requiredDuringSchedulingIgnoredDuringExecution with matchLabels to require pods to be scheduled on nodes with pods labeled role=database.
A pod with the following affinity rule fails to schedule:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: backend
topologyKey: failure-domain.beta.kubernetes.io/zoneWhat is the most likely cause?
Affinity requires matching pods to exist in the specified topology.
The requiredDuringScheduling rule demands the pod be scheduled on a node in the same zone as pods labeled app=backend. If none exist, scheduling fails.
You want to deploy multiple replicas of a critical service so that no two replicas run on the same node to improve availability.
Which pod anti-affinity configuration achieves this?
To avoid co-locating pods on the same node, use anti-affinity with hostname topology.
Option D uses podAntiAffinity with requiredDuringSchedulingIgnoredDuringExecution and topologyKey kubernetes.io/hostname, ensuring pods do not run on the same node.