0
0
Kubernetesdevops~20 mins

Pod affinity and anti-affinity in Kubernetes - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pod Affinity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Pod Affinity Basics

What is the main purpose of pod affinity in Kubernetes?

ATo schedule pods to run on nodes that already have pods with matching labels, improving locality.
BTo automatically scale pods based on CPU usage.
CTo prevent pods from running on nodes with certain labels to avoid resource conflicts.
DTo restrict pods to run only on nodes with specific hardware capabilities.
Attempts:
2 left
💡 Hint

Think about how pods can be placed close to similar pods for better communication.

💻 Command Output
intermediate
2:00remaining
Pod Anti-Affinity Scheduling Result

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/hostname

The cluster has nodes with existing pods labeled app=frontend on some nodes.

AThe new pod will be scheduled only on nodes that have pods with label app=frontend.
BThe new pod will not be scheduled on any node that already runs a pod with label app=frontend.
CThe new pod will be scheduled on any node regardless of existing pods.
DThe new pod will be scheduled only on nodes without the label app=frontend.
Attempts:
2 left
💡 Hint

Anti-affinity means avoiding nodes with certain pods.

Configuration
advanced
2:30remaining
Correct Pod Affinity YAML Configuration

Which YAML snippet correctly configures requiredDuringSchedulingIgnoredDuringExecution pod affinity to schedule pods on nodes with pods labeled role=database?

A
affinity:
  podAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchLabels:
              role: database
          topologyKey: kubernetes.io/hostname
B
affinity:
  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchLabels:
            role: database
        topologyKey: kubernetes.io/hostname
C
affinity:
  podAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchLabels:
            role: database
        topologyKey: kubernetes.io/hostname
D
affinity:
  podAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
            - key: role
              operator: NotIn
              values:
                - database
        topologyKey: kubernetes.io/hostname
Attempts:
2 left
💡 Hint

Look for required affinity with matching labels and correct topology key.

Troubleshoot
advanced
2:30remaining
Pod Fails to Schedule Due to Affinity Rules

A pod with the following affinity rule fails to schedule:

affinity:
  podAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchLabels:
            app: backend
        topologyKey: failure-domain.beta.kubernetes.io/zone

What is the most likely cause?

ANo existing pods with label app=backend are running in the same zone, so the scheduler cannot place the pod.
BThe topologyKey is deprecated and causes the scheduler to ignore the rule.
CThe pod affinity rule syntax is invalid and causes a scheduling error.
DThe pod is missing resource requests, so affinity rules are ignored.
Attempts:
2 left
💡 Hint

Affinity requires matching pods to exist in the specified topology.

🔀 Workflow
expert
3:00remaining
Implementing Pod Anti-Affinity for High Availability

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?

A
affinity:
  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchLabels:
            app: critical-service
        topologyKey: failure-domain.beta.kubernetes.io/zone
B
affinity:
  podAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchLabels:
            app: critical-service
        topologyKey: kubernetes.io/hostname
C
affinity:
  podAntiAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchLabels:
              app: critical-service
          topologyKey: failure-domain.beta.kubernetes.io/zone
D
affinity:
  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchLabels:
            app: critical-service
        topologyKey: kubernetes.io/hostname
Attempts:
2 left
💡 Hint

To avoid co-locating pods on the same node, use anti-affinity with hostname topology.