將 Public NAT 與 GKE 搭配使用
本頁面說明如何使用 Google Kubernetes Engine (GKE) 設定範例 Public NAT。設定 Public NAT 前,請先閱讀 Public NAT 總覽。
必要條件
設定公用 NAT 前,請先完成下列步驟。
取得 IAM 權限
roles/compute.networkAdmin 角色可授權您在 Cloud Router 上建立 NAT 閘道、預留及指派 NAT IP 位址,並指定流量應透過 NAT 閘道進行網路位址轉譯的子網路。
設定 Google Cloud
開始之前,請先在 Google Cloud中設定下列項目。
- 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
在 Google Cloud 控制台中,前往「VPC networks」(虛擬私有雲網路) 頁面。
按一下「建立虛擬私有雲網路」。
輸入
custom-network1
的「Name」(名稱)。在「子網路」下方,將「子網路建立模式」設為「自訂」。
在「New subnet」(新的子網路) 下方,輸入「Name」(名稱):
subnet-us-east-192
。在「Region」(區域) 中,選取「us-east4」。
輸入
192.168.1.0/24
的「IP 位址範圍」。依序點選「完成」和「建立」。
在專案中建立新的自訂模式虛擬私有雲 (VPC) 網路:
gcloud compute networks create custom-network1 \ --subnet-mode custom
輸出:
NAME MODE IPV4_RANGE GATEWAY_IPV4 custom-network1 custom
為您的第一個地區指定子網路前置字串。在本例中,我們將
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
輸出:
NAME REGION NETWORK RANGE subnet-us-east-192 us-east4 custom-network1 192.168.1.0/24
在 Google Cloud 控制台中,前往「Kubernetes clusters」(Kubernetes 叢集) 頁面。
按一下 [Create cluster] (建立叢集)。
在「Name」(名稱) 中輸入
nat-test-cluster
。將「Location type」(位置類型) 設為「Zonal」(可用區)。
將「Zone」(區域) 設為 us-east4-c。
在導覽窗格中,按一下「Networking」(網路)。
選取「私人叢集」。
取消勾選「使用外部 IP 位址存取控制層」核取方塊。
輸入
172.16.0.0/28
的控制層 IP 範圍。將「Network」(網路) 設為
custom-network1
。如要建立並啟動叢集,請按一下「建立」。
在 Google Cloud 控制台中,前往「Firewall policies」(防火牆政策) 頁面。
點按「建立防火牆規則」。
輸入
allow-ssh
的「Name」(名稱)。指定
custom-network1
的「網路」。將「Direction of traffic」(流量方向) 設為「Ingress」(輸入)。
將「Action on match」(相符時執行的動作) 設為「Allow」(允許)。
將「Targets」(目標) 設為「All instances in the network」(網路中的所有執行個體)。
將「Source filter」(來源篩選器) 設為「IPv4 ranges」(IPv4 範圍)。
將「Source IP ranges」(來源 IP 範圍) 設為
35.235.240.0/20
。將「Protocols and ports」(通訊協定和通訊埠) 設為「Specified protocols and ports」(指定的通訊協定和通訊埠)。
勾選「tcp」tcp核取方塊,然後輸入通訊埠
22
。點選「建立」。
前往 Google Cloud 控制台的「Identity-Aware Proxy」頁面。
選取「SSH and TCP resources」(SSH 和 TCP 資源) 分頁標籤。
在「All Tunnel Resources」>「us-east4-c」下方,選取清單中第一個節點旁的核取方塊。名稱類似於
gke-nat-test-cluster-default-pool-b50db58d-075t
。記下節點名稱,稍後會用來測試連線。
在右側窗格中,按一下「新增主體」。
如要授予使用者、群組或服務帳戶資源存取權,請在「新增主體」欄位中指定他們的電子郵件地址。
如果只是測試這項功能,可以輸入自己的電子郵件地址。
如要透過 Cloud IAP 的 TCP 轉送功能授予主體資源存取權,請在「角色」下拉式清單中選取「Cloud IAP」>「受 IAP 保護的通道使用者」。
按一下 [儲存]。
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
找出您已建立 IAP SSH 權限的節點。 在「Connect」(連線) 欄中,按一下「SSH」下拉式箭頭,然後選取「Open in browser window」(在瀏覽器視窗中開啟)。
如果您是第一次連線至執行個體,Google Cloud 會為您產生 SSH 金鑰。
在節點提示中,找出
kube-dns
容器的程序 ID:pgrep '^kube-dns$'
存取容器:
sudo nsenter --target PROCESS_ID --net /bin/bash
從
kube-dns
嘗試連線至網際網路:curl example.com
您應該不會看到任何結果。如果沒有,可能是因為您未將叢集建立為私人叢集,或是發生其他問題。如要進行疑難排解,請參閱「VM 可以在沒有 Public NAT 的情況下連上網際網路」。
如要結束指令,可能需要輸入
Ctrl+C
。找出其中一個叢集節點的名稱:
gcloud compute instances list
節點名稱看起來會像
gke-nat-test-cluster-default-pool-1a4cbd06-3m8v
。請記下節點名稱,並在下列指令中將NODE_NAME
替換為該名稱。連線至節點:
gcloud compute ssh NODE_NAME \ --zone us-east4-c \ --tunnel-through-iap
在節點提示中,找出
kube-dns
容器的程序 ID:pgrep '^kube-dns$'
存取容器:
sudo nsenter --target PROCESS_ID --net /bin/bash
從
kube-dns
嘗試連線至網際網路:curl example.com
您應該不會看到任何結果。如要結束指令,可能需要輸入
Ctrl+C
。前往 Google Cloud 控制台的「Cloud NAT」頁面。
按一下 [Get started] (開始使用) 或 [Create NAT gateway] (建立 NAT 閘道)。
輸入
nat-config
的「閘道名稱」。將「VPC network」(虛擬私有雲網路) 設為
custom-network1
。將「Region」(地區) 設為「us-east4」。
在「Cloud Router」下方,選取「建立新路由器」。
- 輸入
nat-router
的「Name」(名稱)。 - 點選「建立」。
- 輸入
點選「建立」。
建立 Cloud Router:
gcloud compute routers create nat-router \ --network custom-network1 \ --region us-east4
在路由器中新增設定:
gcloud compute routers nats create nat-config \ --router-region us-east4 \ --router nat-router \ --nat-all-subnet-ip-ranges \ --auto-allocate-nat-external-ips
- 設定 Public NAT 閘道。
- 建立範例 Compute Engine 設定。
設定 GKE 範例
如果您想瞭解如何使用簡單的 Public NAT 設定搭配 GKE,請參閱這個範例。
步驟 1:建立虛擬私有雲網路和子網路
如果您已有網路和子網路,可以略過這個步驟。
主控台
gcloud
Terraform
您可以使用 Terraform 模組建立自訂虛擬私有雲網路和子網路。
步驟 2:建立私人叢集
主控台
gcloud
gcloud container clusters create "nat-test-cluster" \ --zone "us-east4-c" \ --username "admin" \ --cluster-version "latest" \ --machine-type "e2-medium" \ --disk-type "pd-standard" \ --disk-size "100" \ --scopes "https://www.googleapis.com/auth/compute","https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" \ --num-nodes "3" \ --enable-private-nodes \ --enable-private-endpoint \ --master-ipv4-cidr "172.16.0.0/28" \ --enable-ip-alias \ --network "projects/PROJECT_ID/global/networks/custom-network1" \ --subnetwork "projects/PROJECT_ID/regions/us-east4/subnetworks/subnet-us-east-192" \ --max-nodes-per-pool "110" \ --enable-master-authorized-networks \ --addons HorizontalPodAutoscaling,HttpLoadBalancing \ --enable-autoupgrade \ --enable-autorepair
Terraform
您可以使用 Terraform 資源建立私人叢集。
步驟 3:建立允許 SSH 連線的防火牆規則
主控台
gcloud
gcloud compute firewall-rules create allow-ssh \ --network custom-network1 \ --source-ranges 35.235.240.0/20 \ --allow tcp:22
Terraform
您可以使用 Terraform 資源建立防火牆規則。
步驟 4:為其中一個節點建立 IAP SSH 權限
在後續步驟中,請使用 IAP 連線至節點。
主控台
gcloud
請按照主控台操作說明執行這個步驟。
步驟 5:登入節點,確認節點無法連上網際網路
主控台
gcloud
步驟 6:使用 Cloud Router 建立 NAT 設定
您必須在與使用 Public NAT 的執行個體相同的區域中建立 Cloud Router。Public NAT 僅用於將 NAT 資訊置入 VM。而不做為實際 NAT 閘道的一部分。
這項設定允許地區中的所有執行個體將 Public NAT 用於所有主要和別名 IP 範圍,還能自動為 NAT 閘道分配外部 IP 位址。如需更多選項,請參閱 Google Cloud CLI 說明文件。
主控台
gcloud
Terraform
您可以使用 Terraform 資源建立 Cloud Router。
您可以使用 Terraform 模組建立 NAT 設定。
步驟 7:再次嘗試連上網際網路
NAT 設定最多需要 3 分鐘才會套用,因此請先至少等待 1 分鐘,再重新嘗試存取網際網路。
如果尚未登入 kube-dns
,請按照步驟 5 的程序重新連線。登入後,請重新執行 curl
指令:
curl example.com
輸出內容應包含下列內容:
<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>