Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Kubernetes GPU Workloads & SSD DB Node Selectors

· 6 min read · Home Tech

In this article
  1. Background: Why Scheduling Constraints Matter
  2. How Node Selectors Work
  3. How Node Affinity Works
  4. Choosing Between Selector and Affinity
  5. Taints and Tolerations: The Missing Piece
  6. Common Problems and Misconceptions
  7. Real-World Example: GPU Inference Service
  8. SSD-Heavy Database Example: Cassandra
  9. Frequently Asked Questions

Kubernetes places GPU workloads and SSD-heavy database pods using node selectors and node affinity, which are scheduling constraints that match pods to nodes based on labels. For GPU workloads, you label nodes with their GPU type (e.g., gpu-type=nvidia-tesla-t4) and set a node selector or required affinity rule on the pod; for SSD-heavy databases, you label nodes with storage-type=ssd and use the same mechanism, often combined with taints and tolerations to reserve those nodes exclusively. This article explains how both features work, how to choose between them, and the practical pitfalls you will hit in real clusters.

Background: Why Scheduling Constraints Matter

Kubernetes schedules pods to nodes using the kube-scheduler, which scores nodes for fit. By default, any node that has enough CPU and memory is a candidate. That breaks for specialized hardware: a GPU pod will fail to start on a node without GPUs, and a database that needs NVMe SSDs will run on spinning disks, causing terrible latency. Node labels are the foundation—they are key-value pairs attached to nodes (e.g., disktype=ssd, gpu=nvidia-a100). Node selectors and affinity are the two ways to tell the scheduler which labels a node must have.

How Node Selectors Work

A node selector is the simplest constraint: a nodeSelector field in the pod spec is a map of label keys to values. The scheduler only places the pod on nodes that have all listed labels with matching values.

spec:
  nodeSelector:
    gpu: nvidia-tesla-t4

This is exact-match only—you cannot express “GPU with at least 16GB memory” or “SSD with NVMe” using a selector alone. It is also a hard requirement: if no node matches, the pod stays Pending forever. Node selectors are ideal for small clusters or when you have a handful of distinct node types. They are also the easiest to debug because the label set is visible in kubectl get nodes --show-labels.

How Node Affinity Works

Node affinity is a more expressive version of the same idea, defined under spec.affinity.nodeAffinity. It supports two types:

  • requiredDuringSchedulingIgnoredDuringExecution – hard requirement, like a node selector but with operators (In, NotIn, Exists, Gt, Lt).
  • preferredDuringSchedulingIgnoredDuringExecution – soft preference; the scheduler tries to honor it but will place the pod elsewhere if no match exists.

Example for a GPU pod that prefers a specific GPU model but can fall back to any GPU:

affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
      - matchExpressions:
        - key: gpu
          operator: In
          values: ["nvidia-tesla-t4", "nvidia-a100"]
    preferredDuringSchedulingIgnoredDuringExecution:
    - weight: 100
      preference:
        matchExpressions:
        - key: gpu-model
          operator: In
          values: ["a100"]

For SSD-heavy databases, you might use a required affinity for disktype=ssd and a preferred affinity for disktype=nvme to get faster storage when available. Affinity also allows NotIn to avoid nodes with HDDs, which a node selector cannot do.

Choosing Between Selector and Affinity

Use a node selector when your requirement is a simple equality and you have no fallback. Use affinity when you need operators (e.g., Exists for any GPU), soft preferences, or multiple terms. In practice, most production setups use affinity because it is more flexible and can be combined with anti-affinity to spread database replicas across nodes. A common pattern: required affinity for the hardware type, preferred affinity for the specific model, and a separate pod anti-affinity to avoid placing two database replicas on the same node.

Taints and Tolerations: The Missing Piece

Node selectors and affinity only control where pods can go. They do not stop other pods from landing on GPU or SSD nodes. To reserve those nodes, you add a taint (e.g., gpu=true:NoSchedule) and add a matching toleration to the GPU pod. The scheduler then refuses to place any pod without the toleration on that node. This is essential for GPU nodes because a CPU-only pod could otherwise consume memory and cause GPU pods to fail. For SSD-heavy databases, taints prevent batch jobs from filling the node and causing IO contention.

Common Problems and Misconceptions

  • “I labeled the node but the pod is Pending.” Check the label key/value spelling—a typo in the label or selector is the most common cause. Use kubectl describe node to see labels and events.
  • “Affinity is ignored for already-running pods.” The “IgnoredDuringExecution” part means changes to node labels do not evict running pods. You must restart the pod to re-evaluate scheduling.
  • “Node selector is enough for GPU.” It is not—without taints, other pods can land on the GPU node and steal memory. Always combine with taints/tolerations.
  • “Preferred affinity guarantees placement.” It does not; it is a score, not a guarantee. If no node matches, the pod runs on a non-matching node.
  • “SSD node selector ensures fast I/O.” It only ensures the node has an SSD label, not that the database uses the SSD path. You still need to mount the SSD volume and configure the database to use it.

Real-World Example: GPU Inference Service

Suppose you run a TensorFlow inference service on a cluster with two node pools: one with T4 GPUs and one with A100s. You label the nodes gpu=nvidia-tesla-t4 and gpu=nvidia-a100, and taint both with gpu=true:NoSchedule. Your deployment spec includes:

  1. A required affinity for gpu In (nvidia-tesla-t4, nvidia-a100).
  2. A preferred affinity for gpu-model In (a100) to prefer the faster GPU.
  3. A toleration for gpu=true:NoSchedule.
  4. Resource requests for nvidia.com/gpu: 1 to get the GPU device plugin to allocate one GPU.

This ensures the pod only runs on GPU nodes, prefers A100s, and no other pod can occupy those nodes without a GPU toleration.

SSD-Heavy Database Example: Cassandra

For a Cassandra cluster that needs NVMe SSDs, you label nodes storage-type=nvme and taint them with ssd=true:NoSchedule. Your statefulset uses:

  • Required affinity for storage-type In (nvme).
  • Toleration for ssd=true:NoSchedule.
  • Pod anti-affinity to spread replicas across different nodes.
  • Local persistent volumes (or hostPath) mounted from the SSD directory, because node selector alone does not choose the disk.

Frequently Asked Questions

Can I use node selector to require a GPU with a minimum memory?

No. Node selectors only do exact equality. Use node affinity with the Gt operator on a numeric label (e.g., gpu-memory-gb) to require a minimum value, but you must label nodes with that numeric value yourself.

What happens if a node loses its label after a pod is running?

Nothing, unless you have a controller that evicts pods. The “IgnoredDuringExecution” semantics mean the pod keeps running. If you want the pod to move, you need to manually delete it or use a tool like descheduler.

Do I need both nodeSelector and nodeAffinity?

No. They are mutually exclusive in the pod spec—you cannot set both. Use affinity for everything, as it is a superset of selector functionality.

How do I see which nodes have GPUs or SSDs?

Run kubectl get nodes --show-labels and grep for your label keys. For GPUs, the node will also have a resource like nvidia.com/gpu in its capacity, visible with kubectl describe node.

Can I use node affinity to avoid a specific node?

Yes, use NotIn operator. For example, disktype NotIn (hdd) prevents placement on HDD nodes. This is a common way to keep databases off slow disks.

What is the difference between node affinity and pod affinity?

Node affinity matches pods to nodes based on node labels. Pod affinity matches pods to other pods (e.g., place two pods on the same node). For hardware placement, you only need node affinity.