本教程介绍如何使用堡垒主机通过互联网访问 Google Kubernetes Engine (GKE) 中配置为仅在内部端点上公开控制平面的 GKE 集群。
您可以创建没有公共端点客户端访问权限的 GKE 集群,这意味着控制平面仅在内部端点上公开。此访问权限选项可阻止对控制平面进行的所有互联网访问,从而提高集群安全性。但是,停用对公共端点的访问权限会阻止您与集群远程交互,除非您将远程客户端的 IP 地址添加为已获授权的网络。
本教程介绍如何设置堡垒主机(一种专用于应对攻击的特殊用途主机)。堡垒主机使用 Tinyproxy 将客户端流量转发到集群。您可以使用 Identity-Aware Proxy (IAP) 从远程客户端 安全地访问堡垒主机。
目标
- 创建一个 GKE 集群,该集群的控制平面仅在内部端点上公开。
- 部署 Compute Engine 虚拟机 (VM) 以充当集群子网中的堡垒主机。
- 使用 IAP 通过互联网将远程客户端连接到集群。
费用
在本文档中,您将使用的以下收费组件: Google Cloud
您可使用 价格计算器 根据您的预计使用情况来估算费用。
完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅清理。
准备工作
- 登录您的 Google Cloud 账号。 如果您是新手,请创建一个账号来评估我们的产品在实际场景中的表现。 Google Cloud新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
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.
Enable the GKE, Compute Engine, Identity-Aware Proxy APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
初始化 gcloud CLI 后,对其进行更新并安装所需组件:
gcloud components update gcloud components install alpha beta
-
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.
Enable the GKE, Compute Engine, Identity-Aware Proxy APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
初始化 gcloud CLI 后,对其进行更新并安装所需组件:
gcloud components update gcloud components install alpha beta
创建一个 GKE 集群,该集群的控制平面仅在内部端点上公开
创建一个新的 GKE 集群,该集群的控制平面仅在内部端点上公开。将集群放在它自己的子网中。您可以使用 Google Cloud CLI 或 Google Cloud 控制台来执行此操作。
gcloud
运行以下命令:
gcloud container clusters create-auto CLUSTER_NAME \
--location=CONTROL_PLANE_LOCATION \
--create-subnetwork=name=SUBNET_NAME \
--enable-master-authorized-networks \
--enable-private-nodes \
--enable-private-endpoint
替换以下内容:
CLUSTER_NAME:新集群的名称。CONTROL_PLANE_LOCATION:集群控制平面的 Compute Engine 区域。SUBNET_NAME:要在其中放置集群的新子网的名称。
控制台
创建虚拟私有云子网
进入 Google Cloud 控制台中的 VPC 网络页面。
点击默认网络。
在子网部分中,点击添加子网。
在添加子网对话框中,指定以下内容:
- 名称:新子网的名称。
- 区域:子网的区域。此值必须与集群区域相同。
- IP 地址范围:指定
10.2.204.0/22或与 VPC 网络中其他范围不冲突的另一个范围。 - 对于专用 Google 访问通道,请选择开启选项。
点击 Add (添加)。
创建一个 GKE 集群,该集群的控制平面仅在内部端点上公开
前往 Google Kubernetes Engine 页面,在 Google Cloud 控制台中。
点击 创建。
点击 GKE Autopilot 的配置。
为新集群指定名称和区域。该区域必须与子网相同。
在网络部分中,选择专用集群选项。
取消选中使用外部 IP 地址的控制平面复选框。
从节点子网下拉列表中,选择您创建的子网。
(可选)为集群配置其他设置。
点击创建。
您还可以使用指定了 --master-ipv4-cidr 标志的 GKE Standard 集群。
创建堡垒主机虚拟机
在专用集群内部网络中创建 Compute Engine 虚拟机以充当可以管理集群的堡垒主机。
gcloud
创建 Compute Engine 虚拟机:
gcloud compute instances create INSTANCE_NAME \
--zone=COMPUTE_ZONE \
--machine-type=e2-micro \
--network-interface=no-address,network-tier=PREMIUM,subnet=SUBNET_NAME
替换以下内容:
INSTANCE_NAME:虚拟机的名称。COMPUTE_ZONE:虚拟机的 Compute Engine 可用区。将其放在集群所在的区域中。SUBNET_NAME:要在其中放置虚拟机的子网。
控制台
在 Google Cloud 控制台中,前往虚拟机实例页面。
点击创建实例。
指定下列内容:
- 名称:虚拟机的名称。
- 区域和可用区:虚拟机的区域和可用区。请使用与您的集群相同的区域。
- 机器类型:机器类型。选择小型机器类型,例如
e2-micro。 - 对于网络接口,请选择与集群相同的 VPC 网络和子网。
- (可选)为实例配置其他设置。
点击创建。
创建防火墙规则
如需允许 IAP 连接到您的堡垒主机虚拟机,请创建防火墙规则。
部署代理
配置堡垒主机和专用集群后,您必须在主机中部署代理守护程序,以将流量转发到集群控制平面。在本教程中,您需要安装 Tinyproxy。
在虚拟机中启动会话:
gcloud compute ssh INSTANCE_NAME --tunnel-through-iap --project=PROJECT_ID安装 Tinyproxy:
sudo apt install tinyproxy打开 Tinyproxy 配置文件:
sudo vi /etc/tinyproxy/tinyproxy.conf在该文件中,执行以下操作:
- 验证端口是否为
8888。 搜索
Allow部分:/Allow 127将以下行添加到
Allow部分:Allow localhost
- 验证端口是否为
保存文件并重启 Tinyproxy:
sudo service tinyproxy restart退出会话:
exit
从远程客户端连接到集群
配置 Tinyproxy 后,您必须使用集群凭据设置远程客户端并指定代理。在远程客户端上执行以下操作:
获取集群的凭据:
gcloud container clusters get-credentials CLUSTER_NAME \ --location=CONTROL_PLANE_LOCATION \ --project=PROJECT_ID替换以下内容:
CLUSTER_NAME:专用集群的名称。CONTROL_PLANE_LOCATION:集群控制平面的 Compute Engine 位置。为区域级集群提供区域,或为可用区级集群提供可用区。PROJECT_ID:集群的 Google Cloud 项目的 ID。
使用 IAP 通过隧道连接到堡垒主机:
gcloud compute ssh INSTANCE_NAME \ --tunnel-through-iap \ --project=PROJECT_ID \ --zone=COMPUTE_ZONE \ --ssh-flag="-4 -L8888:localhost:8888 -N -q -f"指定代理:
export HTTPS_PROXY=localhost:8888 kubectl get ns输出是专用集群中的命名空间列表。
停止侦听远程客户端
如果您随时想在远程客户端上还原更改,则应终止 TCP 端口 8888 上的侦听器进程。执行此操作的命令因客户端操作系统而异。
netstat -lnpt | grep 8888 | awk '{print $7}' | grep -o '[0-9]\+' | sort -u | xargs sudo kill
问题排查
企业网络中的防火墙限制
如果您使用的是具有严格防火墙的企业网络,则在没有请求例外的情况下,您可能无法完成本教程。如果您请求例外,则堡垒主机的来源 IP 地址范围默认为 35.235.240.0/20。
清理
为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除项目
- 在 Google Cloud 控制台中,前往 管理资源 页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击 关闭以删除项目。
删除各个资源
删除您在本教程中部署的堡垒主机:
gcloud compute instances delete INSTANCE_NAME \ --zone=COMPUTE_ZONE删除集群:
gcloud container clusters delete CLUSTER_NAME \ --location=CONTROL_PLANE_LOCATION删除子网:
gcloud compute networks subnets delete SUBNET_NAME \ --region=CONTROL_PLANE_LOCATION