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 > ⚠️ **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 Google Cloud load balancing, regional and global network architectures, managed instance groups, firewall rules, SSL certificates, traffic routing, and failover testing through practical exercises. Always att
# ==============================================================================
# Color Variables & Orbit of Ops Branding
# ==============================================================================
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
BLUE='\e[1;34m'
MAGENTA='\e[1;35m'
RED='\e[1;31m'
RESET='\e[0m'
BOLD='\e[1m'
clear
echo -e "${CYAN}${BOLD}"
cat << "EOF"
____ _ _ _ __ ___
/ __ \ | | (_) | / _| / _ \
| | | |_ __| |__ _| |_ ___ | |_ | | | |_ __ ___
| | | | '__| '_ \| | __| / _ \ | _| | | | | '_ \/ __|
| |__| | | | |_) | | |_ | (_) || | | |_| | |_) \__ \
\____/|_| |_.__/|_|\__| \___/ |_| \___/| .__/|___/
| |
|_|
EOF
echo -e "${RESET}"
echo -e "${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP539 AUTOMATION INITIALIZED <<<${RESET}\n"
# ==============================================================================
# PRE-FLIGHT CHECKS & VARIABLES
# ==============================================================================
export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Please enter the Regions specified in your lab instructions:${RESET}"
read -p "$(echo -e ${CYAN}${BOLD}Enter REGION_A: ${RESET})" REGION_A
export REGION_A
read -p "$(echo -e ${CYAN}${BOLD}Enter REGION_B: ${RESET})" REGION_B
export REGION_B
echo -e "\n✅ Project ID: ${GREEN}$PROJECT_ID${RESET}"
echo -e "✅ Region A: ${GREEN}$REGION_A${RESET}"
echo -e "✅ Region B: ${GREEN}$REGION_B${RESET}\n"echo -e "${BLUE}${BOLD}[Orbit of Ops] Step 1: Deploying Regional MIG (Internal)...${RESET}"
# Auto-detect the exact URI of the template to bypass Regional/Global scope errors
export TEMPLATE_URI=$(gcloud compute instance-templates list --filter="name:template-proxy-internal" --uri | head -n 1)
gcloud compute instance-groups managed create mig-proxy-internal \
--template=$TEMPLATE_URI \
--size=1 \
--region=$REGION_B || true
gcloud compute instance-groups managed set-named-ports mig-proxy-internal \
--named-ports=tcp80:80 \
--region=$REGION_B || true
echo -e "${CYAN}${BOLD}[Orbit of Ops] Step 2: Defining Proxy Firewall Rules...${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 || 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 || true
echo -e "${BLUE}${BOLD}[Orbit of Ops] Step 3: Configuring Internal Proxy NLB...${RESET}"
gcloud compute health-checks create tcp hc-internal-proxy \
--region=$REGION_B \
--port=80 || true
SUBNET_B=$(gcloud compute networks subnets list --network=lb-network --filter="region:($REGION_B)" --format="value(name)" | head -n 1)
gcloud compute addresses create ip-internal-proxy \
--region=$REGION_B \
--subnet=$SUBNET_B \
--purpose=SHARED_LOADBALANCER_VIP || true
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 || true
gcloud compute backend-services add-backend internal-proxy-backend \
--instance-group=mig-proxy-internal \
--instance-group-region=$REGION_B \
--region=$REGION_B || true
gcloud compute target-tcp-proxies create target-proxy-internal \
--backend-service=internal-proxy-backend \
--region=$REGION_B || 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 \
--target-tcp-proxy=target-proxy-internal \
--ports=110 || true
echo -e "${CYAN}${BOLD}[Orbit of Ops] Step 4: Deploying Client VM...${RESET}"
gcloud compute instances create vm-client-internal \
--zone=${REGION_B}-b \
--machine-type=e2-micro \
--network=lb-network \
--subnet=$SUBNET_B \
--tags=allow-ssh || true
echo -e "${GREEN}${BOLD}✅ Task 1 Provisioning Complete! Waiting 45 seconds for propagation...${RESET}"
sleep 45
export LB_IP=$(gcloud compute addresses describe ip-internal-proxy --region=$REGION_B --format="value(address)")
echo -e "${YELLOW}${BOLD}Internal LB IP: $LB_IP${RESET}"echo -e "${BLUE}${BOLD}[Orbit of Ops] Step 5: Deploying Global External Backends...${RESET}"
gcloud compute instance-groups managed create mig-alb-api-a --template=template-alb-api --size=1 --region=$REGION_A
gcloud compute instance-groups managed set-named-ports mig-alb-api-a --named-ports=http80:80 --region=$REGION_A
gcloud compute instance-groups managed create mig-alb-api-b --template=template-alb-api --size=1 --region=$REGION_B
gcloud compute instance-groups managed set-named-ports mig-alb-api-b --named-ports=http80:80 --region=$REGION_B
echo -e "${CYAN}${BOLD}[Orbit of Ops] Step 6: Configuring Global Backend Components...${RESET}"
gcloud compute health-checks create http http-check-alb --global --port=80
gcloud compute backend-services create service-alb-global \
--global \
--protocol=HTTP \
--health-checks=http-check-alb \
--port-name=http80
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
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
echo -e "${BLUE}${BOLD}[Orbit of Ops] Step 7: Setting up SSL & Frontend...${RESET}"
openssl genrsa -out key.pem 2048
openssl req -new -x509 -key key.pem -out cert.pem -days 1 -subj "/CN=example.com"
gcloud compute ssl-certificates create cert-self-signed --certificate=cert.pem --private-key=key.pem --global
gcloud compute addresses create ip-alb-global --global
gcloud compute url-maps create url-map-alb --default-service=service-alb-global
gcloud compute target-https-proxies create https-proxy-alb --url-map=url-map-alb --ssl-certificates=cert-self-signed
gcloud compute forwarding-rules create https-forwarding-rule \
--global \
--target-https-proxy=https-proxy-alb \
--ports=443 \
--address=ip-alb-global
gcloud compute firewall-rules create fw-allow-health-check-and-proxy \
--network=default \
--direction=INGRESS \
--action=ALLOW \
--rules=tcp:80 \
--source-ranges=130.211.0.0/22,35.191.0.0/16 \
--target-tags=tag-alb-api || true
echo -e "${YELLOW}${BOLD}⏳ Waiting 60 seconds for global routing tables to propagate...${RESET}"
sleep 60
echo -e "${MAGENTA}${BOLD}[Orbit of Ops] Step 8: Simulating Failover...${RESET}"
INSTANCE_A=$(gcloud compute instances list --filter="name~'^mig-alb-api-a'" --format="value(name)" | head -1)
ZONE_A=$(gcloud compute instances list --filter="name=$INSTANCE_A" --format="value(zone.basename())")
gcloud compute ssh "$INSTANCE_A" --zone="$ZONE_A" --quiet --command="sudo systemctl stop nginx"
echo -e "\n${GREEN}${BOLD}🎉 TASK 2 AND 3 COMPLETE!${RESET}"
echo -e "${YELLOW}${BOLD}>>> Failover triggered successfully. You can now verify the tasks in your Qwiklabs panel! <<<${RESET}"