What you need

To complete this lab, you need:

Internet access

Access to a supported Internet browser:

What you do

What you learn

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.

Step 1 Create a Project

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.

Step 1 Create a bucket to host the startup script

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.

Property

Value

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:________________________________________

Step 2 Create the startup script in Cloudshell

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]

Step 3 Upload the startup script to the bucket

gsutil cp mystartupscript gs://[Bucket-name]/

Step 4 Verify that the script is in the bucket

Console: Products and Services > Storage > Browser

Click on the bucket name. The file mystartupscript should be in the bucket.

Step 1 Create the first VM

Console: Products and Services > Compute Engine > VM instances

Click on [Create instance]

Property

Value

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.

Step 2 Verify web server

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

Step 3 Create the second and third VMs

Using Step 1 as a guide, create two more webservers. The only difference will be the names and the second metadata value.

webserver1

Property

Value

Name:

webserver2

Metadata:

Key = my-server-id
Value = WebServer-2

webserver2

Property

Value

Name:

webserver3

Metadata:

Key = my-server-id
Value = WebServer-3

Step 4 Verify that all the web servers are functioning

Browse to the external IP for webserver2 and webserver3. Verify that the unique name are being displayed on the home page.

Step 1 Add tags to identify the VMs for Network Load Balancing

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.

Step 2 Reserve an External Static IP for use with the load balancer

Console: Products and Services > Networking > External IP addresses

Click on [Reserve static address]

Property

Value

Name:

network-lb-ip

Description:

IP address to be used with network load balancer

Type:

regional

Region

us-central1

Attached to:

None

Click [Reserve]

Step 3 Create a health-check

Console: Products and Services > Compute Engine > Health checks

Click on [Create a health check]

Property

Value

Name:

Webserver-health

Protocol:

HTTP

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:

Step 4 Create the target pool and and connect it to the health check

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

Step 5 Add the VMs (webservers) that into the target pool

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.

Step 6 Create the forwarding rule

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

Step 7 Verify the load balancing configuration in console

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.

Step 1 Send continuous traffic to the 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.

Step 2 Force a server outage

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.

Step 3 View the load balancer status in console

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.

Step 4 End the test

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.

Step 1 Delete the Forwarding rule

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.

Step 2 Release the Static IP

Go to the External Static IP.

Console: Products and Services > Networking > External IP addresses

Click on [Release Static Address]

Step 3 Delete the load balancer back-end target pool

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.

Step 4 Verify or manually delete the health check

Console: Products and Services > Compute Engine > Health checks

Delete the health check webserver-health if it still exists.

Step 5 Verify that the load balancer is deleted

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.

Step 6 Delete the VMs

Console: Products and Services > Compute Engine > VM instances

Delete the remaining servers.

Step 7 Delete the script bucket

Console: Products and Services > Storage > Browser

Delete the bucket.

┬ęGoogle, Inc. or its affiliates. All rights reserved. Do not distribute.