How to Force Kubernetes to Run Your Pod on the Right Node
From the publisher’s feed, collected in the ZeroLabs newsroom.
You have a GPU node sitting there, ready. Kubernetes just scheduled your GPU workload somewhere else entirely. Here’s how to actually control that.nodeSelector vs nodeAffinity vs Taints and Tolerations Explained (Kubernetes)Need a Pod to land on a GPU node or specialized hardware instead of anywhere the scheduler feels like? Here’s exactly how nodeSelector, nodeAffinity, taints, and tolerations work together to control Pod placement, with practical YAML examples.You’ve got a Node in your cluster with an expensive GPU attached, specifically provisioned for exactly one kind of workload. You deploy that workload. And Kubernetes, doing exactly what it was designed to do, schedules it onto a completely ordinary, GPU-less Node instead — while your GPU sits there, idle, hosting some unrelated web server that couldn’t care less about specialized hardware.Nothing is broken here. This is the default scheduler behaving exactly as designed: spread Pods across available Nodes based on resource requests, with no inherent concept of “this workload specifically needs that particular kind of machine.” If you want the scheduler to actually respect hardware requirements, licensing constraints, or deliberate workload separation, you need to tell it — explicitly — using one of several placement mechanisms Kubernetes provides for exactly this purpose.There are four related but genuinely distinct tools here — nodeSelector, nodeAffinity, taints, and tolerations — and the confusion around them mostly comes from people reaching for the wrong one, or not realizing that two of them work as a matched pair rather than standing alone.The default scheduler isn’t careless. It’s just neutral. Placement preferences don’t exist until you write them down.Why the Default Scheduler Doesn’t Already Know ThisIt’s worth understanding why this gap exists at all, rather than assuming it’s an oversight. The Kubernetes scheduler’s default behavior is built around general-purpose fairness — balancing resource requests (the CPU and memory numbers from the earlier article in this series) across available Nodes, trying to avoid overloading any single machine. It has no built-in awareness that “Node-7 has a GPU” matters more than “Node-7 has spare CPU,” unless something specifically tells it so.This is deliberate, not a missing feature. Not every cluster has specialized hardware. Not every workload cares which Node it lands on. Baking hardware-awareness into the default scheduling logic would add complexity that most clusters simply don’t need — so instead, Kubernetes exposes a set of optional mechanisms you apply only when placement actually matters for a specific workload.Key takeaway: The scheduler treats all Nodes as roughly interchangeable by default, on purpose — placement rules exist as opt-in tools for exactly the situations where that assumption is wrong.Step One: Labeling the NodeBefore any placement mechanism can work, the Node itself needs to be identifiable — Kubernetes needs a way to know “this specific Node has a GPU” in the first place. That’s done with a Node label:kubectl label nodes node-7 hardware=gpuThis attaches a simple key-value tag directly to the Node object, the same labeling mechanism you’ve already seen used on Pods throughout this series for Services to find their targets. Verify it landed correctly:kubectl get nodes --show-labelsEverything that follows in this article depends on this label existing — nodeSelector and nodeAffinity both work by matching against Node labels, so if this step is skipped or the label doesn't match exactly, nothing downstream will function, and you'll be left wondering why your placement rule "isn't working" when really, there's nothing for it to match against yet.Key takeaway: Node placement rules are entirely dependent on Node labels existing first — this is the step people skip and then spend an hour debugging the wrong thing.nodeSelector: The Simple, Blunt OptionnodeSelector is the most basic placement mechanism — a straightforward, exact-match requirement added directly to a Pod's spec.apiVersion: apps/v1kind: Deploymentmetadata: name: gpu-appspec: replicas: 1 selector: matchLabels: app: gpu-app template: metadata: labels: app: gpu-app spec: nodeSelector: hardware: gpu containers: - name: app image: my-gpu-app:latestThat nodeSelector block does exactly one thing: it tells the scheduler "only place this Pod on a Node with the label hardware: gpu." If no such Node exists, or none has room, the Pod simply stays Pending rather than landing somewhere unsuitable — which, while inconvenient to see, is exactly the correct, safe behavior. A Pending Pod is recoverable once capacity exists. A GPU workload silently running on a Node with no GPU is a much worse outcome.The honest limitation here is right there in the name — it’s a selector, not a preference system. You get exact-match, all-or-nothing logic: either a Node has this exact label, or your Pod won’t go there. No “prefer this, but accept that if nothing better’s available.” No matching against multiple possible values. For anything more nuanced than a flat requirement, you need the next tool.Key takeaway: nodeSelector is genuinely useful for simple, hard requirements — but it's binary by nature, with no concept of preference or flexibility built in.nodeAffinity: The Flexible, Expressive OptionnodeAffinity does the same fundamental job — matching Pods to Nodes based on labels — but with meaningfully more expressiveness, including the ability to distinguish between a hard requirement and a soft preference.spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: hardware operator: In values: - gpu preferredDuringSchedulingIgnoredDuringExecution: - weight: 50 preference: matchExpressions: - key: zone operator: In values: - us-east-1aThat verbose field name — requiredDuringSchedulingIgnoredDuringExecution — is intimidating on first read, but it's actually just describing its behavior precisely, once you break it apart: required during scheduling (this must be true for the Pod to be placed at all), ignored during execution (if the Node's label somehow changes after the Pod is already running, Kubernetes won't retroactively evict it over that — it only checks at scheduling time).The genuinely valuable addition here is the preferred variant. Unlike nodeSelector's all-or-nothing behavior, a preference tells the scheduler "try to satisfy this if you can, but it's fine to proceed without it if no matching Node is available." The weight value lets you express relative importance across multiple soft preferences, which the scheduler factors into its placement scoring rather than treating as a hard gate.The operator: In field also unlocks matching logic nodeSelector can't express at all — In, NotIn, Exists, DoesNotExist, Gt, and Lt, meaning you can match against multiple acceptable values, or express "any Node that has this label at all, regardless of value," rather than being locked to one exact key-value pair.nodeSelector asks "does this Node match, yes or no." nodeAffinity asks "how well does this Node match, and how much do I actually care."Key takeaway: Reach for nodeAffinity the moment you need either flexible matching logic (multiple acceptable values, existence checks) or the ability to express a preference rather than a hard requirement.Taints and Tolerations: The Other Half of the StoryHere’s where a genuinely common point of confusion comes in, so it’s worth being direct about it upfront: nodeSelector and nodeAffinity are about Pods choosing Nodes. Taints and tolerations solve the opposite problem — Nodes rejecting Pods that haven't specifically been told they're allowed in.This distinction matters more than it might first seem. Imagine you’ve correctly set nodeAffinity to prefer the GPU Node for your GPU workload — but nothing stops an entirely unrelated Pod, one that never mentioned GPUs at all, from also landing on that same Node, simply because it had spare CPU and memory capacity that the scheduler was happy to use. Your GPU workload might be running there correctly, but so might three other things that have no business sharing that expensive hardware.A taint, applied to the Node itself, repels Pods by default — unless a Pod specifically carries a matching toleration granting it permission to ignore that repulsion.kubectl taint nodes node-7 hardware=gpu:NoScheduleThis taint tells the scheduler: “do not place any Pod on this Node, unless that Pod explicitly tolerates this exact taint.” Now, even a Pod with plenty of free capacity to spare won’t land here by default — the Node has effectively closed its doors to everyone except Pods carrying the right key.spec: tolerations: - key: "hardware" operator: "Equal" value: "gpu" effect: "NoSchedule" nodeSelector: hardware: gpuNotice this Pod spec includes both a toleration and a nodeSelector. That's not redundant — it's the correct, complete pattern, and understanding why is the real payoff of this whole comparison.Key takeaway: Taints and tolerations control who’s allowed in. nodeSelector/nodeAffinity control where a Pod wants to go. They solve genuinely opposite problems, which is exactly why serious GPU-scheduling setups use both together.Why You Need Both, TogetherThis is the piece that resolves most of the lingering confusion, so it’s worth stating plainly: nodeAffinity alone doesn't stop other Pods from landing on your GPU Node — it only controls where your Pod prefers to go. Taints alone don't guarantee your GPU workload actually lands on the GPU Node — a toleration only grants permission to be scheduled there; it doesn't express any preference or requirement to actually go there instead of somewhere else entirely.Put those two observations together, and the correct real-world pattern becomes obvious:Taint the GPU Node, so ordinary workloads are repelled from it by default.Add a toleration to your GPU workload’s Pod spec, so it’s specifically permitted to land there despite the taint.Add a nodeSelector or nodeAffinity to that same Pod spec, so it's actually directed there — not just permitted, but positively steered toward that Node rather than wherever else it might otherwise land.Skip the taint, and other unrelated Pods can still consume your GPU Node’s capacity. Skip the affinity/selector, and your GPU workload might tolerate the taint correctly but still get scheduled on some entirely different, taint-free Node instead, since a toleration is passive permission, not an active pull.Key takeaway: Taints keep unwanted Pods out. Affinity/selector pulls your intended Pod in. Real specialized-hardware setups need both mechanisms working together, not either one alone.Side-by-Side ComparisonTrying It YourselfUsing a local cluster from earlier in this series, label one of your Nodes and try the full pattern end to end:kubectl label nodes hardware=gpukubectl taint nodes hardware=gpu:NoSchedulekubectl apply -f gpu-deployment.yaml # includes both toleration and nodeSelectorkubectl get pods -o wide # confirm which Node it actually landed onkubectl describe node # confirm the taint is appliedThen, as a genuinely instructive test, try deploying an ordinary Pod with no toleration at all, and watch it get scheduled anywhere except your tainted Node — direct, visible confirmation that the gate is actually working, not just present in your YAML.A Quick Word on Removing a TaintTaints aren’t permanent — if a Node’s role changes, or you’re decommissioning a dedicated-hardware policy, removing one is a single command, using a trailing dash to indicate removal:kubectl taint nodes node-7 hardware=gpu:NoSchedule-It’s a small syntax detail, but forgetting that trailing - is a common typo that instead tries to apply a taint using that exact string as a key, which fails confusingly rather than doing what you meant.The Real Mental Model to KeepThe distinction worth carrying forward isn’t the specific YAML syntax — that’s easy enough to look up each time you need it. It’s the underlying split: some placement problems are about a Pod expressing where it wants to go, and some are about a Node expressing who it’s willing to accept. nodeSelector and nodeAffinity live entirely in the first category. Taints and tolerations live entirely in the second. Confusing which category your actual problem belongs to is the fastest way to write technically valid YAML that still doesn't produce the placement behavior you expected.Once that split is clear, the question “how do I force my Pod onto the right Node” stops being one problem and becomes two much more specific ones — “how do I steer this Pod toward where I want it,” and, separately, “how do I keep everything else away from where I don’t.”Have you run a mixed cluster with specialized hardware — GPUs, high-memory Nodes, specific storage-attached Nodes? What placement mistakes did you make before landing on the taint-plus-affinity pattern?How to Force Kubernetes to Run Your Pod on the Right Node was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.