0
0
Kubernetesdevops~20 mins

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

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

Which of the following best describes the difference between requiredDuringSchedulingIgnoredDuringExecution and preferredDuringSchedulingIgnoredDuringExecution in Kubernetes node affinity?

ArequiredDuringSchedulingIgnoredDuringExecution means pods prefer matching nodes but can schedule elsewhere; preferredDuringSchedulingIgnoredDuringExecution means pods must be scheduled on matching nodes.
BrequiredDuringSchedulingIgnoredDuringExecution means pods must be scheduled on matching nodes; preferredDuringSchedulingIgnoredDuringExecution means pods prefer matching nodes but can schedule elsewhere if none match.
CBoth mean pods must be scheduled on matching nodes, but preferredDuringSchedulingIgnoredDuringExecution also enforces node labels at runtime.
DBoth mean pods prefer matching nodes, but requiredDuringSchedulingIgnoredDuringExecution ignores node labels during scheduling.
Attempts:
2 left
💡 Hint

Think about what happens if no nodes match the affinity rules.

💻 Command Output
intermediate
1:30remaining
Output of Pod Scheduling with Node Affinity

Given a Kubernetes cluster with nodes labeled zone=us-east-1a and zone=us-east-1b, and a pod spec with this node affinity:

{
  "requiredDuringSchedulingIgnoredDuringExecution": {
    "nodeSelectorTerms": [
      {"matchExpressions": [{"key": "zone", "operator": "In", "values": ["us-east-1a"]}]}
    ]
  }
}

What will be the output of kubectl get pods -o wide regarding the node the pod is scheduled on?

AThe pod will be scheduled only on a node labeled zone=us-east-1a.
BThe pod will be scheduled on any node regardless of zone label.
CThe pod will fail to schedule because no nodes match the affinity.
DThe pod will be scheduled on a node labeled zone=us-east-1b.
Attempts:
2 left
💡 Hint

Check the meaning of requiredDuringSchedulingIgnoredDuringExecution.

Configuration
advanced
2:00remaining
Correct Node Anti-Affinity YAML

Which of the following YAML snippets correctly configures a pod to avoid scheduling on nodes that already run pods with label app=frontend using requiredDuringSchedulingIgnoredDuringExecution node anti-affinity?

A
affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - labelSelector:
        matchExpressions:
        - key: app
          operator: In
          values:
          - frontend
      topologyKey: kubernetes.io/hostname
B
affinity:
  podAntiAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
    - labelSelector:
        matchExpressions:
        - key: app
          operator: In
          values:
          - frontend
      topologyKey: kubernetes.io/hostname
C
affinity:
  podAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - labelSelector:
        matchExpressions:
        - key: app
          operator: In
          values:
          - frontend
      topologyKey: kubernetes.io/hostname
D
affinity:
  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - labelSelector:
        matchExpressions:
        - key: app
          operator: In
          values:
          - frontend
      topologyKey: kubernetes.io/hostname
Attempts:
2 left
💡 Hint

Remember that node anti-affinity uses podAntiAffinity, not nodeAffinity or podAffinity.

Troubleshoot
advanced
2:00remaining
Pod Fails to Schedule Due to Node Affinity

A pod with this node affinity fails to schedule:

affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
      - matchExpressions:
        - key: disk
          operator: In
          values:
          - ssd

What is the most likely reason?

ANo nodes in the cluster have the label <code>disk=ssd</code>.
BThe pod spec is missing a container image.
CThe pod is trying to schedule on nodes labeled <code>disk=hdd</code> which conflicts with affinity.
DThe nodeAffinity field is misspelled and ignored by the scheduler.
Attempts:
2 left
💡 Hint

Check if any nodes have the required label.

Best Practice
expert
2:30remaining
Best Practice for High Availability Using Node Anti-Affinity

You want to deploy multiple replicas of a critical service to ensure high availability. Which node anti-affinity configuration best helps spread pods across different nodes to avoid single points of failure?

AUse <code>preferredDuringSchedulingIgnoredDuringExecution</code> pod affinity with <code>topologyKey: kubernetes.io/hostname</code> to prefer pods on the same node.
BUse <code>preferredDuringSchedulingIgnoredDuringExecution</code> pod anti-affinity with <code>topologyKey: kubernetes.io/hostname</code> to softly prefer pods on different nodes.
CUse <code>requiredDuringSchedulingIgnoredDuringExecution</code> pod anti-affinity with <code>topologyKey: kubernetes.io/hostname</code> to force pods on different nodes.
DUse <code>requiredDuringSchedulingIgnoredDuringExecution</code> node affinity with <code>topologyKey: failure-domain.beta.kubernetes.io/zone</code> to schedule all pods in the same zone.
Attempts:
2 left
💡 Hint

Think about how to guarantee pods do not land on the same node.