
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Create a VPC Network and Subnet
/ 20
Create a Private Cluster
/ 20
Create a firewall rule that allows SSH connections
/ 20
Create IAP SSH permissions for one of your nodes
/ 20
Create a NAT configuration using Cloud Router
/ 20
This lab uses the Modular NAT Gateway on Compute Engine for Terraform to automate creation of a NAT gateway managed instance group. You direct traffic from the instances by using tag-based routing, although only instances with matching tags use the NAT gateway route.
Under normal circumstances, Kubernetes Engine nodes route all egress traffic through the internet gateway associated with their node cluster. The internet gateway connection, in turn, is defined by the Compute Engine network associated with the node cluster. Each node in the cluster has an ephemeral external IP address. When nodes are created and destroyed during autoscaling, new node IP addresses are allocated automatically.
The default gateway behavior works well under normal circumstances. However, you might want to modify how ephemeral external IP addresses are allocated in order to:
In this lab, you will learn how to:
The following diagram shows an overview of the architecture:
This Qwiklabs 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:
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.
Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Choose an account page.
On the Choose an account page, click Use Another Account. The Sign in page opens.
Paste the username that you copied from the Connection Details panel. Then copy and paste the password.
After a few moments, the Cloud console opens in this tab.
Google 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.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
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:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
Compute Engine routes have a default priority of 1000, with lower numbers indicating higher priority. The Terraform module creates a Compute Engine route with priority 800, redirecting all outbound traffic from the Kubernetes Engine nodes to the NAT gateway instance instead of using the default internet gateway. The example code in the module also creates a static route with priority 700, redirecting traffic from the Kubernetes Engine nodes to the Kubernetes Engine master, which preserves normal cluster operation by splitting the egress traffic.
After the NAT gateway instance is up and running, the startup script configures IP forwarding and adds the firewall rules needed to perform address translation.
variables.tf
configuration file.You will use this file to reference variables which will be re-used throughout the lab.
On the Cloud Shell toolbar, click Open Editor. To switch between Cloud Shell and the code editor, click Open Editor or Open Terminal as required.
Paste the following into your variables.tf
file:
main.tf
file.This file will contain the configuration for your resources.
main.tf
file:Still in your Cloud Shell Editor, click Terminal > New Terminal.
In your terminal, ensure your account is activated:
Paste the link in a new tab and follow the instructions using your student account. You will get a verification code to use for authentication.
In your terminal, initialize terraform:
Authorize Cloud Shell if prompted.
Enter yes
when prompted to confirm the Terraform deployment.
Click Check my progress to verify the objective.
main.tf
file:Enter yes when prompted.
Click Check my progress to verify the objective.
main.tf
file:Enter yes when prompted.
Click Check my progress to verify the objective.
In the Cloud Console, navigate to IAM & Admin > Identity-Aware Proxy from the navigation bar.
Click Enable API.
Click Go to Identity-Aware Proxy.
Select the SSH and TCP Resources tab.
Select the checkbox next to the first node in the list under All Tunnel Resources > us-east4-c. Its name will be similar to gke-nat-test-cluster-default-pool-b50db58d-075t
.
In the right pane, click Add principal.
In the New principals field, enter your student email address that you logged into the lab with (e.g.
Grant yourself access to the resources through Cloud IAP's TCP forwarding feature, in the Role drop-down list, select Cloud IAP > IAP-secured Tunnel User.
Click Save.
Click Check my progress to verify the objective.
Copy the name of the node you gave SSH permissions to from the IAP panel.
Back in your terminal, assign the name to a NODE_NAME
variable:
You will reference that node in the future commands.
When asked to continue, press y
.
Then, press enter twice to use an empty password.
You should get no result.To end the command, you might have to enter Ctrl+C
.
You must create the Cloud Router in the same region as the instances that use Cloud NAT. Cloud NAT is only used to place NAT information onto the VMs. It is not used as part of the actual NAT gateway.
This configuration allows all instances in the region to use Cloud NAT for all primary and alias IP ranges. It also automatically allocates the external IP addresses for the NAT gateway. For more options, see the gcloud command-line interface documentation.
exit
command to leave your ssh tunnel:main.tf
file to create a Cloud Router:main.tf
file to create a NAT configuration:terraform init
to install the cloud-nat
module:terraform apply
to create your newly added resources:Enter yes
when prompted.
Click Check my progress to verify the objective.
It might take up to three minutes for the NAT configuration to propagate, so wait at least a minute before trying to access the internet again.
It should now successfully display the output of the example domain page!
Read more about Terraform for Google in the documentation.
Manual Last Updated September 13, 2024
Lab Last Tested September 13, 2024
Copyright 2022 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.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one