Adding Cloud Storage buckets to load balancers

Using Cloud Storage buckets as load balancer backends

This document shows you how to modify the example HTTP(S) load balancer configuration to route requests for static content to Cloud Storage buckets. After you configure a load balancer with Cloud Storage buckets, requests to URL paths that begin with /static are sent to the storage buckets, and all other requests are sent to virtual machine instances. This document also shows how to create a configuration that routes traffic between two backend buckets, supporting the US and EU regions.

Before you begin

  1. If you are using the gcloud or gsutil utilities, you can install them both by referencing the Quickstart: Using the gsutil Tool document.
  2. Set a default project.

    Console


    a. Go to the Home page in the Google Cloud Platform Console.
    Go to the Google Cloud Platform home page

    b. To the right of Google Cloud Platform, select a project from the pull-down menu.

    gcloud or gsutil


     gcloud config set project [PROJECT_ID]
    

    or

     gsutil config set project [PROJECT_ID]
    
    • [PROJECT_ID] - the project you will use for this guide.
  1. Complete the guide for content-based and cross-regional load balancing. The instructions in this document require the resources you'll build using that guide.

Configuring Cloud Storage buckets and the load balancing service

The following instructions assume that you are uploading a single object to each Cloud Storage bucket and then setting permissions for the object.

To make all objects in the Cloud Storage buckets public, use the instructions in Making groups of objects publicly readable.

To use wildcards (globs) to upload and set permissions for multiple objects, refer to Wildcard Names. For more information on uploading objects, see Uploading Objects.

Configuring the Cloud Storage buckets

Create two Cloud Storage buckets and upload an object to each of them. Bucket names must be globally unique. This example creates two multi-regional buckets, in the same regions as the instances in the HTTP(S) load balancing example.

Console


Create a Cloud Storage bucket in the United States.

  • 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 Standard for Storage class.
  • Choose United States for Location.
  • Click Create.
  • Repeat the steps to create a second bucket, this time selecting European Union under Location.

    Enter the us bucket.

    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 that it can be served through load balancing.

    1. Click the menu icon associated with the object that you want to make public.

      The menu icon is three vertical dots to the far right of the object's row.

    2. Select Edit permissions from the menu.

    3. Click Add item.

    4. Add a permission for allUsers.

      • Select User for the Entity.
      • Enter allUsers for the Name.
      • Select Reader for the Access.
    5. Click Save.

    After it is shared publicly, a link icon appears in the public access column. You can click this icon to get the URL for the object.

    Repeat the steps, this time in the eu bucket.

    gsutil


    1. Create a Cloud Storage 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!

    Repeat the steps, this time running gsutil mb -l eu gs://my-awesome-bucket/ to specify the location as eu.

    Enter the us bucket.

    1. 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
    2. 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

    Repeat the steps, this time in the eu bucket.

    Creating backend buckets for your Cloud Storage buckets

    This procedure assumes that you have completed the HTTP(S) load balancing example.

    Instead of creating a backend service for static content as you did for video content, create two new backend buckets that point to the Cloud Storage buckets 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-us.
    6. Next to Cloud storage bucket, select Browse.
    7. Find and select the us bucket you created before.
    8. Click Select.
    9. Click Create to create the backend bucket.
    10. Do not click Update for the load balancer yet.

    Repeat the steps to create a second backend bucket, this time entering static-bucket-eu in the Name field, and selecting the eu bucket you created before.

    gcloud


    gcloud compute backend-buckets create static-bucket \
        --gcs-bucket-name [EXAMPLE_BUCKET]
    
    • [EXAMPLE_BUCKET] - the us bucket you created

    Repeat the command, this time replacing EXAMPLE_BUCKET with the eu 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/us/* in the Paths field.
    4. Enter static-bucket-us in the Backends field.
    5. Click Update for the load balancer.

    Repeat the steps, entering /static/eu/* in the Paths field and static-bucket-eu in the Backends field.

    gcloud


    Add a path rule for the US:

    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/us/*=static-bucket-us"
    

    Add a path rule for the EU:

    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/eu/*=static-bucket-eu"

    Fetching resources from your Cloud Storage buckets

    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/[REGION]/[OBJECT_NAME] or https://[IP_ADDRESS]/static/[REGION]/[OBJECT_NAME] in your browser, depending on which protocol your load balancer uses, and press Enter. Try inputting both us and eu as [REGION].

    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 HTTP(S) 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/[REGION]/[OBJECT_NAME]
      

      or

      curl -k https://[IP_ADDRESS]/static/[REGION]/[OBJECT_NAME]
      
      • [IP_ADDRESS] - the IP address of your global forwarding rule
      • [OBJECT_NAME] - use the name of the example object you uploaded
      • [REGION] - either us or eu. Try using each one.

    This command responds with [OBJECT_NAME] from your [EXAMPLE_BUCKET] Cloud Storage bucket.

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

    What's next

    หน้านี้มีประโยชน์ไหม โปรดแสดงความคิดเห็น