Compute Engine
Deploying, configuring, and managing virtual machines using Google Compute Engine
Google Compute Engine (GCE) is GCP's Infrastructure as a Service (IaaS) offering that lets you create and run virtual machines on Google's infrastructure. Compute Engine offers scalable, high-performance virtual machines that can run Linux and Windows Server images in a variety of configurations.
Key Features
Flexible machine types: Predefined or custom machine configurations
Global infrastructure: Deploy VMs across 24+ regions and 73+ zones
Managed instance groups: Autoscaling, auto-healing, and rolling updates
Custom images: Create your own VM images or use public images
Spot VMs: Use excess Compute Engine capacity at steep discounts
Sole-tenant nodes: Physical isolation for compliance requirements
Fast networking: Up to 100 Gbps networking
Local and persistent storage options: Various disk types for different workloads
Live migration: Hardware maintenance without VM restarts
GPU and TPU support: Accelerators for ML/AI workloads
Deploying VMs with Terraform
Basic VM Deployment
Here's a basic example of deploying a Linux VM with Terraform:
Windows VM with Persistent Disk
This example creates a Windows Server VM with an additional persistent disk:
High Availability with Managed Instance Group
This example creates a regional managed instance group with autoscaling:
VM Deployment with Packer
Packer is a great tool for creating custom machine images. Here's how to build a custom GCE image:
Build the image with:
Then use the image in your Terraform configuration:
Configuration Management with Ansible
Ansible can be used to configure VMs once they're deployed. Create an inventory file that uses GCP dynamic inventory:
Create a playbook to configure web servers:
Execute the playbook against GCP instances:
Monitoring with Google Cloud Operations (formerly Stackdriver)
Install the monitoring agent on your VMs using Terraform's metadata_startup_script:
CI/CD Pipeline with GitHub Actions
Set up a continuous deployment pipeline for your infrastructure:
Using Pulumi for GCE Deployment
Pulumi is an alternative to Terraform that lets you use familiar programming languages. Here's a Python example:
Best Practices
VM Naming Conventions: Use a consistent naming convention that includes environment, purpose, and a unique identifier.
Right-sizing VMs: Start with appropriate sizes and use sustained use discounts.
Use Startup Scripts with Caution: For critical configuration, prefer custom images over extensive startup scripts.
Cost Optimization:
Use preemptible/spot VMs for batch workloads
Use sustained use discounts and committed use discounts
Schedule VM startups/shutdowns for non-24/7 workloads
Security Hardening:
Use OS Login instead of SSH keys
Lock down firewall rules to specific IPs
Use service accounts with minimal permissions
Enable shielded VM options
Use Instance Templates: Define your infrastructure once and reuse it.
Implement Auto-healing and Auto-scaling: For production workloads.
Automate Backups: Use scheduled snapshots for critical data.
Use Labels and Tags: Apply tags for network rules and labels for resource organization.
Automate VM Management: Use automation for patching, updates, and configuration changes.
Common Pitfalls
Not planning for VM placement: Placing all VMs in a single zone creates a single point of failure.
Ignoring quotas: GCP has default quotas that can cause unexpected deployment failures.
Hardcoding credentials: Never store service account keys in your code or image.
Using the default service account: It often has broader permissions than necessary.
Neglecting cleanup: Set up automated processes to delete unused resources.
Manual configuration drift: Always manage infrastructure as code to prevent configuration drift.
Overlooking network security: Don't open firewall rules too broadly.
Choosing the wrong machine type: Oversized VMs waste money; undersized VMs cause performance issues.
Advanced VM Configurations
Spot VMs for Cost Savings
Confidential VM for Enhanced Security
GPU-Accelerated VM
Windows VM with Automated Password Reset
Further Reading
Last updated