A preemptible VM in Google Cloud Platform (GCP) is an instance that can be terminated by Google with a 30-second notice if the system requires resources. These instances are more cost-effective than regular instances, but they are not suitable for long-running or critical workloads, as they have a maximum runtime of 24 hours.
Here’s a detailed explanation of how to create a preemptible Compute Engine instance in GCP using the Console, gcloud CLI, and Terraform:
1. Using GCP Console:
a. Go to the GCP Console: https://console.cloud.google.com/
b. Navigate to Compute Engine > VM instances.
c. Click on the “Create” button to start creating a new instance.
d. Fill in the required fields such as instance name, region, zone, and machine type.
e. Choose a boot disk by clicking on the “Change” button under the “Boot disk” section. Select the desired OS image and disk size.
f. Expand the “Management, security, disks, networking, sole tenancy” section.
g. In the “Management” tab, under “Availability policy,” check the “Preemptibility” checkbox.
h. Configure additional settings like network, external IP, and firewall rules, as required.
i. Click the “Create” button to launch the preemptible instance.
2. Using gcloud CLI:
First, make sure you have the Google Cloud SDK (https://cloud.google.com/sdk/docs/install) installed.
a. Open the terminal (Command Prompt or PowerShell on Windows, Terminal on macOS or Linux).
b. Authenticate with your Google Cloud account using the following command:
gcloud auth login
c. Set your GCP project:
gcloud config set project PROJECT_ID
d. Launch a new preemptible instance with the desired configuration:
gcloud compute instances create INSTANCE_NAME \
--image-family IMAGE_FAMILY \
--image-project IMAGE_PROJECT \
--machine-type MACHINE_TYPE \
--boot-disk-size DISK_SIZE_GB \
--zone ZONE \
--preemptible
Replace INSTANCE_NAME, IMAGE_FAMILY, IMAGE_PROJECT, MACHINE_TYPE, DISK_SIZE_GB, and ZONE with appropriate values.
3. Using Terraform:
First, make sure you have Terraform installed (https://learn.hashicorp.com/tutorials/terraform/install-cli).
a. Create a new directory for your Terraform configuration:
mkdir gcp-instance
cd gcp-instance
b. Create a main.tf
file with the following content:
provider "google" {
credentials = file("<PATH_TO_YOUR_SERVICE_ACCOUNT_JSON>")
project = "<YOUR_PROJECT_ID>"
region = "us-central1"
}
resource "google_compute_instance" "preemptible" {
name = "preemptible-instance"
machine_type = "n1-standard-1"
zone = "us-central1-a"
scheduling {
preemptible = true
automatic_restart = false
}
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
network = "default"
access_config {
// Ephemeral external IP
}
}
}
Replace <PATH_TO_YOUR_SERVICE_ACCOUNT_JSON>
and <YOUR_PROJECT_ID>
with appropriate values.
c. Initialize Terraform:
terraform init
d. Review the execution plan:
terraform plan
e. Apply the changes to create the preemptible instance:
terraform apply
After creating the preemptible VM, you can use it similarly to a regular instance
Leave a Reply