Network load balancer on GCP cloud

By Jason Tzu-Cheng Chuang 2022-10-06

You must have a valid Google account and log in to Google Cloud Console. https://console.cloud.google.com/

  • Click “Activate Cloud Shell” icon at the top of the Google Cloud console.
  • Click “Continue”.
  • (Optional) You can list the active account name with this command:
    gcloud auth list
    
  • (Optional) You can list the project ID with this command:
    gcloud config list project
    
  • In Cloud Shell, set the default zone, default region:
    gcloud config set compute/zone asia-east1-a
    gcloud config set compute/region asia-east1
    

Layer 4 Network Load Balancer

Create multiple web server instances

Create a virtual machine www1 in your default zone.

gcloud compute instances create www1 \
    --zone=asia-east1-a \
    --tags=network-lb-tag \
    --machine-type=e2-medium \
    --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'

Create a virtual machine www2 in your default zone.

gcloud compute instances create www2 \
    --zone=asia-east1-a \
    --tags=network-lb-tag \
    --machine-type=e2-medium \
    --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'

Create a firewall rule to allow external traffic to the VM instances:

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

Run the following to list your instances. You’ll see their IP addresses in the EXTERNAL_IP column:

gcloud compute instances list

Configure the load balancing service

Create a static external IP address for your load balancer:

gcloud compute addresses create network-lb-ip-1 \
    --region asia-east1

Add a legacy HTTP health check resource:

gcloud compute http-health-checks create basic-check

Add a target pool in the same region as your instances. Run the following to create the target pool and use the health check, which is required for the service to function:

gcloud compute target-pools create www-pool \
    --region asia-east1 --http-health-check basic-check

Add the instances to the pool:

gcloud compute target-pools add-instances www-pool \
    --instances www1,www2

Add a forwarding rule:

gcloud compute forwarding-rules create www-rule \
    --region  asia-east1 \
    --ports 80 \
    --address network-lb-ip-1 \
    --target-pool www-pool

Sending traffic to your instances

gcloud compute forwarding-rules describe www-rule --region asia-east1

IPADDRESS=$(gcloud compute forwarding-rules describe www-rule --region asia-east1 --format="json" | jq -r .IPAddress)

echo $IPADDRESS

curl http://$IPADDRESS

You will see the result from www1 and www2 when you curl the IPADDRESS more times