Google Cloud Platform (GCP) offers two primary methods for launching Compute Engine virtual machines (VMs): the Google Cloud Console (web interface) and the Cloud SDK (gcloud
command-line tool). This guide demonstrates a hybrid approach, leveraging both tools for streamlined and customizable instance deployment.
Prerequisites
- Active GCP Project: Ensure you have an active Google Cloud Platform project.
- SSH Key Pair:
- If needed, generate an SSH key pair on your local machine using
ssh-keygen
. - Add the public key to your project’s metadata:
- In the Cloud Console, navigate to Compute Engine > Metadata > SSH Keys.
- Click “Edit,” then “Add Item,” and paste your public key.
- If needed, generate an SSH key pair on your local machine using
- Firewall Rule: Configure a firewall rule permitting ingress SSH traffic (port 22) from your authorized IP address(es).
Step 1: Initial Configuration (Google Cloud Console)
-
Open the Cloud Console and navigate to Compute Engine > VM instances.
-
Click Create Instance.
-
Provide the following details:
- Name: A descriptive name for your instance.
- Region/Zone: The desired geographical location for your instance.
- Machine Type: Select the appropriate vCPU and memory configuration for your workload.
- Boot Disk:
- Image: Choose your preferred operating system (e.g., Ubuntu, Debian).
- Boot disk type: Typically, “Standard Persistent Disk (pd-standard)” is suitable.
- Size: Specify the desired storage capacity.
- Firewall: Enable “Allow HTTP traffic” and “Allow HTTPS traffic” if required.
- Networking: Adjust network settings if you have specific requirements.
- Advanced Options (Optional):
- Preemptibility: If cost optimization is a priority, consider preemptible instances.
- Availability Policy: For high availability, configure a regional policy.
-
Click “Create” to initiate instance creation.
Step 2: Advanced Configuration (Cloud SDK)
-
Authenticate: Ensure you are authenticated with your GCP project:
gcloud auth login gcloud config set project your-project-id
-
Create Instance: Execute the following
gcloud
command, replacing placeholders with your specific values:gcloud compute instances create instance-name \ --zone=your-zone \ --machine-type=machine-type \ --image=image-name \ --image-project=image-project \ --boot-disk-size=disk-sizeGB \ --boot-disk-type=pd-balanced \ --metadata-from-file=startup-script=gs://your-bucket/startup.sh \ --tags=http-server,https-server \ --maintenance-policy=maintenance-policy \ --preemptible # (Optional)
-
Additional Disks (Optional): To attach additional disks, use:
gcloud compute instances attach-disk instance-name \ --disk=disk-name \ --zone=your-zone
Step 3: Connect via SSH:
gcloud compute ssh instance-name --zone=your-zone