VM¶
Prerequisites:
- I assume that you have Google Cloud account and
gcloudCLI
Create VM¶
In this section, I will describe how to list and choose parameters required to create a VM instance, and then create it.
-
Authenticate in Google Cloud —
gcloud auth login -
You can set default project with
gcloud config set project project_name, and unset it withgcloud config unset project. -
Choose region and zone
List regions along with available resources —
gcloud compute regions list --project project_nameList zones —
gcloud compute zones list --project project_nameRegion picker — allows to pick the best region according to multiple metrics (compute price & latency).
-
Set default regions and zones for project:
-
Choose image
List images, families and image-projects —
gcloud compute images list --project project_name | grep -v -- "-arm64\|-pro-" | grep -i "PROJECT\|FAMILY\|ubuntu"Pick “image project” and “image family”.
-
Choose disk
Disk types and pricing
- Description of different disk types
(such as
standard,balanced,ssd, etc…). - Disks pricing.
Each VM instance has at least one disk attached to it.
List disk types —
gcloud compute disk-types list --filter="zone:( ezone_name )" --project project_name - Description of different disk types
(such as
-
Choose instance configuration
-
Choose machine family and series based on its capabilities (range of cores, CPU manufacturer, RAM, etc…). (1)
- N2D is good for confidential computing.
E2-standardis good for cost.E2-mediumis has even less cost, but with shared core & CPU bursting.
- N2D is good for confidential computing.
-
Consider shared core & CPU bursting (machine series
e2-micro,e2-small,e2-medium). -
Choose security features:
- Confidential computing
(machine series
N2D). - Shielded VM
(secure boot, UEFI, TPM, Integrity monitoring).
- List of images with Shielded VM support. If the image supports Shielded VM features, the following line appears in the output of
- Use these option during VM creation:
--shielded-integrity-monitoring,--shielded-secure-boot,--shielded-vtpmto enable it. Or this command to enable Shielded VM features in already existing VM.
- Confidential computing
(machine series
-
Check availability in chosen region —
gcloud compute machine-types list --filter="zone:( zone_name )" --project project_name. -
Take into account pricing:
- Pricing of machines, disks, GPUs, etc…
- Google Cloud Pricing Calculator.
- Consider 1 & 3 year commitments.
-
Finally, now we have all information to create our VM instance.
gcloud compute instances create vm_name \
--image-family image_family \
--image-project image_project \
--boot-disk-size disk_size \
--boot-disk-type disk_type \
--machine-type machine_type \
--shielded-integrity-monitoring \
--shielded-secure-boot \
--project project_name
Other VM CRUD Doperations
- Delete instance:
gcloud compute instances delete vm_name - Stop instance:
gcloud compute instances stop vm_name - Start instance:
gcloud compute instances start vm_name
Allocate IP address¶
Delete existing ephemeral IP address:
- Where
external-natis a value of thenamefield from output of:
Finally, assign IP address to VM:
gcloud compute instances add-access-config vm_name --access-config-name="external-nat" --address=(gcloud compute addresses list --filter="name=('vm_name-ip')" --format="get(address)")
Other IP CRUD operations
- List IP addresses:
gcloud compute addresses list - Delete IP address:
gcloud compute addresses delete vm_name-ip
Setup the VM¶
Initialise users¶
Open terminal in browser, add users and install packages:
sudo adduser --gecos "" --disabled-password user
sudo usermod -a -G sudo user
sudo apt update && sudo apt full-upgrade -y && sudo apt install -y nano git locate
Allow password-less sudo:
Setup SSH access¶
cat ~/.ssh/id_ed25519.pub or ssh-add -L to ~/.ssh/authorized_keys.
Get host’s ssh key fingerprint (to compare it with what local ssh will print when you connect to the VM from local host):
- where the IP comes from output of:
gcloud compute addresses list --filter="name=('vm_name-ip')" --format="get(address)"
Setup rpi user¶
-
sudo adduser --gecos "" --disabled-password rpi(1)- Add the user
-
mkdir -p ~/.ssh && nano ~/.ssh/authorized_keys && exit(1)- Populate
~/.ssh/authorized_keyswith public key of pi user on RaspberryPi
- Populate
-
sudo usermod -s /usr/sbin/nologin rpi(1)- Set
nologinshell to therpiuser
- Set
Tweak SSH config on the VM:
| sudo nano /etc/ssh/sshd_config | |
|---|---|
Connect from local host to the device¶
Now you can use this VM IP as a bastion host (1) to connect to RaspberryPi.
-
Baston hosts are usually public-facing, hardened systems that serve as an entrypoint to systems behind a firewall or other restricted location, and they are especially popular with the rise of cloud computing