搭配 Compute Engine 使用 Public NAT

本頁面將示範公開 NAT 閘道,說明如何為 Compute Engine VM 執行個體提供網路位址轉譯服務。開始之前,請先閱讀公用 NAT 總覽

事前準備

設定公用 NAT 之前,請先完成下列步驟:

取得身分與存取權管理權限

roles/compute.networkAdmin 角色可讓您在 Cloud Router 上建立 NAT 閘道、保留及指派 NAT IP 位址,以及指定應透過 NAT 閘道使用網路位址轉譯的子網路。

設定 Google Cloud

開始之前,請先在 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. Install the Google Cloud CLI.

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

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

    gcloud init
  7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  9. Install the Google Cloud CLI.

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

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

    gcloud init
  12. 範例

    以下是端對端範例,展示公開 NAT 閘道範例和使用公開 NAT 閘道的 Compute Engine VM 範例。

    步驟 1:建立虛擬私有雲網路和子網路

    如果您已建立網路和子網路,可以略過這個步驟。

    主控台

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

      前往「VPC networks」(VPC 網路) 頁面

    2. 按一下「建立虛擬私有雲網路」

    3. 輸入 custom-network1 的「Name」

    4. 在「子網路」下,將「子網路建立模式」設為「自訂」

    5. 在「New subnet」(新的子網路) 下方,輸入「Name」(名稱) subnet-us-east-192

    6. 在「Region」中,選取「us-east4」

    7. 輸入 192.168.1.0/24 的「IP 位址範圍」

    8. 依序點選「完成」和「建立」

    gcloud

    1. 在專案中建立新的自訂模式 VPC 網路:

      gcloud compute networks create custom-network1 \
          --subnet-mode custom
    2. 為您的第一個地區指定子網路前置字串。在本例中,我們將 192.168.1.0/24 指派給區域 us-east4

      gcloud compute networks subnets create subnet-us-east-192 \
         --network custom-network1 \
         --region us-east4 \
         --range 192.168.1.0/24

    Terraform

    您可以使用 Terraform 模組建立自訂虛擬私有雲 (VPC) 網路和子網路。

    module "test-vpc-module" {
      source       = "terraform-google-modules/network/google"
      version      = "~> 10.0"
      project_id   = var.project_id # Replace this with your project ID in quotes
      network_name = "custom-network1"
      mtu          = 1460
    
      subnets = [
        {
          subnet_name   = "subnet-us-east-192"
          subnet_ip     = "192.168.1.0/24"
          subnet_region = "us-east4"
        }
      ]
    }

    步驟 2:建立沒有外部 IP 位址的 VM 執行個體

    主控台

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

      前往 VM 執行個體頁面

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

    3. 為執行個體指定 nat-test-1 的「名稱」

    4. 將「區域」設為 us-east4

    5. 將「Zone」(可用區) 設為 us-east4-c

    6. 按一下「管理、安全性、磁碟、網路、單獨租用」連結。

    7. 按一下 [網路] 分頁標籤。

    8. 在「Network interfaces」下方,按一下虛擬機器的預設介面 「Edit」

      1. 將「Network」(網路) 設為 custom-network1
      2. 將「子網路」設為 subnet-us-east-192
      3. 將「外部 IP」設為「無」
      4. 按一下 [完成]
    9. 如要建立並啟動執行個體,請按一下「建立」

    gcloud

    gcloud compute instances create nat-test-1 \
        --image-family debian-9 \
        --image-project debian-cloud \
        --network custom-network1 \
        --subnet subnet-us-east-192 \
        --zone us-east4-c \
        --no-address

    Terraform

    您可以使用 Terraform 資源建立 VM 執行個體。

    resource "google_compute_instance" "default" {
      project      = var.project_id
      zone         = "us-east4-c"
      name         = "nat-test-1"
      machine_type = "e2-medium"
      boot_disk {
        initialize_params {
          image = "debian-cloud/debian-9"
        }
      }
      network_interface {
        network    = "custom-network1"
        subnetwork = var.subnet # Replace with a reference or self link to your subnet, in quotes
      }
    }

    步驟 3:建立允許 SSH 連線的防火牆規則

    主控台

    1. 在 Google Cloud 控制台中,前往「Firewall policies」(防火牆政策) 頁面。

      前往「防火牆政策」頁面

    2. 點按「建立防火牆規則」

    3. 輸入 allow-ssh 的「Name」

    4. 指定 custom-network1 的「Network」

    5. 將「流量方向」設為「Ingress」

    6. 將「Action on match」(相符時執行的動作) 設為「Allow」(允許)

    7. 將「Targets」設為「All instances in the network」

    8. 將「Source filter」(來源篩選器) 設為「IPv4 ranges」(IPv4 範圍)

    9. 將「Source IP ranges」(來源 IP 範圍) 設為 35.235.240.0/20

    10. 將「Protocols and ports」(通訊協定和通訊埠) 設為「Specified protocols and ports」(指定的通訊協定和通訊埠)

    11. 勾選「tcp」tcp核取方塊,然後輸入通訊埠 22

    12. 按一下 [建立]。

    gcloud

    gcloud compute firewall-rules create allow-ssh \
        --network custom-network1 \
        --source-ranges 35.235.240.0/20 \
        --allow tcp:22

    Terraform

    您可以使用 Terraform 資源建立防火牆規則。

    resource "google_compute_firewall" "rules" {
      project = var.project_id
      name    = "allow-ssh"
      network = var.network # Replace with a reference or self link to your network, in quotes
    
      allow {
        protocol = "tcp"
        ports    = ["22"]
      }
      source_ranges = ["35.235.240.0/20"]
    }

    步驟 4:為測試執行個體建立 IAP SSH 權限

    在後續步驟中,請使用 Identity-Aware Proxy (IAP) 連線至測試執行個體。

    主控台

    1. 前往 Google Cloud 控制台的「Identity-Aware Proxy」頁面。

      前往「Identity-Aware Proxy」頁面

    2. 選取「SSH and TCP resources」(SSH 和 TCP 資源) 分頁標籤。

    3. 如要更新資源的成員權限,請選取「All Tunnel Resources」>「us-east4-c」>「nat-test-1」旁的核取方塊。

    4. 在右側窗格中,按一下「新增成員」

    5. 如要授予使用者、群組或服務帳戶存取資源的權限,請在「New members」(新成員) 欄位中指定他們的電子郵件地址。

      如果您只是要測試這項功能,可以輸入自己的電子郵件地址。

    6. 如要透過 Cloud IAP 的 TCP 轉送功能,將資源存取權授予成員,請在「角色」下拉式清單中,依序選取「Cloud IAP」>「IAP-secured Tunnel User」(受 IAP 保護的通道使用者)

    7. 按一下 [儲存]

    gcloud

    這個指令會使用 IAP 授予專案中所有 VM 執行個體的 SSH 存取權。如果您想使用 IAP 授予個別 VM 的 SSH 存取權,請參閱 Google Cloud 控制台操作說明。

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member=MEMBER_INFO \
        --role=roles/iap.tunnelResourceAccessor

    更改下列內容:

    • PROJECT_ID:您的專案 ID
    • MEMBER_INFO:以半形逗號分隔的清單,當中列有成員 type:email 組合。範例:
      • 個別使用者:user:test-user@example.com
      • 群組:group:admins@example.com
      • 服務帳戶:serviceAccount:test123@example.domain.com

    Terraform

    您可以使用 Terraform 資源為測試執行個體建立 IAP SSH 權限。

    resource "google_project_iam_member" "project" {
      project = var.project_id
      role    = "roles/iap.tunnelResourceAccessor"
      member  = "serviceAccount:test123@example.domain.com"
    }

    步驟 5:登入 nat-test-1,確認無法連上網際網路

    主控台

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

      前往 VM 執行個體頁面

    2. 針對 nat-test-1,請在「Connect」欄中按一下「SSH」下拉式箭頭,然後選取「Open in browser window」

    3. 在 VM 的命令提示中輸入 curl example.com,然後按下 Enter 鍵。

      您應該不會看到任何結果。如果是這樣,您可能已使用外部 IP 位址建立 nat-test-1,或者可能有其他問題。如要排解問題,請參閱「VM 在沒有 Cloud NAT 的情況下,可能會意外連上網際網路」。

      如要結束指令,您可能需要輸入 Ctrl+C

    gcloud

    1. 將 Compute Engine 安全殼層金鑰新增至本機主機:

      ssh-add ~/.ssh/google_compute_engine
      
    2. 連線至 nat-test-1 並執行指令:

      gcloud compute ssh nat-test-1 \
          --zone us-east4-c \
          --command "curl example.com" \
          --tunnel-through-iap

      您應該不會看到任何結果。如果是這樣,您可能已使用外部 IP 位址建立 nat-test-1,或者可能有其他問題。如要排解問題,請參閱「VM 在沒有 Cloud NAT 的情況下,可能會意外連上網際網路」。

      如要結束指令,您可能需要輸入 Ctrl+C

    步驟 6:使用 Cloud Router 建立 NAT 設定

    您必須在使用公開 NAT 的執行個體所在的地區建立 Cloud Router。Cloud Router 僅用於將 NAT 資訊置入 VM。不會用於實際的 NAT 閘道。

    這項設定允許地區中的所有執行個體將 Public NAT 用於所有主要和別名 IP 範圍,還能自動為 NAT 閘道分配外部 IP 位址。如需更多選項,請參閱 Google Cloud CLI 說明文件。

    主控台

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

      前往 Cloud NAT 頁面

    2. 按一下 [Get started] (開始使用) 或 [Create NAT gateway] (建立 NAT 閘道)

    3. 輸入 nat-config 的「閘道名稱」

    4. 將「VPC network」(虛擬私有雲網路) 設為 custom-network1

    5. 將「區域」設為 us-east4

    6. 在「Cloud Router」下方,選取「建立新路由器」

      1. 輸入 nat-router 的「Name」
      2. 按一下 [建立]。
    7. 按一下 [建立]。

    gcloud

    1. 建立 Cloud Router:

      gcloud compute routers create nat-router \
          --network custom-network1 \
          --region us-east4
    2. 為路由器新增設定:

      gcloud compute routers nats create nat-config \
          --router-region us-east4 \
          --router nat-router \
          --nat-all-subnet-ip-ranges \
          --auto-allocate-nat-external-ips

    Terraform

    您可以使用 Terraform 資源建立 Cloud Router。

    resource "google_compute_router" "router" {
      project = var.project_id
      name    = "nat-router"
      network = var.network
      region  = "us-east4"
    }

    您可以使用 Terraform 模組建立 NAT 設定。

    module "cloud-nat" {
      source  = "terraform-google-modules/cloud-nat/google"
      version = "~> 5.0"
    
      project_id                         = var.project_id
      region                             = "us-east4"
      router                             = google_compute_router.router.name
      name                               = "nat-config"
      source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
    }

    步驟 7:嘗試再次連上網際網路

    NAT 設定最多需要 3 分鐘才會套用至 VM,因此請先至少等待 1 分鐘,再重新嘗試存取網際網路。

    主控台

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

      前往 VM 執行個體頁面

    2. 針對 nat-test-1,請在「Connect」欄中按一下「SSH」下拉式箭頭,然後選取「Open in browser window」

    3. 在 VM 的命令提示中輸入 curl example.com,然後按下 Enter 鍵。

    gcloud

    連線至 nat-test-1 並執行指令:

    gcloud compute ssh nat-test-1 \
        --zone us-east4-c \
        --command "curl example.com" \
        --tunnel-through-iap
    

    輸出內容應包含下列內容:

    
    <html>
    <head>
    <title>Example Domain</title>
    ...
    ...
    ...
    </head>
    
    <body>
    <div>
        <h1>Example Domain</h1>
        <p>This domain is established to be used for illustrative examples in documents. You can use this
        domain in examples without prior coordination or asking for permission.</p>
        <p><a href="http://www.iana.org/domains/example">More information...</a></p>
    </div>
    </body>
    </html>
    

    後續步驟