Sunday, November 21, 2021

Loadbalancer challenge lab

 

Your challenge

As soon as you sit down at your desk and open your new laptop, you receive several requests from the Nucleus team. Read through each description, and then create the resources.

Task 1: Create a project jumphost instance

You will use this instance to perform maintenance for the project.

Requirements:

  • Name the instance nucleus-jumphost-598 .
  • Use an f1-micro machine type.
  • Use the default image type (Debian Linux).

Click Check my progress to verify the objective.


gcloud compute instances create nucleus-jumphost-598 --machine-type=f1-micro



ask 2: Create a Kubernetes service cluster

The team is building an application that will use a service running on Kubernetes. You need to:

  • Create a cluster (in the us-east1-b zone) to host the service.
  • Use the Docker container hello-app (gcr.io/google-samples/hello-app:2.0) as a place holder; the team will replace the container with their own work later.
  • Expose the app on port 8080 .

Click Check my progress to verify the objective.

gcloud container clusters create my-cluster1

student_01_45ca4c80250a@cloudshell:~ (qwiklabs-gcp-04-30f2c219d6ce)$ gcloud container clusters get-credentials my-cluster1Fetching cluster endpoint and auth data.

kubeconfig entry generated for my-cluster1.

student_01_45ca4c80250a@cloudshell:~ (qwiklabs-gcp-04-30f2c219d6ce)$ kubectl create deployment hello-app --image=gcr.io/google-samples/hello-app:2.0

deployment.apps/hello-app created

student_01_45ca4c80250a@cloudshell:~ (qwiklabs-gcp-04-30f2c219d6ce)$ kubectl expose deployment hell0app --type=LoadBalancer --port 8080

Error from server (NotFound): deployments.apps "hell0app" not found

student_01_45ca4c80250a@cloudshell:~ (qwiklabs-gcp-04-30f2c219d6ce)$ kubectl expose deployment hello-app --type=LoadBalancer --port 8080

service/hello-app exposed

student_01_45ca4c80250a@cloudshell:~ (qwiklabs-gcp-04-30f2c219d6ce)$



Task 3: Set up an HTTP load balancer

You will serve the site via nginx web servers, but you want to ensure that the environment is fault-tolerant. Create an HTTP load balancer with a managed instance group of 2 nginx web servers. Use the following code to configure the web servers; the team will replace this with their own configuration later.

cat << EOF > startup.sh
#! /bin/bash
apt-get update
apt-get install -y nginx
service nginx start
sed -i -- 's/nginx/Google Cloud Platform - '"\$HOSTNAME"'/' /var/www/html/index.nginx-debian.html
EOF
Copied!

You need to:

  • Create an instance template.
  • Create a target pool.
  • Create a managed instance group.
  • Create a firewall rule named as allow-tcp-rule-554 to allow traffic (80/tcp).
  • Create a health check.
  • Create a backend service, and attach the managed instance group.
  • Create a URL map, and target the HTTP proxy to route requests to your URL map.
  • Create a forwarding rule.




udent_01_21094d6b9493@cloudshell:~ (qwiklabs-gcp-03-6db14b791dde)$ history
    1  gcloud compute create instances nucleus-jumphost-937 --machine-type=f1-micro
    2  gcloud compute  instances create  nucleus-jumphost-937 --machine-type=f1-micro
    3  gcloud config set compute/zone us-east1-b
    4  gcloud config list
    5  gcloud container clusters create nucleus-cluster
    6  gcloud container clusters get-credentials nucleus-cluster
    7  kubectl create deployment hello-app --image=gcr.io/google-samples/hello-app:2.0
    8  kubectl expose deployment hello-app --type=loadbalancer port 8081
    9  kubectl expose deployment hello-app --type=Loadbalancer port 8081
   10  kubectl get pods
   11  kubectl get service
   12  kubectl get deployment
   13  kubectl expose deployment hello-app --type=LoadBalancer --port 8081
   14  kubectl get service
   15  gcloud compute instance-template crate lb-backend-template --region=us-east1 --network=default --subnet=default --tags=allow-health-check --machine-type=f1-micro --metadata=startup-script='#! /bin/bash
apt-get update
apt-get install -y nginx
service nginx start
echo "nginx google cloud platform - $HOSTNAME" | \
tee /var/www/html/index.nginx-debian.html'
   16  gcloud compute instance-templates crate lb-backend-template --region=us-east1 --network=default --subnet=default --tags=allow-health-check --machine-type=f1-micro --metadata=startup-script='#! /bin/bash
apt-get update
apt-get install -y nginx
service nginx start
echo "nginx google cloud platform - $HOSTNAME" | \
tee /var/www/html/index.nginx-debian.html'
   17  gcloud compute instance-templates create lb-backend-template --region=us-east1 --network=default --subnet=default --tags=allow-health-check --machine-type=f1-micro --metadata=startup-script='#! /bin/bash
apt-get update
apt-get install -y nginx
service nginx start
echo "nginx google cloud platform - $HOSTNAME" | \
tee /var/www/html/index.nginx-debian.html'
   18  gcloud describe instance-templates lb-backend-template
   19  gcloud compute instance-templates describe lb-backend-template
   20  gcloud compute target-pools create www-pool --region us-east1 -http-check-check basic-check
   21  gcloud compute target-pools create www-pool --region us-east1 --http-check-check basic-check
   22  gcloud compute target-pools create www-pool --region us-east1 --http-health-check basic-check
   23  gcloud compute http-health-checks create basic-check
   24  gcloud compute target-pools create www-pool --region us-east1 --http-health-check basic-check
   25  gcloud compute instance-groups managed create lb-backend-group --template=lb-backend-template --size=2 --zone=us-east1-b
   26  kubectl get pods
   27  gcloud compute firewall-rules create grant-tcp-rule-935 --network=default --action=allow --direction=ingress --source-ranges=130.211.0.0/22,35.191.0.0/16 --target-tags=allow-health-check --rules=tcp:80
   28  gcloud compute instances list
   29  gcloud compute addresses create lb-ipv1-1 --ip-version=IPV4
   30  gcloud compute addresses create lb-ipv1-1 --ip-version=IPV4 --global
   31  gcloud compute addresses describe lb-ipv1-1 --format="get(address)" --global
   32  gcloud compute backend-services create web-backend-service --port=HTTP --port-name=http --health-checks=basic-check --global
   33  gcloud compute backend-services create web-backend-service --protocol=HTTP --port-name=http --health-checks=basic-check --global
   34  history
   35  gcloud compute health-checks create http basic-check --port 80
   36  gcloud compute backend-services create web-backend-service --protocol=HTTP --port-name=http --health-checks=basic-check --global
   37  gcloud compute backend-services add-backend web-backed-service --instance-group=lb-backend-group --instance-group-zone=us-east1-b --global
   38  gcloud compute backend-services add-backend web-backend-service --instance-group=lb-backend-group --instance-group-zone=us-east1-b --global
   39  gcloud compute url-maps create web-map-http --default-service web-backend-service
   40  gcloud compute forwarding-rules create http-content-rule --address=lb-ipv1-1 --global --target-http-proxy=http-lb-proxy --ports=80
   41  gcloud compute target-http-proxies create http-lb-proxy --url-map web-map-http
   42  gcloud compute forwarding-rules create http-content-rule --address=lb-ipv1-1 --global --target-http-proxy=http-lb-proxy --ports=80
   43  history
   44  gcloud compute instance-templates create lb-backend-template --region=us-east1 --network=default --subnet=default --tags=allow-health-check --machine-type=f1-micro --metadata=startup-script='#! /bin/bash
apt-get update
apt-get install -y nginx
service nginx start
echo "nginx google cloud platform - $HOSTNAME" | \
   45  history | grep instance-groups
   46  echo $HOSTNAME
   47  history











Sunday, November 14, 2021

GCP: Network and Load Balancer Lab

 

Set Up Network and HTTP Load Balancers

1 hour5 Credits

GSP007

Google Cloud Self-Paced Labs

Overview

In this hands-on lab you'll learn the differences between a network load balancer and an HTTP load balancer and how to set them up for your applications running on Compute Engine virtual machines (VMs).

There are several ways you can load balance on Google Cloud. This lab takes you through the set up of the following load balancers:

You are encouraged to type the commands yourself, which can help you learn the core concepts. Many labs include a code block that contains the required commands. You can easily copy and paste the commands from the code block into the appropriate places during the lab.

What you'll do

  • Set up a network load balancer.

  • Set up an HTTP load balancer.

  • Get hands-on experience learning the differences between network load balancers and HTTP load balancers.

Setup

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.

What you need

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
  • Time to complete the lab.

Note: If you already have your own personal Google Cloud account or project, do not use it for this lab.

Note: If you are using a Chrome OS device, open an Incognito window to run this lab.

How to start your lab and sign in to the Google Cloud Console

  1. 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 a panel populated with the temporary credentials that you must use for this lab.

    Open Google Console

  2. Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Sign in

    Tip: Open the tabs in separate windows, side-by-side.

  3. In the Sign in page, paste the username that you copied from the left panel. Then copy and paste the password.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Training credentials. If you have your own Google Cloud account, do not use it for this lab (avoids incurring charges).

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

In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

Cloud Shell icon

Click Continue.

cloudshell_continue.png

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:

Cloud Shell Terminal

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

You can list the active account name with this command:

gcloud auth list
Copied!

(Output)

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)

(Example output)

Credentialed accounts:
 - google1623327_student@qwiklabs.net

You can list the project ID with this command:

gcloud config list project
Copied!

(Output)

[core]
project = <project_ID>

(Example output)

[core]
project = qwiklabs-gcp-44776a13dea667a6

Task 1: Set the default region and zone for all resources

  1. In Cloud Shell, set the default zone:

    gcloud config set compute/zone us-central1-a
    Copied!
  2. Set the default region:

    gcloud config set compute/region us-central1
    Copied!

    Learn more about choosing zones and regions here: Regions and Zones documentation

Task 2: Create multiple web server instances

For this load balancing scenario, create three Compute Engine VM instances and install Apache on them, then add a firewall rule that allows HTTP traffic to reach the instances.

  1. Create three new virtual machines in your default zone and give them all the same tag. The code provided sets the zone to us-central1-a. Setting the tags field lets you reference these instances all at once, such as with a firewall rule. These commands also install Apache on each instance and give each instance a unique home page.

gcloud compute instances create www1 \
  --image-family debian-9 \
  --image-project debian-cloud \
  --zone us-central1-a \
  --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"
Copied!
gcloud compute instances create www2 \
  --image-family debian-9 \
  --image-project debian-cloud \
  --zone us-central1-a \
  --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"
Copied!
gcloud compute instances create www3 \
  --image-family debian-9 \
  --image-project debian-cloud \
  --zone us-central1-a \
  --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"
Copied!
  1. Create a firewall rule to allow external traffic to the VM instances:

gcloud compute firewall-rules create www-firewall-network-lb \
    --target-tags network-lb-tag --allow tcp:80
Copied!

Now you need to get the external IP addresses of your instances and verify that they are running.

  1. Run the following to list your instances. You'll see their IP addresses in the EXTERNAL_IP column:

gcloud compute instances list
Copied!
  1. Verify that each instance is running with curl, replacing [IP_ADDRESS] with the IP address for each of your VMs:

curl http://[IP_ADDRESS]
Copied!

Check your lab progress

Click Check my progress below to verify that you've created a group of web servers.

Assessment Completed!

Create multiple web server instances

Assessment Completed!

Task 3: Configure the load balancing service

When you configure the load balancing service, your virtual machine instances will receive packets that are destined for the static external IP address you configure. Instances made with a Compute Engine image are automatically configured to handle this IP address.

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

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

    (Output)

    Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-xxxxxxxxxxx/regions/us-central1/addresses/network-lb-ip-1].
  2. Add a legacy HTTP health check resource:

    gcloud compute http-health-checks create basic-check
    Copied!
  3. Add a target pool in the same region as your instances. Run the following to create the target pool and use the health check, which is required for the service to function:

gcloud compute target-pools create www-pool \
    --region us-central1 --http-health-check basic-check
Copied!
  1. Add the instances to the pool:

gcloud compute target-pools add-instances www-pool \
    --instances www1,www2,www3
Copied!
  1. Add a forwarding rule:

gcloud compute forwarding-rules create www-rule \
    --region us-central1 \
    --ports 80 \
    --address network-lb-ip-1 \
    --target-pool www-pool
Copied!

Check your lab progress

Click Check my progress below to verify that you've created an L4 network load balancer that points to the web servers.

Assessment Completed!

Configure the load balancing service

Assessment Completed!

Task 4: Sending traffic to your instances

Now that the load balancing service is configured, you can start sending traffic to the forwarding rule and watch the traffic be dispersed to different instances.

Enter the following command to view the external IP address of the www-rule forwarding rule used by the load balancer:

gcloud compute forwarding-rules describe www-rule --region us-central1
Copied!

Use curl command to access the external IP address, replacing IP_ADDRESS with an external IP address from the previous command:

while true; do curl -m1 IP_ADDRESS; done
Copied!

The response from the curl command alternates randomly among the three instances. if your response is initially unsuccessful, wait approximately 30 seconds for the configuration to be fully loaded and for your instances to be marked healthy before trying again.

Use Ctrl + c to stop running the command.

Task 5: Create an HTTP load balancer

HTTP(S) Load Balancing is implemented on Google Front End (GFE). GFEs are distributed globally and operate together using Google's global network and control plane. You can configure URL rules to route some URLs to one set of instances and route other URLs to other instances. Requests are always routed to the instance group that is closest to the user, if that group has enough capacity and is appropriate for the request. If the closest group does not have enough capacity, the request is sent to the closest group that does have capacity.

To set up a load balancer with a Compute Engine backend, your VMs need to be in an instance group. The managed instance group provides VMs running the backend servers of an external HTTP load balancer. For this lab, backends serve their own hostnames.

  1. First, create the load balancer template:

gcloud compute instance-templates create lb-backend-template \
   --region=us-central1 \
   --network=default \
   --subnet=default \
   --tags=allow-health-check \
   --image-family=debian-9 \
   --image-project=debian-cloud \
   --metadata=startup-script='#! /bin/bash
     apt-get update
     apt-get install apache2 -y
     a2ensite default-ssl
     a2enmod ssl
     vm_hostname="$(curl -H "Metadata-Flavor:Google" \
     http://169.254.169.254/computeMetadata/v1/instance/name)"
     echo "Page served from: $vm_hostname" | \
     tee /var/www/html/index.html
     systemctl restart apache2'
Copied!
  1. Create a managed instance group based on the template:

gcloud compute instance-groups managed create lb-backend-group \
   --template=lb-backend-template --size=2 --zone=us-central1-a
Copied!
  1. Create the fw-allow-health-check firewall rule. This is an ingress rule that allows traffic from the Google Cloud health checking systems (130.211.0.0/22 and 35.191.0.0/16). This lab uses the target tag allow-health-check to identify the VMs.

gcloud compute firewall-rules create fw-allow-health-check \
    --network=default \
    --action=allow \
    --direction=ingress \
    --source-ranges=130.211.0.0/22,35.191.0.0/16 \
    --target-tags=allow-health-check \
    --rules=tcp:80
Copied!
  1. Now that the instances are up and running, set up a global static external IP address that your customers use to reach your load balancer.

gcloud compute addresses create lb-ipv4-1 \
    --ip-version=IPV4 \
    --global
Copied!

Note the IPv4 address that was reserved:

gcloud compute addresses describe lb-ipv4-1 \
    --format="get(address)" \
    --global
Copied!
  1. Create a healthcheck for the load balancer:

gcloud compute health-checks create http http-basic-check \
    --port 80
Copied!
  1. Create a backend service:

gcloud compute backend-services create web-backend-service \
    --protocol=HTTP \
    --port-name=http \
    --health-checks=http-basic-check \
    --global
Copied!
  1. Add your instance group as the backend to the backend service:

gcloud compute backend-services add-backend web-backend-service \
    --instance-group=lb-backend-group \
    --instance-group-zone=us-central1-a \
    --global
Copied!
  1. Create a URL map to route the incoming requests to the default backend service:

gcloud compute url-maps create web-map-http \
    --default-service web-backend-service
Copied!
  1. Create a target HTTP proxy to route requests to your URL map:

gcloud compute target-http-proxies create http-lb-proxy \
    --url-map web-map-http
Copied!
  1. Create a global forwarding rule to route incoming requests to the proxy:

gcloud compute forwarding-rules create http-content-rule \
    --address=lb-ipv4-1\
    --global \
    --target-http-proxy=http-lb-proxy \
    --ports=80
Copied!

Check your lab progress

Click Check my progress below to verify that you've created an L7 HTTP(S) load balancer.

Assessment Completed!

Create an HTTP load balancer

Assessment Completed!

Task 6: Testing traffic sent to your instances

  1. In the Cloud Console, from the Navigation menu, go to Network services > Load balancing.

  2. Click on the load balancer that you just created (web-map-http).

  3. In the Backend section, click on the name of the backend and confirm that the VMs are Healthy. If they are not healthy, wait a few moments and try reloading the page.

  4. When the VMs are healthy, test the load balancer using a web browser, going to http://IP_ADDRESS/, replacing IP_ADDRESS with the load balancer's IP address.

This may take three to five minutes. If you do not connect, wait a minute, and then reload the browser.

Your browser should render a page with content showing the name of the instance that served the page, along with its zone (for example, Page served from: lb-backend-group-xxxx).

Congratulations!