Featured image of post GCP #2: Deploying VM

GCP #2: Deploying VM

learning GCP:Deploying and Implementing a Cloud Solution

Compute Engine

Launching Instances

  1. Click buger menu icon (on the top left) > Compute Engines > VM instances
  2. then click Create instance button on the top bar menu (under the search bar)
  3. There are 7 side menus: Machine configuration, OS and storage, Data protection, Networking, Observability, Security, and Advanced. We’re going to set just some of them - to reduce the learning cost - and left the rest as default. Feel free to play around with it.
    • Machine configuration
      • set Name, Region and Zone as you wish
      • choose e2-micro machine type with spot provisioning model
    • On the “OS and storage” configuration, click “Change” button. Let OS version as it is (Debian) but change the “Boot disk type” to “Standard persistent disk”
  4. Click “Create” button on the bottom. Then you’ll see the vm instance you just created.

Instance with script

You can install some packages during the initialization. From the step 3 above, click the “Advanced” configuration and add “Startup script” (more info see https://docs.cloud.google.com/compute/docs/instances/startup-scripts), for example

1
2
3
4
#! /bin/bash
apt-get update
apt-get install -y apache2
echo "Hello from my GCP VM" > /var/www/html/index.html

Ensure you have “External IPv4 address” set up on the “Networking - Edit network interface” configuration and check “Allow HTTP(s) traffic”

My Cheap Instance

I use spot model (see https://docs.cloud.google.com/compute/docs/instances/spot) for learning or experiment since it is very affordable. Here is my instance configuration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
gcloud compute instances create test-vm-jkt \
    --project=<PROJECT_ID> \
    --zone=asia-southeast2-c \
    --machine-type=e2-micro \
    --network-interface=stack-type=IPV4_ONLY,subnet=default,no-address \
    --metadata=enable-osconfig=TRUE \
    --no-restart-on-failure \
    --maintenance-policy=TERMINATE \
    --provisioning-model=SPOT \
    --instance-termination-action=STOP \
    --create-disk=auto-delete=yes,boot=yes,device-name=test-vm-jkt,image=projects/debian-cloud/global/images/debian-12-bookworm-v20260210,mode=rw,size=10,type=pd-standard

whereas test-vm-jkt is the instance name. Get project id using command gcloud projects list or check via console. Try to change flag --no-restart-on-failure to --restart-on-failure, since it is SPOT model, the flag will be ignored. if you want to create self-healing spot vm, use MIG template to create the instance.

SSH connection

  • ensure you’re set on the right project, check via gcloud config list
  • if not, set the project gcloud config set project PROJECT_ID. you can see the PROJECT_ID via gcloud projects list
  • ensure the instance you want to connect is on the list via gcloud compute instances list, remember the NAME of the instance that you want to connect.
    • if you find the instance status is TERMINATED, you shall activate with gcloud compute instances start INSTANCE_NAME --zone ZONE_NAME
    • changing start to stop above for terminating the instance, if you want to play arround
  • if it’s okay, run gcloud compute ssh INSTANCE_NAME --project PROJECT_ID
    • you may add flag --tunnel-through-iap to make it work
    • some distro require username, e.g. Fendora run gcloud compute ssh core@test-vm-1 --zone=us-central1-f.
  • if you face the problem during connection, you can check the vm detail with command gcloud compute instances describe INSTANCE_NAME

Kubernetes Service

We learn how to orchestrate containers with Google Kubernetes Engine (GKE)

Serveless Service

Cloud Run

Cloud Function

Choosing Serverless

Database

Automation

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus