In this lesson, you will learn how to use Node Affinity and Tolerations to ensure your resource-heavy .NET background jobs run exclusively on specialized, high-memory Kubernetes nodes.
The Problem: Scheduling Bias
By default, Kubernetes treats all nodes as equivalent, which is dangerous for memory-intensive .NET background jobs (like large-scale data processing or complex report generation). If a heavy job lands on a general-purpose node, it may trigger aggressive Garbage Collection (GC) thrashing or, worse, an Out-of-Memory (OOM) kill.
Node Affinity
Node Affinity allows you to constrain your .NET Pods to nodes that meet specific criteria, such as having a larger amount of RAM. You label your nodes (e.g., workload=high-memory), and then configure your deployment manifest to mandate that your background worker only schedules onto those labeled nodes.
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: workload
operator: In
values:
- high-memory
Tolerations
While affinity tells the scheduler where the pod must go, Tolerations allow the pod to land on nodes that have "Taints." Imagine you apply a NoSchedule taint to your high-memory nodes to prevent general web services from consuming those resources. Your background job needs a "key" to unlock that door; the toleration is that key.
tolerations:
- key: "workload"
operator: "Equal"
value: "high-memory"
effect: "NoSchedule"
Why this matters for .NET
When you combine these, you create a dedicated "execution zone" for your background services. By isolating these pods, you ensure that the ServerGarbageCollection setting in your runtimeconfig.json can operate within the full available memory of the node without being evicted by noisy neighbors. This stability prevents the unpredictable latency spikes that often plague complex .NET background tasks in shared-cluster environments.
Key Takeaway: Use Node Affinity to route heavy .NET workloads to specialized hardware and pair it with Taints and Tolerations to prevent general-purpose pods from encroaching on those high-memory resources.