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)



Network Load Balancing

This guide demonstrates a basic load balancing scenario where you have multiple web servers on Compute Engine instances that you want to balance the traffic across. This scenario sets up a layer 3 load balancing configuration to distribute HTTP traffic across healthy instances. Basic HTTP health checks will be configured to ensure that traffic is only sent to healthy instances.

Prerequisites

Install the gcloud command line interface, as described on the Load Balancing Overview page.

This guide assumes that you are familiar with bash .

Configure Google Compute Engine

For this load balancing scenario, you will create three Compute Engine instances with Apache installed. You will create a startup script that you will pass to the gcloud compute instances create command that will install Apache on each instance when the instances start. You will 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.

  1. Create some startup scripts for your new instances.

    Depending on your operating system, your startup script contents might differ:

    • If you are planning to using Debian on your Compute Engine instances, run the following command:

      $ echo "apt-get update && apt-get install -y apache2 && hostname > /var/www/index.html" > \
          $HOME/lb_startup.sh
      
    • If you are planning on use CentOS on your Compute Engine instances, run the following command:

      $ echo "yum -y install httpd && service httpd restart && 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. Choose a zone and a region for your virtual machines:

    $ ZONE="us-central1-b"
    $ REGION="us-central1"
    
  4. Create three new virtual machines:

    $ gcloud compute instances create www1 www2 www3 --zone $ZONE \
        --tags $TAG --metadata-from-file startup-script=$HOME/lb_startup.sh
    
  5. Create a firewall rule to allow external traffic to this virtual machine instances:

    $ gcloud compute firewalls create www-firewall \
        --target-tags $TAG --allowed 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 of the instance's external IP address:

$ curl IP_ADDRESS

To get the IP addresses of your instances, use gcloud compute instances get .

Configure the load balancing service

Next, you will set up the load balancing service.

  1. Add an HTTP health check object.

    $ 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.

  2. Add a target pool in the same region as your virtual machine instances.

    The region of your virtual machine instances is us-central1 . You are also going to use your newly-created health check object for this target pool.

    $ gcloud compute target-pools create www-pool \
        --region $REGION --health-check basic-check
    

    Add your instances to the target pool:

    $ gcloud compute target-pools add-instances www-pool \
        --instances $ZONE/instances/www1,$ZONE/instances/www2,$ZONE/instances/www3 \
        --region $REGION --zone $ZONE
    

    Instances within a target pool must belong to the same region but can be spread out across different zones in the same region. For example, you can have instances in zone us-central1-a and instances in zone us-central1-b in one target pool because they are in the same region, us-central1 .

  3. Add a forwarding rule serving on behalf of an external IP and port range that points to your target pool.

    You can choose to use a reserved static IP address or an ephemeral IP address assigned by Google Compute Engine for your forwarding rules. For this example, you are going to use an ephemeral IP address by leaving out the --ip flag in the command:

    $ gcloud compute forwarding-rules create www-rule \
        --region $REGION --port-range 80 --target-pool www-pool
    

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 forwarding rule, you need to look up the forwarding rule's external IP address:

$ gcloud compute forwarding-rules describe FORWARDING_RULE

The forwarding rule's IP address is the value of the IPAddress key in the command's output. Assign the external IP address for the www-rule forwarding rule to the IP alias:

$ IP="<n.n.n.n>"

Next, use the curl command to access the IP address. The response will alternate randomly among the three instances. If your response is initially unsuccessful, you may need to wait ~30 seconds for the configuration to be fully loaded and for your instances to be marked healthy before trying again:

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

Authentication required

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

Signing you in...

Google Developers needs your permission to do that.