Skip to content

Instantly share code, notes, and snippets.

@girorme
Created November 12, 2024 04:18
Show Gist options
  • Save girorme/016b425094f2ebf42b55e1e4655bb4e1 to your computer and use it in GitHub Desktop.
Save girorme/016b425094f2ebf42b55e1e4655bb4e1 to your computer and use it in GitHub Desktop.
GCP - Load balancing with https and urlmap (backend bucket)

1. Step: Create a Backend Bucket

  • Purpose: The backend bucket is where your static files (e.g., images, CSS, etc.) are stored.
  • Command:
    gcloud compute backend-buckets create origin-cdn-store \
        --gcs-bucket-name=your-gcs-bucket-name
  • What it does: This creates a backend bucket named origin-cdn-store, which is linked to a Google Cloud Storage (GCS) bucket where your content is hosted.

2. Step: Create the URL Map

  • Purpose: The URL map defines how traffic should be routed based on URL patterns or hostnames.
  • Command:
    gcloud compute url-maps create store-url-map-prd \
        --default-backend-bucket=origin-cdn-store
  • What it does: This creates a URL map called store-url-map-prd. Any traffic not explicitly matched by a specific rule will be routed to the origin-cdn-store backend bucket by default.

3. Step: Add Host and Path Rules to the URL Map

  • Purpose: To route traffic based on specific hostnames (like cdn.store.com.br) and paths (like /images/*).
  • Command:
    gcloud compute url-maps add-path-matcher store-url-map-prd \
        --default-backend-bucket=origin-cdn-store \
        --path-matcher-name=cdn-rules \
        --new-hosts=cdn.store.com.br \
        --path-rules="/=origin-cdn-store,/images/*=origin-cdn-store"
  • What it does:
    • Adds a path matcher named cdn-rules to the URL map.
    • Routes all traffic from the domain cdn.store.com.br to the backend bucket origin-cdn-store.
    • Specifies that both the root path (/) and any paths under /images/ should also be routed to origin-cdn-store.

4. Step: Verify the URL Map

  • Purpose: To ensure that your URL map was created correctly and routes are set up.
  • Command:
    gcloud compute url-maps describe store-url-map-prd
  • What it does: This shows the configuration of your URL map, including the host rules and path matchers you've set up.

5. Step: Create the HTTPS Proxy

  • Purpose: The proxy handles HTTPS traffic and links the URL map with your SSL certificate.
  • Command:
    gcloud compute target-https-proxies create store-https-proxy-prd \
        --url-map=store-url-map-prd \
        --ssl-certificates=store-prd-cert
  • What it does: This creates an HTTPS proxy (store-https-proxy-prd) that secures the connection using store-prd-cert and routes traffic according to the rules in the URL map (store-url-map-prd).

6. Step: Create a Forwarding Rule

  • Purpose: The forwarding rule tells Google Cloud where to send incoming traffic (to your proxy) and which IP address it should listen on.
  • Command:
    gcloud compute forwarding-rules create store-https-lb-forwarding-rule-prd \
        --address=store-prd-lb-cdn-ip \
        --global \
        --target-https-proxy=store-https-proxy-prd \
        --ports=443
  • What it does: This creates a forwarding rule that listens for HTTPS traffic on port 443 and routes it to your HTTPS proxy. The static IP store-prd-lb-cdn-ip is assigned to this rule, so traffic to your domain (cdn.store.com.br) is sent to the load balancer.

Summary of the Flow:

  1. Backend Bucket: Stores your content (origin-cdn-store).
  2. URL Map: Routes traffic to the backend bucket, using rules to match specific hostnames and paths.
  3. HTTPS Proxy: Ensures traffic is securely routed and handles SSL certificates.
  4. Forwarding Rule: Directs traffic to your proxy and assigns a static IP.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment