arrow_back

Cloud DNS: Traffic Steering using Geolocation Policy

가입 로그인
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

Cloud DNS: Traffic Steering using Geolocation Policy

Lab 1시간 30분 universal_currency_alt 크레딧 1개 show_chart 입문
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

GSP1008

Google Cloud self-paced labs logo

Overview

Cloud DNS routing policies enable users to configure DNS based traffic steering. A user can either create a Weighted Round Robin (WRR) routing policy or a Geolocation (GEO) routing policy. You can configure routing policies by creating special ResourceRecordSets with special routing policy values.

Use WRR to specify different weights per ResourceRecordSet for the resolution of domain names. Cloud DNS routing policies help ensure that traffic is distributed across multiple IP addresses by resolving DNS requests according to the configured weights.

​​In this lab you will configure and test the Geolocation routing policy. Use GEO to specify source geolocations and to provide DNS answers corresponding to those geographies. The geolocation routing policy applies the nearest match for the source location when the traffic source location doesn't match any policy items exactly.

What you learn

You will learn how to:

  1. Launch client VMs, one in each region
  2. Launch server VMs, one in each region except
  3. Create a private zone, for example.com
  4. Create a Geolocation routing policy using gcloud commands
  5. Test the configuration

Architecture

Use the default VPC network to create all the virtual machines (VM) and launch client VMs in 3 Google Cloud locations: , and . To demonstrate the behavior of Geolocation routing policy, you will create the server VMs only in 2 regions: and .

The default VPC diagram

You will use Cloud DNS routing policies and create ResourceRecordSets for geo.example.com and configure the Geolocation policy to help ensure that a client request is routed to a server in the client's closest region.

The Cloud DNS routing policies diagram

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).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

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 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
  2. 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.
  3. 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.

  4. Click Next.

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

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

Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left. Navigation menu icon

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.

  1. Click Activate Cloud Shell Activate Cloud Shell icon 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, . The output contains a line that declares the Project_ID for this session:

Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}

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

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

Output:

ACTIVE: * ACCOUNT: {{{user_0.username | "ACCOUNT"}}} To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = {{{project_0.project_id | "PROJECT_ID"}}} Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Task 1. Enable APIs

Ensure that the Compute and the Cloud DNS APIs are enabled. In this section, you will enable the APIs manually, using gcloud commands.

Enable Compute Engine API

  • Run the gcloud services enable command to enable the Compute Engine API:
gcloud services enable compute.googleapis.com

This command can take a few minutes to complete.

Enable Cloud DNS API

  • Run the gcloud services enable command to enable the Cloud DNS API:
gcloud services enable dns.googleapis.com

This command can take a few minutes to complete.

Verify that the APIs are enabled

  • Run the gcloud services list command to list all the enabled APIs. We should see compute.googleapis.com and dns.googleapis.com in the listed output.
gcloud services list | grep -E 'compute|dns'

Output:

NAME: compute.googleapis.com NAME: dns.googleapis.com

Task 2. Configure the firewall

Before you create the client VMs and the web servers, you need to create two firewall rules.

Note: The firewall-rules create command can take a few minutes to complete. Please wait for the "Creating firewall...done" message before proceeding to the next step.
  1. To be able to SSH into the client VMs, run the following to create a firewall rule to allow SSH traffic from Identity Aware Proxies (IAP):
gcloud compute firewall-rules create fw-default-iapproxy \ --direction=INGRESS \ --priority=1000 \ --network=default \ --action=ALLOW \ --rules=tcp:22,icmp \ --source-ranges=35.235.240.0/20

Output:

Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-01-c5d669dffb06/global/firewalls/fw-default-iapproxy]. Creating firewall...done. NAME: fw-default-iapproxy NETWORK: default DIRECTION: INGRESS PRIORITY: 1000 ALLOW: tcp:22,icmp DENY: DISABLED: False
  1. To allow HTTP traffic on the web servers, each web server will have a "http-server" tag associated with it. You will use this tag to apply the firewall rule only to your web servers:
gcloud compute firewall-rules create allow-http-traffic --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp:80 --source-ranges=0.0.0.0/0 --target-tags=http-server

Output:

Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/routing-policy-lab/global/firewalls/allow-http-traffic]. Creating firewall...done. NAME: allow-http-traffic NETWORK: default DIRECTION: INGRESS PRIORITY: 1000 ALLOW: tcp:80 DENY: DISABLED: False

Click Check my progress to verify the objective. Configure the Firewall

Task 3. Launch client VMs

Now that the APIs are enabled, and the firewall rules are in place, the next step is to set up the environment. In this section, you will create 3 client VMs, one in each region.

Launch client in

  • Run the gcloud compute instances create command to create the client VMs:
gcloud compute instances create us-client-vm --machine-type e2-medium --zone {{{project_0.startup_script.zone_1|ZONE1}}}

This command can take a few minutes to complete. Please wait for a "Created" message before moving to the next step.

Output:

Created [https://www.googleapis.com/compute/v1/projects/routing-policy-lab/zones/{{{project_0.startup_script.zone_1|ZONE1}}}/instances/us-client-vm]. NAME: us-client-vm ZONE: {{{project_0.startup_script.zone_1|ZONE1}}} MACHINE_TYPE: e2-medium PREEMPTIBLE: INTERNAL_IP: 10.142.0.2 EXTERNAL_IP: 34.138.90.216 STATUS: RUNNING

Launch client in

  • Run the following to create the client VMs:
gcloud compute instances create europe-client-vm --machine-type e2-medium --zone {{{project_0.startup_script.zone_2|ZONE2}}}

Output:

Created [https://www.googleapis.com/compute/v1/projects/routing-policy-lab/zones/{{{project_0.startup_script.zone_2|ZONE2}}}/instances/europe-client-vm]. NAME: europe-client-vm ZONE: {{{project_0.startup_script.zone_2|ZONE2}}} MACHINE_TYPE: e2-medium PREEMPTIBLE: INTERNAL_IP: 10.154.0.2 EXTERNAL_IP: 35.242.164.177 STATUS: RUNNING

Launch client in

  • Run the following to create the client VMs:
gcloud compute instances create asia-client-vm --machine-type e2-medium --zone {{{project_0.startup_script.zone_3|ZONE3}}}

Output:

Created [https://www.googleapis.com/compute/v1/projects/routing-policy-lab/zones/{{{project_0.startup_script.zone_3|ZONE3}}}/instances/asia-client-vm]. NAME: asia-client-vm ZONE: {{{project_0.startup_script.zone_3|ZONE3}}} MACHINE_TYPE: e2-medium PREEMPTIBLE: INTERNAL_IP: 10.160.0.2 EXTERNAL_IP: 34.93.179.212 STATUS: RUNNING

Click Check my progress to verify the objective. Launch client VMs

Task 4. Launch Server VMs

Now that the client VM's are up and running, the next step is to create the server VMs. You will use a startup script to configure and set up the web servers. As mentioned earlier, you will create the server VMs only in 2 regions: and .

  • Run the gcloud compute instances create command to create the server VMs. The compute instance create command can take a few minutes to complete. Please wait for a "Created" message before moving to the next step.

Launch server in

  • Run the following to command:
gcloud compute instances create us-web-vm \ --zone={{{project_0.startup_script.zone_1|ZONE1}}} \ --machine-type=e2-medium \ --network=default \ --subnet=default \ --tags=http-server \ --metadata=startup-script='#! /bin/bash apt-get update apt-get install apache2 -y echo "Page served from: {{{project_0.startup_script.region_1|REGION1}}}" | \ tee /var/www/html/index.html systemctl restart apache2'

Output:

Created [https://www.googleapis.com/compute/v1/projects/routing-policy-lab/zones/{{{project_0.startup_script.zone_1|ZONE1}}}/instances/us-web-vm]. NAME: us-web-vm ZONE: {{{project_0.startup_script.zone_1|ZONE1}}} MACHINE_TYPE: e2-medium PREEMPTIBLE: INTERNAL_IP: 10.142.0.3 EXTERNAL_IP: 34.73.110.151 STATUS: RUNNING

Launch server in

  • Run the following to command:
gcloud compute instances create europe-web-vm \ --zone={{{project_0.startup_script.zone_2|ZONE2}}} \ --machine-type=e2-medium \ --network=default \ --subnet=default \ --tags=http-server \ --metadata=startup-script='#! /bin/bash apt-get update apt-get install apache2 -y echo "Page served from: {{{project_0.startup_script.region_2|REGION2}}}" | \ tee /var/www/html/index.html systemctl restart apache2'

Output:

Created [https://www.googleapis.com/compute/v1/projects/routing-policy-lab/zones/{{{project_0.startup_script.zone_2|ZONE2}}}/instances/europe-web-vm]. NAME: europe-web-vm ZONE: {{{project_0.startup_script.zone_2|ZONE2}}} MACHINE_TYPE: e2-medium PREEMPTIBLE: INTERNAL_IP: 10.154.0.3 EXTERNAL_IP: 35.234.156.62 STATUS: RUNNING

Click Check my progress to verify the objective. Launch Server VMs

Task 5. Setting up environment variables

Before you configure Cloud DNS, note the Internal IP addresses of the web servers. You need these IPs to create the routing policy. In this section, you will use the gcloud compute instances describe command to save the internal IP addresses as environment variables.

  1. Command to save IP address for the vm in :
export US_WEB_IP=$(gcloud compute instances describe us-web-vm --zone={{{project_0.startup_script.zone_1|ZONE1}}} --format="value(networkInterfaces.networkIP)")
  1. Command to save the IP address for vm in :
export EUROPE_WEB_IP=$(gcloud compute instances describe europe-web-vm --zone={{{project_0.startup_script.zone_2|ZONE2}}} --format="value(networkInterfaces.networkIP)")

Task 6. Create the private zone

Now that your client and server VMs are running, it's time to configure the DNS settings. Before creating the A records for the web servers, you need to create the Cloud DNS Private Zone.

For this lab, use the example.com domain name for the Cloud DNS zone.

  • Use the gcloud dns managed-zones create command to create the zone:
gcloud dns managed-zones create example --description=test --dns-name=example.com --networks=default --visibility=private

Output:

Created [https://dns.googleapis.com/dns/v1/projects/routing-policy-lab/managedZones/example].

Task 7. Create Cloud DNS Routing Policy

In this section, configure the Cloud DNS Geolocation Routing Policy. You will create a record set in the example.com zone that you created in the previous section.

Create

  • Use the gcloud beta dns record-sets create command to create the geo.example.com recordset:
gcloud beta dns record-sets create geo.example.com \ --ttl=5 --type=A --zone=example \ --routing_policy_type=GEO \ --routing_policy_data="{{{project_0.startup_script.region_1|REGION1}}}=$US_WEB_IP;{{{project_0.startup_script.region_2|REGION2}}}=$EUROPE_WEB_IP"

You are creating an A record with a Time to Live (TTL) of 5 seconds. The policy type is GEO, and the routing_policy_data field accepts a semicolon-delimited list of the format "$${region}:$${rrdata},${rrdata}".

Output:

NAME: geo.example.com. TYPE: A TTL: 5 DATA: {{{project_0.startup_script.region_1|REGION1}}}: 10.142.0.3; {{{project_0.startup_script.region_2|REGION2}}}: 10.154.0.3

Verify

  • Use the dns record-sets list command to verify that the geo.example.com DNS record is configured as expected:
gcloud beta dns record-sets list --zone=example

The output shows that an A record with a TTL of 5 is created for geo.example.com, and the data matches our server set up in each region.

Output:

NAME: example.com. TYPE: NS TTL: 21600 DATA: ns-gcp-private.googledomains.com. NAME: example.com. TYPE: SOA TTL: 21600 DATA: ns-gcp-private.googledomains.com. cloud-dns-hostmaster.google.com. 1 21600 3600 259200 300 NAME: geo.example.com. TYPE: A TTL: 5 DATA: {{{project_0.startup_script.region_1|REGION1}}}: 10.142.0.3; {{{project_0.startup_script.region_2|REGION2}}}: 10.154.0.3

Click Check my progress to verify the objective. Create the Private Zone

Task 8. Testing

It's time to test the configuration. In this section, you will SSH into all the client VMs. Since all of the web server VMs are behind the geo.example.com domain, you will use cURL command to access this endpoint.

Since you are using a Geolocation policy, the expected result is that:

  • The client in the US should always get a response from the region.
  • The client in Europe should always get a response from the region.

Testing from the client VM in

  1. Use the gcloud compute ssh command to log into the client VM:
gcloud compute ssh europe-client-vm --zone {{{project_0.startup_script.zone_2|ZONE2}}} --tunnel-through-iap
  1. Follow prompts to SSH into the machine. When asked to enter the passphrase, leave the field blank and press the Enter key twice.

Once complete, the command line should change to "user_name@europe-client-vm:~$"

Output:

Warning: Permanently added 'compute.4621780534809863836' (ECDSA) to the list of known hosts. Linux europe-client-vm 4.19.0-18-cloud-amd64 #1 SMP Debian 4.19.208-1 (2021-09-29) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. user_name@europe-client-vm:~$

Use curl to access the web server

  1. Now that you are in the client VM, use the cURL command to access the geo.example.com endpoint. The loop is configured to run the command ten times with a sleep timer of 6 seconds:
for i in {1..10}; do echo $i; curl geo.example.com; sleep 6; done

Since the TTL on the DNS record is set to 5 seconds, a sleep timer of 6 seconds has been added. The sleep timer will make sure that you get an uncached DNS response for each cURL request. This command will take approximately one minute to complete.

The expected output is "Page served from: ":

1 Page served from: {{{project_0.startup_script.region_2|REGION2}}} 2 Page served from: {{{project_0.startup_script.region_2|REGION2}}} 3 Page served from: {{{project_0.startup_script.region_2|REGION2}}} 4 Page served from: {{{project_0.startup_script.region_2|REGION2}}} 5 Page served from: {{{project_0.startup_script.region_2|REGION2}}} 6 Page served from: {{{project_0.startup_script.region_2|REGION2}}} 7 Page served from: {{{project_0.startup_script.region_2|REGION2}}} 8 Page served from: {{{project_0.startup_script.region_2|REGION2}}} 9 Page served from: {{{project_0.startup_script.region_2|REGION2}}} 10 Page served from: {{{project_0.startup_script.region_2|REGION2}}}
  1. Run this test multiple times and analyze the output to see which server is responding to the request. The client should always receive a response from a server in the client's region.

Getting back to Cloud Shell

  • Once you have run the test multiple times, exit the client VM in Europe by typing "exit" in the VM's command prompt. This will bring you back to the Cloud Shell console.

Testing from the client VM in

Now perform the same test from the client VM in the US.

  1. Use the gcloud command below to SSH into the us-client-vm:
gcloud compute ssh us-client-vm --zone {{{project_0.startup_script.zone_1|ZONE1}}} --tunnel-through-iap
  1. Use the curl command to access geo.example.com:
for i in {1..10}; do echo $i; curl geo.example.com; sleep 6; done
  1. Now analyze the output to see which server is responding to the request. The client should always receive a response from a server in the client's region. The expected output is "Page served from: ".

  2. Once you have run the test multiple times, exit the client VM in the US by typing "exit" in the VM's command prompt.

Testing from the client VM in

So far you have tested the setup from and . You have servers running in both the regions and have matching record sets for both the regions in Cloud DNS routing policy. There is no matching policy item for in the Cloud DNS routing policy that was created in this lab.

The Geolocation policy will apply a "nearest" match for source location when the source of the traffic doesn't match any policy items exactly. This means that the client in the region should be directed to the nearest web server.

In this section, you will resolve the geo.example.com domain from the client VM in Asia and will analyze the response.

  1. SSH into the us-client-vm:
gcloud compute ssh asia-client-vm --zone {{{project_0.startup_script.zone_3|ZONE3}}} --tunnel-through-iap
  1. Then access geo.example.com:
for i in {1..10}; do echo $i; curl geo.example.com; sleep 6; done
  1. Analyze the output to see which server is responding to the request. Since there is no policy item for , Cloud DNS will direct the client to the nearest server.

  2. Once you have run the test multiple times, exit the client VM in Asia by typing "exit" in the VM's command prompt.

Task 9. Delete lab resources

Although all resources you used in this lab will be deleted when you finish, it is good practice to remove resources you no longer need to avoid unnecessary charges.

  • The following gcloud commands will delete all the resources that were created in the lab:
#delete VMS gcloud compute instances delete -q us-client-vm --zone {{{project_0.startup_script.zone_1|ZONE1}}} gcloud compute instances delete -q us-web-vm --zone {{{project_0.startup_script.zone_1|ZONE1}}} gcloud compute instances delete -q europe-client-vm --zone {{{project_0.startup_script.zone_2|ZONE2}}} gcloud compute instances delete -q europe-web-vm --zone {{{project_0.startup_script.zone_2|ZONE2}}} gcloud compute instances delete -q asia-client-vm --zone {{{project_0.startup_script.zone_3|ZONE3}}} #delete FW rules gcloud compute firewall-rules delete -q allow-http-traffic gcloud compute firewall-rules delete fw-default-iapproxy #delete record set gcloud beta dns record-sets delete geo.example.com --type=A --zone=example #delete private zone gcloud dns managed-zones delete example

Congratulations

In this lab you configured and used Cloud DNS routing policies with Geolocation routing policy. You also verified the configuration and behavior of the Cloud DNS routing policy by observing the HTTP response when accessing the web servers.

Next steps / Learn more

You can read more about Cloud DNS routing policies.

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 October 12, 2023

Lab Last Tested October 12, 2023

End your lab

When you have completed your lab, click End Lab. Your account and the resources you've used are removed from the lab platform.

You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.

The number of stars indicates the following:

  • 1 star = Very dissatisfied
  • 2 stars = Dissatisfied
  • 3 stars = Neutral
  • 4 stars = Satisfied
  • 5 stars = Very satisfied

You can close the dialog box if you don't want to provide feedback.

For feedback, suggestions, or corrections, please use the Support tab.

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.