Bird
Raised Fist0
GCPcloud~5 mins

Preemptible and Spot VMs in GCP - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Sometimes cloud virtual machines (VMs) can be cheaper if they can be stopped by the cloud provider when needed. Preemptible and Spot VMs are special types of VMs that cost less but can be turned off unexpectedly. They help save money when you can handle interruptions.
When running batch jobs that can restart if interrupted.
When testing software that does not need to run all the time.
When you want to save money on VMs for short tasks.
When running workloads that can tolerate sudden VM shutdowns.
When you want to use spare cloud capacity at a lower cost.
Config File - instance-template.yaml
instance-template.yaml
apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeInstanceTemplate
metadata:
  name: example-preemptible-template
spec:
  machineType: e2-medium
  scheduling:
    preemptible: true
  disks:
  - boot: true
    initializeParams:
      sourceImage: projects/debian-cloud/global/images/family/debian-11
  networkInterfaces:
  - network: default

This file defines a VM template for Google Cloud that creates a preemptible VM.

machineType: The size of the VM.

scheduling.preemptible: Set to true to make the VM preemptible, meaning it can be stopped by Google Cloud at any time.

disks: Defines the boot disk and operating system image.

networkInterfaces: Connects the VM to the default network.

Commands
This command creates a VM template named 'example-preemptible-template' with a medium machine type and marks it as preemptible to save costs. It uses the latest Debian 11 image.
Terminal
gcloud compute instance-templates create example-preemptible-template --machine-type=e2-medium --preemptible --image-family=debian-11 --image-project=debian-cloud
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/global/instanceTemplates/example-preemptible-template].
--preemptible - Marks the VM as preemptible to allow Google Cloud to stop it anytime.
--machine-type - Specifies the size of the VM.
--image-family - Selects the OS image family for the VM.
This command creates a VM named 'example-preemptible-vm' in the us-central1-a zone using the preemptible template created earlier.
Terminal
gcloud compute instances create example-preemptible-vm --zone=us-central1-a --source-instance-template=example-preemptible-template
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/example-preemptible-vm].
--source-instance-template - Uses the VM template to create the instance.
--zone - Specifies the zone where the VM will run.
This command checks if the VM is preemptible by showing the scheduling property.
Terminal
gcloud compute instances describe example-preemptible-vm --zone=us-central1-a --format='value(scheduling.preemptible)'
Expected OutputExpected
true
--format - Formats the output to show only the preemptible status.
This command lists the VM to verify it is running and shows its status and zone.
Terminal
gcloud compute instances list --filter='name=example-preemptible-vm'
Expected OutputExpected
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS example-preemptible-vm us-central1-a e2-medium TRUE 10.128.0.5 34.68.123.45 RUNNING
--filter - Filters the list to show only the VM with the given name.
Key Concept

If you remember nothing else from this pattern, remember: Preemptible and Spot VMs save money by allowing the cloud to stop them anytime, so use them only for tasks that can handle interruptions.

Common Mistakes
Creating a preemptible VM for a critical database that must always run.
Preemptible VMs can stop at any time, causing data loss or downtime for critical services.
Use regular VMs for critical workloads and reserve preemptible VMs for batch or fault-tolerant jobs.
Not specifying the --preemptible flag when creating the VM.
The VM will be a regular VM and cost more, missing the cost-saving benefit.
Always include --preemptible when you want a preemptible VM.
Assuming preemptible VMs run indefinitely without interruption.
Google Cloud can stop preemptible VMs at any time, usually within 24 hours.
Design your applications to handle VM shutdowns and restart gracefully.
Summary
Create a VM template with the --preemptible flag to define a low-cost VM that can be stopped anytime.
Use the template to create VMs that save money but can be interrupted by the cloud provider.
Check the VM's preemptible status and running state with gcloud commands to confirm setup.

Practice

(1/5)
1. What is the main advantage of using Preemptible or Spot VMs in Google Cloud?
easy
A. They offer unlimited storage capacity
B. They provide guaranteed uptime for critical applications
C. They cost less but can be stopped at any time
D. They automatically scale without user input

Solution

  1. Step 1: Understand the cost and availability trade-off

    Preemptible and Spot VMs are cheaper because Google can stop them anytime to reclaim resources.
  2. Step 2: Identify the main benefit

    The main benefit is cost savings with the risk of interruption, not guaranteed uptime or unlimited storage.
  3. Final Answer:

    They cost less but can be stopped at any time -> Option C
  4. Quick Check:

    Cost savings with interruptions = D [OK]
Hint: Cheaper VMs can be stopped anytime, so cost saving is main benefit [OK]
Common Mistakes:
  • Thinking they guarantee uptime
  • Assuming they scale automatically
  • Confusing with storage features
2. Which of the following is the correct way to specify a Spot VM in a Google Cloud VM creation command?
easy
A. gcloud compute instances create my-vm --spot
B. gcloud compute instances create my-vm --interruptible
C. gcloud compute instances create my-vm --preemptible
D. gcloud compute instances create my-vm --ephemeral

Solution

  1. Step 1: Recall the flag for Spot VMs

    Spot VMs use the flag --spot in the gcloud command.
  2. Step 2: Differentiate from Preemptible flag

    --preemptible is for older Preemptible VMs, --spot is the newer recommended option.
  3. Final Answer:

    gcloud compute instances create my-vm --spot -> Option A
  4. Quick Check:

    Spot VM flag = --spot [OK]
Hint: Spot VMs use --spot flag, not --preemptible [OK]
Common Mistakes:
  • Using --preemptible for Spot VMs
  • Confusing --interruptible as a flag
  • Using unrelated flags like --ephemeral
3. Consider this snippet of a VM creation command:
gcloud compute instances create test-vm --zone=us-central1-a --spot --machine-type=e2-medium
What will happen if Google Cloud needs the resources back?
medium
A. The VM will automatically migrate to another zone
B. The VM will continue running without interruption
C. The VM will be stopped immediately without warning
D. The VM will receive a 30-second warning before stopping

Solution

  1. Step 1: Understand Spot VM behavior on resource reclamation

    Spot VMs are interruptible but Google Cloud sends a 30-second warning before stopping them.
  2. Step 2: Eliminate other options

    Immediate stop without warning is incorrect; automatic migration does not happen for Spot VMs; uninterrupted running contradicts the interruptible nature.
  3. Final Answer:

    The VM will receive a 30-second warning before stopping -> Option D
  4. Quick Check:

    Spot VMs get 30-second warning before stop = A [OK]
Hint: Spot VMs get 30-second warning before stopping [OK]
Common Mistakes:
  • Assuming immediate stop without warning
  • Thinking Spot VMs migrate automatically
  • Believing Spot VMs never stop
4. You created a VM with the command:
gcloud compute instances create my-vm --preemptible --zone=us-east1-b
But you want to switch to Spot VM instead. What is the correct fix?
medium
A. Add --spot flag without removing --preemptible
B. Replace --preemptible with --spot in the command
C. Change the zone to a Spot-only zone
D. Use --interruptible flag instead of --preemptible

Solution

  1. Step 1: Identify the correct flag for Spot VMs

    Spot VMs require the --spot flag, not --preemptible.
  2. Step 2: Correct the command by replacing flags

    Remove --preemptible and add --spot to switch VM type.
  3. Final Answer:

    Replace --preemptible with --spot in the command -> Option B
  4. Quick Check:

    Switching VM type requires flag replacement = B [OK]
Hint: Remove --preemptible, add --spot to switch VM type [OK]
Common Mistakes:
  • Adding --spot without removing --preemptible
  • Changing zone expecting Spot-only zones
  • Using non-existent --interruptible flag
5. You want to run a batch job that can tolerate interruptions and save costs. Which approach best uses Spot VMs to handle sudden stops and restarts?
hard
A. Design the job to save progress frequently and restart automatically on VM preemption
B. Use Spot VMs without any checkpointing or restart logic
C. Run the job on standard VMs to avoid interruptions
D. Use Spot VMs but disable automatic restarts

Solution

  1. Step 1: Understand Spot VM interruption nature

    Spot VMs can stop anytime, so jobs must handle interruptions gracefully.
  2. Step 2: Choose a strategy to handle interruptions

    Saving progress frequently and restarting automatically ensures job completion despite stops.
  3. Step 3: Eliminate unsafe options

    Running without checkpointing risks data loss; standard VMs cost more; disabling restarts loses fault tolerance.
  4. Final Answer:

    Design the job to save progress frequently and restart automatically on VM preemption -> Option A
  5. Quick Check:

    Checkpointing + auto-restart = reliable Spot VM use [OK]
Hint: Checkpoint progress and auto-restart for Spot VM jobs [OK]
Common Mistakes:
  • Ignoring checkpointing and restart logic
  • Choosing standard VMs for cost savings
  • Disabling automatic restarts on Spot VMs