Skip to content

Instantly share code, notes, and snippets.

@r3xakead0
Last active November 10, 2025 17:32
Show Gist options
  • Select an option

  • Save r3xakead0/91423a6d2aea73ccd60c24eea299219d to your computer and use it in GitHub Desktop.

Select an option

Save r3xakead0/91423a6d2aea73ccd60c24eea299219d to your computer and use it in GitHub Desktop.
Create multiple web server instances with load balancing service in GCP
#!/bin/sh
# Set default region and zone
gcloud config set compute/region europe-west1
gcloud config set compute/zone europe-west1-b
# Create a static IP address
gcloud compute addresses create network-lb-ip-1 \
--region europe-west1
# Create a health check
gcloud compute http-health-checks create basic-check
# Create a target pool and add instances
gcloud compute target-pools create www-pool \
--region europe-west1 --http-health-check basic-check
# Add instances to the target pool
gcloud compute target-pools add-instances www-pool \
--instances web1,web2,web3
# Create a forwarding rule to route traffic to the target pool
gcloud compute forwarding-rules create www-rule \
--region europe-west1 \
--ports 80 \
--address network-lb-ip-1 \
--target-pool www-pool
#!/bin/sh
# Set default region and zone
gcloud config set compute/region europe-west1
gcloud config set compute/zone europe-west1-b
# Cleanup resources
gcloud compute http-health-checks delete basic-check
gcloud compute addresses delete network-lb-ip-1 \
--region europe-west1
gcloud compute firewall-rules delete www-firewall-network-lb
gcloud compute instances delete web1 --zone=europe-west1-b
gcloud compute instances delete web2 --zone=europe-west1-b
gcloud compute instances delete web3 --zone=europe-west1-b
#!/bin/sh
# Set default region and zone
gcloud config set compute/region europe-west1
gcloud config set compute/zone europe-west1-b
# Create three VM instances with Apache installed
gcloud compute instances create web1 \
--zone=europe-west1-b \
--tags=network-lb-tag \
--machine-type=e2-small \
--image-family=debian-12 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "<h3>Web Server: web1</h3>" | tee /var/www/html/index.html'
gcloud compute instances create web2 \
--zone=europe-west1-b \
--tags=network-lb-tag \
--machine-type=e2-small \
--image-family=debian-12 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "<h3>Web Server: web2</h3>" | tee /var/www/html/index.html'
gcloud compute instances create web3 \
--zone=europe-west1-b \
--tags=network-lb-tag \
--machine-type=e2-small \
--image-family=debian-12 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "<h3>Web Server: web3</h3>" | tee /var/www/html/index.html'
# Create a firewall rule to allow HTTP traffic
gcloud compute firewall-rules create www-firewall-network-lb \
--target-tags network-lb-tag --allow tcp:80
# Get the external IP address of the forwarding rule
gcloud compute instances list
curl http://[IP_ADDRESS]
@r3xakead0
Copy link
Author

you need to create three Compute Engine VM instances using the configuration that follows, install Apache on them, and then add a firewall rule www-firewall-network-lb that allows HTTP traffic to reach the instances.

Note: Make sure all the instances are under the default network.

  1. Set the following values, leaving all others at their defaults:
Property Value (type value or select option as specified)
VM Instance -1 web1
VM Instance -2 web2
VM Instance -3 web3
Region europe-west1
Zone europe-west1-b
Series E2
Machine type e2-small
Tags network-lb-tag
image-family debian-12
image-project debian-cloud

Use the following script for installing Apache on each server (updating web-number such as web1 or web2 or web3 as per the instance's name):

#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "<h3>Web Server: web-number</h3>" | tee /var/www/html/index.html

Note: You can verify that each instance is running with curl, replacing [IP_ADDRESS] with the IP address for each of your VMs:

curl http://[IP_ADDRESS]

If it doesn't work, try resetting the VM.

@r3xakead0
Copy link
Author

you need to create the resources that support the load balancing service.

  1. Set the following values, leaving all others at their defaults:
Property Value (type value or select option as specified)
Static external IP network-lb-ip-1
Target-pool www-pool
Ports 80
  1. Once the load balancing service is configured, start sending traffic to the forwarding rule and watch the traffic be dispersed to different instances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment