How to perform:
- Configure the default region and zone for your resources.
- Create multiple web server instances.
- Configure a load balancing service.
- Configure a forwarding rule to distribute traffic
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ gcloud config set compute/region us-west1
Updated property [compute/region].
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ gcloud config set compute/zone us-west1-b
Updated property [compute/zone].
Task 2. Create multiple web server instances
For this load balancing scenario, you create three Compute Engine VM instances and install Apache on them, then add a firewall rule that allows HTTP traffic to reach the instances.
The code provided sets the zone to us-west1-b. Setting the tags field lets you reference these instances all at once, such as with a firewall rule. These commands also install Apache on each instance and give each instance a unique home page.
Create a virtual machine, www1, in your default zone using the following code:
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ gcloud compute instances create www1 \
--zone=us-west1-b \
--tags=network-lb-tag \
--machine-type=e2-small \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "
<h3>Web Server: www1</h3>" | tee /var/www/html/index.html'
Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/zones/us-west1-b/instances/www1].
NAME: www1
ZONE: us-west1-b
MACHINE_TYPE: e2-small
PREEMPTIBLE:
INTERNAL_IP: 10.138.0.2
EXTERNAL_IP: 136.109.183.66
STATUS: RUNNING
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ gcloud compute instances create www2 \
--zone=us-west1-b \
--tags=network-lb-tag \
--machine-type=e2-small \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "
<h3>Web Server: www2</h3>" | tee /var/www/html/index.html'
Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/zones/us-west1-b/instances/www2].
NAME: www2
ZONE: us-west1-b
MACHINE_TYPE: e2-small
PREEMPTIBLE:
INTERNAL_IP: 10.138.0.3
EXTERNAL_IP: 8.229.181.237
STATUS: RUNNING
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ gcloud compute instances create www3 \
--zone=us-west1-b \
--tags=network-lb-tag \
--machine-type=e2-small \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "
<h3>Web Server: www3</h3>" | tee /var/www/html/index.html'
Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/zones/us-west1-b/instances/www3].
NAME: www3
ZONE: us-west1-b
MACHINE_TYPE: e2-small
PREEMPTIBLE:
INTERNAL_IP: 10.138.0.4
EXTERNAL_IP: 34.83.162.145
STATUS: RUNNING
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ gcloud compute firewall-rules create www-firewall-network-lb \
--target-tags network-lb-tag --allow tcp:80
Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/global/firewalls/www-firewall-network-lb].
Creating firewall...done.
NAME: www-firewall-network-lb
NETWORK: default
DIRECTION: INGRESS
PRIORITY: 1000
ALLOW: tcp:80
DENY:
DISABLED: False
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ gcloud compute instances list
NAME: www1
ZONE: us-west1-b
MACHINE_TYPE: e2-small
PREEMPTIBLE:
INTERNAL_IP: 10.138.0.2
EXTERNAL_IP: 136.109.183.66
STATUS: RUNNING
NAME: www2
ZONE: us-west1-b
MACHINE_TYPE: e2-small
PREEMPTIBLE:
INTERNAL_IP: 10.138.0.3
EXTERNAL_IP: 8.229.181.237
STATUS: RUNNING
NAME: www3
ZONE: us-west1-b
MACHINE_TYPE: e2-small
PREEMPTIBLE:
INTERNAL_IP: 10.138.0.4
EXTERNAL_IP: 34.83.162.145
STATUS: RUNNING
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ curl 136.109.183.66
<h3>Web Server: www1</h3>
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ curl 8.229.181.237
<h3>Web Server: www2</h3>
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ curl 34.83.162.145
<h3>Web Server: www3</h3>
\student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$
Task 3. Configure the load balancing service
Task 3. Configure the load balancing service
When you configure the load balancing service, your virtual machine instances receives packets that are destined for the static external IP address you configure. Instances made with a Compute Engine image are automatically configured to handle this IP address.
Create a static external IP address for your load balancer:
gcloud compute addresses create network-lb-ip-1 \
--region us-west1
Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/regions/us-west1/addresses/network-lb-ip-1].
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$
Add a legacy HTTP health check resource:
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ gcloud compute http-health-checks create basic-check
Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/global/httpHealthChecks/basic-check].
NAME: basic-check
HOST:
PORT: 80
REQUEST_PATH: /
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ gcloud compute target-pools create www-pool \
--region us-west1 --http-health-check basic-check
Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/regions/us-west1/targetPools/www-pool].
NAME: www-pool
REGION: us-west1
SESSION_AFFINITY: NONE
BACKUP:
HEALTH_CHECKS: basic-check
verify that you've created an L4 Network Load Balancer that points to the web servers.
Task 4. Create the target pool and forwarding rule
A target pool is a group of backend instances that receive incoming traffic from external passthrough NLBs. All backend instances of a target pool must reside in the same Google Cloud region.
Run the following to create the target pool and use the health check, which is required for the service to function:
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ gcloud compute target-pools add-instances www-pool \
--instances www1,www2,www3
Updated [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/regions/us-west1/targetPools/www-pool].
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ gcloud compute forwarding-rules create www-rule \
--region us-west1 \
--ports 80 \
--address network-lb-ip-1 \
--target-pool www-pool
Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/regions/us-west1/forwardingRules/www-rule].
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$
Task 5. Send traffic to your instances
Now that the load balancing service is configured, you can start sending traffic to the forwarding rule and watch the traffic be dispersed to different instances.
Enter the following command to view the external IP address of the www-rule forwarding rule used by the load balancer:
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ gcloud compute forwarding-rules describe www-rule --region us-west1
IPAddress: 34.169.2.109
IPProtocol: TCP
creationTimestamp: '2026-04-22T09:20:37.267-07:00'
description: ''
fingerprint: D_gAvdSipkg=
id: '132738789151165370'
kind: compute#forwardingRule
labelFingerprint: 42WmSpB8rSM=
loadBalancingScheme: EXTERNAL
name: www-rule
networkTier: PREMIUM
portRange: 80-80
region: https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/regions/us-west1
selfLink: https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/regions/us-west1/forwardingRules/www-rule
selfLinkWithId: https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/regions/us-west1/forwardingRules/132738789151165370
target: https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-2cdda1984aab/regions/us-west1/targetPools/www-pool
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ IPADDRESS=$(gcloud compute forwarding-rules describe www-rule --region us-west1 --format="json" | jq -r .IPAddress)
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ echo $IPADDRESS
34.169.2.109
Use the curl command to access the external IP address, replacing IP_ADDRESS with an external IP address from the previous command:
student_01_0334519f03d0@cloudshell:~ (qwiklabs-gcp-01-2cdda1984aab)$ while true; do curl -m1 $IPADDRESS; done
<h3>Web Server: www1</h3>
<h3>Web Server: www3</h3>
<h3>Web Server: www2</h3>
<h3>Web Server: www3</h3>
<h3>Web Server: www2</h3>
<h3>Web Server: www1</h3>
<h3>Web Server: www3</h3>
<h3>Web Server: www2</h3>
<h3>Web Server: www3</h3>
<h3>Web Server: www3</h3>
<h3>Web Server: www1</h3>
<h3>Web Server: www3</h3>
^C
The response from the curl command alternates randomly among the three instances. If your response is initially unsuccessful, wait approximately 30 seconds for the configuration to be fully loaded and for your instances to be marked healthy before trying again.
Use Ctrl + C to stop running the command.
No comments:
Post a Comment