将 Public NAT 与 Compute Engine 搭配使用
本页面演示了一个 Public NAT 网关,该网关为 Compute Engine 虚拟机实例提供网络地址转换服务。在开始之前,请先阅读 Public NAT 概览。
前提条件
在设置 Public NAT 之前,您需要执行以下操作。
获取 IAM 权限
通过 roles/compute.networkAdmin 角色,您有权在 Cloud Router 路由器上创建 NAT 网关、预留和分配 NAT IP 地址,以及指定哪些子网的流量应该使用 NAT 网关的网络地址转换。
设置 Google Cloud
开始之前,先在 Google Cloud中设置以下各项。
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Install the Google Cloud CLI.
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Install the Google Cloud CLI.
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init 在 Google Cloud 控制台中,前往 VPC 网络页面。
点击创建 VPC 网络。
输入
custom-network1作为名称。在子网下,将子网创建模式设置为自定义。
在新子网下,输入
subnet-us-east-192作为名称。在地区中,选择 us-east4。
输入
192.168.1.0/24作为 IP 地址范围。点击完成,然后点击创建。
在项目中创建新的自定义模式 VPC 网络:
gcloud compute networks create custom-network1 \ --subnet-mode custom为第一个地区指定子网前缀。在本例中,我们为地区
us-east4分配了192.168.1.0/24。gcloud compute networks subnets create subnet-us-east-192 \ --network custom-network1 \ --region us-east4 \ --range 192.168.1.0/24
在 Google Cloud 控制台中,前往虚拟机实例页面。
点击创建实例。
为您的实例指定
nat-test-1作为名称。将地区设置为 us-east4。
将区域设置为 us-east4-c。
点击管理、安全、磁盘、网络、单独租用链接。
点击网络标签页。
在网络接口下,点击虚拟机默认接口所对应的 修改。
- 将网络设置为
custom-network1。 - 将子网设置为
subnet-us-east-192。 - 将外部 IP 设置为 None。
- 点击完成。
- 将网络设置为
要创建并启动该实例,请点击创建。
在 Google Cloud 控制台中,前往防火墙政策页面。
点击创建防火墙规则。
输入
allow-ssh作为名称。指定
custom-network1作为网络。将流量方向设置为入站。
将对匹配项执行的操作设置为允许。
将目标设置为网络中的所有实例。
将来源过滤条件设置为 IPv4 范围。
将来源 IP 地址范围设置为
35.235.240.0/20。将协议和端口设置为指定的协议和端口。
选中 tcp 复选框,然后输入端口
22。点击创建。
在 Google Cloud 控制台中,前往 Identity-Aware Proxy 页面。
选择 SSH 和 TCP 资源标签页。
要更新成员对资源的权限,请选中所有隧道资源 > us-east4-c > nat-test-1 旁边的复选框。
在右侧窗格中,点击添加成员。
要向用户、群组或服务账号授予资源访问权限,请在新成员字段中指定其电子邮件地址。
如果您只是测试此功能,则可以输入自己的电子邮件地址。
要通过 Cloud IAP 的 TCP 转发功能向成员授予资源访问权限,请在角色下拉列表中选择 Cloud IAP > 受 IAP 保护的隧道用户。
点击保存。
PROJECT_ID:您的项目 IDMEMBER_INFO:以英文逗号分隔的成员type:email对列表。示例:- 针对个人用户:
user:test-user@example.com - 对于群组:
group:admins@example.com - 对于服务账号:
serviceAccount:test123@example.domain.com
- 针对个人用户:
在 Google Cloud 控制台中,前往虚拟机实例页面。
对于
nat-test-1,请在连接列中,点击 SSH 下拉箭头,然后选择在浏览器窗口中打开。在虚拟机的命令提示符下,输入
curl example.com,然后按 Enter 键。您应该不会获得任何结果。否则,您可能使用外部 IP 地址创建了
nat-test-1,或者可能存在其他问题。如需排查问题,请参阅
示例
以下是一个端到端示例,演示了一个示例 Public NAT 网关和一个使用 Public NAT 网关的示例 Compute Engine 虚拟机。
第 1 步:创建 VPC 网络和子网
如果您已有网络和子网,则可以跳过此步骤。
控制台
gcloud
Terraform
您可以使用 Terraform 模块创建自定义虚拟私有云 (VPC) 网络和子网。
第 2 步:创建没有外部 IP 地址的虚拟机实例
控制台
gcloud
gcloud compute instances create nat-test-1 \
--image-family debian-9 \
--image-project debian-cloud \
--network custom-network1 \
--subnet subnet-us-east-192 \
--zone us-east4-c \
--no-addressTerraform
您可以使用 Terraform 资源创建虚拟机实例。
第 3 步:创建一条允许 SSH 连接的防火墙规则
控制台
gcloud
gcloud compute firewall-rules create allow-ssh \
--network custom-network1 \
--source-ranges 35.235.240.0/20 \
--allow tcp:22Terraform
您可以使用 Terraform 资源创建防火墙规则。
第 4 步:为您的测试实例创建 IAP SSH 权限
在稍后的步骤中,您将使用 Identity-Aware Proxy (IAP) 连接到您的测试实例。
控制台
gcloud
此命令使用 IAP 向项目中的所有虚拟机实例授予 SSH 访问权限。如果要使用 IAP 向单个虚拟机授予 SSH 访问权限,请按照 Google Cloud 控制台说明操作。
gcloud projects add-iam-policy-binding PROJECT_ID \
--member=MEMBER_INFO \
--role=roles/iap.tunnelResourceAccessor替换以下内容:
Terraform
您可以使用 Terraform 资源为您的测试实例创建 IAP SSH 权限。