Created
November 10, 2025 17:15
-
-
Save r3xakead0/1b17cfaafd1de05cb66670fe8b536e22 to your computer and use it in GitHub Desktop.
Create a HTTP load balancer in GCP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Set default region and zone | |
| gcloud config set compute/region europe-west1 | |
| gcloud config set compute/zone europe-west1-b | |
| # Create an instance template | |
| gcloud compute instance-templates create lb-backend-template \ | |
| --region=europe-west1 \ | |
| --network=default \ | |
| --subnet=default \ | |
| --tags=allow-health-check \ | |
| --machine-type=e2-medium \ | |
| --image-family=debian-12 \ | |
| --image-project=debian-cloud \ | |
| --metadata=startup-script='#!/bin/bash | |
| apt-get update | |
| apt-get install apache2 -y | |
| a2ensite default-ssl | |
| a2enmod ssl | |
| vm_hostname="$(curl -H "Metadata-Flavor:Google" \ | |
| http://metadata.google.internal/computeMetadata/v1/instance/name)" | |
| echo "Page served from: $vm_hostname" | \ | |
| tee /var/www/html/index.html | |
| systemctl restart apache2' | |
| # Create a managed instance group | |
| gcloud compute instance-groups managed create lb-backend-group \ | |
| --template=lb-backend-template --size=2 --zone=europe-west1-b | |
| # Create a firewall rule to allow health checks | |
| 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 | |
| # Create a global static IP address | |
| gcloud compute addresses create lb-ipv4-1 \ | |
| --ip-version=IPV4 \ | |
| --global | |
| # Create a health check | |
| gcloud compute health-checks create http http-basic-check \ | |
| --port 80 | |
| # Create a backend service and add the instance group | |
| gcloud compute backend-services create web-backend-service \ | |
| --protocol=HTTP \ | |
| --port-name=http \ | |
| --health-checks=http-basic-check \ | |
| --global | |
| # Add the instance group to the backend service | |
| gcloud compute backend-services add-backend web-backend-service \ | |
| --instance-group=lb-backend-group \ | |
| --instance-group-zone=europe-west1-b \ | |
| --global | |
| # Create a URL map to route requests to the backend service | |
| gcloud compute url-maps create web-map-http \ | |
| --default-service web-backend-service | |
| # Create an HTTP proxy to route requests | |
| gcloud compute target-http-proxies create http-lb-proxy \ | |
| --url-map web-map-http | |
| # Create a global forwarding rule to route incoming requests to the proxy | |
| gcloud compute forwarding-rules create http-content-rule \ | |
| --address=lb-ipv4-1\ | |
| --global \ | |
| --target-http-proxy=http-lb-proxy \ | |
| --ports=80 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to create resources for the HTTP load balancer. You can read more about Managed instance groups (MIGs).
Set the values as follows, leaving all others at their defaults: