All guides

Spot instances for Kubernetes nodes

Spot capacity is spare cloud compute, sold at a discount and reclaimed at short notice: two minutes on AWS and 30 seconds on Azure. EKS, AKS, GKE and Karpenter each document how to schedule and drain workloads on it.

A wall covered in coloured sticky notes

What spot capacity is

  • AWS. Spot Instances are spare Amazon EC2 capacity at a discount from On-Demand prices. EC2 interrupts them when it needs the capacity back.
  • Azure. Azure Spot Virtual Machines use Azure's unused capacity at a cost saving. Azure evicts them when it needs the capacity back, and they carry no SLA.
  • Google Cloud. Spot VMs are excess Compute Engine capacity at lower prices than standard VMs. Compute Engine can preempt them at any time, and they are excluded from its SLA.

The interruption notice on each cloud

  • AWS. A Spot Instance interruption notice is issued two minutes before EC2 stops or terminates the instance. A rebalance recommendation signals that an instance is at higher risk of interruption. It can arrive sooner than the two-minute notice.
  • Azure. Azure evicts a Spot VM with 30 seconds' notice. In-VM notifications come through Scheduled Events, on a best-effort basis.
  • Google Cloud. Compute Engine sends a preemption notice and then starts a shutdown period that is best effort and lasts up to 30 seconds. By default, GKE gives pods a 30-second graceful termination period: 15 seconds for regular pods, then 15 seconds for critical system pods. The period can be extended to 120 seconds.

On EKS, AWS warns that pods on spot nodes may not receive the full two-minute window, because evictions start after a delay and occur sequentially. It recommends termination grace periods of 30 seconds or less for spot workloads.

Workloads that fit

AWS describes Spot Instances as a good fit for stateless, fault-tolerant, flexible applications, such as batch workloads, queue processing applications and stateless API endpoints. GKE's documentation recommends Spot VMs for fault-tolerant workloads only. Its examples are batch processing, stateless applications and jobs that save their progress frequently.

On-demand and spot node pools together

Keep a pool of standard nodes in the cluster. GKE's documentation gives the reason: critical system components such as DNS need a reliable place to be scheduled, and a node pool on standard VMs is one. On AKS, a spot node pool cannot be the default node pool.

  • EKS. A managed node group has one capacity type, On-Demand or Spot. One cluster can hold both kinds of group.
  • Karpenter. A NodePool lists the allowed values of karpenter.sh/capacity-type. One that allows both spot and on-demand launches on-demand capacity when no spot offerings are available.
  • AKS. The documented command adds a spot pool to an existing cluster:

Shell

export SPOT_NODEPOOL="spotnodepool"

az aks nodepool add \
    --resource-group $RESOURCE_GROUP \
    --cluster-name $AKS_CLUSTER \
    --name $SPOT_NODEPOOL \
    --priority Spot \
    --eviction-policy Delete \
    --spot-max-price -1 \
    --enable-cluster-autoscaler \
    --min-count 1 \
    --max-count 3 \
    --no-wait

With a max price of -1, the VM is not evicted for pricing reasons. Microsoft recommends the cluster autoscaler for spot pools.

Taints, labels and tolerations

Each service labels its spot nodes:

  • EKS managed node groups: eks.amazonaws.com/capacityType: SPOT
  • AKS: kubernetes.azure.com/scalesetpriority: spot
  • GKE: cloud.google.com/gke-spot=true
  • Karpenter: karpenter.sh/capacity-type with the value spot

AKS also adds the taint kubernetes.azure.com/scalesetpriority=spot:NoSchedule to a spot pool. GKE's documentation tells you to taint the nodes that use Spot VMs. On EKS, taints can be applied to managed node groups. Only pods with the matching toleration are then scheduled on those nodes.

This is the pod spec fragment from the AKS documentation. It tolerates the taint, requires the spot label and prefers a second label of your choice:

YAML

spec:
  containers:
  - name: spot-example
  tolerations:
  - key: "kubernetes.azure.com/scalesetpriority"
    operator: "Equal"
    value: "spot"
    effect: "NoSchedule"
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: "kubernetes.azure.com/scalesetpriority"
            operator: In
            values:
            - "spot"
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: another-node-label-key
            operator: In
            values:
            - another-node-label-value

GKE's documentation adds that with only a toleration, pods can be scheduled onto either Spot VMs or standard VMs with capacity.

Disruption budgets and the drain

  • EKS managed node groups enable Capacity Rebalancing. On a rebalance recommendation, EKS attempts to launch a replacement, then cordons and drains the old node. If the two-minute notice arrives before the replacement is ready, EKS starts draining. AWS describes the drain as best effort.
  • AKS node auto-drain cordons and drains a node on a preempt event. Microsoft describes it as a best effort service.
  • GKE uses graceful node shutdown over the termination period described above.
  • Karpenter handles interruptions when its --interruption-queue argument is configured. On a spot interruption warning it begins draining the node and provisions a new node in parallel. It does not act on rebalance recommendations.

A PodDisruptionBudget limits the number of pods of a replicated application that are down at the same time from voluntary disruptions. This is the Kubernetes documentation's example. Change the name and the selector to match your application:

YAML

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: zk-pdb
spec:
  maxUnavailable: 1
  selector:
    matchLabels:
      app: zookeeper

Karpenter evicts pods with the Eviction API, which respects the budget. AWS states that pods on spot nodes can still be forcibly terminated with no termination signal, particularly on nodes with high pod density or during concurrent reclamations.

More than one instance type

AWS recommends that a spot managed node group use multiple instance types with the same vCPU and memory, which increases the number of spot capacity pools it can draw from. It also recommends deploying multiple spot managed node groups.

What to keep off spot

  • Fault-intolerant applications. AWS recommends On-Demand for cluster management tools such as monitoring, for deployments that require StatefulSets, and for stateful applications such as databases.
  • Workloads that need a guaranteed graceful termination. AWS recommends On-Demand capacity for these.
  • Critical system components. GKE's documentation says to use taints and tolerations so that critical pods are not scheduled onto spot node pools.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides