Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



Cross-Region Load Balancing

Limited Preview

This is a Limited Preview release of HTTP load balancing. As a result, it might be changed in backward-incompatible ways and is not recommended for production use. It is not subject to any SLA or deprecation policy. Request to be whitelisted to use this feature .


This guide demonstrates configuring Compute Engine instances in different regions and using HTTP load balancing to distribute traffic across the regions.

Scenario

You need to load balance traffic for site www.example.com . You want to ensure that requests are routed to the closest region; however, you also want to ensure that in the event of a failure, or of instances in a region reaching capacity, that the requests can be sent to a healthy instance in the next closest region.

When you finish configuring this scenario, your load balanced services will have a single global IP address that you can use in your DNS records. This single entry to your services means that DNS-based load balancing is not required. This avoids problems with stale geo mapping data.

In this scenario, you will configure a load balancing configuration as shown in the following graphic:

Cross-region load balancing: API objects

Configure Google Compute Engine

For this scenario, you will create three Compute Engine instances in different regions and zones. On each instance, you will create a startup script that you will pass to the instances create subcommand of gcloud compute that installs Apache on each instance when the instances start. You'll also add a firewall rule that allows HTTP traffic to reach the instances.

The commands below are all run on your local system and assume a bash command prompt.

If you haven't run the gcloud command previously, first run gcloud auth login to authenticate and configure your default project.

  1. Create some startup scripts for your new instances. Depending on your operating system, your startup script contents might differ:

    Debian

    $ echo "apt-get update && apt-get install -y apache2 && hostname > /var/www/index.html" > \
        $HOME/lb_startup.sh
    

    CentOS

    $ echo "yum -y install httpd && service httpd restart && echo \"\`/bin/hostname\`\" > /var/www/html/index.html" > \
        $HOME/lb_startup.sh
    
  2. Create a tag for your future virtual machines, so you can apply a firewall to them later:

    $ TAG="www-tag"
    
  3. Create four instances in different regions:

    $ gcloud compute instances create www-1 www-2 \
        --zone us-central1-b --tags $TAG \
        --metadata-from-file startup-script=$HOME/lb_startup.sh
    
    $ gcloud compute instances create www-3 www-4 \
        --zone europe-west1-b --tags $TAG \
        --metadata-from-file startup-script=$HOME/lb_startup.sh
    
  4. Create a firewall rule to allow external traffic to this virtual machine instances:

    $ gcloud compute firewalls create www-firewall \
        --target-tags $TAG --allow tcp:80
    

Now that your virtual machine instances are prepared, you can start setting up your load balancing configuration. You can verify that your instances are running by sending a curl request to each instance's external IP address:

$ curl IP_ADDRESS

To get the IP addresses of your instances:

$ gcloud compute instances describe INSTANCE

Configure the load balancing service

Next, you will set up the load balancing service.

The following commands use the gcloud preview component. To install, run gcloud components update preview .

  1. Create a resource view for each of your regions.

    $ gcloud preview resource-views create us-resources --zone us-central1-b
    $ gcloud preview resource-views resources --resourceview us-resources \
        addinstance www-1 www-2 --zone us-central1-b
    
    $ gcloud preview resource-views create europe-resources --zone europe-west1-b
    $ gcloud preview resource-views resources --resourceview europe-resources \
        addinstance www-3 www-4 --zone europe-west1-b
    
  2. Retrieve the full URIs for these new resource views:

    $ gcloud preview resource-views list --zone us-central1-b
    $ gcloud preview resource-views list --zone europe-west1-b
    

    The URIs look similar to:

    https://www.googleapis.com/resourceviews/v1beta1/projects/<project-id>/zones/us-central1-b/resourceViews/us-resources
    https://www.googleapis.com/resourceviews/v1beta1/projects/<project-id>/zones/europe-west1-b/resourceViews/europe-resources
    

    Copy the URIs; you'll need them in a later step.

  3. Add an HTTP health check object named basic-check .

    $ gcloud compute http-health-checks create basic-check
    

    This example uses the default settings for the health check mechanism, but you can also customize this on your own.

  4. Create a backend service object named web-service :

    $ gcloud compute backend-services create web-service \
        --http-health-check basic-check
    
  5. Edit web-service to update the backend values:

    $ gcloud compute backend-services edit web-service
    

    Your text editor is launched with the resource information. The health check specified during creation is listed, along with sample resources:

    healthChecks:
    - https://www.googleapis.com/compute/v1/projects/<project-id>/global/httpHealthChecks/basic-check
    port: 80
    timeoutSec: 30
    
    # Example resource:
    # -----------------
    <snip>
    

    Uncomment the following lines and replace the group value with the resource view URIs you set aside earlier (to retrieve them again, run gcloud preview resource-views list --zone ZONE ):

    backends:
    - balancingMode: RATE
      group: https://www.googleapis.com/resourceviews/v1beta1/projects/<project-id>/zones/us-central1-b/resourceViews/us-resources
      maxRate: 100
    - balancingMode: RATE
      group: https://www.googleapis.com/resourceviews/v1beta1/projects/<project-id>/zones/europe-west1-b/resourceViews/europe-resources
      maxRate: 150
    

    The file format treats indentation strictly; make sure that the backends object is indented identically to the existing healthChecks object.

    Save and exit your text editor.

  6. Map all of your incoming URLs to your backend service by creating a URL map and setting the --default-service flag:

    $ gcloud compute url-maps create web-map --default-service web-service
    

    In this scenario you are not dividing your traffic to different backends based on the URL. If you want to divide your traffic by URL, see content- based routing .

  7. Create a target HTTP proxy to route requests to your UrlMap object.

    $ gcloud compute target-http-proxies create web-proxy --url-map web-map
    
  8. Create a global forwarding rule to handle and route incoming requests.

    $ gcloud compute forwarding-rules create http-rule --global \
        --target-http-proxy web-proxy --port-range 80
    

    After creating the global forwarding rule, it can take several minutes for your configuration to propagate. You can check the progress of the configuration propagation using the gcloud compute backend-services get-health command.

Now that you have configured your load balancing service, you can start sending traffic to the forwarding rule and watch the traffic be dispersed to different instances.

Send traffic to your instances

To start sending traffic to your newly configured load balancing service, you need the global forwarding rule's external IP address, which was printed when you ran the gcloud compute forwarding-rules create command, or you can look up the IP address with gcloud compute forwarding-rules describe .

  1. Assign the external IP address to the IP variable:

    $ IP="<n.n.n.n>"
    
  2. Use the curl command to test the response for various URLs for your services and check that the responses print the instance name for the matching URL, for example:

    $ while true; do curl -m1 $IP; done
    

In this situation, curl will not place a heavy load on your load balancing so you should see responses from the region closest to you. To simulate a user in a different geography, you can use a web proxy to make the requests. You can also SSH to one of your virtual machine instances in different region and run the curl command from that location to see the request go to an instance in the region closest to that instance.

Simulate an outage

You can simulate an outage for one or more instances in a region so that you can see how the load will be balanced among the remaining healthy instances.

While the curl command from the previous step is still running, if you stop Apache on your instances, the health checks will fail and you will see responses to your curl command shift to the remaining healthy instances:

  1. Connect to an an instance:

    $ gcloud compute ssh www-1 --zone us-central1-b
    
  2. Stop Apache on that instance:

    Debian

    me@www-1:~$ service apache2 stop
    

    CentOS

    me@www-1:~$ service httpd stop
    
  3. Check the results of the curl command to view the result of simulating an outage. Repeat these steps for additional instances as desired.

Next steps

Now that you are familiar with HTTP load balancing, you can build your own configuration to match your services and your requirements. The first step in that direction might be to point your domain to your global load balanced IP address with DNS. For a high performance, global DNS solution built on Google's infrastructure, see Google Cloud DNS . You might also investigate using content-based load balancing and URL-based routing rules, which can be combined with regional HTTP load balancing.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.