Image

GCP VM

Basic Commands

To list all instances sorted by zone:

gcloud compute instances list --sort-by=ZONE

To SSH to a VM (IAP tunnel):

gcloud compute ssh vm-internal --zone us-central1-c --tunnel-through-iap

Advanced Commands

Create VM instance with specific network and subnet:

gcloud compute instances create privatenet-us-vm --zone=us-west4-a --machine-type=e2-micro --subnet=privatesubnet-us --image-family=debian-11 --image-project=debian-cloud --boot-disk-size=10GB --boot-disk-type=pd-standard --boot-disk-device-name=privatenet-us-vm

To see information about unused and used memory and space space on custom VM:

free

To see details about the RAM installed on VM (Linux):

sudo dmidecode -t 17

To verify the number of processors:

nproc

To see details about the CPUs installed on VM (Linux):

lscpu

VM Lifecycle

GCP-VM-lifecycle.png

Moving a VM instance to a ew zone

  • Automated process (moving within region):
    • gcloud compute instance move
    • Update references to VM; not automatic
  • Manual process (moving between regions):
    • Snapshot all persistent disks on the source VM
    • Create new persistent disks in destination zone restored from snapshots
    • Create new VM in the destination zone and attach new persistent disks
    • Assign static IP to new VM
    • Update references to VM
    • Delete the snapshots, original disks, and original VM

Running Server on VM

In order not to attach a server to a SSH terminal (which would shut down the moment the terminal closes), one can use screen, an application that allows one to create a virtual terminal that can be “detached” (background process) or “reattached” (foreground process).

When a virtual terminal is detached to the background, it will run whether you are logged in or not.

sudo apt-get install -y screen

To detach the screen terminal, press Ctrl+ACtrl+D. The terminal continues to run in the background. To reattach the terminal, run the following command:

sudo screen -r mcs

Providing Credentials to VM

In the SSH, hit Upload File and upload the necessary .json credentials file. Then run:

gcloud auth activate-service-account --key-file credentials.json

© Filip Niklas 2024. All poetry rights reserved. Permission is hereby granted to freely copy and use notes about programming and any code.