One CLI for the cloud,gcloud.
The Google Cloud CLI creates, configures, and scales GCP resources — Compute Engine VMs, networking, and load balancers — straight from a terminal or script. This guide walks the GROUP/COMMAND structure, Compute Engine basics, and wiring an external HTTP load balancer on top of a managed instance group.
Script it, don't click it
Every gcloud command follows the same GROUP COMMAND grammar, so the console workflow you clicked through once becomes a script you can rerun — from a single VM to a full load-balanced fleet.
- Compute — create VMs with zones, images, and startup scripts.
- MIGs — identical VMs from one template, autoscaled and autohealed.
- Load balancing — forwarding rule → proxy → URL map → backends.
On this page
What this guide covers
GCloud Compute
The gcloud compute GROUP | COMMAND structure and creating, configuring, and manipulating VMs.
HTTP load balancer
Instance templates, managed instance groups, health checks, backend services, URL maps, and proxies.
Forwarding rules
The frontend of every Google Cloud load balancer — matching traffic and handing it to the target proxy.
Startup scripts
Bootstrap VMs on first boot with a startup-script metadata flag — apt-get install nginx and go.
Start here
Overview
gcloud is the command-line interface for Google Cloud Platform (GCP) — it creates, configures, and scales resources like Compute Engine VMs, networks, and load balancers from a terminal or script. Every command follows a gcloud GROUP COMMAND structure (e.g. gcloud compute instances create).
This guide covers Compute Engine and building an HTTP load balancer with managed instance groups.
Virtual machines
GCloud Compute
-
GCloud Compute Guide is still a work in progress; these are active notes from my current R&D.
shell gcloud compute --help- This will display all of the commands that will help you utilize the
compute engine.
- This will display all of the commands that will help you utilize the
-
The are split into two major concepts, with
GROUPandCOMMAND.- According to Google, the compute command helps create, configure and manipulate the virtual machines within your pre-set project.
- The
SYNOPSISisgcloud compute GROUP | COMMAND [GCLOUD_WIDE_FLAG ...]
Scale out
Load Balancer
Load balancing on the GCloud compuete platform.
-
Command to run 3 instances of
nginxwith an ingress load balancer. Additional Documentation on Nginx Here-
Shell command for VM that is running nginx inside of a
debianoperating system.-
Terminal window gcloud compute instances create www-server-1 \--zone=us-west1-b \--tags=network-lb-tag \--machine-type=e2-medium \--image-family=debian-11 \--image-project=debian-cloud \--metadata=startup-script=start_nginx.sh-
start_nginx.sh ->
-
#!/bin/bashapt-get updateapt-get install nginx -y
-
-
For switching from Nginx to Apache2, replace the
nginxwithapache2. -
To check the status on
ubuntu, run thesudo systemctl status nginxORsudo systemctl status apache2.
-
-
Example of a Load Balance Template:
-
The shell below is an example of an instance template that creates the load balance backend template.
-
Terminal window gcloud compute instance-templates create lb-backend-template \--region=us-west1 \--network=default \--subnet=default \--tags=allow-health-check \--machine-type=e2-medium \--image-family=debian-11 \--image-project=debian-cloud \--metadata=startup-script=start_nginx_script.sh
-
-
Key concept is : Managed instance groups MIGs
- Mage instance groups or MIGs enable you to operate applications on multiple identical / clone virtual machines, thus allowing your orchestration to become scalable and highly available. This is done by utilizing the components within the automated MIG services, which includes: autoscaling, autohealing, regional (multiple zone) deployment, and automatic updating.
-
-
Manage Instance Group for the load balancer:
-
Terminal window gcloud compute instance-groups managed create lb-backend-group \--template=lb-backend-template --size=2 --zone=us-west1-b -
Health Check:
-
Terminal window gcloud compute firewall-rules create fw-allow-health-check \--network=default \--action=allow \--direction=ingress \--source-ranges=130.211.0.0/22,35.191.0.0/16 \--target-tags=allow-health-check \--rules=tcp:80``` -
Backend-Services for gcloud compute
-
Terminal window gcloud compute backend-services create web-backend-service \--protocol=HTTP \--port-name=http \--health-checks=http-basic-check \--global-
Add Instance Group as the Backend to the Backend Service:
-
Terminal window gcloud compute backend-services add-backend web-backend-service \--instance-group=lb-backend-group \--instance-group-zone=us-west1-b \--global
-
-
Create a URL Map for routing the requests to the default backend services.
-
Terminal window gcloud compute url-maps create web-map-http \--default-service web-backend-service``` -
Extra information regarding the URL Map: Note: URL map is a Google Cloud configuration resource used to route requests to backend services or backend buckets. For example, with an external HTTP(S) load balancer, you can use a single URL map to route requests to different destinations based on the rules configured in the URL map: Requests for Video go to one backend service. Requests for Audio go to a different backend service. Requests for Images go to a Cloud Storage backend bucket. Requests for any other host and path combination go to a default backend service.
-
Create a target HTTP proxy to route requests:
-
Terminal window gcloud compute target-http-proxies create http-lb-proxy \--url-map web-map-http
-
-
Global forwarding rule to route incoming requests to the proxy:
-
Terminal window gcloud compute forwarding-rules create http-content-rule \--address=lb-ipv4-1\--global \--target-http-proxy=http-lb-proxy \--ports=80
-
-
-
-
-
-
-
Frontend config
Google Rules
Google Forwarding Rules
Section titled “Google Forwarding Rules”Note: A forwarding rule and its corresponding IP address represent the frontend configuration of a Google Cloud load balancer. Learn more about the general understanding of forwarding rules from the Forwarding rule overview Guide.
Questions
Frequently asked
What is the gcloud CLI?
gcloud is the command-line interface for Google Cloud Platform. It lets you create, configure, and scale resources — Compute Engine VMs, networks, load balancers, storage, and more — from a terminal or scripts. Commands follow a gcloud GROUP COMMAND structure, for example gcloud compute instances create.
How do I create a VM with gcloud?
Use gcloud compute instances create NAME with flags for zone, machine type, and image, e.g. gcloud compute instances create www-1 --zone=us-west1-b --machine-type=e2-medium --image-family=debian-11 --image-project=debian-cloud. A startup-script metadata flag can install software like Nginx on first boot.
What is a managed instance group (MIG)?
A managed instance group runs multiple identical VMs from a single instance template. MIGs provide autoscaling, autohealing, multi-zone (regional) deployment, and automatic rolling updates, making them the backend for scalable, highly available load-balanced applications.
How does an HTTP load balancer work on Google Cloud?
An external HTTP load balancer chains a global forwarding rule and IP to a target HTTP proxy, which uses a URL map to route requests to backend services. Each backend service points at one or more instance groups and a health check, so traffic only reaches healthy VMs.
What is a forwarding rule in Google Cloud?
A forwarding rule and its IP address form the frontend of a Google Cloud load balancer. It matches incoming traffic on a protocol and port and forwards it to a target proxy or backend, tying the public entry point to the load balancing configuration behind it.