Skip to content

Instantly share code, notes, and snippets.

@DawnBreather
Last active February 27, 2024 18:15
Show Gist options
  • Save DawnBreather/26e9fe3f1064aa49b0ac949860fba32f to your computer and use it in GitHub Desktop.
Save DawnBreather/26e9fe3f1064aa49b0ac949860fba32f to your computer and use it in GitHub Desktop.
GCP: Create multi-regional Load-Balancer
# Google Cloud Multi-Regional Load Balancer Setup for Cloud Run
# Prerequisites:
# - Google Cloud SDK installed and initialized
# - Cloud Run services deployed in desired regions
# Step 1: Define Variables
# Replace these variables with your specific service names, regions, and project details
SERVICE_NAMES=("service1" "service2") # Example service names
REGIONS=("us-central1" "europe-west1") # Example regions
PROJECT_ID="your-project-id"
NEG_NAMES=("neg-us" "neg-eu") # Example NEG names, corresponding to each service and region
# Step 2: Create Serverless Network Endpoint Groups (NEGs) for Each Cloud Run Service
for i in "${!SERVICE_NAMES[@]}"; do
gcloud compute network-endpoint-groups create "${NEG_NAMES[$i]}" \
--region="${REGIONS[$i]}" \
--network-endpoint-type=SERVERLESS \
--cloud-run-service="${SERVICE_NAMES[$i]}" \
--project="${PROJECT_ID}"
done
# Step 3: Create a Global Backend Service
BACKEND_SERVICE_NAME="my-backend-service"
gcloud compute backend-services create "${BACKEND_SERVICE_NAME}" \
--global \
--project="${PROJECT_ID}"
# Attach NEGs to the Backend Service
for i in "${!REGIONS[@]}"; do
gcloud compute backend-services add-backend "${BACKEND_SERVICE_NAME}" \
--global \
--network-endpoint-group="${NEG_NAMES[$i]}" \
--network-endpoint-group-region="${REGIONS[$i]}" \
--project="${PROJECT_ID}"
done
# Step 4: Create a URL Map
URL_MAP_NAME="my-url-map"
gcloud compute url-maps create "${URL_MAP_NAME}" \
--default-service "${BACKEND_SERVICE_NAME}" \
--project="${PROJECT_ID}"
# Step 5: Setup HTTP(S) Load Balancing (Choose one)
# Option A: Setup for HTTP
IP_NAME="my-global-ip"
HTTP_PROXY_NAME="my-http-proxy"
FORWARDING_RULE_NAME="my-http-forwarding-rule"
# Reserve a Global IP Address
gcloud compute addresses create "${IP_NAME}" --global --project="${PROJECT_ID}"
# Create an HTTP Proxy and Forwarding Rule
gcloud compute target-http-proxies create "${HTTP_PROXY_NAME}" \
--url-map="${URL_MAP_NAME}" \
--project="${PROJECT_ID}"
gcloud compute forwarding-rules create "${FORWARDING_RULE_NAME}" \
--global \
--target-http-proxy="${HTTP_PROXY_NAME}" \
--ports=80 \
--address="${IP_NAME}" \
--project="${PROJECT_ID}"
# Option B: Setup for HTTPS (Uncomment and configure if using HTTPS)
# SSL_CERT_NAME="my-ssl-cert"
# HTTPS_PROXY_NAME="my-https-proxy"
# FORWARDING_RULE_NAME="my-https-forwarding-rule"
#
# # Create an SSL Certificate (Replace with your certificate details)
# gcloud compute ssl-certificates create "${SSL_CERT_NAME}" \
# --certificate="[CERTIFICATE_FILE]" \
# --private-key="[PRIVATE_KEY_FILE]" \
# --project="${PROJECT_ID}"
#
# # Create an HTTPS Proxy and Forwarding Rule
# gcloud compute target-https-proxies create "${HTTPS_PROXY_NAME}" \
# --url-map="${URL_MAP_NAME}" \
# --ssl-certificates="${SSL_CERT_NAME}" \
# --project="${PROJECT_ID}"
#
# gcloud compute forwarding-rules create "${FORWARDING_RULE_NAME}" \
# --global \
# --target-https-proxy="${HTTPS_PROXY_NAME}" \
# --ports=443 \
# --address="${IP_NAME}" \
# --project="${PROJECT_ID}"
# Step 6: Test Your Setup
# After setup, test by accessing the global IP or configuring a DNS A record pointing to it. Ensure traffic is routed to the closest region and fail-over works.
# Remember to replace placeholders with your actual project and service details. This script is a guideline; adjustments may be required based on your specific setup and requirements.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment