This content originally appeared on DEV Community and was authored by Latchu@DevOps
Google Cloud Compute Engine gives you several ways to create VM boot disks:
Public Images (Linux, Windows, prebuilt images)
Custom Images
Snapshots
Existing Disks
In this guide, weβll focus on Disk Images β creating them from an existing VM, launching a new VM from that image, and finally managing deprecation & deletion.
Step 1: Introduction to Disk Images
An image is essentially a replica of a disk containing the OS and applications needed to boot a VM.
Why use Images?
- Consistency across multiple VMs
- Faster provisioning with pre-installed apps
- Reusable across projects & environments
Weβll:
- Create a VM and install a webserver
- Create a disk image from that VM
- Launch a new VM from the image
- Learn how to deprecate & delete images
Step 2: Explore Available Images
Go to:
Compute Engine β Storage β Images
Here youβll find:
- Google-provided public images (Ubuntu, Debian, Windows, etc.)
- Custom images youβve created
Step 3: Create a VM Instance
Weβll create a VM (demo8-vm1) and install a simple NGINX webserver using a startup script.
Startup script (save as webserver-install.sh):
#!/bin/bash
sudo apt install -y telnet
sudo apt install -y nginx
sudo systemctl enable nginx
sudo chmod -R 755 /var/www/html
HOSTNAME=$(hostname)
sudo echo "<!DOCTYPE html> <html> <body style='background-color:rgb(250, 210, 210);'> <h1>Welcome to Latchu@DevOps - WebVM App1 </h1> <p><strong>VM Hostname:</strong> $HOSTNAME</p> <p><strong>VM IP Address:</strong> $(hostname -I)</p> <p><strong>Application Version:</strong> V1</p> <p>Google Cloud Platform - Demos</p> </body></html>" | sudo tee /var/www/html/index.html
Create VM with startup script:
gcloud compute instances create demo8-vm1 \
--zone=us-central1-a \
--machine-type=e2-micro \
--network-interface=subnet=default \
--tags=http-server \
--metadata-from-file=startup-script=webserver-install.sh
Access app in browser:
http://<external-ip-address>
Stop VM (before creating image):
gcloud compute instances stop demo8-vm1 --zone=us-central1-a
Step 4: Create Disk Image
Weβll now create a disk image from demo8-vm1.
Console:
- Go to Compute Engine β Storage β Disks β demo8-vm1 β Actions β Create Image
- Name: demo8-vm1-disk-image-v1
- Source: Disk β demo8-vm1
- Location: Multi-regional (US)
- Family: mynginx-apps
- Encryption: Google-managed key
- Click on Create
CLI:
gcloud compute images create demo8-vm1-disk-image-v1 \
--project=gcpdemos \
--family=mynginx-apps \
--source-disk=demo8-vm1 \
--source-disk-zone=us-central1-a \
--storage-location=us
Verify:
gcloud compute images list --filter='name:demo8-vm1-disk-image-v1'
Step 5: Review Disk Image Properties
- Go to Compute Engine -> Storage -> Images -> demo8-vm1-disk-image-v1
- Review Disk Image Properties
Step-06: Create VM Instance from Disk Image
- Go to Compute Engine -> Storage -> Images -> demo8-vm1-disk-image-v1
- Click on CREATE INSTANCE
- Name: demo8-vm2-from-disk-image
- Labels: environment: dev
- Region: us-central1
- Zone: us-central1-a
- Machine Family:
Series: E2
Machine Type: e2-micro
- Availability policies:
VM provisioning model: Standard
- Display Device: unchecked (leave to default)
- Confidential VM Service: unchecked (leave to default)
- Container: unchecked (leave to default)
- Boot Disk: Custom Image
Name: demo8-vm1-disk-image-v1
Boot Disk Type: New Balanced Persistent Disk
Size (GB): 10
Image: demo8-vm1-disk-image-v1
- Identity and API Access:
Service Account: Compute Engine default Service Account
Access Scopes: Allow default Access
- Firewall
Allow HTTP Traffic
- Advanced Options: LEAVE TO DEFAULTS
- Click on Create
- KEY OBSERVATION: We didn’t pass any Startup Script here to install Webserver, but newly created VM instance will have the webserver.
# Create VM Instance from a Disk Image
gcloud compute instances create demo8-vm2-from-disk-image \
--project=gcpdemos \
--zone=us-central1-a \
--machine-type=e2-micro \
--network-interface=subnet=default \
--tags=http-server \
--create-disk=image=projects/gcpdemos/global/images/demo8-vm1-disk-image-v1
Step-07:
Verify newly created VM
If you access the IP over the browser
# Access Application
curl http://<EXTERNAL-IP-ADDRESS>
Observation:
1. You will see the hostname will be as `demo8-vm1` in index.html even though hostnae of VM is `demo8-vm2-from-disk-image` because it is a disk clone from demo8-vm1 using Disk Image creation
2. This is just an observation to tell that we are using demo8-vm1 disk here from Disk Image
Step-08:
How to create new disk using Disk Image ?
- Goto Storage -> Disks -> CREATE DISK
- SOURCE: Image
- SOURE IMAGE: demo8-vm1-disk-image-v1
- Create and Verify Image list
Step-09:
Disk Image – Deprecate Option
- Go to Compute Engine -> Storage -> Images -> demo8-vm1-disk-image-v1 -> Actions -> Deprecate
Step-10:
Disk Image – Delete Option
- Go to Compute Engine -> Storage -> Images -> demo8-vm1-disk-image-v1 -> Select CHECK BOX
- Click on DELETE
Step-11:
Delete or Stop VM to avoid charges
# Delete VM: demo8-vm2-from-disk-image
gcloud compute instances delete demo8-vm2-from-disk-image --zone=us-central1-a
gcloud compute instances list --filter='name:demo8-vm2-from-disk-image'
# Delete VM: demo8-vm1 (DONT DELETE WE WILL USE IN NEXT DEMO)
gcloud compute instances delete demo8-vm1 --zone=us-central1-a
gcloud compute instances list --filter='name:demo8-vm1'
This content originally appeared on DEV Community and was authored by Latchu@DevOps