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.

See Setting Up Network Load Balancing for an explanation of network load balancing.

Prerequisites

Install the gcloud command-line tool, as described on the Prerequisites page.

You must create a static external IP address for the load balancer. If you are using an image provided by Compute Engine, your instances are automatically configured to handle this IP address. If you are using any other image, you will have to configure this address as an alias on eth0 or as a loopback on each instance.

If you haven't run the gcloud command-line tool previously, first run gcloud init to authenticate.

This guide assumes that you are familiar with bash.

Configure Compute Engine

For this load balancing scenario, you will create three Compute Engine instances and install Apache on them. 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 three new virtual machines in a given zone and give them all the same tag. This example sets the zone to us-central1-b. Setting the tags field allows you to reference these instances all at once, such as with a firewall rule. These commands also install apache on each instance and gives each instance a unique home page.

    gcloud compute instances create www1 \
        --image-family debian-8 \
        --image-project debian-cloud \
        --zone us-central1-b \
        --tags network-lb-tag \
        --metadata startup-script="#! /bin/bash
          sudo apt-get update
          sudo apt-get install apache2 -y
          sudo service apache2 restart
          echo '<!doctype html><html><body><h1>www1</h1></body></html>' | tee /var/www/html/index.html
          EOF"
    
    gcloud compute instances create www2 \
        --image-family debian-8 \
        --image-project debian-cloud \
        --zone us-central1-b \
        --tags network-lb-tag \
        --metadata startup-script="#! /bin/bash
          sudo apt-get update
          sudo apt-get install apache2 -y
          sudo service apache2 restart
          echo '<!doctype html><html><body><h1>www2</h1></body></html>' | tee /var/www/html/index.html
          EOF"
    
    gcloud compute instances create www3 \
        --image-family debian-8 \
        --image-project debian-cloud \
        --zone us-central1-b \
        --tags network-lb-tag \
        --metadata startup-script="#! /bin/bash
          sudo apt-get update
          sudo apt-get install apache2 -y
          sudo service apache2 restart
          echo '<!doctype html><html><body><h1>www3</h1></body></html>' | tee /var/www/html/index.html
          EOF"
    
  2. Create a firewall rule to allow external traffic to these virtual machine instances.

    gcloud compute firewall-rules create www-firewall-network-lb \
        --target-tags network-lb-tag --allow tcp:80
    
  3. Get the external IP addresses of your instances.

    gcloud compute instances list
    
  4. Verify that each instance running.

    $ curl http://[EXTERNAL_IP_ADDRESS]
    

Configure the load balancing service

Next, you will set up the load balancing service.

  1. Create a static external IP address for your load balancer.

    gcloud compute addresses create network-lb-ip-1 \
        --region us-central1
    

    Your virtual machine instances will receive packets that are destined for this IP address. If you are using an image provided by Compute Engine, your instances are automatically configured to handle this IP address. If you are using any other image, you will have to configure this address as an alias on eth0 or as a loopback on each instance.

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

  3. Add a target pool in the same region as your virtual machine instances. Use the health check created in the prior step for this target pool. Target pools require a health check service in order to function.

    gcloud compute target-pools create www-pool \
        --region us-central1 --http-health-check basic-check
    
  4. Add your instances to the target pool

    gcloud compute target-pools add-instances www-pool \
        --instances www1,www2,www3 \
        --instances-zone us-central1-b
    

    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-f and instances in zone us-central1-b in one target pool because they are in the same region, us-central1.

  5. Add a forwarding rule serving on behalf of an external IP and port range that points to your target pool. For the --address field, use either the numeric IP address or its fully qualified name.

    gcloud compute forwarding-rules create www-rule \
        --region us-central1 \
        --ports 80 \
        --address network-lb-ip-1 \
        --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

  1. Look up the forwarding rule's external IP address:

    gcloud compute forwarding-rules describe www-rule --region us-central1
    
  2. 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 might 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_ADDRESS]; done

Send feedback about...

Compute Engine Documentation