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


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

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

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

目標

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

費用

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

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

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

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

事前準備

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the GKE, Compute Engine, Identity-Aware Proxy APIs.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. After initializing the gcloud CLI, update it and install the required components:

    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.

    Go to project selector

  10. Make sure that billing is enabled for your Google Cloud project.

  11. Enable the GKE, Compute Engine, Identity-Aware Proxy APIs.

    Enable the APIs

  12. Install the Google Cloud CLI.

  13. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  14. To initialize the gcloud CLI, run the following command:

    gcloud init
  15. After initializing the gcloud CLI, update it and install the required components:

    gcloud components update
    gcloud components install alpha beta

建立私人叢集

建立無法透過用戶端存取公開端點的新私人叢集。將叢集放在自己的子網路中。您可以使用 Google Cloud CLI 或 Google Cloud 控制台執行這項操作。

gcloud

執行下列指令:

gcloud container clusters create-auto CLUSTER_NAME \
    --region=COMPUTE_REGION \
    --create-subnetwork=name=SUBNET_NAME \
    --enable-master-authorized-networks \
    --enable-private-nodes \
    --enable-private-endpoint

更改下列內容:

  • CLUSTER_NAME:新叢集的名稱。
  • COMPUTE_REGION:叢集的 Compute Engine 地區
  • SUBNET_NAME:要放置叢集的新子網路名稱。

控制台

建立虛擬私有雲子網路

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

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

  2. 按一下「預設」網路。

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

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

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

建立私人叢集

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

    前往「Google Kubernetes Engine」

  2. 按一下「 Create」(建立)

  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. 「區域」和「可用區」: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 \
        --region=COMPUTE_REGION \
        --project=PROJECT_ID
    

    更改下列內容:

    • CLUSTER_NAME:私有叢集的名稱。
    • COMPUTE_REGION:叢集所在的區域。
    • 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. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

刪除個別資源

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

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

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

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