Adding a Cloud Storage bucket to content-based load balancing

Using a Cloud Storage bucket as a load balancer backend

This guide shows how to modify the content-based load balancing configuration to route requests for static content to a Cloud Storage bucket. Specifically, requests to URL paths that begin with /static will be sent to your storage bucket, and all other requests will be sent to your virtual machine instances.

Before you begin

  1. Complete the Before you begin portion of the Cloud Storage Quickstart: Using the gsutil Tool. This installs the Cloud SDK, which includes the gcloud and gsutil command-line tools you need.
  2. Set a default project.

    gcloud config set project [PROJECT_ID]
    

    or

    gsutil config set project [PROJECT_ID]
    

    • [PROJECT_ID] - the project you will use for this guide
  3. Complete the guide for content-based load balancing. The material here builds on the resources you'll build using that guide.

Configuring a Cloud Storage bucket and the load balancing service

The following instructions assume you are uploading and setting permissions for a single object. If you would like to use wildcards (globs) to upload and set permissions for multiple objects, see Wildcard Names.

Configuring a bucket

Create a bucket and upload an object to it. Bucket names have to be globally unique. In this example, we create a regional bucket in the same region as the instances in the content-based load balancing example.

Console


Create a bucket

  • Open the Cloud Storage browser in the Google Cloud Platform Console.
    Open the Cloud Storage browser
  • Click CREATE BUCKET.
  • Enter a unique Name for your bucket.
    • Do not include sensitive information in the bucket name, because the bucket namespace is global and publicly visible.
  • Choose Multi-Regional for Storage class.
  • Choose United States for Location.
  • Click Create.
  • Create the static folder

    1. While in the bucket, click Create folder.
    2. Enter a folder name of static.
    3. Click Create.

    Upload an object from your local directory.

    1. While in the folder, click Upload files.
    2. Browse to the object in your file system, then select it.
    3. Click Open.

    Make the object publicly readable so it can be served through load balancing.

    1. Select the checkbox next to the object.
    2. Click Share publicly.

    gsutil


    1. Create a bucket.

    2. Open a terminal window.
    3. Use the gsutil mb command and a unique name to create a bucket:
      gsutil mb gs://my-awesome-bucket/
      

      This uses a bucket named "my-awesome-bucket." You must choose your own, globally-unique, bucket name.

      If successful, the command returns:

      Creating gs://my-awesome-bucket/...
      
    4. You've just created a bucket where you can store your stuff!

    5. Upload an object from your local directory. If the file is in another directory, provide the complete path.

      gsutil cp [OBJECT_NAME] gs://[EXAMPLE_BUCKET]/static/[OBJECT_NAME]
      

      • [EXAMPLE_BUCKET] - the bucket you already created
      • [OBJECT_NAME] - the filename of an object to upload
    6. Make the object publicly readable so it can be served through load balancing.

      gsutil acl ch -u AllUsers:R gs://[EXAMPLE_BUCKET]/static/[OBJECT_NAME]
      

      • [EXAMPLE_BUCKET] - the bucket you already created
      • [OBJECT_NAME] - the filename of the object you uploaded

    Creating a backend bucket for your Cloud Storage bucket

    This procedure assumes you have completed the content-based load balancing example.

    Instead of creating a backend service for static content like you did for video content, create a new backend bucket that points to the Cloud Storage bucket you created above.

    Console


    1. Go to the Load balancing page in the Google Cloud Platform Console.
      Go to the Load balancing page
    2. Click Edit in the web-map row.
    3. Click the right arrow next to Backend configuration.
    4. Click Create or select a backend bucket, then select Create a backend bucket.
    5. Enter a Name of static-bucket.
    6. Next to Cloud storage bucket, select Browse.
    7. Find and select the bucket you created before.
    8. Click Select. Do not click Update yet.

    gcloud


    gcloud compute backend-buckets create static-bucket \
        --gcs-bucket-name [EXAMPLE_BUCKET]
    

    • [EXAMPLE_BUCKET] - the bucket you created

    Modifying the web-map URL map

    Add a rule to your existing web-map URL map that maps /static/* to the backend bucket created above. This routes all requests that have /static/* to your Cloud Storage bucket.

    Console


    1. Click the right arrow next to Host and path rules.
    2. In the third row, enter * in the hosts field.
    3. Enter /static/* in the paths field.
    4. Enter static-bucket in the Backends field.
    5. Click Update.

    gcloud


    gcloud compute url-maps add-path-matcher web-map \
        --default-service web-map-backend-service \
        --path-matcher-name bucket-matcher \
        --backend-bucket-path-rules=/static/*=static-bucket
    

    Fetching resources from your Cloud Storage bucket

    Console


    1. Go to the Addresses page in the Google Cloud Platform Console.
      Go to the Addresses page
    2. Find the row for lb-ip-1 and make a note of the IP address in the *External address field.
    3. Enter http://[IP_ADDRESS]/static/[OBJECT_NAME] or https://[IP_ADDRESS]/static/[OBJECT_NAME] in your browser, depending on which protocol your load balancer uses, and press Enter.

    gcloud


    1. Find the IP address of your global forwarding rule.

      gcloud compute forwarding-rules list
      

    2. The address you want is the address of the rule you created when you ran through the content-based load balancing example. Test that your Cloud Storage bucket is receiving traffic using curl or by visiting the URL in your browser.

      curl http://[IP_ADDRESS]/static/[OBJECT_NAME]
      

      or

      curl -k https://[IP_ADDRESS]/static/[OBJECT_NAME]
      

      • [IP_ADDRESS] - the IP address of your global forwarding rule
      • [OBJECT_NAME] - use the name of the example object you uploaded

    This should respond with [OBJECT_NAME] from your [EXAMPLE_BUCKET] Cloud Storage bucket.

    If you store an object in the bucket with name of static/path/to/[OBJECT_NAME], you can retrieve it with a URL of http://[IP_ADDRESS]/static/path/to/[OBJECT_NAME].

    What's next

    Monitor your resources on the go

    Get the Google Cloud Console app to help you manage your projects.

    Send feedback about...

    Compute Engine Documentation