GCP Compute Engine — Common VM Actions

 

GCP Compute Engine — Common VM Actions

1. VM Metadata

Every Compute Engine VM has a metadata server.

Think of it as:

Metadata server = a place where a VM can get information about itself.

For example, a VM can retrieve information such as:

  • VM name
  • Zone
  • Region
  • Internal IP
  • External IP
  • Other instance information

Why is this useful?

It is especially useful with startup scripts and shutdown scripts.

For example:

VM starts
   ↓
Startup script runs
   ↓
Script asks Metadata Server:
"What is my external IP?"
   ↓
Metadata Server returns the IP
   ↓
Script uses that IP

The important part is that the script doesn't need you to manually enter the VM's IP address.

Example

Suppose you create 10 VMs.

Instead of writing:

VM1 → use 34.x.x.x
VM2 → use 35.x.x.x
VM3 → use 34.x.x.x

you can create one reusable startup script:

Get my external IP from metadata
        ↓
Use that IP to configure the application

The same script can run on every VM.

Key point

Metadata makes scripts reusable and less dependent on hard-coded VM-specific information.


2. Startup and Shutdown Scripts

Startup script

Runs when a VM starts.

Example:

VM starts
   ↓
Startup script
   ↓
Install/configure software
   ↓
Start application

Shutdown script

Runs when the VM shuts down and can perform cleanup tasks.

Google recommends storing these scripts in Cloud Storage, especially when you want to manage/reuse them easily.


3. Moving a VM to Another Zone/Region

Sometimes you may need to move a VM.

For example:

Current:
europe-west1-c

        ↓ MOVE

Destination:
us-west1-b

Why would you move it?

Possible reasons:

  • Geographical requirements
  • Reduce latency
  • A zone is being deprecated
  • Infrastructure redesign

Important: You don't simply change the VM's zone

The high-level process is:

Existing VM
    ↓
Create Machine Image
    ↓
Create new VM from Machine Image
    ↓
Choose new zone/region
    ↓
Start new VM

For example:

myinstance
europe-west1-c
      │
      │ Machine Image
      ↓
myinstance
us-west1-b

If the original VM had:

mybootdisk
mydatadisk

those disks/data need to be accounted for during the migration.

After moving

Some server-generated properties change.

Therefore, you may need to update things that reference the old VM, such as:

  • Target VMs
  • Target pools
  • Other resource references

Simple understanding

Moving a VM usually means creating an equivalent VM in the new location rather than physically picking up the existing VM and changing its location.


4. Persistent Disk Snapshots

A snapshot is basically a point-in-time backup of a persistent disk.

Persistent Disk
      ↓
   Snapshot
      ↓
Backup / Restore / Migration

Important

Snapshots are available for:

✅ Persistent Disk

❌ Local SSD


5. Why Use Snapshots?

Snapshots have several important uses.

A. Backup

You can protect important data.

Persistent Disk
      ↓
   Snapshot
      ↓
Durable backup

If something goes wrong, you can use the snapshot to restore the data.


B. Move data between zones

Suppose your data is currently associated with one zone:

Zone A
  ↓
Persistent Disk
  ↓
Snapshot
  ↓
New Persistent Disk
  ↓
Zone B

This can help when you want the data closer to the application to reduce latency.


C. Change disk type

A snapshot can also help you move data to another disk type.

Example:

Standard HDD
     ↓
 Snapshot
     ↓
SSD Persistent Disk

So if you need better disk performance, you can restore the snapshot onto an SSD persistent disk.


6. Snapshot vs Image

This is important because they can look similar.

Image

Primarily used to create/configure VMs.

Image
  ↓
Create VM

Useful for things like:

  • Creating VM instances
  • Instance templates
  • Reusing a configured VM setup

Snapshot

Primarily used for backing up persistent disk data.

Persistent Disk
      ↓
   Snapshot
      ↓
Backup / Restore

Easy way to remember

Image → VM creation
Snapshot → Disk backup/recovery


7. Snapshots Are Incremental

Snapshots are incremental and automatically compressed.

This means after the first snapshot, later snapshots don't need to repeatedly store all unchanged data.

Conceptually:

First snapshot
→ large amount of data

Second snapshot
→ mainly changes

Third snapshot
→ mainly new changes

Therefore, regular snapshots can be:

  • Faster
  • More storage-efficient
  • Lower cost than repeatedly creating full disk images

8. Snapshot Schedule

You don't have to manually create every snapshot.

You can create a snapshot schedule.

For example:

Every day
    ↓
Automatic snapshot
    ↓
Persistent Disk backup

This can be used to regularly back up:

  • Zonal Persistent Disks
  • Regional Persistent Disks

9. Restore a Snapshot

A snapshot can be restored into a new persistent disk.

For example:

Snapshot
   ↓
New Persistent Disk
   ↓
Attach to VM

This is also useful when moving data to another zone.


10. Resize a Persistent Disk

Another common Compute Engine operation is increasing disk size.

Example:

100 GB
  ↓
500 GB

You can increase the capacity of a persistent disk while it is attached to a running VM.

You don't need to create a snapshot just to increase its size.

Bonus

Increasing disk size can also provide better I/O performance, depending on the disk type/configuration.


⚠️ Very Important: Disk Cannot Be Shrunk

You can:

100 GB → 200 GB → 500 GB

But you cannot directly do:

500 GB → 200 GB ❌

So:

Persistent disks can be expanded, but they cannot be reduced in size.

Therefore, choose the increase carefully.


🧠 Final Mental Model

Keep these four operations separate:

                 COMPUTE ENGINE
                      │
       ┌──────────────┼───────────────┐
       ↓              ↓               ↓
   Metadata        VM Move        Disk Actions
       │              │               │
       ↓              ↓               ├─ Snapshot
Get VM info       Machine Image        │
for scripts       → New VM             ├─ Restore
                                       │
                                       └─ Resize
                                          ↑
                                       Increase only

Quick revision

ConceptMain purpose
Metadata ServerVM gets information about itself
Startup ScriptAutomatically configure things when VM starts
Machine ImageCreate/recreate VM configuration
VM MoveRecreate VM in another zone/region
SnapshotBackup persistent disk data
Snapshot ScheduleAutomatically create regular backups
Snapshot RestoreCreate a new disk from backup
Disk ResizeIncrease disk capacity
Disk Shrink❌ Not supported

Most important distinction:
Machine Image = recreate a VM
Snapshot = backup/move/restore disk data
Metadata = let the VM discover its own information.

Comments

Popular posts from this blog

Async/await

First negative in every window of size k

Valid Parentheses