0
0
GCPcloud~15 mins

Compute commands (instances, disks) in GCP - Deep Dive

Choose your learning style9 modes available
Overview - Compute commands (instances, disks)
What is it?
Compute commands in Google Cloud Platform (GCP) are instructions you give to manage virtual machines (called instances) and storage units (called disks). These commands let you create, start, stop, delete, and inspect instances and disks. They help you control your cloud computers and their storage easily from the command line.
Why it matters
Without compute commands, managing cloud computers and their storage would be slow and error-prone, requiring manual clicks in a web interface. Compute commands let you automate tasks, work faster, and handle many machines at once. This makes cloud computing practical and scalable for real projects.
Where it fits
Before learning compute commands, you should understand basic cloud concepts like virtual machines and storage. After mastering these commands, you can explore automation with scripts, infrastructure as code, and advanced cloud services like load balancing and autoscaling.
Mental Model
Core Idea
Compute commands are simple instructions that let you control cloud computers and their storage from your keyboard.
Think of it like...
It's like using a remote control to turn your TV on or off, change channels, or adjust volume, but here you control virtual computers and their disks instead.
┌───────────────┐      ┌───────────────┐
│  User types   │ ---> │  Compute CLI  │
│  commands    │      │  interprets   │
└───────────────┘      └───────────────┘
          │                    │
          ▼                    ▼
┌─────────────────────┐  ┌─────────────────────┐
│  Virtual Machines    │  │  Virtual Disks       │
│  (Instances)        │  │  (Storage)           │
└─────────────────────┘  └─────────────────────┘
Build-Up - 7 Steps
1
FoundationUnderstanding Instances and Disks
🤔
Concept: Learn what virtual machines (instances) and disks are in GCP.
Instances are like computers in the cloud that run your programs. Disks are like hard drives that store data for these instances. Each instance uses one or more disks to keep its files and operating system.
Result
You can picture cloud computers and their storage as separate but connected parts.
Knowing what instances and disks are helps you understand why you need commands to manage each separately.
2
FoundationInstalling and Accessing gcloud CLI
🤔
Concept: Set up the command-line tool to send compute commands.
Download and install the Google Cloud SDK, which includes the gcloud command-line tool. Then, log in with your Google account and set your project with: $ gcloud auth login $ gcloud config set project [PROJECT_ID]
Result
You have a tool ready to send commands to your cloud resources.
Having the gcloud CLI ready is essential because all compute commands run through it.
3
IntermediateCreating and Managing Instances
🤔Before reading on: do you think creating an instance requires specifying a disk, or is it automatic? Commit to your answer.
Concept: Learn how to create, start, stop, and delete virtual machines using commands.
To create an instance: $ gcloud compute instances create my-instance --zone=us-central1-a This command creates a VM with a default boot disk. To stop it: $ gcloud compute instances stop my-instance --zone=us-central1-a To start it again: $ gcloud compute instances start my-instance --zone=us-central1-a To delete it: $ gcloud compute instances delete my-instance --zone=us-central1-a
Result
You can control the lifecycle of cloud computers from your keyboard.
Understanding instance lifecycle commands lets you manage costs and availability by controlling when machines run.
4
IntermediateWorking with Disks Separately
🤔Before reading on: do you think disks can exist without instances, or only attached to them? Commit to your answer.
Concept: Learn how to create, attach, detach, and delete disks independently of instances.
Create a disk: $ gcloud compute disks create my-disk --size=100GB --zone=us-central1-a Attach it to an instance: $ gcloud compute instances attach-disk my-instance --disk=my-disk --zone=us-central1-a Detach it: $ gcloud compute instances detach-disk my-instance --disk=my-disk --zone=us-central1-a Delete the disk: $ gcloud compute disks delete my-disk --zone=us-central1-a
Result
You can manage storage independently, moving disks between machines or keeping data safe.
Knowing disks are separate resources helps you design flexible storage strategies.
5
IntermediateListing and Inspecting Resources
🤔Before reading on: do you think listing instances shows all zones or just one? Commit to your answer.
Concept: Learn commands to see what instances and disks exist and their details.
List instances in a zone: $ gcloud compute instances list --zones=us-central1-a List all disks: $ gcloud compute disks list Get details about an instance: $ gcloud compute instances describe my-instance --zone=us-central1-a Get details about a disk: $ gcloud compute disks describe my-disk --zone=us-central1-a
Result
You can check your cloud resources' status and configuration anytime.
Being able to inspect resources prevents mistakes and helps with troubleshooting.
6
AdvancedUsing Flags for Customization
🤔Before reading on: do you think you can specify machine type and disk type during instance creation? Commit to your answer.
Concept: Learn how to customize instances and disks with options like machine type, disk type, and network settings.
Create an instance with a specific machine type and disk: $ gcloud compute instances create custom-instance --zone=us-central1-a --machine-type=n1-standard-2 --boot-disk-type=pd-ssd --boot-disk-size=50GB Create a disk with a specific type: $ gcloud compute disks create fast-disk --size=200GB --type=pd-ssd --zone=us-central1-a
Result
You can tailor your cloud computers and storage to your needs for performance and cost.
Customizing resources lets you optimize for your workload, balancing speed and budget.
7
ExpertAutomating with Scripts and Filters
🤔Before reading on: do you think you can combine commands with filters to manage many instances at once? Commit to your answer.
Concept: Learn how to use command filters and scripts to automate managing multiple instances and disks efficiently.
List instances with a name pattern: $ gcloud compute instances list --filter="name~'web-server-.*'" Stop all instances matching a filter: $ gcloud compute instances stop $(gcloud compute instances list --filter="name~'test-.*'" --format="value(name)") --zone=us-central1-a Use shell scripts to loop over instances and perform actions automatically.
Result
You can manage large fleets of cloud computers quickly and reliably.
Automation with filters and scripts is key to scaling cloud operations and avoiding manual errors.
Under the Hood
When you run a compute command, the gcloud CLI sends a request to Google Cloud's Compute Engine API. This API talks to Google's backend systems that manage physical servers and storage. The API translates your command into actions like allocating hardware, setting up networking, and attaching disks. The system tracks state so commands like start or stop change the instance's status safely.
Why designed this way?
Google designed compute commands as a simple interface over a powerful API to separate user control from complex backend operations. This design allows automation, security, and scalability. Instead of users managing hardware directly, they send high-level commands that the system handles reliably.
User Command
   │
   ▼
gcloud CLI
   │
   ▼
Compute Engine API
   │
   ▼
Backend Systems
   ├─ Physical Servers
   ├─ Storage Systems
   └─ Network Infrastructure
Myth Busters - 4 Common Misconceptions
Quick: Does deleting an instance also delete its attached disks by default? Commit to yes or no.
Common Belief:Deleting an instance automatically deletes all its disks.
Tap to reveal reality
Reality:By default, only the boot disk is deleted; additional attached disks remain unless explicitly deleted.
Why it matters:Assuming all disks delete can cause unexpected data loss or leftover storage costs.
Quick: Can you attach the same disk to multiple running instances at once? Commit to yes or no.
Common Belief:You can attach one disk to many instances simultaneously.
Tap to reveal reality
Reality:Standard persistent disks can only be attached to one instance at a time; multi-writer disks are special and limited.
Why it matters:Trying to share disks incorrectly can cause data corruption or errors.
Quick: Does stopping an instance release its external IP address? Commit to yes or no.
Common Belief:Stopping an instance frees its external IP address automatically.
Tap to reveal reality
Reality:Ephemeral external IPs are released on stop, but static IPs remain reserved until released manually.
Why it matters:Misunderstanding IP behavior can cause connectivity issues or unexpected charges.
Quick: Is the gcloud CLI the only way to manage instances and disks? Commit to yes or no.
Common Belief:You must use gcloud CLI to manage compute resources.
Tap to reveal reality
Reality:You can also use the Cloud Console web UI, REST API, client libraries, and Terraform.
Why it matters:Knowing alternatives helps choose the best tool for different tasks and automation.
Expert Zone
1
Instance creation can be slow if you specify custom images or large disks because the system prepares resources behind the scenes.
2
Detached disks retain data and can be reattached to other instances, enabling data migration or backup strategies.
3
Using filters with gcloud commands can combine with labels to manage resources by environment, team, or purpose efficiently.
When NOT to use
Compute commands are not ideal for managing very large fleets manually; instead, use infrastructure as code tools like Terraform or Deployment Manager for repeatable, version-controlled setups.
Production Patterns
In production, teams use scripts with gcloud commands combined with CI/CD pipelines to automate instance scaling, patching, and backups. Disks are often managed separately for data persistence and snapshots.
Connections
Infrastructure as Code
Builds-on
Understanding compute commands is essential before using infrastructure as code tools that automate these commands at scale.
Networking Basics
Related concept
Managing instances often requires knowledge of networking to configure firewalls, IPs, and routing.
Remote Control Systems
Similar pattern
Like remote controls send commands to devices, compute commands send instructions to cloud resources, showing a universal control pattern.
Common Pitfalls
#1Deleting an instance but forgetting to delete attached disks, causing unexpected storage charges.
Wrong approach:$ gcloud compute instances delete my-instance --zone=us-central1-a
Correct approach:$ gcloud compute instances delete my-instance --zone=us-central1-a $ gcloud compute disks delete my-disk --zone=us-central1-a
Root cause:Assuming instance deletion cleans up all related storage automatically.
#2Trying to attach a disk to multiple instances without using multi-writer disks.
Wrong approach:$ gcloud compute instances attach-disk instance-1 --disk=shared-disk --zone=us-central1-a $ gcloud compute instances attach-disk instance-2 --disk=shared-disk --zone=us-central1-a
Correct approach:Attach the disk to only one instance at a time or use special multi-writer disks with proper configuration.
Root cause:Not understanding disk attachment limitations.
#3Stopping an instance and expecting to keep its ephemeral external IP address.
Wrong approach:$ gcloud compute instances stop my-instance --zone=us-central1-a # Expecting IP to remain assigned
Correct approach:Reserve a static IP and assign it to the instance to keep it after stopping.
Root cause:Confusing ephemeral and static IP address behavior.
Key Takeaways
Compute commands let you control cloud virtual machines and their storage easily from the command line.
Instances and disks are separate resources; managing them independently gives flexibility and control.
Using the gcloud CLI, you can create, start, stop, delete, and inspect instances and disks with simple commands.
Automation with filters and scripts is essential for managing many resources efficiently and avoiding manual errors.
Understanding the lifecycle and behavior of instances and disks prevents costly mistakes like data loss or unexpected charges.