本教學課程說明如何使用堡壘主機,透過網際網路存取 Google Kubernetes Engine (GKE) 中的私人叢集。
您可以建立無法透過用戶端存取公開端點的 GKE 私人叢集。這個存取選項會禁止所有網際網路存取控制層,藉此提升叢集安全性。不過,停用公開端點存取權後,除非將遠端用戶端的 IP 位址新增為授權網路,否則您無法遠端與叢集互動。
本教學課程說明如何設定堡壘主機,這種主機是專為抵禦攻擊而設計的特殊用途主機。防禦主機會使用 Tinyproxy 將用戶端流量轉送至叢集。您可以使用 Identity-Aware Proxy (IAP),從遠端用戶端安全地存取堡壘主機。
目標
- 建立無法存取公開端點的私人叢集。
- 部署 Compute Engine 虛擬機器 (VM),在叢集子網路中做為防禦主機。
- 使用 IAP 透過網際網路將遠端用戶端連線至叢集。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
您可以使用 Pricing Calculator,根據預測用量估算費用。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。
事前準備
- 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.
-
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. -
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
初始化 gcloud CLI 後,請更新 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. -
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
初始化 gcloud CLI 後,請更新 gcloud CLI 並安裝必要元件:
gcloud components update gcloud components install alpha beta
CLUSTER_NAME:新叢集的名稱。CONTROL_PLANE_LOCATION:叢集控制層的 Compute Engine 區域。SUBNET_NAME:要放置叢集的新子網路名稱。前往 Google Cloud 控制台的「VPC networks」(VPC 網路) 頁面。
點選「預設」網路。
在「子網路」區段中,按一下「新增子網路」。
在「新增子網路」對話方塊中,指定下列項目:
- 「名稱」:新子網路的名稱。
- 區域:子網路的區域。這必須與叢集區域相同。
- IP 位址範圍:指定
10.2.204.0/22或其他範圍,不得與虛擬私有雲網路中的其他範圍衝突。 - 在「Private Google Access」部分,選取「啟用」選項。
按一下「新增」。
前往 Google Cloud 控制台的「Google Kubernetes Engine」頁面。
按一下 「建立」。
按一下「GKE Autopilot」的「Configure」(設定)。
指定新叢集的「名稱」和「地區」。區域必須與子網路相同。
在「網路」部分,選取「私人叢集」選項。
取消勾選「使用外部 IP 位址存取控制層」核取方塊。
從「Node subnet」(節點子網路) 下拉式清單中,選取您建立的子網路。
視需要設定叢集的其他設定。
點選「建立」。
INSTANCE_NAME:VM 名稱。COMPUTE_ZONE:VM 的 Compute Engine 區域。請將此項目放在與叢集相同的地區。SUBNET_NAME:您要在其中放置 VM 的子網路。前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
點選「建立執行個體」。
指定下列屬性:
- 名稱:VM 的名稱。
- 「區域」和「可用區」:VM 的區域和可用區。使用與叢集相同的區域。
- 機器類型:機器類型。選擇小型機器類型,例如
e2-micro。 - 在「網路介面」部分,選取與叢集相同的虛擬私有雲網路和子網路。
- 視需要設定執行個體的其他設定。
點選「建立」。
啟動 VM 工作階段:
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取得叢集的憑證:
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"指定 Proxy:
export HTTPS_PROXY=localhost:8888 kubectl get ns輸出內容是私有叢集中的命名空間清單。
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
刪除您在本教學課程中部署的 Bastion 主機:
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
建立私人叢集
建立無法透過用戶端存取公開端點的私人叢集。將叢集放在自己的子網路中。您可以使用 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
更改下列內容:
控制台
建立虛擬私有雲子網路
建立私人叢集
您也可以使用指定 --master-ipv4-cidr 旗標的 GKE 標準叢集。
建立防禦主機 VM
在私人叢集內部網路中建立 Compute Engine VM,做為可管理叢集的防禦主機。
gcloud
建立 Compute Engine VM:
gcloud compute instances create INSTANCE_NAME \
--zone=COMPUTE_ZONE \
--machine-type=e2-micro \
--network-interface=no-address,network-tier=PREMIUM,subnet=SUBNET_NAME
更改下列內容:
控制台
建立防火牆規則
如要允許 IAP 連線至防禦主機 VM,請建立防火牆規則。
部署 Proxy
設定防禦主機和私人叢集後,您必須在主機中部署 Proxy Daemon,將流量轉送至叢集控制層。在本教學課程中,您會安裝 Tinyproxy。
從遠端用戶端連線至叢集
設定 Tinyproxy 後,您必須使用叢集憑證設定遠端用戶端,並指定 Proxy。在遠端用戶端上執行下列操作:
停止監聽遠端用戶端
如要隨時還原遠端用戶端的變更,請結束 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 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。