Hands on with Load Balancing on Google Compute Engine


Google Compute Engine recently
announced support for load balancing, allowing you to
easily distribute workloads and traffic evenly across two
or more virtual machines. I’m now going to show you the
process of setting up load balancing across two
web servers. Here’s the Google Cloud Console,
where you can see that I’ve already created
two virtual machines. Both VMs already have Apache web
server installed, and I’ve updated the default web page on
both servers to display the name of the server. I’ve also updated the background
color of each default web page to match the
name of the server, so we can quickly tell which server’s page
we’re looking at, to test that everything is working
as expected. The other setup step that’s
already been done is to create a firewall to allow internet
requests on port 80. We can see that by going to
Networks and selecting our default network. And here, under the Firewall
section, you can see that I’ve already created a firewall named
http1 and that it’s set up to allow traffic
on port 80. Now let’s take a look at the
new load balancing feature. There are three sections
associated with the feature, target pools, forwarding rules,
and health checks. We’re about to create a
load-balanced target pool of multiple servers, so we need to
establish a way to confirm that each machine in the pool
is working as expected. We can do that by creating a
health check under the Health Check section. We’ll click the New Health
Check button to create a health check. For the name, we’ll enter
basic-health-check. For the time out, we’ll
specify five seconds. And for the Unhealthy if value,
we’ll specify one consecutive failure, and then
click Create to create the health check. And that’s done. Now let’s create a target pool
of VM instances that uses the health check we just created. Start the target pool creation
process by clicking the New Target Pool button. We’ll name this pool
us-central1-pool. For the region, we’ll select the
us-central1 region, which is where our VM instances
reside. Next, we’ll add our VM instances
to the pool– first the red-server, then
the blue-server. Next, we’ll select the health
check that we just created and then click Create to create
our target pool. And it’s done. One thing to note is that you
can edit these target pools on the fly after they
are created. So to add more servers to a
given pool, all you have to do is create more VM instances,
and then Edit An Existing Target Pool to add
the servers. Removing servers is
done the same way. And then click Save to
save your changes. Now that we’ve got our target
pool created, the last thing we need to do is to create a
forwarding rule that will make our target pool accessible
at a single IP address. Click the New Forwarding Rule
button to create a new rule. We’ll name this rule
us-central1-web. For the region, we’ll specify
us-central1, which is where we created our target pool. For the external IP, we have the
option to create a static IP address, but we’ll go with
the default option here, which is Ephemeral. For the protocol, we’ll specify
TCP, which is also the default option. And here is where we can specify
a specific port or range of ports to associate
with the rule. We’ll specify port 80, the
default port for HTTP-based web traffic. And here’s where we can
select the target pool we just created. And finally, we’ll click Create
to create the rule. And it’s done. We’ve now got a load-balanced
cluster of two web servers. Let’s test it out right now. The only things we need to test
it out are a browser and the IP address of the
forwarding rule that we just created. So let’s copy this IP address,
and paste it into a new tab, and hit Enter. And there’s the custom default
page for the red-server. Now when I hit Refresh, let’s
see if we get a response from the other web server in our
load-balanced target pool. And there’s the custom default
web page for the blue-server. Great! The serving load is being
balanced between the two servers in our target pool. Let’s try it a few more times to
see that the load is being distributed on a round-robin
basis. Now let’s stop one of the
servers to confirm that the health check for our load
balancing pool properly handles the situation and
redirects requests to the other server that’s
still available. We’ll go back to the Cloud
Console and select VM Instances, and click on the
red-server, and then scroll down to the SSH link. We’ll copy that SSH command and
jump over to a terminal window to run it. We’re now logged on
to the instance. Let’s stop the web server. Here’s the command we’ll
run to stop Apache. We’ll start by typing sudo to
run the command as superuser, then the location of the Apache
service, followed by the stop command. Let’s run that. And the Apache web service on
this instance is now stopped. Let’s go back to our target pool
and see how it responds to this change. And here we can see that the
health check for the red server is showing that
it’s unhealthy. Our target pool now only
contains one healthy VM instance, blue-server. So if we go to the browser where
we were previously sent the default page of the
red-server, we should be able to reload the page and be
redirected to the blue-server without skipping a beat. Let’s do that now. Here’s the browser window
where we were previously forwarded to the red-server. When I click Reload, yes! We just got forwarded to the
blue-server, which is the only healthy VM that’s still
available in our load balancing target pool. Let’s try it a few more times. You can see that we’re
consistently being routed to the healthy server in
our target pool. Excellent! I’ve shown you a very simple
example of using load balancing on Compute Engine. In addition to using the Cloud
Console to set up load balancing, we also offer a
command-line tool and RESTful APIs, so that you can integrate
load balancing directly into your code. Load balancing on Compute Engine
is highly scalable, so you can confidently create
multiple target pools, forwarding rules, and
health checks. Best of all, our load balancing
requires no manual pre-warming, even when
you anticipate significant traffic spikes. As you’ve just seen, it’s simple
to set up a cluster of load-balanced web servers on
Compute Engine, and it’s easy to customize the setup
to meet your needs. Thank you for watching.

3 Comments

Add a Comment

Your email address will not be published. Required fields are marked *