Skip to content
tezvyn:
Menu

Node Affinity: Tell Your Pods Where to Go

Source: kubernetes.ioMediumHow cards are made

Node Affinity: Tell Your Pods Where to Go

Node affinity is like giving pods a 'preferred seating' list for nodes. You guide the scheduler to nodes with specific labels, like those with GPUs or in a certain zone. The footgun is confusing 'required' (a hard rule) with 'preferred' (a suggestion).

Why it exists

The default Kubernetes scheduler treats all nodes as mostly equal, which is fine for general-purpose workloads. But some workloads need specific hardware or must run in certain locations. Node affinity was created to give developers a flexible way to influence scheduling decisions without hard-coding pod placement to a single, specific node.

The mental model

Think of node affinity as a dating app profile for your pod. The pod spec describes the ideal node it wants to be scheduled on, based on labels. It can have hard requirements ('deal-breakers') like needing a GPU, and soft preferences ('nice-to-haves') like preferring a node in a specific availability zone. The Kubernetes scheduler then plays matchmaker, trying to find the best fit.

How it works

Node affinity rules are defined in a pod's specification and work by matching labels on nodes. There are two main types. First, requiredDuringSchedulingIgnoredDuringExecution is a hard rule; the pod will not be scheduled unless a node with a matching label is found. Second, preferredDuringSchedulingIgnoredDuringExecution is a soft rule; the scheduler will try to find a matching node and give it a higher score, but will schedule the pod elsewhere if no perfect match exists. The 'IgnoredDuringExecution' part means that if a node's labels change after the pod is already running, Kubernetes won't evict the pod.

When to use it

Use node affinity to place pods on nodes with special hardware like GPUs or fast SSDs. It's also essential for controlling pod placement across availability zones for high availability, or keeping pods within a geographic region for data sovereignty and latency reasons. You can also use it to direct workloads to nodes with specific licenses.

When not to use it

Avoid using node affinity for simple, stateless applications that can run anywhere. Overly complex affinity rules can make scheduling difficult, leading to unschedulable pods and an underutilized cluster. For simply repelling pods from certain nodes, taints and tolerations are a better tool. For pinning a pod to one specific node, the simpler nodeSelector field is often sufficient.

One canonical example

To ensure a machine learning pod runs only on a node with a specific GPU, you would add a nodeAffinity rule to the pod spec. This rule would contain a requiredDuringSchedulingIgnoredDuringExecution block that specifies a nodeSelectorTerms matching the label gpu-type=nvidia-a100. If no node in the cluster has this label, the pod will remain in a 'Pending' state until a suitable node becomes available.

Interview question

A pod *must* run on a node with a GPU, but *prefers* to be in a specific availability zone. Which Node Affinity rules should be used?

  • a.Both preferredDuringSchedulingIgnoredDuringExecution for both the GPU and the availability zone.
  • b.preferredDuringSchedulingIgnoredDuringExecution for the GPU, and requiredDuringSchedulingIgnoredDuringExecution for the availability zone.
  • c.Both requiredDuringSchedulingIgnoredDuringExecution for both the GPU and the availability zone.
  • d.requiredDuringSchedulingIgnoredDuringExecution for the GPU, and preferredDuringSchedulingIgnoredDuringExecution for the availability zone.Correct
Why?

The card states that 'requiredDuringSchedulingIgnoredDuringExecution' is a hard rule for 'deal-breakers' like needing a GPU, while 'preferredDuringSchedulingIgnoredDuringExecution' is a soft rule for 'nice-to-haves' like preferring a specific zone. Option C is a tempting distractor, but making both requirements hard could prevent the pod from scheduling if no node meets both strict criteria.

Just read this? Test yourself on what you have been reading.

Read the original → kubernetes.io

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on kubernetes — each one lists the topics its interview covers.

See open roles