Introduction to Google Compute Engine
What is a Virtual Machine?
A Virtual Machine (VM) is a software-based emulation of a physical computer that runs an operating system and applications. Think of it as a computer within a computer, where:
- It has its own CPU, memory, storage, and network interface
- It runs its own operating system independently
- It can run applications just like a physical computer
- It's isolated from other VMs on the same physical machine
Key Characteristics of VMs:
- Isolation: Each VM operates independently
- Portability: Can be moved between physical machines
- Scalability: Easy to create, modify, or delete
- Resource Sharing: Multiple VMs can run on a single physical machine
Alternatives to Virtual Machines
-
Physical Servers
- Traditional dedicated hardware
- Direct access to physical resources
- No virtualization overhead
-
Containers
- Lightweight, portable units of software
- Share the host OS kernel
- Faster startup and lower overhead
-
Serverless Computing
- Event-driven execution
- No server management
- Pay-per-use pricing
What is Google Compute Engine?
Google Compute Engine (GCE) is Google Cloud's Infrastructure as a Service (IaaS) offering that lets you create and run virtual machines on Google's infrastructure.
Basic Features:
-
Machine Types
- Predefined configurations (e.g., n1-standard-1)
- Custom machine types
- GPU-enabled instances
- Memory-optimized instances
-
Storage Options
- Persistent Disks (SSD and HDD)
- Local SSDs
- Cloud Storage integration
-
Networking
- Virtual Private Cloud (VPC)
- Load balancing
- Cloud CDN
- Cloud NAT
-
Management Tools
- Google Cloud Console
- Command-line interface (gcloud)
- REST APIs
- Infrastructure as Code (Terraform)
How to Manage Google Compute Engine
Google Cloud Console provides a web-based interface to create, manage, and monitor virtual machines without using the command line. Follow these steps to manage GCE through the console.
1. Creating a New VM Instance
To create a virtual machine instance using the Google Cloud Console:
-
Navigate to Compute Engine
- Go to the Google Cloud Console.
- Select a project from top left.
- Click on the Create a VM button.
-
Click on "Create Instance"
- Click the "Create Instance" button at the top.
-
Configure Your VM Instance
- Name: Enter a unique name for your instance.
- Region & Zone: Choose the region (e.g.,
us-central1
) and zone (us-central1-a
) based on your latency and compliance needs. - Machine Type:
- For general workloads:
e2-standard-2
- For compute-intensive tasks:
c2-standard-4
- For memory-intensive tasks:
m2-ultramem-160
- For general workloads:
- Boot Disk:
- Click "Change" to select an OS (Ubuntu, Debian, CentOS, Windows, etc.).
- Choose disk type (SSD for high performance, Standard HDD for lower cost).
- Firewall Settings:
- Check "Allow HTTP traffic" and "Allow HTTPS traffic" if serving a web application.
- Click "Create" to launch the VM.
-
Access the VM
- Once created, find your VM under Compute Engine > VM Instances.
- Click "SSH" next to your instance name to open an SSH session directly in the browser.
2. Managing VM Resources
Starting & Stopping Instances
- In the VM Instances list, locate your instance.
- Click the three-dot menu (⋮) next to your instance.
- Choose:
- Start – If the instance is stopped.
- Stop – To shut down the instance.
- Restart – To reboot the instance.
Resizing a Machine Type
- Click on your instance name.
- Under Machine Configuration, click Edit.
- Select a new machine type (more vCPUs, RAM, or GPU-enabled options).
- Click Save and confirm the restart.
Attaching Additional Storage
- Navigate to Compute Engine > Disks.
- Click "Create Disk".
- Choose:
- Type: Standard persistent disk, SSD, or local SSD.
- Size: Define the disk capacity.
- Click Create.
- To attach to a VM:
- Go to VM Instances, select your instance, click Edit.
- Under Additional Disks, click Attach Existing Disk.
- Select the disk and click Save.
3. Configuring Networking
Setting Up a Static External IP
By default, VMs have an ephemeral IP that changes when restarted. To assign a static IP:
- Navigate to VPC network > External IP addresses.
- Click "Reserve Static Address".
- Assign it to your VM and click Reserve.
Creating a Firewall Rule
- Go to VPC Network > Firewall.
- Click "Create Firewall Rule".
- Define:
- Name: e.g.,
allow-web-traffic
- Targets: Select
All instances
or specify your instance. - Source IP Ranges:
0.0.0.0/0
(for public access) or restrict to a specific IP range. - Protocols & Ports: Choose
TCP
and enter ports like80, 443
for web servers.
- Name: e.g.,
- Click Create.
4. Monitoring & Logs
Viewing Metrics & Performance
- Navigate to Compute Engine > VM Instances.
- Click on an instance to see CPU usage, memory, disk, and network metrics.
- Click Monitoring to set up alerts for CPU usage, disk IO, and uptime.
Viewing Logs
- Go to Logging > Logs Explorer.
- Filter logs by:
- Resource type:
GCE VM Instance
- Log severity: Errors, warnings, info, etc.
- Resource type:
- Use Cloud Monitoring for real-time logs.
5. Security Best Practices
Enabling IAM & Access Controls
- Go to IAM & Admin > IAM.
- Assign roles:
Compute Admin
– Full access to GCE.Compute Viewer
– Read-only access.Custom Roles
– Restrict access to specific actions.
Enabling Shielded VMs (For Security)
- While creating a VM, enable Shielded VM Features for protection against rootkits and firmware attacks.
Setting Up SSH Key Authentication
- Go to Compute Engine > Metadata.
- Click SSH Keys > Add Key.
- Paste your public SSH key (
~/.ssh/id_rsa.pub
). - Click Save.
6. Backups & Snapshots
Creating a Snapshot (Backup)
- Navigate to Compute Engine > Snapshots.
- Click "Create Snapshot".
- Select the disk to back up.
- Define the snapshot schedule (e.g., daily, weekly).
- Click Create.
Restoring a VM from a Snapshot
- Create a new disk from a snapshot under Compute Engine > Disks.
- Attach it to a new or existing VM.
7. Setting Up a Load Balancer (For Multiple VMs)
If running a scalable app, use a Load Balancer to distribute traffic:
- Go to Network Services > Load Balancing.
- Click Create Load Balancer.
- Select HTTP(S) Load Balancer.
- Configure a Backend Service with VM instances.
- Set up a Frontend with a public IP.
- Click Create.
Additional Technical Information
Instance Groups
- Managed Instance Groups (MIGs): Automatically scales VMs.
- Unmanaged Groups: Manually controlled VMs.
Preemptible vs. Spot Instances
- Preemptible Instances: Lower-cost but may be terminated by Google.
- Spot Instances: Replaces preemptibles with a bidding system.
Custom Machine Types
- Create VMs with exact CPU & RAM needs instead of predefined sizes.
GPU & AI Compute
- Attach NVIDIA GPUs for ML workloads.
- Use TPUs for AI-based tasks.
Hybrid & Multi-Cloud
- Connect GCE to on-prem or other clouds using Anthos.
I have a created a VM Instance for Google Compute Engine. How can I SSH into it?
1. Using Google Cloud Console (Browser SSH)
This is the easiest way and requires no setup.
- Go to the Google Cloud Console.
- Navigate to Compute Engine > VM Instances.
- Find your VM instance in the list.
- Click the SSH button next to your instance.
- A new browser window opens with a terminal session connected to your VM.
2. Add Your Public Key to Google Compute Engine
- Go to Google Cloud Console.
- Navigate to Compute Engine > VM Instances.
- Click on your VM instance.
- Click Edit.
- Scroll down to the SSH Keys section.
- Click Add Item.
- Copy and paste your public SSH key from your local machine
cat ~/.ssh/id_rsa.pub
- Click Save.
Conclusion
Google Compute Engine provides a powerful and flexible platform for running virtual machines in the cloud. Whether you're running a small website or a large-scale enterprise application, GCE offers the tools and features needed to build, deploy, and manage your workloads effectively.
Remember to:
- Start with the right machine type
- Implement proper security measures
- Monitor costs and performance
- Follow best practices for reliability
- Use automation tools for efficiency