In this guide, we will learn how to set up a web server system that can handle more users on Google Cloud Platform (GCP). We will use Compute Engine to make web server computers (called instances). We will also use load balancers to share the website traffic so no server gets too busy.
Steps we will do:
Create three web server virtual machines.
Set up firewall rules to allow traffic.
Configure the network load balancer and the HTTP load balancer to share traffic between servers.
This guide will help you finish the GSP313 Google Cloud lab called "Implement Load Balancing on Compute Engine: Challenge Lab.
Task 1. Create multiple web server instances
The first task is to make three Debian VM instances (web1, web2, web3) using GCP Compute Engine. Each VM will run Apache and show a simple webpage that tells which server it is.
| Property | Value |
|---|
| Region | us-west1 |
| Zone | us-west1-b |
| Series | E2 |
| Machine Type | e2-small |
| Image Family | debian-12 |
| Image Project | debian-cloud |
| Network Tag | network-lb-tag |
Startup Script
Each VM has a startup script that installs Apache and makes a simple HTML page.
'#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "
<h3>Web Server: web<num></num>
</h3>" | tee /var/www/html/index.html'
1. Create VM web1
gcloud compute instances create web1 \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--zone=us-west1-b \
--machine-type=e2-small \
--tags=network-lb-tag \
--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'
2. Create VM web2
gcloud compute instances create web2 \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--zone=us-west1-b \
--machine-type=e2-small \
--tags=network-lb-tag \
--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'
3. Create VM web3
gcloud compute instances create web3 \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--zone=us-west1-b \
--machine-type=e2-small \
--tags=network-lb-tag \
--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'
4. Create Firewall Rule for HTTP
gcloud compute firewall-rules create www-firewall-network-lb \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--network=default \
--action=ALLOW \
--rules=tcp:80 \
--source-ranges=0.0.0.0/0 \
--target-tags=network-lb-tag \
--description="Allow HTTP traffic to instances with network-lb-tag"
Task 2. Configure Network Load Balancing
For this task, need to make the resources that help the load balancer work.
Set these values and leave all other settings as default:
| Property | Value (type value or select option as specified) |
|---|
| Static external IP | network-lb-ip-1 |
| Target-pool | www-pool |
| Ports | 80 |
1. Reserve a Static IP
gcloud compute addresses create network-lb-ip-1 \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--region=us-west1
2. Create HTTP Health Check
gcloud compute http-health-checks create basic-check \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--port=80 \
--request-path=/
3. Create Target Pool
gcloud compute target-pools create www-pool \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--region=us-west1 \
--http-health-check=basic-check
4. Add Instances to Target Pool
gcloud compute target-pools add-instances www-pool \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--region=us-west1 \
--instances=web1,web2,web3 \
--instances-zone=us-west1-b
5. Create Forwarding Rule
gcloud compute forwarding-rules create www-rule \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--region=us-west1 \
--ports=80 \
--address=network-lb-ip-1 \
--target-pool=www-pool
Task 3. Create an HTTP load balancer
For this task, need to make the resources for the HTTP load balancer.
Set the values below and leave all other settings as default:
| Property | Value (type value or select option as specified) |
|---|
| Backend Template | lb-backend-template |
| tags | allow-health-check |
| Managed instance group | lb-backend-group |
| machine-type | e2-medium |
| image-family and image-project | same as previously created VMs |
| fw-allow-health-check | fw-allow-health-check |
| Allow source-ranges | 130.211.0.0/22, 35.191.0.0/16 |
| Traffic | ingress |
| Port | 80 |
| external IP address | lb-ipv4-1 |
| health check | http-basic-check |
| URL map | web-map-http |
| Target http proxy | http-lb-proxy |
1. Create Instance Template
gcloud compute instance-templates create lb-backend-template \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--region=us-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'
2. Create Managed Instance Group
gcloud compute instance-groups managed create lb-backend-group \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--zone=us-west1-b \
--template=lb-backend-template \
--size=2 \
--description="Managed instance group for HTTP Load Balancer"
3. Create Firewall Rule for Health Checks
gcloud compute firewall-rules create fw-allow-health-check \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--network=default \
--action=ALLOW \
--rules=tcp:80 \
--source-ranges=130.211.0.0/22,35.191.0.0/16 \
--target-tags=allow-health-check \
--description="Allow health check traffic for HTTP Load Balancer"
4. Reserve Global Static IP
gcloud compute addresses create lb-ipv4-1 \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--ip-version=IPV4 \
--global
5. Create Global HTTP Health Check
gcloud compute health-checks create http http-basic-check \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--port=80 \
--request-path=/ \
--global
6. Create Backend Service
gcloud compute backend-services create web-backend-service \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--protocol=HTTP \
--port-name=http \
--health-checks=http-basic-check \
--global \
--description="Backend service for web servers"
7. Set Named Ports
gcloud compute instance-groups managed set-named-ports lb-backend-group \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--zone=us-west1-b \
--named-ports=http:80
8. Add Backend to Backend Service
gcloud compute backend-services add-backend web-backend-service \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--instance-group=lb-backend-group \
--instance-group-zone=us-west1-b \
--balancing-mode=UTILIZATION \
--global
9. Create URL Map
gcloud compute url-maps create web-map-http \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--default-service=web-backend-service \
--global
10. Create HTTP Proxy
gcloud compute target-http-proxies create http-lb-proxy \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--url-map=web-map-http \
--global
11. Create Global Forwarding Rule
gcloud compute forwarding-rules create http-fwd-rule \
--project=qwiklabs-gcp-03-8beba1f05a19 \
--load-balancing-scheme=EXTERNAL \
--network-tier=PREMIUM \
--global \
--target-http-proxy=http-lb-proxy \
--address=lb-ipv4-1 \
--ports=80
Conclusion
In this lab, you learned how to build a web server system on Google Cloud Platform (GCP) that can handle more users with load balancing.
You made three web servers and set firewall rules to let web traffic pass. Then you built a Network Load Balancer and an HTTP Load Balancer to share traffic between the servers.
This setup makes your website faster and more stable because the load balancer sends users to servers that are free and working well.
Now you have a simple and strong web server system running on Google Cloud.