To complete this lab, you need:
Internet access
Access to a supported Internet browser:
In this lab you are going to exercise several features of Virtual Machines to create a pool of worker machines fed by a network load balancer. The pool provides distributed workload for scalable capacity and high availability, a common solution pattern. Along the way you will configure boot-time installation of software via a startup script and use metadata to both initiate the script and pass user-defined values to the script.
To make cleanup easier, create a new google cloud project.
Remember the project ID, a unique name across all Google Cloud projects. It will be referred to later in this codelab as PROJECT_ID
.
Console: Products and Services > Storage > Browser
Click on [Create Bucket]
Create a bucket. It must have a globally unique name.
You can use the Project ID. For example, if the Project ID is learngcp-154920
you could name the bucket learngcp-154920-scripts
.
|
|
Name: | <Project_ID>-scripts |
Zone: | [x] Multi-regional United States |
Note the bucket name. It will be used repeatedly later in this lab.
Bucket Name:________________________________________
Open Cloudshell:
Create the startup script with vi or nano:
vi mystartupscript
[i]
Copy and paste this script:
#! /bin/bash
VALUE_OF_MY_SERVER_ID=$(curl http://metadata.google.internal/computeMetadata/v1/instance/attributes/my-server-id -H "Metadata-Flavor: Google")
apt-get update
apt-get install -y apache2
cat <<EOF > /var/www/html/index.html
<html><body><h1>Virtual Machine Lab, Architecting GCP Infrastructure </h1>
<p>Server Name is: $VALUE_OF_MY_SERVER_ID</p>
</body></html>
EOF
[ESC][:][w][q]
gsutil cp mystartupscript gs://[Bucket-name]/
Console: Products and Services > Storage > Browser
Click on the bucket name. The file mystartupscript
should be in the bucket.
Console: Products and Services > Compute Engine > VM instances
Click on [Create instance]
|
|
Name: | webserver1 |
Zone: | us-central1-c |
Machine type: | 1 vCPU (n1-standard-1) |
Identity and API access
Access scopes:
Select set access for each API
Verify that Storage is set to Read only, to allow this VM to read from the Cloud Storage bucket during the boot process.
Firewall: Allow HTTP traffic
Metadata: Add the key:value pair
Key = startup-script-url
Value = gs://
[Bucket-name]
/mystartupscript
Click [+ Add Item]
Add another key:value pair, this time with the unique name of your webserver.
Key = my-server-id
Value = WebServer-1
Click [Create] to create the instance.
Verify that the startup script was successful by opening a web browser and browsing to the VM's external IP address.
Console: Products and Services > Compute Engine > VM instances
Click on [Create instance]
Locate the external IP and copy it into a browser window.
You should see the new index page with the name "WebServer-1".
Using Step 1 as a guide, create two more webservers. The only difference will be the names and the second metadata value.
webserver1
|
|
Name: | webserver2 |
Metadata: | Key = |
webserver2
|
|
Name: | webserver3 |
Metadata: | Key = |
Browse to the external IP for webserver2 and webserver3. Verify that the unique name are being displayed on the home page.
Console: Products and Services > Compute Engine > VM instances
Click on [Create instance]
Click on the name of the first VM to get to the details page.
Click [Edit] to make changes.
In the Tag section, add the tag "network-lb
"
(That is a lowercase "L" for load balancer)
Click [Save]:
Repeat this process to add the network-lb
tag to the other two VMs.
Console: Products and Services > Networking > External IP addresses
Click on [Reserve static address]
|
|
Name: |
|
Description: |
|
Type: | regional |
Region | us-central1 |
Attached to: | None |
Click [Reserve]
Console: Products and Services > Compute Engine > Health checks
Click on [Create a health check]
|
|
Name: |
|
Protocol: |
|
Port: | 80 |
Request path: | / |
Check interval: | 5 seconds |
Timeout | 5 seconds |
Healthy threshold: | 2 consecutive successes |
Unhealthy threshold: | 2 consecutive failures |
Click [Create]
Click the icon to open the Cloudshell terminal:
The target pool will be named webserver-pool
and will be connect to the health check you created in the previous step. Copy and paste the following command into Cloudshell.
gcloud compute target-pools create webserver-pool \
--region us-central1 --http-health-check webserver-health
Add the three VMs, webserver1
, webserver2
, and webserver3
into the target pool webserver-pool
. Copy and paste the following command into Cloudshell.
gcloud compute target-pools add-instances webserver-pool \
--instances webserver1,webserver2,webserver3 \
--instances-zone=us-central1-c
This completes the load balancer back-end configuration.
You will need the reserved static IP address. Enter the following command to view the address.
gcloud compute addresses list
Use this address to complete the configuration of the forwarding rule in Cloudshell:
gcloud compute forwarding-rules create webserver-rule \
--region us-central1 --ports 80 \
--address [STATIC_EXTERNAL_IP] --target-pool webserver-pool
Console: Products and Services > Networking > External IP addresses
You should now see that the static IP is now in use by a forwarding rule.
Console: Products and Services > Networking > Load balancing
You should now see the webserver-pool
load balancer.
In Cloudshell, locate the External Static IP address:
Use the following command to start sending repeated requests to the load balancer. You should see the server name changing in the results, proving that load balancing is working.
gcloud compute addresses list
Use this address to complete the configuration of the test in Cloudshell:
while true; do curl -m1 [STATIC_EXTERNAL_IP]; done
Leave the test running.
Navigate to the VMs and delete the first VM, webserver1.
Observe the results in Cloudshell.
Console: Products and Services > Compute Engine > VM instances
Select webserver1
and delete it.
In console, go to the load balancer and click on the line containing the load balancer to see details. You should see that webserver1 is listed, but marked as out of service.
Console: Products and Services > Networking > Load balancing
Click on the load balancer name webserver-pool
to see the details.
In Cloudshell use [CTRL][C]
to break out of the test script.
You will need to disassemble the load balancer front-end and back-end, verifying that all the parts have been deleted to avoid additional charges.
Go to the External Static IP. It cannot be deleted because it is still in use by a forwarding rule.
Console: Products and Services > Networking > External IP addresses
Click on the Forwarding rule link.
Delete the Forwarding rule.
Go to the External Static IP.
Console: Products and Services > Networking > External IP addresses
Click on [Release Static Address]
Console: Products and Services > Networking > Load balancing
Click on the "trash can" icon on the right side of the line to delete the target pool.
Check the "Health checks" so that the health check will be deleted at the same time.
Verify that the health check has been deleted. If you forget to check the Health checks on the dialog box, you can delete the webserver-health
health check manually.
Console: Products and Services > Compute Engine > Health checks
Delete the health check webserver-health
if it still exists.
The load balancer may not have completely deleted before.
Verify and delete if needed.
Console: Products and Services > Networking > Load balancing
Click on the "trash can" icon on the right side of the line to delete the target pool.
Console: Products and Services > Compute Engine > VM instances
Delete the remaining servers.
Console: Products and Services > Storage > Browser
Delete the bucket.
©Google, Inc. or its affiliates. All rights reserved. Do not distribute.