使用防禦主機遠端存取私人叢集

本教學課程說明如何使用堡壘主機,透過網際網路存取 Google Kubernetes Engine (GKE) 中的私人叢集

您可以建立無法透過用戶端存取公開端點的 GKE 私人叢集。這個存取選項會禁止所有網際網路存取控制層,藉此提升叢集安全性。不過,停用公開端點存取權後,除非將遠端用戶端的 IP 位址新增為授權網路,否則您無法遠端與叢集互動。

本教學課程說明如何設定堡壘主機,這種主機專為抵禦攻擊而設計。防禦主機會使用 Tinyproxy 將用戶端流量轉送至叢集。您可以使用 Identity-Aware Proxy (IAP),從遠端用戶端安全地存取堡壘主機。

目標

  • 建立無法存取公開端點的私人叢集。
  • 部署 Compute Engine 虛擬機器 (VM),在叢集子網路中做為防禦主機。
  • 透過網際網路,使用 IAP 將遠端用戶端連線至叢集。

費用

在本文件中,您會使用下列 Google Cloud的計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用期資格。

完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. 安裝 Google Cloud CLI。

  6. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  7. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  8. 初始化 gcloud CLI 後,請更新 gcloud CLI 並安裝必要元件:

    gcloud components update
    gcloud components install alpha beta
  9. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  10. Verify that billing is enabled for your Google Cloud project.

  11. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  12. 安裝 Google Cloud CLI。

  13. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  14. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  15. 初始化 gcloud CLI 後,請更新 gcloud CLI 並安裝必要元件:

    gcloud components update
    gcloud components install alpha beta

建立私人叢集

建立無法透過用戶端存取公開端點的私人叢集。將叢集放在自己的子網路中。您可以使用 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:要放置叢集的新子網路名稱。

控制台

建立虛擬私有雲子網路

  1. 前往 Google Cloud 控制台的「VPC networks」(VPC 網路) 頁面。

    前往「VPC networks」(虛擬私有雲網路)

  2. 點選「預設」網路。

  3. 在「子網路」區段中,按一下「新增子網路」

  4. 在「新增子網路」對話方塊中,指定下列項目:

    1. 「名稱」:新子網路的名稱。
    2. 區域:子網路的區域。這必須與叢集區域相同。
    3. IP 位址範圍:指定 10.2.204.0/22 或其他範圍,不得與虛擬私有雲網路中的其他範圍衝突。
    4. 在「Private Google Access」部分,選取「啟用」選項。
  5. 按一下「新增」。

建立私人叢集

  1. 前往 Google Cloud 控制台的「Google Kubernetes Engine」頁面。

    前往「Google Kubernetes Engine」

  2. 點選 「建立」

  3. 按一下「GKE Autopilot」的「Configure」(設定)

  4. 指定新叢集的「名稱」和「地區」。區域必須與子網路相同。

  5. 在「網路」部分,選取「私人叢集」選項。

  6. 取消勾選「使用外部 IP 位址存取控制層」核取方塊。

  7. 從「Node subnet」(節點子網路) 下拉式清單中,選取您建立的子網路。

  8. 視需要設定叢集的其他設定。

  9. 點選「建立」

您也可以使用指定 --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

更改下列內容:

  • INSTANCE_NAME:VM 的名稱。
  • COMPUTE_ZONE:VM 的 Compute Engine 區域。請將此項目放在與叢集相同的地區。
  • SUBNET_NAME:要放置 VM 的子網路。

控制台

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體

  2. 點選「建立執行個體」

  3. 指定下列屬性:

    1. 名稱:VM 的名稱。
    2. 「Region」(區域)和「Zone」(可用區):VM 的區域和可用區。使用與叢集相同的區域。
    3. 機器類型:機器類型。選擇小型機型,例如 e2-micro
    4. 在「網路介面」部分,選取與叢集相同的虛擬私有雲網路和子網路。
    5. 視需要設定執行個體的其他設定。
  4. 點選「建立」

建立防火牆規則

如要允許 IAP 連線至防禦主機 VM,請建立防火牆規則

部署 Proxy

設定防禦主機和私人叢集後,您必須在主機中部署 Proxy Daemon,將流量轉送至叢集控制層。在本教學課程中,您將安裝 Tinyproxy

  1. 啟動 VM 工作階段:

    gcloud compute ssh INSTANCE_NAME --tunnel-through-iap --project=PROJECT_ID
    
  2. 安裝 Tinyproxy:

    sudo apt install tinyproxy
    
  3. 開啟 Tinyproxy 設定檔:

    sudo vi /etc/tinyproxy/tinyproxy.conf
    
  4. 在檔案中執行下列操作:

    1. 確認通訊埠為 8888
    2. 搜尋「Allow」部分:

        /Allow 127
      
    3. Allow 區段新增下列程式碼:

        Allow localhost
      
  5. 儲存檔案並重新啟動 Tinyproxy:

    sudo service tinyproxy restart
    
  6. 結束工作階段:

    exit
    

從遠端用戶端連線至叢集

設定 Tinyproxy 後,您必須使用叢集憑證設定遠端用戶端,並指定 Proxy。在遠端用戶端上執行下列操作:

  1. 取得叢集的憑證:

    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。
  2. 使用 IAP 建立堡壘主機的通道:

    gcloud compute ssh INSTANCE_NAME \
        --tunnel-through-iap \
        --project=PROJECT_ID \
        --zone=COMPUTE_ZONE \
        --ssh-flag="-4 -L8888:localhost:8888 -N -q -f"
    
  3. 指定 Proxy:

    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 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。

刪除專案

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

刪除個別資源

  1. 刪除您在本教學課程中部署的跳板主機:

    gcloud compute instances delete INSTANCE_NAME \
        --zone=COMPUTE_ZONE
    
  2. 刪除叢集:

    gcloud container clusters delete CLUSTER_NAME \
        --location=CONTROL_PLANE_LOCATION
    
  3. 刪除子網路:

    gcloud compute networks subnets delete SUBNET_NAME \
        --region=CONTROL_PLANE_LOCATION