Puntos de control
Create instance template and open firewall on port 80
/ 5
Create the instance group and set it to auto-scale based on the CPU
/ 5
Setup internal load balancer
/ 5
Create a public facing web server
/ 5
Internal Load Balancer
GSP041
Overview
Google Cloud's Internal Load Balancer lets you load balance TCP/UDP traffic without exposing your VM's public IP to the Internet.
This example uses a distributed architecture; a Web Tier service connects to an Internal Tier which is distributed across several machines:
In this lab, you create a public-facing web server to serve the result of several "complex" calculations, in this case, calculating prime numbers. The prime numbers are each calculated by a micro service that is run on a managed instance group and scaled automatically.
What you'll do
- Learn about the components of an Internal Load Balancer
- Create a managed instance group of backends
- Point an internal load balancer to the backends
- Test the internal load balancer, and call it from a public facing web server
Prerequisites
- Basic knowledge of Compute Engine (for example from the Compute the Cosmos Codelab)
- Basic networking and TCP/IP knowledge
- Basic Unix/Linux command line knowledge
- Some knowledge about VPCs is helpful, for example from the other labs in this series
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud console
-
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
- The Open Google Cloud console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account. -
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}} You can also find the Username in the Lab Details panel.
-
Click Next.
-
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}} You can also find the Password in the Lab Details panel.
-
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
- Click Activate Cloud Shell at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
- Click Authorize.
Output:
- (Optional) You can list the project ID with this command:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Set the region and zone
- Set the project region and zone for this lab:
- Create a variable for region:
- Create a variable for zone:
Learn more from the Regions & Zones documentation.
gcloud
on your own machine, the config settings are persisted across sessions. But in Cloud Shell, you need to set this for every new session or reconnection.
Task 1. Create a virtual environment
Python virtual environments are used to isolate package installation from the system.
- Install the
virtualenv
environment:
- Build the virtual environment:
- Activate the virtual environment.
Task 2. Create Backend Managed Instance Group
Use gcloud
in Cloud Shell to set up the Managed Instance Group for calculating the prime numbers.
Create the startup script which includes a simple web server in Python that will return True or False depending on if a number is prime or not.
- Start by creating your
backend.sh
script in the home directory:
- Click the Open Editor by clicking the icon at the top of the Cloud Shell. If prompted click Open in a new window.
Launching the Code Editor moves Cloud Shell to a new window and opens the Code Editor. After a few seconds the workspace comes up.
-
Select the
backend.sh
file in the left pane. -
Now add the following script into the editor on the right:
-
Click File > Save.
-
Click Open Terminal on the toolbar of Cloud Shell. Enter the following command to create the instance template for a simple instance with no external IP address:
- Now open the firewall to port
80
:
Click Check my progress below to verify you're on track in this lab.
- Next create the instance group:
- And set it to auto scale based on the CPU:
- When this finishes running, go back to the Console tab. Navigate to the VM instance: Compute Engine > VM instances. You now see three backends:
The backends are now ready to serve traffic.
Click Check my progress below to verify you're on track in this lab.
Task 3. Setup internal load balancer
Now set up the Internal Load Balancer and link it with the instance group created earlier.
An internal load balancer consists of a:
- Forwarding rule which binds an internal IP address.
- Backend service (which is regional) linking to one or more backend instance groups (which are zonal).
- Health check attached to the backend service that determines which instances can receive new connections.
The following diagram shows how instances are load balanced using multiple instances in multiple backend groups in different zones.
You create a single backend service in this lab.
A health check is needed to balance between healthy instances only.
- Since the HTTP service is provided, see if a 200 response on a specific URL path (in this case /2 to check if 2 is prime) is populated:
- Create a backend service:
- Add the instance group:
- Now create the forwarding rule to finalize the load balancer setup with a static IP of
:
The service is now able to be queried via the internal address
Click Check my progress below to verify you're on track in this lab.
Task 4. Test the load balancer
Duration is 1 min
To test the load balancer, you need to create a new instance in the same network as the internal load balancer and SSH into it. That is because the cloud shell lives outside the project network you created.
- Using
gcloud
in Cloud Shell, create the new instance:
- Then SSH into it:
- Check if a few numbers are prime by querying the IP of the load balancer with the curl command:
The output does not come with a carriage return, so you see it straight in front of the next command line like this:
The service responded correctly: that 2 and 5 are prime numbers, but 4 is not.
- Leave the test instance:
- Then delete it:
- Type in Y to confirm the deletion.
Task 5. Create a public facing web server
Duration is 8 min
Create a public facing web server that returns a matrix based on a bunch of prime number calculations balanced via the internal load balancer.
You could make this an auto-scaling managed instance group load balanced by the external HTTP(S) load balancer, but for the sake of simplicity, create a single serving instance.
First, create the startup script for the front end.
- Start by creating your
frontend.sh
script in the home directory:
- You should still see the Code Editor open. But if not, launch the Code Editor by selecting it in the shell:
After a few seconds the workspace comes up.
- Now add this script into the editor on the right:
-
Click File > Save.
-
In Cloud Shell create an instance running this web server:
- Open the firewall for the front end:
-
In the Navigation menu, click Compute Engine > VM instances. Refresh your browser if you don't see the frontend instance.
-
Open the External IP for the frontend in your browser:
You should see a matrix like this, showing all prime numbers, up to 100, in green:
- Try adding a number to the path, like http://your-ip/10000, to see all prime numbers starting from that number.
Click Check my progress below to verify you're on track in this lab.
Congratulations!
You have created a frontend web server which runs 100 queries to a load balanced internal back end through a front end request.
Next steps / Learn more
- Work through the Google Cloud post " Using Google's cloud networking products: a guide to all the guides"
- Read the Compute Engine Networking Documentation
- Learn about Subnetworks
- Post questions and find answers on Stackoverflow under the google-compute-engine or google-cloud-platform tags.
Google Cloud training and certification
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated Date: February 10, 2024
Lab Last Tested Date: July 13, 2023
Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.