Skip to content

Lesson 14 of 19 · 18m

Taints, Affinity, and Topology Spread: Where Pods Actually Land

The scheduler filters then scores, a placed pod is never rebalanced, and spreading replicas is a constraint you write — not a default.

When you submit a Pod, kube-scheduler runs two passes. First it filters: nodeSelector, taints, requested CPU and memory, hostPorts, volume node affinity — every hard constraint deletes nodes from the candidate set. Then it scores what's left: image already pulled, spread preference, affinity weights — and picks the best score. If the candidate set is empty, the pod sits Pending forever. If two nodes tie, it picks one. Nowhere in that pipeline does Kubernetes promise to spread your replicas.

That is the wrong mental model to start with. "The Deployment spreads pods across nodes" is something people assume because it looks true on a three-node test cluster. There is no spread guarantee. Two replicas on the same node is a valid, correctly-scored result — and on a cluster that grew asymmetrically, it is the result you will get.

The hard walls: taints and node affinity

A taint lives on the node and says "don't schedule here." Three effects:

  • NoSchedule — new pods won't land unless they carry a matching toleration.
  • PreferNoSchedule — the soft version; the scheduler tries to avoid it.
  • NoExecute — running pods without the toleration get evicted; tolerationSeconds controls how long they may stay.

Taint on the node, toleration on the pod:

spec:
  tolerations:
    - key: "dedicated"
      operator: "Equal"
      value: "batch"
      effect: "NoSchedule"

The node-side command is kubectl taint nodes <node> dedicated=batch:NoSchedule — and kubectl describe node is where you read what a node already carries before wondering why a pod won't land on it.

This is how control-plane nodes stay clean: they're tainted node-role.kubernetes.io/control-plane:NoSchedule, and only DaemonSets — whose controller injects tolerations automatically — and pods you deliberately permit run there. The same taints drive eviction: node.kubernetes.io/not-ready and unreachable are NoExecute taints the node lifecycle controller adds when a node dies, which is why pods on a dead node resurface elsewhere after tolerationSeconds (default 300) — not instantly.

Node affinity is the pod-side demand. requiredDuringSchedulingIgnoredDuringExecution is a hard filter; preferredDuringSchedulingIgnoredDuringExecution adds score with weight 1–100. Read the name literally: IgnoredDuringExecution means if a node's labels change under a running pod, nothing happens — affinity is a scheduling-time decision, not an ongoing contract.

Spreading replicas for real

podAntiAffinity on topologyKey: kubernetes.io/hostname keeps replicas off the same node:

affinity:
  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      - topologyKey: kubernetes.io/hostname
        labelSelector:
          matchLabels:
            app: web

Required anti-affinity has an edge that bites: ask for per-zone separation with three replicas and only two zones holding capacity, and the third stays Pending. topologySpreadConstraints is the softer, better knob — it spreads by topologyKey up to maxSkew, and whenUnsatisfiable: ScheduleAnyway degrades to scoring instead of refusing. Since 1.27, minDomains forces the spread to cover N domains before skew math applies — set it to your actual zone count, because two saturated zones can technically satisfy "spread" without it.

Priority and preemption

PriorityClass gives pods a value; when the scheduler can't place a high-value pod, it evicts lower-priority pods on the same node to make room. preemptionPolicy: Never turns that off. Victims get graceful termination — PodDisruptionBudgets still apply — and preempted pods don't jump a queue; they get rescheduled like anything else.

What to actually do

  • Replicas that must not share a fault domain: topologySpreadConstraints over topology.kubernetes.io/zone with minDomains equal to your zone count.
  • Nodes reserved for special workloads: taint dedicated=<name>:NoSchedule, then give the workload the toleration plus a nodeAffinity. The taint steers everyone else away; the affinity actually pins you there — a toleration alone just permits, it doesn't place.
  • Remember placement is one-shot. Add nodes or shift load later and nothing rebalances — that's what sigs.k8s.io/descheduler exists for (LowNodeUtilization, RemoveDuplicates strategies). The scheduler scores once, at submit time, and its answer is final until the pod restarts.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.

What is Platform as a Service? The Sweet Spot Between "Too Easy" and "Too Much Work"

Platform as a Service sits between raw servers and fully managed software. You push code, the platform runs it. No Nginx config, no OS patches, no 3 AM server alerts. Here is what PaaS actually gives you, what it costs you in control, and how to decide if it is the right layer for your application.

Article

The World now is Serverless and Container based

A new report from Datadog reveals that serverless computing could be entering the mainstream. This has already been the case in my world for the past few years. Results show that more than half of all organizations are using serverless on one of the three major public cloud providers: Amazon,

Article

PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.

PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...

Article

Self-Hosting Prometheus and Grafana: Infrastructure Monitoring That Actually Works

Build a production-grade monitoring stack with Prometheus and Grafana from scratch. Covers architecture, Docker Compose deployment, Node Exporter, alerting with Alertmanager, PromQL from basics to advanced, pre-built dashboards, and everything SudoSecurity uses to monitor its own infrastructure.

Article