Build Global and Regional Load Balancing Solutions

Solution for Build Global and Regional Load Balancing Solutions. 1 lab: GSP539. Fast copy-paste commands for Google Cloud.

GSP539 — Build Global and Regional Load Balancing Solutions: Challenge Lab

Estimated time: 1 hour 15 minutes

# 🚀 Build Global and Regional Load Balancing Solutions: Challenge Lab | GSP539 > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created for educational purposes, hands-on practice, and Google Cloud certification preparation. This guide is designed to help learners understand global and regional load balancing, internal proxy Network Load Balancers, external Application Load Balancers, HTTPS frontends, backend services, and failover testing. Always attempt the lab yoursel

clear
ORANGE=

\033[38;5;208m'
CYAN=

\033[0;36m'
GREEN=

\033[0;32m'
YELLOW=

\033[0;33m'
MAGENTA=

\033[0;35m'
BLUE=

\033[0;34m'
BOLD=

\033[1m'
RESET=

\033[0m'

echo "${ORANGE}${BOLD}   ____        _   _ _            __   ___              ${RESET}"
echo "${ORANGE}${BOLD}  / __ \      | | (_) |          / _| / _ \             ${RESET}"
echo "${ORANGE}${BOLD} | |  | |_ __| |__  _| |_  ___   | |_ | | | |_ __ ___  ${RESET}"
echo "${ORANGE}${BOLD} | |  | | '__| '_ \| | __| / _ \ |  _|| | | | '_ \ / __| ${RESET}"
echo "${ORANGE}${BOLD} | |__| | |  | |_) | | |_ | (_) || |  | |_| | |_) \__ \ ${RESET}"
echo "${ORANGE}${BOLD}  \____/|_|  |_.__/|_|\__| \___/ |_|   \___/| .__/|___/ ${RESET}"
echo "${ORANGE}${BOLD}                                            | |         ${RESET}"
echo "${ORANGE}${BOLD}                                            |_|         ${RESET}"
echo ""
echo "${MAGENTA}${BOLD}==========================================================${RESET}"
echo "${MAGENTA}${BOLD} STAGE 1: Internal Regional Proxy NLB (Master Build)      ${RESET}"
echo "${MAGENTA}${BOLD}==========================================================${RESET}"

read -p "${BLUE}${BOLD}Enter Region B (e.g., us-east1): ${RESET}" REGION_B
export REGION_B=$REGION_B

echo "${YELLOW}▶ Waiting for template to provision in the background...${RESET}"
while [ -z "$(gcloud compute instance-templates list --filter='name:template-proxy-internal' --format='value(name)')" ]; do sleep 5; done
TEMPLATE_PROXY_URI=$(gcloud compute instance-templates list --filter="name:template-proxy-internal" --format="value(selfLink)" | head -n 1)

echo "${YELLOW}▶ Discovering Exact Subnet...${RESET}"
SUBNET_B=$(gcloud compute networks subnets list --network=lb-network --regions=$REGION_B --format="value(name)" | grep -v "proxy" | head -n 1)
echo "${CYAN}Discovered Subnet B: ${SUBNET_B}${RESET}"

echo "${YELLOW}▶ Creating MIG & Setting Named Port (tcp80)...${RESET}"
gcloud compute instance-groups managed create mig-proxy-internal --template=$TEMPLATE_PROXY_URI --region=$REGION_B --size=1 --quiet 2>/dev/null || true
gcloud compute instance-groups managed set-named-ports mig-proxy-internal --named-ports=tcp80:80 --region=$REGION_B --quiet 2>/dev/null || true

echo "${YELLOW}▶ Creating Firewalls...${RESET}"
gcloud compute firewall-rules create fw-allow-hc-proxy-internal --network=lb-network --action=allow --direction=ingress --source-ranges=130.211.0.0/22,35.191.0.0/16 --target-tags=tag-proxy-internal --rules=tcp:80 --quiet 2>/dev/null || true
gcloud compute firewall-rules create fw-allow-proxy-subnet-internal --network=lb-network --action=allow --direction=ingress --source-ranges=10.129.0.0/23 --target-tags=tag-proxy-internal --rules=tcp:80 --quiet 2>/dev/null || true

echo "${YELLOW}▶ Reserving Shared VIP Internal IP Address...${RESET}"
gcloud compute addresses create ip-internal-proxy --region=$REGION_B --subnet=$SUBNET_B --purpose=SHARED_LOADBALANCER_VIP --quiet 2>/dev/null || true

echo "${YELLOW}▶ Creating Health Check...${RESET}"
gcloud compute health-checks create tcp hc-internal-proxy --region=$REGION_B --port=80 --quiet 2>/dev/null || true

echo "${YELLOW}▶ Deploying Backend Service (with tcp80 Port Name)...${RESET}"
gcloud compute backend-services create internal-proxy-backend --load-balancing-scheme=INTERNAL_MANAGED --protocol=TCP --region=$REGION_B --health-checks=hc-internal-proxy --health-checks-region=$REGION_B --port-name=tcp80 --quiet 2>/dev/null || true
gcloud compute backend-services add-backend internal-proxy-backend --region=$REGION_B --instance-group=mig-proxy-internal --instance-group-region=$REGION_B --quiet 2>/dev/null || true

echo "${YELLOW}▶ Deploying Target TCP Proxy & Forwarding Rule...${RESET}"
gcloud compute target-tcp-proxies create proxy-internal --region=$REGION_B --backend-service=internal-proxy-backend --quiet 2>/dev/null || true
gcloud compute forwarding-rules create rule-internal-proxy --region=$REGION_B --load-balancing-scheme=INTERNAL_MANAGED --network=lb-network --subnet=$SUBNET_B --address=ip-internal-proxy --ports=110 --target-tcp-proxy=proxy-internal --target-tcp-proxy-region=$REGION_B --quiet 2>/dev/null || true

echo "${YELLOW}▶ Deploying Client Validation VM...${RESET}"
ZONE_B=$(gcloud compute zones list --filter="region:($REGION_B)" --format="value(name)" | head -n 1)
gcloud compute instances create vm-client-internal --zone=$ZONE_B --machine-type=e2-micro --network=lb-network --subnet=$SUBNET_B --tags=allow-ssh --quiet 2>/dev/null || true

echo "${GREEN}${BOLD}✓ Master Build Complete!${RESET}"
echo "${ORANGE}${BOLD}Wait ~45 seconds for Google Cloud to register the backend health, then click 'Check my progress' for Task 1.${RESET}"
echo "${MAGENTA}${BOLD}==========================================================${RESET}"
clear
ORANGE=

\033[38;5;208m'
CYAN=

\033[0;36m'
GREEN=

\033[0;32m'
YELLOW=

\033[0;33m'
MAGENTA=

\033[0;35m'
BLUE=

\033[0;34m'
BOLD=

\033[1m'
RESET=

\033[0m'

echo "${ORANGE}${BOLD}   ____        _   _ _            __   ___              ${RESET}"
echo "${ORANGE}${BOLD}  / __ \      | | (_) |          / _| / _ \             ${RESET}"
echo "${ORANGE}${BOLD} | |  | |_ __| |__  _| |_  ___   | |_ | | | |_ __ ___  ${RESET}"
echo "${ORANGE}${BOLD} | |  | | '__| '_ \| | __| / _ \ |  _|| | | | '_ \ / __| ${RESET}"
echo "${ORANGE}${BOLD} | |__| | |  | |_) | | |_ | (_) || |  | |_| | |_) \__ \ ${RESET}"
echo "${ORANGE}${BOLD}  \____/|_|  |_.__/|_|\__| \___/ |_|   \___/| .__/|___/ ${RESET}"
echo "${ORANGE}${BOLD}                                            | |         ${RESET}"
echo "${ORANGE}${BOLD}                                            |_|         ${RESET}"
echo ""
echo "${MAGENTA}${BOLD}==========================================================${RESET}"
echo "${MAGENTA}${BOLD} STAGE 2: Global External ALB (Task 2)                    ${RESET}"
echo "${MAGENTA}${BOLD}==========================================================${RESET}"

read -p "${BLUE}${BOLD}Enter Region A (e.g., us-east4): ${RESET}" REGION_A
read -p "${BLUE}${BOLD}Enter Region B (e.g., us-central1): ${RESET}" REGION_B

echo "${YELLOW}▶ Waiting for template to provision...${RESET}"
while [ -z "$(gcloud compute instance-templates list --filter='name:template-alb-api' --format='value(name)')" ]; do sleep 5; done
TEMPLATE_ALB_URI=$(gcloud compute instance-templates list --filter="name:template-alb-api" --format="value(selfLink)" | head -n 1)

echo "${YELLOW}▶ Creating Regional MIGs (A & B)...${RESET}"
gcloud compute instance-groups managed create mig-alb-api-a --template=$TEMPLATE_ALB_URI --region=$REGION_A --size=1 --quiet 2>/dev/null || true
gcloud compute instance-groups managed set-named-ports mig-alb-api-a --named-ports=http80:80 --region=$REGION_A --quiet 2>/dev/null || true
gcloud compute instance-groups managed create mig-alb-api-b --template=$TEMPLATE_ALB_URI --region=$REGION_B --size=1 --quiet 2>/dev/null || true
gcloud compute instance-groups managed set-named-ports mig-alb-api-b --named-ports=http80:80 --region=$REGION_B --quiet 2>/dev/null || true

echo "${YELLOW}▶ Configuring Global Backend Service (with Rate Limiting)...${RESET}"
gcloud compute health-checks create http http-check-alb --global --port=80 --quiet 2>/dev/null || true
gcloud compute backend-services create service-alb-global --global --protocol=HTTP --health-checks=http-check-alb --port-name=http80 --quiet 2>/dev/null || true
gcloud compute backend-services add-backend service-alb-global --global --instance-group=mig-alb-api-a --instance-group-region=$REGION_A --balancing-mode=RATE --max-rate-per-instance=1 --quiet 2>/dev/null || true
gcloud compute backend-services add-backend service-alb-global --global --instance-group=mig-alb-api-b --instance-group-region=$REGION_B --balancing-mode=RATE --max-rate-per-instance=1 --quiet 2>/dev/null || true

echo "${YELLOW}▶ Generating SSL Certificate and Global IP...${RESET}"
openssl genrsa -out key.pem 2048 2>/dev/null
openssl req -new -x509 -key key.pem -out cert.pem -days 1 -subj "/CN=example.com" 2>/dev/null
gcloud compute ssl-certificates create cert-self-signed --certificate=cert.pem --private-key=key.pem --global --quiet 2>/dev/null || true
gcloud compute addresses create ip-alb-global --global --quiet 2>/dev/null || true

echo "${YELLOW}▶ Deploying Global Load Balancer Frontend...${RESET}"
gcloud compute url-maps create url-map-alb --default-service=service-alb-global --quiet 2>/dev/null || true
gcloud compute target-https-proxies create https-proxy-alb --url-map=url-map-alb --ssl-certificates=cert-self-signed --quiet 2>/dev/null || true
gcloud compute forwarding-rules create https-forwarding-rule --global --target-https-proxy=https-proxy-alb --ports=443 --address=ip-alb-global --quiet 2>/dev/null || true

echo "${YELLOW}▶ Creating Firewall Rule...${RESET}"
ALB_NET_URI=$(gcloud compute instance-templates list --filter="name:template-alb-api" --format="value(properties.networkInterfaces[0].network)" | head -n 1)
ALB_NET_NAME=$(basename $ALB_NET_URI)
gcloud compute firewall-rules create fw-allow-health-check-and-proxy --network=$ALB_NET_NAME --action=allow --direction=ingress --source-ranges=130.211.0.0/22,35.191.0.0/16 --target-tags=tag-alb-api --rules=tcp:80 --quiet 2>/dev/null || true

echo "${GREEN}${BOLD}✓ Stage 2 Complete!${RESET}"
echo "${ORANGE}${BOLD}WAIT 2-3 MINUTES for the Global Load Balancer APIs to propagate, then click 'Check my progress' for Task 2.${RESET}"
echo "${MAGENTA}${BOLD}==========================================================${RESET}"
ORANGE=

\033[38;5;208m'
CYAN=

\033[0;36m'
GREEN=

\033[0;32m'
YELLOW=

\033[0;33m'
MAGENTA=

\033[0;35m'
BLUE=

\033[0;34m'
BOLD=

\033[1m'
RESET=

\033[0m'

echo "${MAGENTA}${BOLD}==========================================================${RESET}"
echo "${MAGENTA}${BOLD} STAGE 3: Simulate Failover (Task 3)                      ${RESET}"
echo "${MAGENTA}${BOLD}==========================================================${RESET}"

echo "${YELLOW}▶ Waiting for MIG A instances to finish booting...${RESET}"
while [ -z "$(gcloud compute instances list --filter='name:mig-alb-api-a*' --format='value(name)')" ]; do echo "${CYAN}Waiting for instance in Region A...${RESET}"; sleep 5; done

INSTANCE=$(gcloud compute instances list --filter="name:mig-alb-api-a*" --format="value(name)" | head -n 1)
ZONE=$(gcloud compute instances list --filter="name:mig-alb-api-a*" --format="value(zone)" | awk -F/ '{print $NF}' | head -n 1)

echo "${BLUE}Target Instance: ${INSTANCE} (Zone: ${ZONE})${RESET}"

echo "${YELLOW}▶ Configuring SSH keys...${RESET}"
mkdir -p ~/.ssh
ssh-keygen -t rsa -f ~/.ssh/google_compute_engine -N "" -q <<< y >/dev/null 2>&1 || true

echo "${YELLOW}▶ Stopping Nginx on Region A backend ($INSTANCE)...${RESET}"
gcloud compute ssh "$INSTANCE" --zone="$ZONE" --quiet --command="sudo systemctl stop nginx"

echo "${GREEN}${BOLD}✓ Nginx successfully stopped. Traffic is failing over to Region B.${RESET}"
echo "${ORANGE}${BOLD}Wait ~60 seconds for Google's health checks to register the failure, then click 'Check my progress' for Task 3.${RESET}"
echo "${MAGENTA}${BOLD}==========================================================${RESET}"