Skip to content

Instantly share code, notes, and snippets.

@fResult
Last active March 11, 2025 14:15
Show Gist options
  • Select an option

  • Save fResult/ffd40b3969f796345af15bdf0599ab2d to your computer and use it in GitHub Desktop.

Select an option

Save fResult/ffd40b3969f796345af15bdf0599ab2d to your computer and use it in GitHub Desktop.
Implement Load Balancing on Compute Engine Failed

GCP Skills Boost - Challenge Lab

Test Passed:

image

But check progress failed:

image

Prepare variable

export ACCOUNT=student-00-93fbfb5f587e@qwiklabs.net
export PROJECT_ID=qwiklabs-gcp-02-9a880db6264f
export REGION=us-west3
export ZONE=us-west3-c
export INSTANCE_NAME=nucleus-jumphost-721
export FIREWALL_RULE=permit-tcp-rule-781

Task 1

gcloud compute instances create $INSTANCE_NAME \
    --zone=$ZONE \
    --tags=network-lb-tag \
    --machine-type=e2-micro \
    --image-family=debian-11 \
    --image-project=debian-cloud

Task 2

0. Create the startup script for the web servers:

cat << EOF > startup.sh
#! /bin/bash
apt-get update
apt-get install -y nginx
service nginx start
sed -i -- 's/nginx/Google Cloud Platform - '"\$HOSTNAME"'/' /var/www/html/index.nginx-debian.html
EOF

1. Create a static external IP address for your load balancer:

gcloud compute addresses create network-lb-ip-global \
  --ip-version=IPV4 \
  --global

2. Create an instance template:

gcloud compute instance-templates create nucleus-webserver-template \
    --machine-type=e2-medium \
    --metadata-from-file=startup-script=startup.sh \
    --image-family=debian-11 \
    --image-project=debian-cloud \
    --region=global \
    --tags=allow-health-check,network-lb-tag

3. Create a managed instance group based on the template:

gcloud compute instance-groups managed create nucleus-webserver-group \
    --template=nucleus-webserver-template \
    --size 2 \
    --zone $ZONE

4.Create a firewall rule to allow traffic on port 80:

gcloud compute firewall-rules create $FIREWALL_RULE \
    --target-tags network-lb-tag --allow tcp:80

gcloud compute firewall-rules create nucleus-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

5. Create a health check:

gcloud compute health-checks create http nucleus-health-check \
    --port 80

6. Create a backend service and add the instance group:

gcloud compute backend-services create nucleus-backend-service \
    --protocol=HTTP \
    --port-name=http \
    --health-checks=nucleus-health-check \
    --global

gcloud compute backend-services add-backend nucleus-backend-service \
    --instance-group=nucleus-webserver-group \
    --instance-group-zone=$ZONE \
    --global

7. Create a URL map to route the incoming requests to the default backend service:

gcloud compute url-maps create nucleus-url-map \
    --default-service=nucleus-backend-service

8. Create a target HTTP proxy to route requests to your URL map:

gcloud compute target-http-proxies create nucleus-http-proxy \
    --url-map=nucleus-url-map

9. Create a global forwarding rule:

gcloud compute forwarding-rules create nucleus-http-forwarding-rule \
    --address=network-lb-ip-global \
    --global \
    --target-http-proxy=nucleus-http-proxy \
    --ports=80

Test Load Balancer

EXTERNAL_IP=$( \
  gcloud compute forwarding-rules describe nucleus-http-forwarding-rule --global --format="get(IPAddress)" \
) \
while true; do curl -m1 $EXTERNAL_IP; done

Evidenced

curl  $EXTERNAL_IP
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Google Cloud Platform - nucleus-webserver-group-t1k0!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to Google Cloud Platform - nucleus-webserver-group-t1k0!</h1>
<p>If you see this page, the Google Cloud Platform - nucleus-webserver-group-t1k0 web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://Google Cloud Platform - nucleus-webserver-group-t1k0.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://Google Cloud Platform - nucleus-webserver-group-t1k0.com/">nginx.com</a>.</p>

<p><em>Thank you for using Google Cloud Platform - nucleus-webserver-group-t1k0.</em></p>
</body>
</html>curl  $EXTERNAL_IP
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Google Cloud Platform - nucleus-webserver-group-zcwp!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to Google Cloud Platform - nucleus-webserver-group-zcwp!</h1>
<p>If you see this page, the Google Cloud Platform - nucleus-webserver-group-zcwp web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://Google Cloud Platform - nucleus-webserver-group-zcwp.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://Google Cloud Platform - nucleus-webserver-group-zcwp.com/">nginx.com</a>.</p>

<p><em>Thank you for using Google Cloud Platform - nucleus-webserver-group-zcwp.</em></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment