将 Public NAT 与 GKE 搭配使用
本页介绍如何使用 Google Kubernetes Engine (GKE) 配置示例 Public NAT 设置。在设置 Public NAT 之前,请先阅读 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输出:
NAME MODE IPV4_RANGE GATEWAY_IPV4 custom-network1 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
输出:
NAME REGION NETWORK RANGE subnet-us-east-192 us-east4 custom-network1 192.168.1.0/24
在 Google Cloud 控制台中,前往 Kubernetes 集群页面。
点击创建集群。
对于名称,输入
nat-test-cluster。将位置类型设置为区域。
将区域设置为 us-east4-c。
在导航窗格中,点击网络。
选择专用集群。
取消选中使用外部 IP 地址的控制平面复选框。
输入
172.16.0.0/28作为控制平面 IP 地址范围。将网络设置为
custom-network1。要创建并启动该集群,请点击创建。
在 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 下方列表中第一个节点旁边的复选框。其名称与
gke-nat-test-cluster-default-pool-b50db58d-075t类似。记下节点的名称;稍后您将用它来测试连接。
在右侧窗格中,点击添加主账号。
要向用户、群组或服务账号授予资源访问权限,请在新主账号字段中指定其电子邮件地址。
如果您只是测试此功能,则可以输入自己的电子邮件地址。
要通过 Cloud IAP 的 TCP 转发功能向主账号授予资源访问权限,请在角色下拉列表中选择 Cloud IAP > 受 IAP 保护的隧道用户。
点击保存。
设置 GKE 示例
如果您想查看使用 GKE 的简单 Public NAT 配置,请使用此示例。
第 1 步:创建 VPC 网络和子网
如果您已有网络和子网,则可以跳过此步骤。
控制台
gcloud
Terraform
您可以使用 Terraform 模块创建自定义虚拟私有云网络和子网。
第 2 步:创建专用集群
控制台
gcloud
gcloud container clusters create "nat-test-cluster" \
--zone "us-east4-c" \
--username "admin" \
--cluster-version "latest" \
--machine-type "e2-medium" \
--disk-type "pd-standard" \
--disk-size "100" \
--scopes "https://www.googleapis.com/auth/compute","https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" \
--num-nodes "3" \
--enable-private-nodes \
--enable-private-endpoint \
--master-ipv4-cidr "172.16.0.0/28" \
--enable-ip-alias \
--network "projects/PROJECT_ID/global/networks/custom-network1" \
--subnetwork "projects/PROJECT_ID/regions/us-east4/subnetworks/subnet-us-east-192" \
--max-nodes-per-pool "110" \
--enable-master-authorized-networks \
--addons HorizontalPodAutoscaling,HttpLoadBalancing \
--enable-autoupgrade \
--enable-autorepair
Terraform
您可以使用 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 权限
在随后的步骤中,使用 IAP 连接到该节点。
控制台
gcloud
对于此步骤,请按照控制台说明操作。