Last active
November 10, 2025 17:32
-
-
Save r3xakead0/91423a6d2aea73ccd60c24eea299219d to your computer and use it in GitHub Desktop.
Create multiple web server instances with load balancing service 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
| #!/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 |
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
| #!/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 |
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
| #!/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] |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you need to create the resources that support the load balancing service.